-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
286 lines (269 loc) · 5.33 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
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
#include "main.h"
#include "conf.h"
#ifdef USEBLE
#include "sub_ble.h"
#endif
#include "sub_bmp.h"
#include "sub_btn.h"
#include "sub_enow.h"
#include "sub_http.h"
#include "sub_json.h"
#include "sub_led.h"
#include "sub_udp.h"
#include "sub_wifi.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
int ssid_count = 5;
String ssid[] = {"ledControl", "spiffs", "spiffs", "scp-121", "salat"};
String pass[] = {"12345678", "spiffs", "spiffs", "bioshock", "salat191919"};
char exbmp[5] = ".bmp";
char exbma[5] = ".bma";
char exgif[5] = ".gif";
char exjpg[5] = ".jpg";
char extxt[5] = ".txt";
struct config conf = {
"v0.18",
"LedHDxx",
{}, //macs
0, //macson
0, //macslen
0, //wait
4, //brgn
10, //mode
20, //leds
5.5, //vcc
0, //cont
0, //psr
false, //skwf
false, //bluetooth
false, //enow
false, //enowone
{22,25,26,27,0,0}, //pins
BTN_PIN,
PWR_PIN,
ADC1_CHANNEL_0 //36
};
struct status state = {
false, //go
true, //loop
false, //next
0, //maxbmp
0, //currbmp
"no_bmp", //currname
0, //setbmp
3, //whdr
4000, //bpm
0, //btt test
0, //maxprog
0, //curr prog
"no_prog", //curr prog name
0, //proglist
false //calcmax
};
int bmpw = 0;
int bmph = 0;
char *p;
File root;
File file;
void setup()
{
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
json_load();
#ifndef ARDUINO_ESP32C3_DEV
if (conf.mode == 10 || conf.mode == 14)
{
conf.pinb = 4;
conf.pinp = 5;
}
if (conf.mode == 10) {conf.wpref = "LedHD72"; conf.vccch = ADC1_CHANNEL_5;} //33
if (conf.mode == 14) {conf.wpref = "LedHD80"; conf.vccch = ADC1_CHANNEL_0;} //36
#endif
pinMode(conf.pinb, INPUT_PULLUP); //init mosfet button
pinMode(conf.pinp, OUTPUT);
digitalWrite(conf.pinp, LOW);
Serial.begin(115200);
while (!Serial); // wait for serial attach
#ifdef ARDUINO_ESP32C3_DEV
conf.wpref = "LedC332";
conf.leds = 32;
conf.vcc = 4.1;
conf.vccch = ADC1_CHANNEL_1; //1
#endif
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(conf.vccch, ADC_ATTEN_DB_2_5);
Serial.println();
Serial.printf("Initializing... %d\n", CORE_DEBUG_LEVEL);
led_init();
led_calccont(conf.cont);
led_brgn();
led_show();
check_up();
conf.psr = ESP.getPsramSize();
Serial.printf("Total PSRAM: %d\n", conf.psr);
if (conf.psr)
{
Serial.printf("Allocate ps %d\n", 3800000);
p = (char*) ps_realloc(p, 3800000);
if(!p) Serial.println("Allocation error psram");
}
else
{
Serial.printf("Allocate ram %d\n", 64000);
p = (char*) realloc(p, 64000);
if(!p) Serial.println("Allocation error ram");
}
Serial.printf("fs begin %d\n", FILESYSTEM.begin());
bmp_init();
bmp_max();
root = FILESYSTEM.open("/");
bmp_next();
delay(100);
led_drawvcc();
if (conf.bt)
{
#ifdef USEBLE
ble_init();
#else
state.go = true;
#endif
}
else
{
Serial.println("wifi_init");
wifi_init();
if (conf.enow)
{
int px = enow_getorder();
for (int i = 0; i < px; i++) led_setpxall(i, green);
led_show();
enow_begin();
delay(200);
int lp = 0;
while (state.go == false)
{
unsigned long time = millis();
while(millis() - time < 100)
{
enow_poll();
check_off();
}
enow_wakeup();
lp++;
if(lp == 100) {led_drawvcc(); lp = 0;}
}
enow_end();
}
else
{
Serial.println("wifi_conn");
if (conf.skwf == false) wifi_conn();
}
Serial.println("http_begin");
http_begin();
Serial.println("udp_begin");
udp_begin();
}
int lp = 0;
while (state.go == false)
{
if (conf.bt)
{
#ifdef USEBLE
ble_poll();
#endif
}
else
{
http_poll();
udp_poll();
ftp_poll();
}
check_off();
lp++;
if(lp == 1000) {led_drawvcc(); lp = 0;}
delay(1);
}
}
void loop()
{
if (state.go == true && state.maxbmp > 0)
{
if (state.whdr == 3)
{
bmp_draw(file.name(), state.bpm);
if (state.loop)
{
bmp_next();
}
if (state.next)
{
bmp_next();
state.next = false;
}
if (state.setbmp)
{
while (state.currbmp != state.setbmp)
{
bmp_next();
}
state.setbmp = 0;
}
}
if (state.whdr == 4)
{
state.setbmp = 0;
if (FILESYSTEM.exists("/" + state.progname))
{
String prgopen = state.progname;
File prgfile = FILESYSTEM.open("/" + prgopen, "r");
String pic;
while (prgfile.available())
{
if (state.whdr != 4 || state.go == false || prgopen != state.progname) break;
pic = prgfile.readStringUntil('\n');
//Serial.println("debug pic [" + pic + "] " + String(pic.length(), DEC) + "] fpos " + String(prgfile.position(), DEC) + " flen " + String(prgfile.size()));
pic.trim();
if (pic == "stop") break;
int spidx = pic.indexOf(" ");
if (spidx == -1 || spidx == pic.length() - 1)
{
Serial.println("prog err: no second arg");
udp_poll();
if (prgfile.position() >= prgfile.size()){
prgfile.close();
Serial.println("file closed");
}
}
else
{
String fpic = pic.substring(0, spidx);
String dspic = pic.substring(spidx + 1);
dspic.trim();
int dpic = dspic.toInt();
bmp_draw(fpic, dpic);
}
}
prgfile.close();
udp_poll();
}
else
{
bmp_rainbow();
bmp_wait(50);
Serial.println("prog err: no prog");
}
}
}
while (state.go == false || state.maxbmp == 0)
{
http_poll();
udp_poll();
ftp_poll();
check_off();
bmp_batlog();
#ifdef USEBLE
if (conf.bt) ble_poll();
#endif
delay(1);
}
}