forked from electro/esp-irblaster
				
			
							parent
							
								
									5937417568
								
							
						
					
					
						commit
						cbf3ebd8e0
					
				@ -0,0 +1,113 @@ | 
				
			||||
//
 | 
				
			||||
// 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
 | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -0,0 +1,51 @@ | 
				
			||||
//
 | 
				
			||||
// Created by MightyPork on 2022/08/20.
 | 
				
			||||
//
 | 
				
			||||
 | 
				
			||||
#ifndef FANCTL_FANCONTROL_H | 
				
			||||
#define FANCTL_FANCONTROL_H | 
				
			||||
 | 
				
			||||
#include <stdint.h> | 
				
			||||
#include "actuators.h" | 
				
			||||
 | 
				
			||||
void fancontrol_init(); | 
				
			||||
 | 
				
			||||
enum ventilation_mode { | 
				
			||||
    VENT_MODE_OFF = 0, | 
				
			||||
    VENT_MODE_FREE = 1, | 
				
			||||
    VENT_MODE_OUT = 3, | 
				
			||||
    VENT_MODE_IN = 5, | 
				
			||||
    VENT_MODE_RECUP = 7, | 
				
			||||
}; | 
				
			||||
 | 
				
			||||
struct FanControlState { | 
				
			||||
    /**
 | 
				
			||||
     * Poloha roletky, 0<->blind_time, inkrement/dekrement 1 za sekundu. | 
				
			||||
     * Pri zmene blind_time se musi hodnota aktualizovat, pokud je na doraze nebo za. | 
				
			||||
     */ | 
				
			||||
    uint16_t blind_position; | 
				
			||||
    /**
 | 
				
			||||
     * Power requested trough register or as the default value | 
				
			||||
     */ | 
				
			||||
    uint16_t set_power; | 
				
			||||
    /**
 | 
				
			||||
     * Cas chodu motoru v aktualnim smeru, inkrement 1 za sekundu. | 
				
			||||
     */ | 
				
			||||
    uint16_t run_time; | 
				
			||||
    /**
 | 
				
			||||
     * "stav chodu motoru". 0=stop, ramp_time = jede. | 
				
			||||
     */ | 
				
			||||
    uint16_t ramp; | 
				
			||||
    /**
 | 
				
			||||
     * skutecny aktualni pohyb motoru (zustava stejny, dokud ramp time nedosahne nuly) | 
				
			||||
     */ | 
				
			||||
    enum motor_direction real_direction; | 
				
			||||
    /**
 | 
				
			||||
     * rezim ventilace | 
				
			||||
     */ | 
				
			||||
    enum ventilation_mode vent_mode; | 
				
			||||
}; | 
				
			||||
 | 
				
			||||
extern struct FanControlState gState; | 
				
			||||
 | 
				
			||||
#endif //FANCTL_FANCONTROL_H
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue