-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhardware.hpp
255 lines (188 loc) · 7.12 KB
/
hardware.hpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// SPDX-FileCopyrightText: 2020-2021 Ivan Ivanov
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef _HARDWARE_
#define _HARDWARE_
#include "sensors.hpp"
#include <TimeLib.h>
//#include <SoftWire.h>
//#include "icon_menu.h"
#include <ArduinoJson.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include "sensors.hpp"
#include "stat_records.hpp"
#include "stat_server.hpp"
#include "display.hpp"
#include "ulp_main.h"
#include "control.hpp"
enum sensor_state_t{
SENSOR_GOTOSLEEP = 0,
SENSOR_SLEEPING,
SENSOR_AWAKE,
SENSOR_WRITE_RESULT
};
static const char * sensor_state_name[] = { "SENSOR_GOTOSLEEP", "SENSOR_SLEEPING", "SENSOR_AWAKE", "SENSOR_WRITE_RESULT" };
enum control_state_t{
CONTROL_GOTOSLEEP = 0,
CONTROL_SLEEPING,
CONTROL_AWAKE,
CONTROL_WIFI_INIT,
};
static const char * control_state_name[] = {"CONTROL_GOTOSLEEP", "CONTROL_SLEEPING", "CONTROL_AWAKE", "CONTROL_WIFI_INIT"};
static const char * espSleepWake[] = {
"ESP_SLEEP_WAKEUP_UNDEFINED",
"ESP_SLEEP_WAKEUP_ALL",
"ESP_SLEEP_WAKEUP_EXT0",
"ESP_SLEEP_WAKEUP_EXT1",
"ESP_SLEEP_WAKEUP_TIMER",
"ESP_SLEEP_WAKEUP_TOUCHPAD",
"ESP_SLEEP_WAKEUP_ULP",
"ESP_SLEEP_WAKEUP_GPIO",
"ESP_SLEEP_WAKEUP_UART"
};
#define IS_GRAPH_ACTIVITY(act) ( GRAPH_BODY_TEMP_ACTIVITY == act || GRAPH_STEPS_ACTIVITY == act )
#define GPIO_BY_NUM2(X) GPIO_NUM_ ## X
#define GPIO_BY_NUM(X) GPIO_BY_NUM2(X)
class Control {
private:
uint64_t _Button_State = 0;
uint64_t _Button_PrevState = 0;
gpio_num_t LEFT_BUTTON = GPIO_BY_NUM( CONTROL_LEFT_BUTTON );
gpio_num_t RIGHT_BUTTON = GPIO_BY_NUM( CONTROL_RIGHT_BUTTON );
gpio_num_t OK_BUTTON = GPIO_BY_NUM( CONTROL_OK_BUTTON );
void printButtonState (const char *func_name, uint32_t line_number, int l_State, int r_State, int o_State );
public:
void init();
bool buttonPressed();
void initWakeup();
gpio_num_t pinOk( ) { return OK_BUTTON;};
gpio_num_t pinLeft( ) { return LEFT_BUTTON;};
gpio_num_t pinRight( ) { return RIGHT_BUTTON;};
uint64_t buttons() { return _Button_State;};
};
#define UID_SIZE 32
#define TOKEN_SIZE 64
#define SERVER_ADDR_SIZE 64
class Config {
private:
bool temp_sensor_switch;
bool step_sensor_switch;
bool pulse_sensor_switch;
bool baro_sensor_switch;
int8_t timezone;
bool auto_update_time_over_wifi;
char server_uid[UID_SIZE];
char server_token[TOKEN_SIZE];
char server_addr[SERVER_ADDR_SIZE];
char *filename = "/config.txt";
bool need_save = false;
void updateUlpConfig();
void initDefaultValues();
public:
void init();
void load();
void save();
void setSaveFlag(bool _save) { need_save = _save;}
void setToMenu();
void cast_from_menu();
void setCurrenDayTimeToMenu();
void setTempSensorSwitch(bool val) { temp_sensor_switch = val;}
void setStepSensorSwitch(bool val) { step_sensor_switch = val;}
void setPulseSensorSwitch(bool val) { pulse_sensor_switch = val;}
void setBaroSensorSwitch(bool val) { baro_sensor_switch = val;}
void setServerUid(const char* u) ;
void setServerToken(const char * t);
void setServerAddr(const char * a);
void setTimezone(int8_t val) { timezone = val;}
bool getTempSensorSwitch(){ return temp_sensor_switch;}
bool getStepSensorSwitch(){ return step_sensor_switch;}
bool getPulseSensorSwitch(){ return pulse_sensor_switch;}
bool getBaroSensorSwitch(){ return baro_sensor_switch;}
char *getServerUid();
char *getServerToken();
char *getServerAddr();
int getTimezone() { return timezone;}
};
void saveWifiConfigCallback();
#define HR_LOG_MAX_SIZE 512000
//#define HR_LOG_MAX_SIZE 256000
//#define HR_LOG_SYNC_STEP 21600 // 6 hours
#define HR_LOG_SYNC_STEP 86200 //24 hours
class Hardware {
private:
bool use_wifi = false;
uint8_t ulpSdaPin = GPIO_SDA ;
uint8_t ulpSclPin = GPIO_SCL;
SoftWire *ulpI2c;// (sdaPin, sclPin);
//int32_t wifi_stage;
Config cfg;
Gyroscope sGyro;
ThermoMeter sTerm;
PulseMeter sPulse;
CurrentMeter sCurrent;
BME280Meter sBME280;
TimeMeter sTime;
Display display;
Control control;
FileSystem log_file;
HrLog hr_stat;
WiFiManager wm;
WiFiManagerParameter *api_uid_server = NULL;
WiFiManagerParameter *api_key_server = NULL;
WiFiManagerParameter *api_addr_server = NULL;
time_t currentTime;
bool isAfterDeepsleep = false;
esp_sleep_wakeup_cause_t wakeupReason;
uint8_t pulse_threshold = 3;
uint32_t displaySleepDelay = 4000; //
uint32_t displaySleepTimer = 0;
Graph graph;
bool debugTraceFsm = false;
bool powerSave = false;
void initSensors();
void serialInit();
void timeRead();
int nextWakeTime();
void goToSleep() ;
void readSensors();
statRecord_t *getCurrentSensorsData(statRecord_t *record);
statRecord_t *getLastSensorsData ();
void printAllStat();
void printStat(statRecord_t *record);
void printFsmState(const char *func_name, uint32_t line_number);
bool isWakeByDeepsleep(esp_sleep_wakeup_cause_t wakeupReason);
bool isNewDay();
void runActivity(uint64_t buttons);
void updateFromConfig();
void prepareGraphData(displayActivity_t act, int32_t _day, int32_t _month, int32_t _year );
struct tm * changeDayFile(int32_t _day);
struct tm * loadNextDayFile();
struct tm * loadPrevDayFile();
void getTimeOverWifi();
void updateSkimlog();
struct tm * takeLogDayFile(unsigned int curValue);
void processHrData();
void processStatData();
public:
void init();
void update();
void runPowerSafe();
// menu logic
void setTime( TimeStorage tm);
void setDate( DateStorage dt);
void setTimezone( int tz , bool need_save);
void setTempSwitch(bool val, bool need_save);
void setPedoSwitch(bool val, bool need_save);
void setPulseSwitch(bool val, bool need_save);
void setBaroSwitch(bool val, bool need_save);
void showActivity(displayActivity_t act);
void updateWebApiConfig();
void showWifiPortal();
void syncTimeViaWifi();
void syncStatViaWifi();
void syncStatHrViaWifi();
void start_graph_logic(displayActivity_t _activity, unsigned int curValue);
void setGraphStarting(bool val) { graph.starting = val; };
bool getGraphStarting() { return graph.starting; };
void showGraph();
};
#endif