forked from doudar/SmartSpin2k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartSpin_parameters.cpp
323 lines (283 loc) · 10.3 KB
/
SmartSpin_parameters.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include "Main.h"
#include "SS2KLog.h"
#include "SmartSpin_parameters.h"
#include <ArduinoJson.h>
#include <SPIFFS.h>
String RuntimeParameters::returnJSON() {
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<RUNTIMECONFIG_JSON_SIZE> doc;
// Set the values in the document
doc["targetIncline"] = targetIncline;
doc["currentIncline"] = currentIncline;
doc["targetWatts"] = targetWatts;
doc["simulatedWatts"] = simulatedWatts.value;
doc["simulatedHr"] = simulatedHr;
doc["simulatedCad"] = simulatedCad;
doc["simulatedSpeed"] = simulatedSpeed;
doc["simulateHr"] = simulateHr;
doc["simulateWatts"] = simulateWatts;
doc["simulateTargetWatts"] = simulateTargetWatts;
doc["simulateCad"] = simulateCad;
doc["ERGMode"] = ERGMode;
doc["shifterPosition"] = shifterPosition;
doc["minStep"] = minStep;
doc["maxStep"] = maxStep;
String output;
serializeJson(doc, output);
return output;
}
// Default Values
void userParameters::setDefaults() {
firmwareUpdateURL = FW_UPDATEURL;
deviceName = DEVICE_NAME;
shiftStep = DEFAULT_SHIFT_STEP;
stealthchop = STEALTHCHOP;
stepperPower = DEFAULT_STEPPER_POWER;
inclineMultiplier = 3.0;
powerCorrectionFactor = 1.0;
ERGSensitivity = ERG_SENSITIVITY;
autoUpdate = AUTO_FIRMWARE_UPDATE;
ssid = DEVICE_NAME;
password = DEFAULT_PASSWORD;
connectedPowerMeter = CONNECTED_POWER_METER;
connectedHeartMonitor = CONNECTED_HEART_MONITOR;
maxWatts = DEFAULT_MAX_WATTS;
stepperDir = true;
shifterDir = true;
udpLogEnabled = false;
}
//---------------------------------------------------------------------------------
//-- return all config as one a single JSON string
String userParameters::returnJSON() {
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<USERCONFIG_JSON_SIZE> doc;
// Set the values in the document
doc["firmwareUpdateURL"] = firmwareUpdateURL;
doc["firmwareVersion"] = FIRMWARE_VERSION;
doc["deviceName"] = deviceName;
doc["shiftStep"] = shiftStep;
doc["stepperPower"] = stepperPower;
doc["stealthchop"] = stealthchop;
doc["inclineMultiplier"] = inclineMultiplier;
doc["powerCorrectionFactor"] = powerCorrectionFactor;
doc["ERGSensitivity"] = ERGSensitivity;
doc["autoUpdate"] = autoUpdate;
doc["ssid"] = ssid;
doc["password"] = password;
doc["connectedPowerMeter"] = connectedPowerMeter;
doc["connectedHeartMonitor"] = connectedHeartMonitor;
doc["foundDevices"] = foundDevices;
doc["maxWatts"] = maxWatts;
doc["shifterDir"] = shifterDir;
doc["stepperDir"] = stepperDir;
doc["udpLogEnabled"] = udpLogEnabled;
String output;
serializeJson(doc, output);
return output;
}
//-- Saves all parameters to SPIFFS
void userParameters::saveToSPIFFS() {
// Delete existing file, otherwise the configuration is appended to the file
SPIFFS.remove(configFILENAME);
// Open file for writing
SS2K_LOG(CONFIG_LOG_TAG, "Writing File: %s", configFILENAME);
File file = SPIFFS.open(configFILENAME, FILE_WRITE);
if (!file) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to create file");
return;
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<USERCONFIG_JSON_SIZE> doc;
// Set the values in the document
// commented items are not needed in save file
doc["firmwareUpdateURL"] = firmwareUpdateURL;
doc["deviceName"] = deviceName;
doc["shiftStep"] = shiftStep;
doc["stepperPower"] = stepperPower;
doc["stealthchop"] = stealthchop;
doc["inclineMultiplier"] = inclineMultiplier;
doc["powerCorrectionFactor"] = powerCorrectionFactor;
doc["ERGSensitivity"] = ERGSensitivity;
doc["autoUpdate"] = autoUpdate;
doc["ssid"] = ssid;
doc["password"] = password;
doc["connectedPowerMeter"] = connectedPowerMeter;
doc["connectedHeartMonitor"] = connectedHeartMonitor;
doc["foundDevices"] = foundDevices;
doc["maxWatts"] = maxWatts;
doc["shifterDir"] = shifterDir;
doc["stepperDir"] = stepperDir;
doc["udpLogEnabled"] = udpLogEnabled;
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to write to file");
}
// Close the file
file.close();
}
// Loads the JSON configuration from a file into a userParameters Object
void userParameters::loadFromSPIFFS() {
setDefaults();
// Open file for reading
SS2K_LOG(CONFIG_LOG_TAG, "Reading File: %s", configFILENAME);
File file = SPIFFS.open(configFILENAME);
// load defaults if filename doesn't exist
if (!file) {
SS2K_LOG(CONFIG_LOG_TAG, "Couldn't find configuration file. using defaults");
return;
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<USERCONFIG_JSON_SIZE> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to read file, using defaults");
return;
}
// Copy values from the JsonDocument to the Config
setFirmwareUpdateURL(doc["firmwareUpdateURL"]);
setDeviceName(doc["deviceName"]);
setShiftStep(doc["shiftStep"]);
setStepperPower(doc["stepperPower"]);
setStealthChop(doc["stealthchop"]);
setInclineMultiplier(doc["inclineMultiplier"]);
setAutoUpdate(doc["autoUpdate"]);
setSsid(doc["ssid"]);
setPassword(doc["password"]);
setConnectedPowerMeter(doc["connectedPowerMeter"]);
setConnectedHeartMonitor(doc["connectedHeartMonitor"]);
setFoundDevices(doc["foundDevices"]);
if (doc["ERGSensitivity"]) { // If statements to upgrade old versions of config.txt that didn't include these
setERGSensitivity(doc["ERGSensitivity"]);
}
if (doc["maxWatts"]) {
setMaxWatts(doc["maxWatts"]);
}
if (!doc["stepperDir"].isNull()) {
setStepperDir(doc["stepperDir"]);
}
if (!doc["shifterDir"].isNull()) {
setShifterDir(doc["shifterDir"]);
}
if (!doc["udpLogEnabled"].isNull()) {
setUdpLogEnabled(doc["udpLogEnabled"]);
}
if (doc["powerCorrectionFactor"]) {
setPowerCorrectionFactor(doc["powerCorrectionFactor"]);
if ((getPowerCorrectionFactor() < MIN_PCF) || (getPowerCorrectionFactor() > MAX_PCF)) {
setPowerCorrectionFactor(1);
}
}
SS2K_LOG(CONFIG_LOG_TAG, "Config File Loaded: %s", configFILENAME);
file.close();
}
// Prints the content of a file to the Serial
void userParameters::printFile() {
// Open file for reading
SS2K_LOG(CONFIG_LOG_TAG, "Contents of file: %s", configFILENAME);
File file = SPIFFS.open(configFILENAME);
if (!file) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to read file");
return;
}
// Close the file
file.close();
}
/*****************************************USERPWC*****************************************/
void physicalWorkingCapacity::setDefaults() {
session1HR = 129; // examples from https://www.cyclinganalytics.com/
session1Pwr = 100;
session2HR = 154;
session2Pwr = 150;
hr2Pwr = false;
}
//-- return all config as one a single JSON string
String physicalWorkingCapacity::returnJSON() {
StaticJsonDocument<500> doc;
doc["session1HR"] = session1HR;
doc["session1Pwr"] = session1Pwr;
doc["session2HR"] = session2HR;
doc["session2Pwr"] = session2Pwr;
doc["hr2Pwr"] = hr2Pwr;
String output;
serializeJson(doc, output);
return output;
}
//-- Saves all parameters to SPIFFS
void physicalWorkingCapacity::saveToSPIFFS() {
// Delete existing file, otherwise the configuration is appended to the file
SPIFFS.remove(userPWCFILENAME);
// Open file for writing
SS2K_LOG(CONFIG_LOG_TAG, "Writing File: %s", userPWCFILENAME);
File file = SPIFFS.open(userPWCFILENAME, FILE_WRITE);
if (!file) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to create file");
return;
}
StaticJsonDocument<500> doc;
doc["session1HR"] = session1HR;
doc["session1Pwr"] = session1Pwr;
doc["session2HR"] = session2HR;
doc["session2Pwr"] = session2Pwr;
doc["hr2Pwr"] = hr2Pwr;
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to write to file");
}
// Close the file
file.close();
}
// Loads the JSON configuration from a file
void physicalWorkingCapacity::loadFromSPIFFS() {
// Open file for reading
SS2K_LOG(CONFIG_LOG_TAG, "Reading File: %s", userPWCFILENAME);
File file = SPIFFS.open(userPWCFILENAME);
// load defaults if filename doesn't exist
if (!file) {
SS2K_LOG(CONFIG_LOG_TAG, "Couldn't find configuration file. Loading Defaults");
setDefaults();
return;
}
StaticJsonDocument<500> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to read file, using default configuration");
setDefaults();
return;
}
// Copy values from the JsonDocument to the Config
session1HR = doc["session1HR"];
session1Pwr = doc["session1Pwr"];
session2HR = doc["session2HR"];
session2Pwr = doc["session2Pwr"];
hr2Pwr = doc["hr2Pwr"];
SS2K_LOG(CONFIG_LOG_TAG, "Config File Loaded: %s", userPWCFILENAME);
file.close();
}
// Prints the content of a file to the Serial
void physicalWorkingCapacity::printFile() {
// Open file for reading
SS2K_LOG(CONFIG_LOG_TAG, "Contents of file: %s", userPWCFILENAME);
File file = SPIFFS.open(userPWCFILENAME);
if (!file) {
SS2K_LOG(CONFIG_LOG_TAG, "Failed to read file");
return;
}
// Close the file
file.close();
}