-
Notifications
You must be signed in to change notification settings - Fork 1
/
WateringSystem.cpp
65 lines (52 loc) · 1.83 KB
/
WateringSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
//
//
#include "WateringSystem.h"
WateringSystem::WateringSystem()
{
this->_ambientalSensor = new DHTSensor(AmbientalTemperaturePin);
this->_plantWaterSensor = new WaterSensor(PlantWaterSensorPin);
this->_pumpWaterSensor = new WaterSensor(PumpWaterSensorPin);
this->_waterPump = new WaterPump(WaterPumpPowerPin, this->_pumpWaterSensor);
this->_waterPumpManualOverride = new ToggleSwitch(WaterPumpOverridePin);
this->_userOverridenPump = new UserOverridenWateringState(*(this->_waterPumpManualOverride), *(this->_waterPump));
IdleSystemState* idleState = new IdleSystemState(*(this->_ambientalSensor));
this->_currentState = idleState;
this->_currentState->EnterState();
//lower index value means higher priority
this->_systemStates[0] = this->_userOverridenPump;
this->_systemStates[1] = idleState;
}
void WateringSystem::Update(Graphics* graphics)
{
//Serial.println("loop");
CpuSpinner::Update();
//todo: a state with a higher priority and enterable should be able to force it out
if (this->_currentState->CanLeaveState()) {
SystemState* nextState = NULL;
for (uint8_t i = 0; i < KnownStatesCount; i++)
{
SystemState* currentState = this->_systemStates[i];
if (currentState->CanEnterState()) {
nextState = currentState;
break;
}
}
if (nextState != NULL && this->_currentState != nextState) {
this->_currentState->LeaveState();
nextState->EnterState();
this->_currentState = nextState;
graphics->Clear();//give a clear screen to work with
}
}
this->_currentState->DoWork();
//RENDER STATE
this->_currentState->Render(*graphics);
/*
states:
4-defaultState - show ambiental temperature, air humidity, soil humidity and some graphics
3-wateringState - while watering the plant
2-manualOverrideState
1-missingWaterState
*/
}