From fe75d4e992ef8df7f01c7e052fd27248f305ada5 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Wed, 26 Oct 2022 22:38:25 +0300 Subject: [PATCH] fan: include fan into state-handler --- esp-vent-main/inc/Fan.h | 11 +++++++---- esp-vent-main/inc/StateHandler/StateHandler.h | 9 ++++----- esp-vent-main/src/Fan.cpp | 19 +++++++++++++------ .../src/StateHandler/StateHandler.cpp | 10 +++++----- esp-vent-main/src/esp-vent-main.cpp | 6 +++--- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/esp-vent-main/inc/Fan.h b/esp-vent-main/inc/Fan.h index ffe61e6..4f61518 100644 --- a/esp-vent-main/inc/Fan.h +++ b/esp-vent-main/inc/Fan.h @@ -6,17 +6,20 @@ */ #include "Modbus/ModbusRegister.h" +#include #ifndef FAN_H_ #define FAN_H_ -class Fan { +class Fan +{ public: - Fan(ModbusRegister * A01); - virtual ~Fan(); + Fan (ModbusRegister *A01); + virtual ~Fan (); + void spin (int speed); private: - ModbusRegister * _A01; + ModbusRegister *_A01; }; #endif /* FAN_H_ */ diff --git a/esp-vent-main/inc/StateHandler/StateHandler.h b/esp-vent-main/inc/StateHandler/StateHandler.h index 9d7d84e..f403cb7 100644 --- a/esp-vent-main/inc/StateHandler/StateHandler.h +++ b/esp-vent-main/inc/StateHandler/StateHandler.h @@ -16,11 +16,10 @@ #include "Counter.h" #include "DigitalIoPin.h" #include "Event.h" +#include "Fan.h" #include "GMP252.h" #include "HMP60.h" #include "LiquidCrystal.h" -#include "Modbus/ModbusMaster.h" -#include "Modbus/ModbusRegister.h" #include "PressureWrapper.h" #include "Timer.h" #include "nlohmann/json.hpp" @@ -78,8 +77,8 @@ typedef void (StateHandler::*state_pointer) (const Event &); class StateHandler { public: - StateHandler (LiquidCrystal *lcd, ModbusRegister *A01, - PressureWrapper *pressure, Timer *global); + StateHandler (LiquidCrystal *lcd, Fan *propeller, PressureWrapper *pressure, + Timer *global); virtual ~StateHandler (); /** Get currently set pressure @@ -135,7 +134,7 @@ private: int saved_curr_value[2] = { 0, 0 }; int sensors_data[4] = { 0 }; LiquidCrystal *_lcd; - ModbusRegister *_A01; + Fan *_propeller; PressureWrapper *_pressure; bool pressure_status; Timer *state_timer; diff --git a/esp-vent-main/src/Fan.cpp b/esp-vent-main/src/Fan.cpp index fde3f3b..12b1463 100644 --- a/esp-vent-main/src/Fan.cpp +++ b/esp-vent-main/src/Fan.cpp @@ -7,12 +7,19 @@ #include "Fan.h" -Fan::Fan(ModbusRegister * A01) { - // TODO Auto-generated constructor stub +Fan::Fan (ModbusRegister *A01) { this->_A01 = A01; } +Fan::~Fan () +{ + // TODO Auto-generated destructor stub } -Fan::~Fan() { - // TODO Auto-generated destructor stub -} - +void +Fan::spin (int speed) +{ + assert ((this->_A01)); + if (speed >= 0 && speed <= 1000) + { + this->_A01->write (speed); + } +} \ No newline at end of file diff --git a/esp-vent-main/src/StateHandler/StateHandler.cpp b/esp-vent-main/src/StateHandler/StateHandler.cpp index b5eda7d..b41a686 100644 --- a/esp-vent-main/src/StateHandler/StateHandler.cpp +++ b/esp-vent-main/src/StateHandler/StateHandler.cpp @@ -7,11 +7,11 @@ #include "StateHandler/StateHandler.h" -StateHandler::StateHandler (LiquidCrystal *lcd, ModbusRegister *_A01, +StateHandler::StateHandler (LiquidCrystal *lcd, Fan *propeller, PressureWrapper *pressure, Timer *global) { this->_lcd = lcd; - this->_A01 = _A01; + this->_propeller = propeller; this->_pressure = pressure; this->state_timer = global; current = &StateHandler::stateInit; @@ -120,7 +120,7 @@ StateHandler::stateManual (const Event &event) switch (event.type) { case Event::eEnter: - this->_A01->write (fan_speed.getCurrent ()); + this->_propeller->spin (fan_speed.getCurrent ()); break; case Event::eExit: break; @@ -139,7 +139,7 @@ StateHandler::stateAuto (const Event &event) switch (event.type) { case Event::eEnter: - this->_A01->write (fan_speed.getCurrent ()); + this->_propeller->spin (fan_speed.getCurrent ()); break; case Event::eExit: break; @@ -149,7 +149,7 @@ StateHandler::stateAuto (const Event &event) case Event::eTick: handleTickValue (event.value); pid (); - this->_A01->write (fan_speed.getCurrent ()); + this->_propeller->spin (fan_speed.getCurrent ()); break; } } diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 675bb67..395834a 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -17,6 +17,7 @@ #endif #include "DigitalIoPin.h" +#include "Fan.h" #include "LiquidCrystal.h" #include "PressureWrapper.h" #include "StateHandler/StateHandler.h" @@ -62,13 +63,12 @@ main (void) /* FAN object */ ModbusMaster fan (1); fan.begin (9600); - ModbusRegister A01 (&fan, 0); - // ModbusRegister DI1(&fan, 4, false); + Fan propeller (new ModbusRegister (&fan, 0)); PressureWrapper sens; glob_time.Sleep (1000); - StateHandler ventMachine (&lcd, &A01, &sens, &glob_time); + StateHandler ventMachine (&lcd, &propeller, &sens, &glob_time); /** Common pins */ DigitalIoPin b_up (0, 7, true, true, true); // A5 SwitchController sw_up (&b_up, &ventMachine, BUTTON_CONTROL_UP);