From 4c3cd99e387bd34faa65a6b93306e50a59a4f85c Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Wed, 26 Oct 2022 23:10:20 +0300 Subject: [PATCH] esp-vent-main: main cleanup and doc - Delete unnused stuff in main - Create common values header - Minor fix --- esp-vent-main/inc/Fan.h | 6 +- esp-vent-main/inc/StateHandler/StateHandler.h | 74 +++++++++---------- esp-vent-main/inc/common_control_values.h | 20 +++++ .../src/StateHandler/StateHandler.cpp | 32 ++++---- esp-vent-main/src/esp-vent-main.cpp | 52 +++++-------- 5 files changed, 95 insertions(+), 89 deletions(-) create mode 100644 esp-vent-main/inc/common_control_values.h diff --git a/esp-vent-main/inc/Fan.h b/esp-vent-main/inc/Fan.h index 4f61518..d4f5cfe 100644 --- a/esp-vent-main/inc/Fan.h +++ b/esp-vent-main/inc/Fan.h @@ -5,12 +5,12 @@ * Author: tylen */ -#include "Modbus/ModbusRegister.h" -#include - #ifndef FAN_H_ #define FAN_H_ +#include "Modbus/ModbusRegister.h" +#include + class Fan { public: diff --git a/esp-vent-main/inc/StateHandler/StateHandler.h b/esp-vent-main/inc/StateHandler/StateHandler.h index f403cb7..a679c46 100644 --- a/esp-vent-main/inc/StateHandler/StateHandler.h +++ b/esp-vent-main/inc/StateHandler/StateHandler.h @@ -22,6 +22,7 @@ #include "LiquidCrystal.h" #include "PressureWrapper.h" #include "Timer.h" +#include "common_control_values.h" #include "nlohmann/json.hpp" /** Buttons enumeration @@ -30,8 +31,7 @@ * from main to StateHandler through * a keyEvent. Enumeration determines the state * of the particular button. - * */ - + */ enum _buttons { /** Raises the bar up */ @@ -93,6 +93,39 @@ public: */ unsigned int getSetSpeed (); + /** Handle the given event of the current state + * + * @param event event to be handled in the current state + */ + void HandleState (const Event &event); + +private: + state_pointer current; + bool current_mode; + Counter value[2] = { { 0, 100 }, { 0, 120 } }; + /* Motor of fan starts at value 90. probably because of some + * weigh of fan, so voltage within range of 0-89 is not + * sufficient to start motor. + * TODO: Value 89 should be scaled to 0 at some point + */ + Counter fan_speed = { 0, 1000 }; + /* Integral controller for PID. should be global, since it + * accumulates error signals encountered since startup + */ + int integral = 0; + int saved_set_value[2] = { 0, 0 }; + int saved_curr_value[2] = { 0, 0 }; + int sensors_data[4] = { 0 }; + LiquidCrystal *_lcd; + Fan *_propeller; + PressureWrapper *_pressure; + bool pressure_status; + Timer *state_timer; + /* CO2 sensor object */ + GMP252 co2; + /* Humidity and temperature sensor object */ + HMP60 humidity; + /** Display values on LCD depending on current mode * * MANUAL MODE: SPEED: XX% PRESSURE: XXPa @@ -103,47 +136,10 @@ public: */ void displaySet (size_t mode); - /** Handle the given event of the current state - * - * @param event event to be handled in the current state - */ - void HandleState (const Event &event); - -private: - state_pointer current; /** Set a new curremt state * @param newstate new state to be set to current */ void SetState (state_pointer newstate); - bool current_mode; - Counter value[2] = { { 0, 100 }, { 0, 120 } }; - - /* Motor of fan starts at value 90. probably because of some - * weigh of fan, so voltage within range of 0-89 is not - * sufficient to start motor. - * TODO: Value 89 should be scaled to 0 at some point - */ - Counter fan_speed = { 0, 1000 }; - - /* Integral controller for PID. should be global, since it - * accumulates error signals encountered since startup - */ - int integral = 0; - - int saved_set_value[2] = { 0, 0 }; - int saved_curr_value[2] = { 0, 0 }; - int sensors_data[4] = { 0 }; - LiquidCrystal *_lcd; - Fan *_propeller; - PressureWrapper *_pressure; - bool pressure_status; - Timer *state_timer; - - /* CO2 sensor object */ - GMP252 co2; - - /* Humidity and temperature sensor object */ - HMP60 humidity; /** Initialization state * diff --git a/esp-vent-main/inc/common_control_values.h b/esp-vent-main/inc/common_control_values.h new file mode 100644 index 0000000..85d80e8 --- /dev/null +++ b/esp-vent-main/inc/common_control_values.h @@ -0,0 +1,20 @@ +/* + * common_control_values.h + * + * Created on: Oct 26, 2022 + * Author: tylen + */ + +#ifndef COMMON_CONTROL_VALUES_H_ +#define COMMON_CONTROL_VALUES_H_ + +enum _global_values +{ + LCD_SIZE = 16, + TIMER_GLOBAL_TIMEOUT = 15000, + TIMER_SENSORS_TIMEOUT = 5000, + TIMER_PRESSURE_TIMEOUT = 500, + TIMER_ERROR_VALUE = -255, +}; + +#endif /* COMMON_CONTROL_VALUES_H_ */ diff --git a/esp-vent-main/src/StateHandler/StateHandler.cpp b/esp-vent-main/src/StateHandler/StateHandler.cpp index b41a686..7b4ef7e 100644 --- a/esp-vent-main/src/StateHandler/StateHandler.cpp +++ b/esp-vent-main/src/StateHandler/StateHandler.cpp @@ -27,30 +27,32 @@ StateHandler::~StateHandler () void StateHandler::displaySet (size_t mode) { - char line_up[16] = { 0 }; - char line_down[16] = { 0 }; + char line_up[LCD_SIZE] = { 0 }; + char line_down[LCD_SIZE] = { 0 }; switch (mode) { case MANUAL: - snprintf (line_up, 16, "SPEED: %02d%", saved_set_value[current_mode]); - snprintf (line_down, 16, "PRESSURE: %02dPa", + snprintf (line_up, LCD_SIZE, "SPEED: %02d%", + saved_set_value[current_mode]); + snprintf (line_down, LCD_SIZE, "PRESSURE: %02dPa", saved_curr_value[current_mode]); break; case AUTO: - snprintf (line_up, 16, "P. SET: %02dPa", saved_set_value[current_mode]); - snprintf (line_down, 16, "P. CURR: %02dPa", + snprintf (line_up, LCD_SIZE, "P. SET: %02dPa", + saved_set_value[current_mode]); + snprintf (line_down, LCD_SIZE, "P. CURR: %02dPa", saved_curr_value[current_mode]); break; case SENSORS: - snprintf (line_up, 16, "PRE:%02d TEM:%02d", sensors_data[PRESSUREDAT], - sensors_data[TEMPERATURE]); - snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY], - sensors_data[CO2]); + snprintf (line_up, LCD_SIZE, "PRE:%02d TEM:%02d", + sensors_data[PRESSUREDAT], sensors_data[TEMPERATURE]); + snprintf (line_down, LCD_SIZE, "HUM:%02d CO2:%02d", + sensors_data[HUMIDITY], sensors_data[CO2]); break; case ERROR_TIMEOUT: - snprintf (line_up, 16, " FORCE STOP "); - snprintf (line_down, 16, "REASON: TIMEOUT"); + snprintf (line_up, LCD_SIZE, " FORCE STOP "); + snprintf (line_down, LCD_SIZE, "REASON: TIMEOUT"); break; default: break; @@ -203,16 +205,16 @@ StateHandler::handleControlButtons (uint8_t button) void StateHandler::handleTickValue (int value) { - if (value % 5000 == 0) + if (value % TIMER_SENSORS_TIMEOUT == 0) { updateSensorValues (); displaySet (SENSORS); } - if (value % 500 == 0) + if (value % TIMER_PRESSURE_TIMEOUT == 0) { SetState (&StateHandler::stateGetPressure); } - if (value < 0) + if (value == TIMER_ERROR_VALUE) { displaySet (ERROR_TIMEOUT); this->fan_speed.setInit (0); diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 395834a..63d570d 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -8,14 +8,6 @@ =============================================================================== */ -#if defined(__USE_LPCOPEN) -#if defined(NO_BOARD_LIB) -#include "chip.h" -#else -#include "board.h" -#endif -#endif - #include "DigitalIoPin.h" #include "Fan.h" #include "LiquidCrystal.h" @@ -23,32 +15,20 @@ #include "StateHandler/StateHandler.h" #include "SwitchController.h" #include "Timer.h" - +#include "board.h" +#include "chip.h" +#include "common_control_values.h" #include -// TODO: insert other include files here - -// TODO: insert other definitions and declarations here - int main (void) { - -#if defined(__USE_LPCOPEN) - // Read clock settings and update SystemCoreClock variable + /* Board init */ SystemCoreClockUpdate (); -#if !defined(NO_BOARD_LIB) - // Set b_up_state and initialize all required blocks and - // functions related to the board hardware Board_Init (); - // Set the LED to the state of "On" - Board_LED_Set (0, true); -#endif -#endif - /** Lcd & stateHandler */ + + /** LCD setup */ Chip_RIT_Init (LPC_RITIMER); - Timer glob_time; - Timer switch_time (false); DigitalIoPin rs (0, 29, false, true, false); DigitalIoPin en (0, 9, false, true, false); DigitalIoPin d4 (0, 10, false, true, false); @@ -56,20 +36,26 @@ main (void) DigitalIoPin d6 (1, 3, false, true, false); DigitalIoPin d7 (0, 0, false, true, false); LiquidCrystal lcd (&rs, &en, &d4, &d5, &d6, &d7); - // lcd.setCursor (0, 0); lcd.print ("Vent-Machine"); - /* FAN object */ + /* Timers */ + Timer glob_time; + Timer switch_time (false); + + /* Modbus Fan setup */ ModbusMaster fan (1); fan.begin (9600); Fan propeller (new ModbusRegister (&fan, 0)); + /* Pressure sensor setup */ PressureWrapper sens; glob_time.Sleep (1000); + /* State Machine */ StateHandler ventMachine (&lcd, &propeller, &sens, &glob_time); - /** Common pins */ + + /** Control switches */ DigitalIoPin b_up (0, 7, true, true, true); // A5 SwitchController sw_up (&b_up, &ventMachine, BUTTON_CONTROL_UP); @@ -79,14 +65,16 @@ main (void) DigitalIoPin b_toggle (0, 5, true, true, true); // A3 SwitchController sw_toggle (&b_toggle, &ventMachine, BUTTON_CONTROL_TOG_MODE); - int getcounterValue = 0; + /* Other declarations */ + int getcounterValue; + while (1) { getcounterValue = glob_time.getCounter (); - if (getcounterValue > 15000) + if (getcounterValue > TIMER_GLOBAL_TIMEOUT) { glob_time.resetCounter (); - ventMachine.HandleState (Event (Event::eTick, -1)); + ventMachine.HandleState (Event (Event::eTick, TIMER_ERROR_VALUE)); } sw_up.listen (); sw_down.listen ();