recuperator fan control with esp32
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
esp-recuperator/main/fancontrol.c

114 lines
3.0 KiB

//
// Created by MightyPork on 2022/08/20.
//
#include "fancontrol.h"
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
#include "settings.h"
#include "actuators.h"
struct FanControlState gState = {};
static void timerCallback(TimerHandle_t xTimer);
void settings_blind_time_set(uint16_t blind_time) {
bool nadoraz = (gState.blind_position >= gSettings.blind_time) || (gState.blind_position >= blind_time);
gSettings.blind_time = blind_time;
if (nadoraz) {
gState.blind_position = blind_time;
}
settings_persist(SETTINGS_blind_time);
}
void fancontrol_init() {
gState.vent_mode = gSettings.initial_mode;
gState.set_power = gSettings.initial_power;
xTimerCreate("fanctl",
pdMS_TO_TICKS(1000),
pdTRUE,
NULL,
timerCallback);
}
static void timerCallback(TimerHandle_t xTimer) {
// posun rolety
if (gAct.blind) {
if (gState.blind_position < gSettings.blind_time) {
gState.blind_position++;
}
} else {
if (gState.blind_position > 0) {
gState.blind_position--;
}
}
if (gAct.power > 0) {
if (gState.real_direction != gAct.dir) {
if (gState.ramp > 0) {
gState.ramp--;
} else {
gState.run_time = 0;
gState.real_direction = gAct.dir;
}
} else {
if (gState.ramp < gSettings.ramp_time) {
gState.ramp++;
}
}
gState.run_time++;
} else {
if (gState.ramp > 0) {
gState.ramp--;
} else {
gState.run_time = 0;
}
}
switch (gState.vent_mode) {
case VENT_MODE_OFF:
act_motor_power_set(0);
act_blind_set(0);
break;
case VENT_MODE_FREE:
act_motor_power_set(0);
act_blind_set(1);
break;
case VENT_MODE_OUT:
act_motor_direction_set(MOTOR_DIR_OUT);
act_blind_set(1);
if (gState.blind_position >= gSettings.blind_time) {
act_motor_power_set(gState.set_power);
}
break;
case VENT_MODE_IN:
act_motor_direction_set(MOTOR_DIR_IN);
act_blind_set(1);
if (gState.blind_position >= gSettings.blind_time) {
act_motor_power_set(gState.set_power);
}
break;
case VENT_MODE_RECUP:
act_blind_set(1);
if (gState.blind_position >= gSettings.blind_time) {
act_motor_power_set(gState.set_power);
}
// Stop condition
if (gState.run_time >= gSettings.recup_time) {
// zmena smeru
gState.run_time = 0;
act_motor_direction_set(1 - gAct.dir);
}
break;
}
if (gAct.dir == gState.real_direction && gState.ramp >= gSettings.ramp_time) {
// Measure temperatures
}
}