state-handler: format code and document

This commit is contained in:
Vasily Davydov 2022-10-25 15:16:19 +03:00
parent 42251f7ed7
commit cd06e2b3ba
2 changed files with 54 additions and 49 deletions

View File

@ -104,15 +104,11 @@ public:
* MANUAL MODE: SPEED: XX% PRESSURE: XXPa * MANUAL MODE: SPEED: XX% PRESSURE: XXPa
* *
* AUTO MODE: P. SET: XXPa P. CURR: XXPa * AUTO MODE: P. SET: XXPa P. CURR: XXPa
*
* @param sensors the current printing mode
*/ */
void displaySet (bool sensors); void displaySet (bool sensors);
/** Display values of sensors readings on LCD
* needed only for Debug purposes
* can be removed in production
*/
void displaySens ();
/** Handle the given event of the current state /** Handle the given event of the current state
* *
* @param event event to be handled in the current state * @param event event to be handled in the current state
@ -127,14 +123,19 @@ private:
void SetState (state_pointer newstate); void SetState (state_pointer newstate);
bool current_mode; bool current_mode;
Counter value[2] = { { 0, 100 }, { 0, 120 } }; Counter value[2] = { { 0, 100 }, { 0, 120 } };
/* motor of fan starts at value 90. probably because of some
/* Motor of fan starts at value 90. probably because of some
* weigh of fan, so voltage within range of 0-89 is not * weigh of fan, so voltage within range of 0-89 is not
* sufficient to start motor. * sufficient to start motor.
* TODO: Value 89 should be scaled to 0 at some point */ * TODO: Value 89 should be scaled to 0 at some point
*/
Counter fan_speed = { 80, 1000 }; Counter fan_speed = { 80, 1000 };
/*integral controller for PID. should be global, since it
* accumulates error signals encountered since startup*/ /* Integral controller for PID. should be global, since it
* accumulates error signals encountered since startup
*/
int integral = 0; int integral = 0;
int saved_set_value[2] = { 0, 0 }; int saved_set_value[2] = { 0, 0 };
int saved_curr_value[2] = { 0, 0 }; int saved_curr_value[2] = { 0, 0 };
int sensors_data[4] = { 0 }; int sensors_data[4] = { 0 };
@ -143,6 +144,7 @@ private:
PressureWrapper *pressure; PressureWrapper *pressure;
bool pressure_status; bool pressure_status;
Timer *state_timer; Timer *state_timer;
/* CO2 sensor object */ /* CO2 sensor object */
GMP252 co2; GMP252 co2;
@ -208,7 +210,7 @@ private:
*/ */
void pid (); void pid ();
int fan_speed_normalized(); int fan_speed_normalized ();
}; };
#endif /* STATE_HANDLER_H_ */ #endif /* STATE_HANDLER_H_ */

View File

