This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFireThing.cpp
144 lines (124 loc) · 3.29 KB
/
FireThing.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
#include "thing/FireThing.h"
#include "Arduino.h"
#include "FS.h"
namespace thing {
namespace {
Config kDefaultConfig = {
"", // firebase host
"", // firebase auth
"/fthing", // path in firebase
"", // wifi ssid
"", // wifi key
0.1, // analog activation threshold
2, // wifi connect attempts
{
D1, // digital in
BUILTIN_LED, // digital out
A0, // analog in
D1, // analog out
D0, // config mode button
}
};
const char kStorageFilename[] = "fthing.cfg";
} // namespace
FireThing::FireThing() : debug_([](const char*) {}) {}
bool FireThing::Setup() {
Config config;
if (!ReadConfigFromStorage(&config)) {
debug_("Failed to read config from storage.");
return false;
}
SetPinModes(config);
if (digitalRead(config.pins.config_mode_button) == HIGH || !ConnectToWiFi(config)) {
wifi_.StartAP();
}
portal_.NotifyOnUpdate([this](const Config& config) {
if (!WriteConfigToStorage(config)) {
debug_("Failed to write config to storage.");
}
SetPinModes(config);
transcriber_.UpdateConfig(config);
ConnectToWiFi(config);
});
portal_.Start(config);
}
void FireThing::Loop() {
wifi_.Loop();
portal_.Loop();
transcriber_.Loop();
}
bool FireThing::DeleteStoredConfig() {
if (!SPIFFS.begin()) {
debug_("Failed to mount FS.");
return false;
}
bool success = SPIFFS.remove(kStorageFilename);
SPIFFS.end();
return success;
}
bool FireThing::ConnectToWiFi(const Config& config) {
debug_("Connecting to wifi:");
debug_(config.wifi_ssid.c_str());
// TODO we should probably not print the key to serial.
debug_(config.wifi_key.c_str());
if (wifi_.Connect(config.wifi_ssid, config.wifi_key)) {
debug_("Connected");
return true;
}
debug_("Failed to Connect.");
return false;
}
void FireThing::SetPinModes(const Config& config) {
pinMode(config.pins.digital_in, INPUT);
pinMode(config.pins.digital_out, OUTPUT);
pinMode(config.pins.analog_in, INPUT);
pinMode(config.pins.analog_out, OUTPUT);
pinMode(config.pins.config_mode_button, INPUT);
}
bool FireThing::ReadConfigFromStorage(Config* config) {
if (!SPIFFS.begin()) {
debug_("Failed to mount FS.");
return false;
}
if (!SPIFFS.exists(kStorageFilename)) {
debug_("Config not found, using default.");
*config = kDefaultConfig;
} else {
File cfg = SPIFFS.open(kStorageFilename, "r");
if (!cfg) {
debug_("Failed to open config for read");
SPIFFS.end();
return false;
}
char buffer[cfg.size()];
cfg.readBytes(buffer, cfg.size());
ConfigJsonSerializer serializer(buffer);
serializer.DeserializeTo(config);
debug_("Config read from disk.");
}
SPIFFS.end();
return true;
}
bool FireThing::WriteConfigToStorage(const Config& config) {
if (!SPIFFS.begin()) {
debug_("Failed to mount FS.");
return false;
}
File cfg = SPIFFS.open(kStorageFilename, "w");
if (!cfg) {
debug_("Failed to open config for write");
SPIFFS.end();
return false;
}
ConfigJsonSerializer serializer(config);
serializer.SerializeTo(&cfg);
SPIFFS.end();
return true;
}
void FireThing::SetDebugHandler(std::function<void(const char* message)> debug) {
debug_ = debug;
wifi_.SetDebugHandler(debug);
portal_.SetDebugHandler(debug);
transcriber_.SetDebugHandler(debug);
}
} // namespace thing