-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
145 lines (126 loc) · 3.76 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <Arduino.h>
#include <SPI.h>
#include <WiFi.h>
#include <list>
#include <map>
#include "ArduinoNvs.h"
#include "logging.h"
#include "submitters/submitter.h"
#include "sensors/home_sensor.h"
#include "states/states.h"
#include "config.h"
#include "time/every.h"
#include "time/after.h"
#ifndef VERSION_NUMBER
#define VERSION_NUMBER "0.0.1"
#endif
// General configuration
#define MICROSECONDS_IN_SECOND 1000000L
#define FAILURE_BACKOFF 5L * MICROSECONDS_IN_SECOND
#define REPORT_PERIOD 300L * MICROSECONDS_IN_SECOND
std::list<ReadingSubmitter*> submitters;
std::list<HomeSensor*> sensors;
State currentState = ST_CONFIG_CHECK;
void goToSleep(long microSeconds) {
std::list<HomeSensor*>::iterator it;
bool allDone = true;
for (it = sensors.begin(); it != sensors.end(); ++it)
{
HomeSensor *item = (*it);
item->switchOff();
}
delay(20);
esp_sleep_enable_timer_wakeup(microSeconds);
esp_deep_sleep_start();
}
static After rebootWait;
static int lastButtonState;
void setup(void)
{
Serial.begin(115200);
Serial.printf("Version %s\n", VERSION_NUMBER);
rebootWait.start();
lastButtonState = digitalRead(0);
}
static Every eachSecond(1000);
static Every eachHalfSecond(500);
static int pressed = 0;
int released = 0;
void loop(void)
{
int buttonState = digitalRead(0);
if (buttonState == 0 && lastButtonState == 1)
{
logln("Pressed");
++pressed;
}
else if (buttonState == 1 && lastButtonState == 0)
{
logln("Released");
++released;
}
lastButtonState = buttonState;
if (pressed >= 2 && released >= 2)
{
logln("Double-click detected - entering re-config mode");
Config::instance()->reconfigure();
ESP.restart();
}
switch (currentState)
{
case ST_CONFIG_CHECK:
currentState = state_config_check(currentState);
break;
case ST_CONFIG_ACTIVATE:
currentState = state_config_activate(currentState);
break;
case ST_CONFIG_ACCEPT:
currentState = state_config_accept(currentState);
break;
case ST_FEATURE_ENABLE:
currentState = state_feature_enable(currentState, &submitters, &sensors);
break;
case ST_NETWORK_ACTIVATE:
currentState = state_network_activate(currentState);
break;
case ST_NETWORK_WAIT:
currentState = state_network_wait(currentState);
break;
case ST_FIRMWARE_CHECK:
currentState = state_firmware_check(currentState);
break;
case ST_SUBMITTER_ACTIVATE:
currentState = state_submitter_activate(currentState, &submitters);
break;
case ST_SUBMITTER_WAIT:
currentState = state_submitter_wait(currentState, &submitters);
break;
case ST_SENSOR_ACTIVATE:
currentState = state_sensor_activate(currentState, &sensors);
break;
case ST_SENSOR_WAIT:
currentState = state_sensor_wait(currentState, &sensors);
break;
case ST_SENSOR_SEND:
currentState = state_sensor_submit(currentState, &submitters, &sensors);
break;
case ST_DONE:
if (rebootWait.isAfter(3000))
{
goToSleep(REPORT_PERIOD);
}
else
{
eachHalfSecond.run([]{ logln("Delay restart to allow reset button checks"); });
}
break;
case ST_REBOOT:
ESP.restart();
break;
case ST_ERROR:
goToSleep(FAILURE_BACKOFF);
break;
default:
eachSecond.run([] { logfmt("I don't understand state: %d\n", currentState); });
}
}