state-handler: #6 add state controller functions

- HandleState to set an event of the state
- SetState to update current state to new
This commit is contained in:
Vasily Davydov 2022-10-05 15:02:02 +03:00
parent f95c15e13c
commit 69d767a73e
2 changed files with 74 additions and 60 deletions

View File

@ -15,10 +15,10 @@
#ifndef STATE_HANDLER_H_ #ifndef STATE_HANDLER_H_
#define STATE_HANDLER_H_ #define STATE_HANDLER_H_
#include "DigitalIoPin.h"
#include "LiquidCrystal.h"
#include "Counter.h" #include "Counter.h"
#include "DigitalIoPin.h"
#include "Event.h" #include "Event.h"
#include "LiquidCrystal.h"
/** Buttons enumeration /** Buttons enumeration
* *
@ -28,7 +28,8 @@
* of the particular button. * of the particular button.
* */ * */
enum _buttons { enum _buttons
{
/** Raises the bar up */ /** Raises the bar up */
BUTTON_CONTROL_UP, BUTTON_CONTROL_UP,
/** Raises the bar down */ /** Raises the bar down */
@ -42,60 +43,62 @@ enum _buttons {
BUTTON_CONTROL_TOG_ACTIVE BUTTON_CONTROL_TOG_ACTIVE
}; };
enum _bars { enum _bars
{
/** 0-100 % */ /** 0-100 % */
FAN_SPEED, FAN_SPEED,
/** 0-120 Pa */ /** 0-120 Pa */
PRESSURE PRESSURE
}; };
enum _mode { enum _mode
{
MANUAL, MANUAL,
AUTO AUTO
}; };
class StateHandler; class StateHandler;
typedef void (StateHandler::*state_pointer)(const Event &); typedef void (StateHandler::*state_pointer) (const Event &);
class StateHandler
class StateHandler { {
public: public:
StateHandler(LiquidCrystal * lcd); StateHandler (LiquidCrystal *lcd);
virtual ~StateHandler(); virtual ~StateHandler ();
/** Get currently set pressure /** Get currently set pressure
* *
* @return pressure in range of 0-120 * @return pressure in range of 0-120
*/ */
unsigned int getSetPressure(); unsigned int getSetPressure ();
/** Get currently set FanSpeed /** Get currently set FanSpeed
* *
* @return speed in range of 0-100 * @return speed in range of 0-100
*/ */
unsigned int getSetSpeed(); unsigned int getSetSpeed ();
/** Display values on LCD depending on current mode /** Display values on LCD depending on current mode
* *
* @param value1 value to be displayed on LCD line 0 * @param value1 value to be displayed on LCD line 0
* @param value2 value to be displayed on LCD line 1 * @param value2 value to be displayed on LCD line 1
*/ */
void displaySet(unsigned int value1, unsigned int value2); void displaySet (unsigned int value1, unsigned int value2);
/** 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
*/ */
void HandleState(const Event &event); void HandleState (const Event &event);
private: private:
state_pointer current; state_pointer current;
/** Set a new curremt state /** Set a new curremt state
* @param newstate new state to be set to current * @param newstate new state to be set to current
*/ */
void SetState(state_pointer newstate); void SetState (state_pointer newstate);
bool current_mode; bool current_mode;
Counter set [2] = {{0, 100}, {0, 120}}; Counter set[2] = { { 0, 100 }, { 0, 120 } };
LiquidCrystal * _lcd; LiquidCrystal *_lcd;
}; };
#endif /* STATE_HANDLER_H_ */ #endif /* STATE_HANDLER_H_ */

View File

@ -61,3 +61,14 @@ StateHandler::SetState (state_pointer newstate)
current = newstate; current = newstate;
(this->*current) (Event (Event::eEnter)); (this->*current) (Event (Event::eEnter));
} }
void StateHandler::HandleState(const Event &event){
(this->*current)(event);
}
void StateHandler::SetState(state_pointer newstate){
(this->*current)(Event(Event::eExit));
current = newstate;
(this->*current)(Event(Event::eEnter));
}