-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicfunctions.h
467 lines (427 loc) · 16.9 KB
/
basicfunctions.h
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <EEPROM.h>
#include <Arduino.h> // for type definitions
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i=0; i<sizeof(value); i++) EEPROM.write(ee++, *p++);
return i;
}
template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p=(byte*)(void*)&value;
unsigned int i;
for (i=0; i<sizeof(value); i++) *p++=EEPROM.read(ee++);
return i;
}
boolean strcharcomp() { msg.toLowerCase(); msg.toCharArray(auxdesc, msg.length()+1); return (strcmp(auxchar,auxdesc)==0); }
char* ICACHE_FLASH_ATTR readdescr(char* namefile, byte ind, byte len)
{
File auxfile=SPIFFS.open(namefile,letrar);
if (auxfile)
{
auxfile.seek((len+2)*(ind), SeekSet);
auxfile.readBytes(auxdesc,len+2);
auxfile.close();
auxdesc[len-1]='\0';
byte n=strlen(auxdesc);
while ((n>0) && ((auxdesc[n-1]==' ')||(auxdesc[n-1]=='\n')||(auxdesc[n-1]=='\r'))) n--;
auxdesc[n]='\0';
}
return auxdesc;
}
char* ICACHE_FLASH_ATTR t(int pos)
{
char auxlang[20]="";
if (conf.lang==0) strcpy(auxlang,confiles[filespanish]);
if (conf.lang==1) strcpy(auxlang,confiles[fileenglish]);
File auxfile=SPIFFS.open(auxlang,letrar);
if (auxfile)
{
auxfile.seek(42*(pos-1), SeekSet);
auxfile.readBytes(auxdesc,42);
auxfile.close();
auxdesc[41]='\0';
byte n=strlen(auxdesc);
while ((n>0) && ((auxdesc[n-1]==' ')||(auxdesc[n-1]=='\n')||(auxdesc[n-1]=='\r'))) n--;
auxdesc[n]='\0';
}
return auxdesc;
}
char* ICACHE_FLASH_ATTR c(int pos)
{
File auxfile=SPIFFS.open(confiles[filecommon],letrar);
if (auxfile)
{
auxfile.seek(42*(pos-1), SeekSet);
auxfile.readBytes(auxdesc,42);
auxfile.close();
auxdesc[41]='\0';
byte n=strlen(auxdesc);
while ((n>0) && ((auxdesc[n-1]==' ')||(auxdesc[n-1]=='\n')||(auxdesc[n-1]=='\r'))) n--;
auxdesc[n]='\0';
}
return auxdesc;
}
void ICACHE_FLASH_ATTR savedescr(char* namefile, char* descr, byte ind, byte len)
{
File auxfile=SPIFFS.open(namefile, rmas);
if (!auxfile) return;
if (auxfile.seek((len+2)*(ind), SeekSet))
{
for (byte i=strlen(descr);i<len-1;i++) descr[i]=' ';
descr[len-1]='\0';
auxfile.print(descr);auxfile.print('\0');
auxfile.print(crlf);
}
auxfile.close();
}
void ICACHE_FLASH_ATTR addlog(File fileact)
{
if (day()<10) fileact.print(cero); fileact.print(day()); fileact.print(barra);
if (month()<10) fileact.print(cero); fileact.print(month()); fileact.print(barra);
if (year()<10) fileact.print(cero); fileact.print(year()); fileact.print(b);
if (hour()<10) fileact.print(cero); fileact.print(hour()); fileact.print(dp);
if (minute()<10) fileact.print(cero); fileact.print(minute()); fileact.print(dp);
if (second()<10) fileact.print(cero); fileact.print(second()); fileact.print(b);
}
void ICACHE_FLASH_ATTR addlog(byte tipo, int code, char *texto)
{
File auxfile=SPIFFS.open("/log.txt","a+");
if (auxfile)
{
if (day()<10) auxfile.print(cero); auxfile.print(day()); auxfile.print(barra);
if (month()<10) auxfile.print(cero); auxfile.print(month()); auxfile.print(barra);
if (year()<10) auxfile.print(cero); auxfile.print(year()); auxfile.print(b);
if (hour()<10) auxfile.print(cero); auxfile.print(hour()); auxfile.print(dp);
if (minute()<10) auxfile.print(cero); auxfile.print(minute()); auxfile.print(dp);
if (second()<10) auxfile.print(cero); auxfile.print(second()); auxfile.print(b);
auxfile.print(code); auxfile.print(b);
auxfile.print(texto);
auxfile.println(crlf);
auxfile.close(); }
}
void ICACHE_FLASH_ATTR addlog(byte tipo, int code, PGM_P texto)
{
// File auxfile=SPIFFS.open("/log.txt",amas);
File auxfile=SPIFFS.open("/log.txt","a+");
if (auxfile)
{
if (day()<10) auxfile.print(cero); auxfile.print(day()); auxfile.print(barra);
if (month()<10) auxfile.print(cero); auxfile.print(month()); auxfile.print(barra);
if (year()<10) auxfile.print(cero); auxfile.print(year()); auxfile.print(b);
if (hour()<10) auxfile.print(cero); auxfile.print(hour()); auxfile.print(dp);
if (minute()<10) auxfile.print(cero); auxfile.print(minute()); auxfile.print(dp);
if (second()<10) auxfile.print(cero); auxfile.print(second()); auxfile.print(b);
auxfile.print(code); auxfile.print(b);
auxfile.println(texto);
auxfile.close(); }
}
byte ICACHE_FLASH_ATTR getbit8(byte tabla[], byte pin)
{ return ((tabla[pin/8] & tab[(pin % 8)])>0)?1:0;}
void ICACHE_FLASH_ATTR setbit8(byte tabla[], byte pin, byte value) {
tabla[pin/8]=((value==0)?tabla[pin/8]&(255^tab[(pin%8)]):tabla[pin/8]|tab[(pin%8)]);}
String ICACHE_FLASH_ATTR extrae(boolean eschar, String cad, String subcad)
{ subcad.concat("\"");
return cad.substring(cad.indexOf(subcad)+subcad.length()+(eschar?2:1), cad.indexOf(",",cad.indexOf(subcad))-(eschar?1:0)); }
void ICACHE_FLASH_ATTR tictac(int pin, int n, int delayed)
{ for (int i=0;i<n;i++)
{digitalWrite(pin,1); delay(delayed); digitalWrite(pin, 0); delay(delayed);} }
void ICACHE_FLASH_ATTR printconceros(int value) { if (value<10) Serial.print(0); Serial.print(value); }
void ICACHE_FLASH_ATTR printhora() {
printconceros(hour()); Serial.print(dp);
printconceros(minute()); Serial.print(dp);
printconceros(second());
}
int ICACHE_FLASH_ATTR posrem(int numrem) {
int p=0;
boolean encontrado=false;
while ((p<maxsalrem) && (!encontrado)) { encontrado=(conf.idremote[p++]==numrem); }
return encontrado?p:0;
}
int ICACHE_FLASH_ATTR readconf()
{
File auxfile=SPIFFS.open(confiles[fileconf],letrar);
if (!auxfile) return 0;
int count=0;
for (count=0;count<sizeof(conf);count++)*(buffconf+count)=auxfile.read();
auxfile.close();
if (conf.HUELLA!=12345678)
Serial.println("HUELLA INCORRECTA");
return count;
}
void ICACHE_FLASH_ATTR saveconf()
{
conf.HUELLA=12345678;
File auxfile=SPIFFS.open(confiles[fileconf], rmas);
if (auxfile) { auxfile.write(buffconf,sizeof(conf)); auxfile.close(); }
}
void ICACHE_FLASH_ATTR printlinea(PGM_P texto) { for (byte i=0;i<20;i++) Serial.print(texto);Serial.println(); }
void ICACHE_FLASH_ATTR createhost(byte ip)
{
strcpy(host,hostraiz);
strcat(host,itoa(conf.netseg, buff, 10));
strcat(host,punto);
strcat(host, itoa(ip, buff, 10));
}
void calcindices(int i)
{
param_number=server.argName(i).toInt();
if (mp<=0) mp=1;
indice=param_number/mp;
resto=param_number%mp;
mival=server.arg(i).toInt();
}
int callhttpGET(char *host, int port, boolean readresp, unsigned long timeout)
{
HTTPClient http;
http.begin(host,port,msg);
http.setConnectTimeout(timeout);
int httpCode=http.GET();
msg="";
if (readresp) if(httpCode==HTTP_CODE_OK) { msg=http.getString(); }
http.end();
return httpCode;
}
boolean checkfile(byte numfile)
{ if (!SPIFFS.exists(confiles[numfile]))
{ filesexist[numfile]=0; Serial.print(confiles[numfile]); Serial.println(" no existe"); return false; } return true;
}
boolean checkfiles()
{
boolean auxB=true;
auxB=auxB && checkfile(0);
auxB=auxB && checkfile(1);
auxB=auxB && checkfile(2);
auxB=auxB && checkfile(3);
auxB=auxB && checkfile(4);
auxB=auxB && checkfile(5);
auxB=auxB && checkfile(6);
auxB=auxB && checkfile(7);
auxB=auxB && checkfile(8);
auxB=auxB && checkfile(9);
auxB=auxB && checkfile(10);
auxB=auxB && checkfile(11);
auxB=auxB && checkfile(12);
auxB=auxB && checkfile(13);
auxB=auxB && checkfile(14);
auxB=auxB && checkfile(15);
auxB=auxB && checkfile(16);
auxB=auxB && checkfile(17);
filesok=auxB;
return auxB;
}
void ICACHE_FLASH_ATTR printP(PGM_P texto1) { char c; while ((c = pgm_read_byte(texto1++))) msg += c; }
void ICACHE_FLASH_ATTR printP(PGM_P texto1, PGM_P texto2) { printP(texto1); printP(texto2);}
void ICACHE_FLASH_ATTR printP(PGM_P texto1, PGM_P texto2, PGM_P texto3) { printP(texto1, texto2); printP(texto3);}
void ICACHE_FLASH_ATTR printP(PGM_P texto1, PGM_P texto2, PGM_P texto3, PGM_P texto4)
{ printP(texto1,texto2); printP(texto3,texto4); }
void ICACHE_FLASH_ATTR printP(PGM_P texto1, PGM_P texto2, PGM_P texto3, PGM_P texto4, PGM_P texto5)
{ printP(texto1, texto2, texto3, texto4); printP(texto5); }
void ICACHE_FLASH_ATTR printP(PGM_P texto1, PGM_P texto2, PGM_P texto3, PGM_P texto4, PGM_P texto5, PGM_P texto6)
{ printP(texto1, texto2, texto3, texto4, texto5); printP(texto6); }
void ICACHE_FLASH_ATTR printI(int value) { printP(itoa(value, buff, 10)); }
void ICACHE_FLASH_ATTR printH(int value) { printP(itoa(value, buff, 16)); }
void ICACHE_FLASH_ATTR printL(long value) { printP(ltoa(value, buff, 10)); }
void ICACHE_FLASH_ATTR printF(float value, byte deci) {
float pdec=value-int(value);
printI(int(value)); if (deci>0) printP(punto);
for (byte i=0;i<deci;i++) if (int(pdec*pow(10,i+1))%10==0) printP(cero); else printI(abs(int(pdec*pow(10,i+1))%10));
}
void ICACHE_FLASH_ATTR printIP(long valor, const char *texto) { printI(valor); printP(texto); }
void ICACHE_FLASH_ATTR printPiP(const char *texto1, int num, const char *texto2)
{ printP(texto1); printI(num); printP(texto2);}
bool ICACHE_FLASH_ATTR autOK()
{
return true; // provisional
if (conf.usepassDev == 0) return true;
if (server.hasHeader("Cookie"))
if (server.header("Cookie").indexOf("ESPSESSIONID=1") != -1)
return true;
return false;
}
void ICACHE_FLASH_ATTR printOpc(boolean colorea, boolean activa, char* texto)
{
if (activa)
printP(th, texto, th_f);
else {
printP(colorea?th:td, href_i, comillas, auxchar, comillas, mayor);
printP(texto, href_f, colorea?th_f:td_f);
}
}
void ICACHE_FLASH_ATTR printOpc(boolean colorea, boolean activa, PGM_P texto)
{
if (activa)
printP(th, texto, th_f);
else {
printP(colorea?th:td, href_i, comillas, auxchar, comillas, mayor);
printP(texto, href_f, colorea?th_f:td_f);
}
}
void ICACHE_FLASH_ATTR printOpc(boolean colorea, boolean activa, PGM_P texto, PGM_P link)
{
if (activa)
printP(th, texto, th_f);
else {
printP(colorea?th:td, href_i, comillas, link, comillas, mayor);
printP(texto, href_f, colorea ? th_f : td_f);
}
}
void ICACHE_FLASH_ATTR printOpc(boolean colorea, boolean activa, PGM_P texto, PGM_P link, byte i)
{
if (activa)
printP(th, texto, th_f);
else {
printP(colorea?th:td, href_i, comillas, link, interr, letran);
printP(ig);
printI(i);
printP(comillas, mayor, texto, href_f, colorea ? th_f : td_f);
}
}
void ICACHE_FLASH_ATTR writeMenu(byte opcprin, byte opcsec)
{
printP(c(body_i), menor);
printP(table, b);
printP(c(tclass));
printP(ig, tmenu, mayor, tr); // formato menú
if (conf.modofi==0)
printOpc(false, (opcprin==1), t(zonas), panelhtm); // PANEL
else if (conf.modofi==1)
printOpc(false, (opcprin==1), c(panel), panelhtm); // Bomba de calor
printOpc(false, (opcprin==3), t(configuracion), sdhtm); // CONFIGURACIÓN
printOpc(false, (opcprin==2), t(programas), sprghtm); // Programas
printOpc(false, (opcprin==4), t(sistema), espsyshtm); // Sistema
if (conf.usepassDev)
{
printP(td, href_i, comillas, barra, loghtm);
if (autOK())
printP(interr, c(DISCONNECTYES), comillas, mayor, t(des));
else
printP(comillas, b, c(color000000), mayor);
printP(t(conectar), href_f, td_f);
}
printP(tr_f, menor, barra, table, mayor);
printP(menor, table, b);
printP(c(tclass), ig);
printP(tmenu, mayor, tr); // formato menú
if (opcprin == 1) // PANELES
{
if (conf.modofi==0) // modo normal
{
for (byte i=0; i<maxpaneles; i++)
if (getbit8(conf.bshowpanel, i))
printOpc(false, opcsec==i, readdescr(confiles[filezonas], i, 20), panelhtm, i);
}
}
else if (opcprin==2) // PROGRAMACIÓN
{
if (conf.modofi==0) printOpc(false, opcsec==5, t(programas), sprghtm);
if (conf.modofi==0) printOpc(false, opcsec==1, t(semanal), ssehtm);
if (conf.modofi==0) printOpc(false, opcsec==2, t(condiciones), svhtm);
if (conf.modofi==0) printOpc(false, opcsec==6, "Combinaciones", aohtm);
if (conf.modofi==0) printOpc(false, opcsec==3, t(fecha), sfhtm);
if (conf.modofi==0) printOpc(false, opcsec==4, t(escenas), seschtm);
if (conf.modofi==0) printOpc(false, opcsec==7, t(webcalls), swchtm);
}
else if (opcprin==3) // CONFIGURACIÓN
{
printOpc(false, opcsec==0, t(dispositivo), sdhtm);
if (conf.modofi==0) printOpc(false, opcsec==5, t(zonas), sphtm);
if (conf.modofi==0) printOpc(false, opcsec==2, t(mandorf), rfhtm);
printOpc(false, opcsec==3, t(tred), snehtm);
printOpc(false, opcsec==4, t(servred), snshtm);
if (conf.modofi==0) printOpc(false, opcsec==1, c(senales), siohtm);
if (conf.modofi==0) printOpc(false, opcsec==10, t(remotos), slkhtm);
if (conf.modofi==0) printOpc(false, opcsec==11, c(salremotas), sremhtm);
if (conf.modofi==1) printOpc(false, opcsec==12, t(bombacalor), sbhtm);
}
else if (opcprin==4) // SISTEMA
{
printOpc(false, opcsec==4, t(statust), espsyshtm);
if (conf.modofi==0) printOpc(false, opcsec==3, t(files), fileshtm);
printOpc(false, opcsec==5, t(seguridad), sshtm);
printOpc(false, opcsec==1, t(actualizar), suhtm);
printOpc(false, opcsec==2, treset, rshtm);
}
printP(tr_f, menor, barra, table, mayor, br);
}
void ICACHE_FLASH_ATTR serversend200() { server.send(200, texthtml, msg); msg=vacio; }
void ICACHE_FLASH_ATTR printtiempo(unsigned long segundos)
{
if (segundos < 60) {
printIP(segundos, comillas);
}
else {
unsigned long minutos = segundos / 60;
segundos = segundos % 60;
if (minutos < 60) {
printIP(minutos, comilla);
printPiP(b, segundos, comillas);
}
else {
unsigned long horas = minutos / 60;
minutos = minutos % 60;
if (horas < 24) {
printIP(horas, letrah);
printPiP(b, minutos, comilla);
}
else {
unsigned int dias = horas / 24;
horas = horas % 24;
printIP(dias, letrad);
printPiP(b, horas, letrah);
}
}
}
}
boolean gpiovis(byte i)
{
boolean auxgpiovis=false;
#ifdef PCBV5
if (i==0) auxgpiovis=(conf.TX433enabled==0); // GPIO 02
else if (i==1) auxgpiovis=(conf.RX433enabled==0); // GPIO 04
else if (i==2) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 12
else if (i==3) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 13
else if (i==4) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 14
else if (i==5) auxgpiovis=false; // no se puede usar con facilidad // GPIO 15
else if (i==6) auxgpiovis=((conf.I2Cenabled==0) && (conf.TFTenabled==0)); // GPIO 21
else if (i==7) auxgpiovis=((conf.I2Cenabled==0) && (conf.SERIAL2enabled==0));// GPIO 22
else if (i==8) auxgpiovis=(conf.SERIAL2enabled==0) ; // GPIO 32
else if (i==9) auxgpiovis=true; // GPIO 33
#elif defined PCBV4
if (i==0) auxgpiovis=(conf.TX433enabled==0); // GPIO 02
else if (i==1) auxgpiovis=(conf.RX433enabled==0); // GPIO 04
else if (i==2) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 12
else if (i==3) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 13
else if (i==4) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 14
else if (i==5) auxgpiovis=false; // GPIO 15
else if (i==6) auxgpiovis=((conf.I2Cenabled==0) && (conf.TFTenabled==0)); // GPIO 21
else if (i==7) auxgpiovis=((conf.I2Cenabled==0) && (conf.SERIAL2enabled==0));// GPIO 22
else if (i==8) auxgpiovis=(conf.SERIAL2enabled==0) ; // GPIO 32
else if (i==9) auxgpiovis=true; // GPIO 33
// listgpiovar[maxgpiovar]={2,4,12,13,14,15,21,22,32,33};
#elif defined PCBTEST
if (i==0) auxgpiovis=(conf.TX433enabled==0); // GPIO 02
else if (i==1) auxgpiovis=(conf.RX433enabled==0); // GPIO 04
else if (i==2) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 12
else if (i==3) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 13
else if (i==4) auxgpiovis=((conf.SPIenabled==0) && (conf.TFTenabled==0)); // GPIO 14
else if (i==5) auxgpiovis=false; // GPIO 15
else if (i==6) auxgpiovis=((conf.I2Cenabled==0) && (conf.TFTenabled==0)); // GPIO 21
else if (i==7) auxgpiovis=((conf.I2Cenabled==0) && (conf.SERIAL2enabled==0));// GPIO 22
else if (i==8) auxgpiovis=(conf.SERIAL2enabled==0) ; // GPIO 32
else if (i==9) auxgpiovis=true; // GPIO 33
#endif
return auxgpiovis;
}
void marktime(char *cad)
{
if (MARKTIME)
{
printhora();
Serial.print(b); Serial.print(cad);
Serial.print(b); Serial.print(millis()-tmark);
Serial.println(" ms");
tmark=millis();
}
}