-
Notifications
You must be signed in to change notification settings - Fork 7
/
tools.cpp
342 lines (243 loc) · 8.95 KB
/
tools.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
//
//
#include "config_TPM.h"
#include "tools.h"
#include "leds.h"
#include "config_fs.h"
#include <time.h>
#include "wifi-ota.h" // needs Wifi and co for data stuctures!!!
// Global Bool Structure soi that we only use 1 Bit and not a byte for a Bool
// use tool.h to read and write
uint8_t global_options[NR_GLOBAL_OPTIONS_BYTES] = { B00000001, B00000000, B00000000 }; //{ B00001111, B00000000 };
extern led_cfg_struct led_cfg;
boolean setup_controlls()
{
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(POTI_BRI_PIN, INPUT);
pinMode(POTI_FPS_PIN, INPUT);
return true;
}
// Bools functions
boolean get_bool(uint8_t bit_nr)
{
boolean return_bool = 0;
uint8_t byte_nr = 0;
while (bit_nr > 7)
{
byte_nr++;
bit_nr = bit_nr - 8;
}
if (byte_nr < NR_GLOBAL_OPTIONS_BYTES)
return_bool = bitRead(global_options[byte_nr], bit_nr);
return return_bool;
}
boolean get_bool(uint8_t inByte, uint8_t bit_nr)
{
boolean return_bool = 0;
return_bool = bitRead(inByte, bit_nr);
return return_bool;
}
void write_bool(uint8_t bit_nr, boolean value)
{
uint8_t byte_nr = 0;
while (bit_nr > 7)
{
byte_nr++;
bit_nr = bit_nr - 8;
}
if (byte_nr < NR_GLOBAL_OPTIONS_BYTES)
bitWrite(global_options[byte_nr], bit_nr, value);
//yield();
}
void load_bool()
{ // Load the global bools from disk or defaults
//debugMe("loading Bools");
//if (FS_Bools_read(0) == false)
debugMe("pre_Bool_LOAD");
//debugMe(FS_Bools_read(0));
//debugMe("PAST_BOOL_LOAD");
write_bool(WIFI_EVENTS, DEF_WIFI_EVENTS);
if (!FS_Bools_read(0) || OVERWRITE_INIT_CONF_ON )
{
debugMe("Loading default device config");
led_cfg.ledMode = constrain(DEF_LED_MODE, 0, 5);
led_cfg.max_bri = constrain(DEF_MAX_BRI, 1, 255);
led_cfg.startup_bri = constrain(DEF_MAX_BRI, 1, 255);
led_cfg.NrLeds = constrain(NUM_LEDS, 1,MAX_NUM_LEDS) ;
led_cfg.DataNR_leds[0] = constrain(DEF_DATA1_NR_LEDS, 0,led_cfg.NrLeds - DEF_DATA1_START_NR);
led_cfg.DataStart_leds[0] = constrain(DEF_DATA1_START_NR, 0, led_cfg.NrLeds) ;
led_cfg.DataNR_leds[1] = constrain(DEF_DATA2_NR_LEDS, 0,led_cfg.NrLeds - DEF_DATA2_START_NR);
led_cfg.DataStart_leds[1] = constrain(DEF_DATA2_START_NR, 0, led_cfg.NrLeds) ;
led_cfg.DataNR_leds[1] = constrain(DEF_DATA3_NR_LEDS, 0,led_cfg.NrLeds - DEF_DATA3_START_NR);
led_cfg.DataStart_leds[2] = constrain(DEF_DATA3_START_NR, 0, led_cfg.NrLeds) ;
led_cfg.DataNR_leds[3] = constrain(DEF_DATA4_NR_LEDS, 0,led_cfg.NrLeds - DEF_DATA4_START_NR);
led_cfg.DataStart_leds[3] = constrain(DEF_DATA4_START_NR, 0, led_cfg.NrLeds) ;
led_cfg.apa102data_rate = constrain(DEF_APA102_DATARATE, 0, 24) ;
led_cfg.bootCFG = 16;
write_bool(DATA1_ENABLE,DEF_DATA1_ENABLE);
write_bool(DATA2_ENABLE,DEF_DATA2_ENABLE);
write_bool(DATA3_ENABLE,DEF_DATA3_ENABLE);
write_bool(DATA4_ENABLE,DEF_DATA4_ENABLE);
write_bool(DEBUG_OUT, DEF_DEBUG_OUT);
write_bool(DEBUG_TELNET, DEF_DEBUG_TELNET);
write_bool(FFT_ENABLE, DEF_FFT_ENABLE);
write_bool(FFT_MASTER, DEF_FFT_MASTER);
write_bool(FFT_MASTER_SEND, DEF_FFT_MASTER_SEND);
write_bool(POT_DISABLE,DEF_DISABLE_HW_POTS);
write_bool(WIFI_EVENTS, DEF_WIFI_EVENTS);
if (WRITE_CONF_AT_INIT || OVERWRITE_INIT_CONF_ON ) FS_Bools_write(0);
}
//else debugMe("Bools Loaded from SPIFFS!");
//debugMe("http Server load = " + String(get_bool(HTTP_ENABLED)));
}
// end bools functions
uint16_t calc_rounded_devision(uint16_t value, uint16_t divider)
{
uint16_t result = 0;
result = (value + (divider/2)) / divider;
return result;
}
uint8_t get_strip_menu_bit(int strip)
{
// return the bit of the strip menu
byte strip_bit = 0;
if (strip > 7) strip_bit += 1;
if (strip > 15) strip_bit += 1;
if (strip > 23) strip_bit += 1;
return strip_bit;
}
boolean isODDnumber(uint8_t number)
{
//if ( (number % 2) == 0) { do_something(); }
//if ( (number & 0x01) == 0)
if (bitRead(number, 0) == true)
return true;
else
return false;
}
uint8_t striptobit(int strip_no)
{
// scale down the Strip no to a bit value 0-7
while (strip_no > 7) strip_no = strip_no - 8;
return strip_no;
}
float byte_tofloat(uint8_t value, uint8_t max_value)
{
float float_out = float(value) / max_value;
return float_out;
}
//debugging
// debugging functions
// can take String, float, uint8_t, int and IPAddress
// and print it to telnet or Serial
void debugMe(String input, boolean line, boolean allways)
{
if (get_bool(DEBUG_TELNET)) WiFi_telnet_print(input, line);
if (get_bool(DEBUG_OUT) || allways)
{
if (line == true)
DEF_SERIAL_PORT.println(input);
else
DEF_SERIAL_PORT.print(input);
}
}
void debugMe(tm input, boolean line, boolean allways)
{
if (get_bool(DEBUG_TELNET)) WiFi_telnet_print(input, line);
if (get_bool(DEBUG_OUT)|| allways)
{
if (line == true)
DEF_SERIAL_PORT.println(&input, "%H:%M:%S %d.%m.%y");
else
DEF_SERIAL_PORT.println(&input, "%H:%M:%S %d.%m.%y");
}
}
void debugMe(float input, boolean line, boolean allways)
{
if (get_bool(DEBUG_TELNET)) WiFi_telnet_print(input, line);
if (get_bool(DEBUG_OUT)|| allways)
{
if (line == true)
DEF_SERIAL_PORT.println(String(input));
else
DEF_SERIAL_PORT.print(String(input));
}
}
void debugMe(uint8_t input, boolean line, boolean allways)
{
if (get_bool(DEBUG_TELNET)) WiFi_telnet_print(input, line);
if (get_bool(DEBUG_OUT)|| allways)
{
if (line == true)
DEF_SERIAL_PORT.println(String(input));
else
DEF_SERIAL_PORT.print(String(input));
}
}
void debugMe(int input, boolean line, boolean allways)
{
if (get_bool(DEBUG_TELNET)) WiFi_telnet_print(input, line);
if (get_bool(DEBUG_OUT)|| allways)
{
if (line == true)
DEF_SERIAL_PORT.println(String(input));
else
DEF_SERIAL_PORT.print(String(input));
}
}
void debugMe(IPAddress input, boolean line , boolean allways)
{
if (get_bool(DEBUG_TELNET)) WiFi_telnet_print(input, line);
if (get_bool(DEBUG_OUT) || allways)
{
if (line == true)
DEF_SERIAL_PORT.println(input);
else
DEF_SERIAL_PORT.print(input);
}
}
// end Debug functions
#include <rom/rtc.h> // required to get the reset reason
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
String print_reset_reason(RESET_REASON reason)
{
String stringy_reason;
switch (reason)
{
case 1: stringy_reason = ("POWERON_RESET : Vbat power on reset"); break; /**<1, Vbat power on reset*/
//case 2: stringy_reason = ("Nr 2 who knows!!!"); break;
case 3: stringy_reason = ("SW_RESET : Software reset digital core"); break; /**<3, Software reset digital core*/
case 4: stringy_reason = ("OWDT_RESET :Legacy watch dog reset digital core"); break; /**<4, Legacy watch dog reset digital core*/
case 5: stringy_reason = ("DEEPSLEEP_RESET : Deep Sleep reset digital core"); break; /**<5, Deep Sleep reset digital core*/
case 6: stringy_reason = ("SDIO_RESET : Reset by SLC module, reset digital core"); break; /**<6, Reset by SLC module, reset digital core*/
case 7: stringy_reason = ("TG0WDT_SYS_RESET : Timer Group0 Watch dog reset digital core"); break; /**<7, Timer Group0 Watch dog reset digital core*/
case 8: stringy_reason = ("TG1WDT_SYS_RESET : Timer Group1 Watch dog reset digital core"); break; /**<8, Timer Group1 Watch dog reset digital core*/
case 9: stringy_reason = ("RTCWDT_SYS_RESET : RTC Watch dog Reset digital core"); break; /**<9, RTC Watch dog Reset digital core*/
case 10: stringy_reason = ("INTRUSION_RESET : Instrusion tested to reset CPU"); break; /**<10, Instrusion tested to reset CPU*/
case 11: stringy_reason = ("TGWDT_CPU_RESET : Time Group reset CPU"); break; /**<11, Time Group reset CPU*/
case 12: stringy_reason = ("SW_CPU_RESET : Software reset CPU"); break; /**<12, Software reset CPU*/
case 13: stringy_reason = ("RTCWDT_CPU_RESET : RTC Watch dog Reset CPU"); break; /**<13, RTC Watch dog Reset CPU*/
case 14: stringy_reason = ("EXT_CPU_RESET : for APP CPU, reseted by PRO CPU"); break; /**<14, for APP CPU, reseted by PRO CPU*/
case 15: stringy_reason = ("RTCWDT_BROWN_OUT_RESET : Reset when the vdd voltage is not stable"); break; /**<15, Reset when the vdd voltage is not stable*/
case 16: stringy_reason = ("RTCWDT_RTC_RESET : RTC Watch dog reset digital core and rtc module"); break; /**<16, RTC Watch dog reset digital core and rtc module*/
default: stringy_reason = ("NO_MEAN");
}
return stringy_reason;
}
String debug_ResetReason(boolean core)
{
String the_reason = ("CPU" + String(core) + " : " + print_reset_reason(rtc_get_reset_reason(core)));
//debugMe("Reset reason:"+ the_reason);
//debugMe("-------------------------------------------");
return the_reason;
}
String debug_GetChipID()
{
uint64_t chipid = ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
Serial.printf("ESP32 Chip ID = %04X", (uint16_t)(chipid >> 32));//print High 2 bytes
Serial.printf("%08X\n", (uint32_t)chipid);//print Low 4bytes.
//debugMe(String(chipid));
//String TEST = printf("ESP32 Chip ID = %04X", (uint16_t)(chipid >> 32));//print High 2 bytes
return "uuuups";
}