@ -31,20 +31,24 @@ StateHandler::displaySet (bool sensors)
char line_up[16] = { 0 }; char line_up[16] = { 0 };
char line_down[16] = { 0 }; char line_down[16] = { 0 };
if(sensors) { if (sensors)
{
snprintf (line_up, 16, "PRE:%02d TEM:%02d", sensors_data[PRESSUREDAT], snprintf (line_up, 16, "PRE:%02d TEM:%02d", sensors_data[PRESSUREDAT],
sensors_data[TEMPERATURE]); sensors_data[TEMPERATURE]);
snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY], snprintf (line_down, 16, "HUM:%02d CO2:%02d", sensors_data[HUMIDITY],
sensors_data[CO2]); sensors_data[CO2]);
}
} else if (current_mode == MANUAL) else if (current_mode == MANUAL)
{ {
snprintf (line_up, 16, "SPEED: %02d%", saved_set_value[current_mode]); snprintf (line_up, 16, "SPEED: %02d%", saved_set_value[current_mode]);
snprintf (line_down, 16, "PRESSURE: %02dPa", saved_curr_value[current_mode]); snprintf (line_down, 16, "PRESSURE: %02dPa",
} else if(current_mode == AUTO) saved_curr_value[current_mode]);
}
else if (current_mode == AUTO)
{ {
snprintf (line_up, 16, "P. SET: %02dPa", saved_set_value[current_mode]); snprintf (line_up, 16, "P. SET: %02dPa", saved_set_value[current_mode]);
snprintf (line_down, 16, "P. CURR: %02dPa", saved_curr_value[current_mode]); snprintf (line_down, 16, "P. CURR: %02dPa",
saved_curr_value[current_mode]);
} }
_lcd->clear (); _lcd->clear ();
@ -53,11 +57,6 @@ StateHandler::displaySet (bool sensors)
_lcd->setCursor (0, 1); _lcd->setCursor (0, 1);
_lcd->print (line_down); _lcd->print (line_down);
} }
void
StateHandler::displaySens ()
{
}
unsigned int unsigned int
StateHandler::getSetPressure () StateHandler::getSetPressure ()
@ -117,7 +116,7 @@ StateHandler::stateManual (const Event &event)
switch (event.type) switch (event.type)
{ {
case Event::eEnter: case Event::eEnter:
this->A01->write (fan_speed_normalized()); this->A01->write (fan_speed_normalized ());
break; break;
case Event::eExit: case Event::eExit:
break; break;
@ -125,9 +124,10 @@ StateHandler::stateManual (const Event &event)
handleControlButtons (event.value); handleControlButtons (event.value);
break; break;
case Event::eTick: case Event::eTick:
if(event.value % 5000 == 0) { if (event.value % 5000 == 0)
SetState(&StateHandler::stateSensors); {
displaySet(SENSORS); SetState (&StateHandler::stateSensors);
displaySet (SENSORS);
} }
if (event.value % 500 == 0) if (event.value % 500 == 0)
{ {
@ -155,11 +155,12 @@ StateHandler::stateAuto (const Event &event)
{ {
SetState (&StateHandler::stateGetPressure); SetState (&StateHandler::stateGetPressure);
} }
if(event.value % 150 == 0) { if (event.value % 150 == 0)
SetState(&StateHandler::stateSensors); {
// displaySens (); SetState (&StateHandler::stateSensors);
// displaySens ();
} }
pid(); pid ();
this->A01->write (fan_speed.getCurrent ()); this->A01->write (fan_speed.getCurrent ());
break; break;
} }
@ -175,7 +176,7 @@ StateHandler::stateSensors (const Event &event)
sensors_data[TEMPERATURE] = humidity.readT (); sensors_data[TEMPERATURE] = humidity.readT ();
sensors_data[HUMIDITY] = humidity.readRH (); sensors_data[HUMIDITY] = humidity.readRH ();
sensors_data[CO2] = co2.read (); sensors_data[CO2] = co2.read ();
// displaySens (); // displaySens ();
break; break;
case Event::eExit: case Event::eExit:
break; break;
@ -183,7 +184,8 @@ StateHandler::stateSensors (const Event &event)
handleControlButtons (event.value); handleControlButtons (event.value);
break; break;
case Event::eTick: case Event::eTick:
// save (pressure->getPressure (), ((current_mode) ? AUTO : MANUAL)); // save (pressure->getPressure (), ((current_mode) ? AUTO :
// MANUAL));
SetState (current_mode ? &StateHandler::stateAuto SetState (current_mode ? &StateHandler::stateAuto
: &StateHandler::stateManual); : &StateHandler::stateManual);
break; break;
@ -250,9 +252,10 @@ StateHandler::save (int eventValue, size_t mode)
} }
int int
StateHandler::fan_speed_normalized() { StateHandler::fan_speed_normalized ()
int speed = value[MANUAL].getCurrent(); {
if(speed <=92) int speed = value[MANUAL].getCurrent ();
if (speed <= 92)
speed += 8; speed += 8;
return speed * 10; return speed * 10;
} }