-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM5TX.hpp
721 lines (641 loc) · 18.9 KB
/
M5TX.hpp
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
////////////////////////////////////////////////////////////////////////////////
// DIY-RCシステムのTX専用ソース
// DIY radio control system by M5Stack series
// https://github.com/hshin-git/M5RadioControl
////////////////////////////////////////////////////////////////////////////////
#ifndef _M5RC_TX_H_
#define _M5RC_TX_H_
//#define DEBUG M5.Lcd
#define DEBUG Serial
////////////////////////////////////////////////////////////////////////////////
// class M5_ADS1X15{}: 送信機用のADコンバータ(I2C接続ADC、4ch中の1chは基準電圧計測に使うので正味3ch)
// setup(): 初期化
// getCount(): 電圧[カウント]
// getVolts(): 電圧[V]
// getPercent(): 電圧比率[%]
// getPercents(): 電圧比率[%]
////////////////////////////////////////////////////////////////////////////////
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
class M5_ADS1X15 {
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads; /* Use this for the 12-bit version */
public:
void setup(int addr=0x48, int sda=32,int sck=33) {
Wire.begin(sda,sck,800000L);
while (1) {
if (ads.begin(addr,&Wire)) {
DEBUG.printf("Found ADS1X15 at (addr=0x%x,sda=%d,sck=%d).\n",addr,sda,sck);
break;
}
else {
DEBUG.println("Failed to initialize ADS.");
delay(1000);
}
}
//ads.setDataRate(RATE_ADS1115_128SPS);
//ads.setDataRate(RATE_ADS1115_250SPS);
//ads.setDataRate(RATE_ADS1115_475SPS);
ads.setDataRate(RATE_ADS1115_860SPS);
DEBUG.println("Getting single-ended readings from AIN0..3");
DEBUG.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015 ADS1115
// ------- -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
}
int getCounts(int ch) {
if (ch>=0 && ch<4) {
return ads.readADC_SingleEnded(ch);
}
return -1;
}
float getVolts(int ch) {
if (ch>=0 && ch<4) {
int cnt = ads.readADC_SingleEnded(ch);
return ads.computeVolts(cnt);
}
return -1;
}
float getPercent(int ch) {
if (ch>=0 && ch<3) {
int c0 = ads.readADC_SingleEnded(ch);
int c3 = ads.readADC_SingleEnded(3);
return c3>0? (c0*100.0)/c3: -1;
}
return -1;
}
bool getPercents(float* ps, int nc) {
if (ps && nc>0) {
int c3 = ads.readADC_SingleEnded(3);
for (int ch=0; ch<nc; ch++) {
int c0 = ads.readADC_SingleEnded(ch);
ps[ch] = c3>0? (c0*100.0)/c3: -1;
}
return true;
}
return false;
}
};
////////////////////////////////////////////////////////////////////////////////
// class SDCserver{}: SDカードのWiFi/HTTPアクセス機能(WiFiをAPモードで起動、SDカード内容をHTTPサーバ公開)
// setup(): サーバ起動
// loop(): サーバ実行
// stop(): サーバ停止
// https://github.com/esp8266/ESPWebServer/tree/master/examples/SDWebServer
////////////////////////////////////////////////////////////////////////////////
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
static const char* _SSID = "M5TX";
static WebServer _SERVER(80);
static bool hasSD = false;
static File uploadFile;
static bool firstTime = true;
class SDCServer {
static void returnOK() {
_SERVER.send(200, "text/plain", "");
}
static void returnFail(String msg) {
_SERVER.send(500, "text/plain", msg + "\r\n");
}
static void listDirectory(String path) {
if (!SD.exists((char *)path.c_str())) {
return returnFail("BAD PATH");
}
File dir = SD.open((char *)path.c_str());
//path = String();
if (!dir.isDirectory()) {
dir.close();
return returnFail("NOT DIR");
}
dir.rewindDirectory();
_SERVER.setContentLength(CONTENT_LENGTH_UNKNOWN);
_SERVER.send(200, "text/html", "");
WiFiClient client = _SERVER.client();
//_SERVER.sendContent("[");
_SERVER.sendContent("<!DOCTYPE html><html><body>");
_SERVER.sendContent("Index of " + path);
_SERVER.sendContent("<ul>");
for (int cnt = 0; true; ++cnt) {
File entry = dir.openNextFile();
if (!entry) {
break;
}
String output;
#if 0
if (cnt > 0) {
output = ',';
}
output += "{\"type\":\"";
output += (entry.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\"";
output += entry.name();
output += "\"";
output += "}";
#else
output += "<li><a href=\"";
output += (entry.isDirectory()? entry.name(): path+entry.name());
output += "\">";
output += entry.name();
output += "</a>";
#endif
_SERVER.sendContent(output);
entry.close();
}
//_SERVER.sendContent("]");
_SERVER.sendContent("</ul></body></html>");
dir.close();
}
static bool loadFromSdCard(String path) {
String dataType = "text/plain";
//if (path.endsWith("/")) {
// path += "index.htm";
//}
if (path.endsWith(".src")) {
path = path.substring(0, path.lastIndexOf("."));
} else if (path.endsWith(".htm")) {
dataType = "text/html";
} else if (path.endsWith(".css")) {
dataType = "text/css";
} else if (path.endsWith(".js")) {
dataType = "application/javascript";
} else if (path.endsWith(".png")) {
dataType = "image/png";
} else if (path.endsWith(".gif")) {
dataType = "image/gif";
} else if (path.endsWith(".jpg")) {
dataType = "image/jpeg";
} else if (path.endsWith(".ico")) {
dataType = "image/x-icon";
} else if (path.endsWith(".xml")) {
dataType = "text/xml";
} else if (path.endsWith(".pdf")) {
dataType = "application/pdf";
} else if (path.endsWith(".zip")) {
dataType = "application/zip";
} else if (path.endsWith(".csv")) {
dataType = "text/csv";
} else if (path.endsWith(".txt")) {
dataType = "text/txt";
}
File dataFile = SD.open(path.c_str());
if (!dataFile) {
return false;
}
if (dataFile.isDirectory()) {
if (SD.exists((char *)(path + "/index.htm").c_str())) {
path += "/index.htm";
dataType = "text/html";
dataFile = SD.open(path.c_str());
} else {
dataFile.close();
listDirectory(path);
return true;
}
}
if (_SERVER.hasArg("download")) {
dataType = "application/octet-stream";
}
if (_SERVER.streamFile(dataFile, dataType) != dataFile.size()) {
DEBUG.println("Sent less data than expected!");
}
dataFile.close();
return true;
}
static void handleFileUpload() {
if (_SERVER.uri() != "/edit") {
return;
}
HTTPUpload& upload = _SERVER.upload();
if (upload.status == UPLOAD_FILE_START) {
if (SD.exists((char *)upload.filename.c_str())) {
SD.remove((char *)upload.filename.c_str());
}
uploadFile = SD.open(upload.filename.c_str(), FILE_WRITE);
DEBUG.print("Upload: START, filename: "); DEBUG.println(upload.filename);
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (uploadFile) {
uploadFile.write(upload.buf, upload.currentSize);
}
DEBUG.print("Upload: WRITE, Bytes: "); DEBUG.println(upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
if (uploadFile) {
uploadFile.close();
}
DEBUG.print("Upload: END, Size: "); DEBUG.println(upload.totalSize);
}
}
static void deleteRecursive(String path) {
File file = SD.open((char *)path.c_str());
if (!file.isDirectory()) {
file.close();
SD.remove((char *)path.c_str());
return;
}
file.rewindDirectory();
while (true) {
File entry = file.openNextFile();
if (!entry) {
break;
}
String entryPath = path + "/" + entry.name();
if (entry.isDirectory()) {
entry.close();
deleteRecursive(entryPath);
} else {
entry.close();
SD.remove((char *)entryPath.c_str());
}
yield();
}
SD.rmdir((char *)path.c_str());
file.close();
}
static void handleDelete() {
if (_SERVER.args() == 0) {
return returnFail("BAD ARGS");
}
String path = _SERVER.arg(0);
if (path == "/" || !SD.exists((char *)path.c_str())) {
returnFail("BAD PATH");
return;
}
deleteRecursive(path);
returnOK();
}
static void handleCreate() {
if (_SERVER.args() == 0) {
return returnFail("BAD ARGS");
}
String path = _SERVER.arg(0);
if (path == "/" || SD.exists((char *)path.c_str())) {
returnFail("BAD PATH");
return;
}
if (path.indexOf('.') > 0) {
File file = SD.open((char *)path.c_str(), FILE_WRITE);
if (file) {
file.write(0);
file.close();
}
} else {
SD.mkdir((char *)path.c_str());
}
returnOK();
}
static void printDirectory() {
if (!_SERVER.hasArg("dir")) {
return returnFail("BAD ARGS");
}
String path = _SERVER.arg("dir");
if (path != "/" && !SD.exists((char *)path.c_str())) {
return returnFail("BAD PATH");
}
File dir = SD.open((char *)path.c_str());
path = String();
if (!dir.isDirectory()) {
dir.close();
return returnFail("NOT DIR");
}
dir.rewindDirectory();
_SERVER.setContentLength(CONTENT_LENGTH_UNKNOWN);
_SERVER.send(200, "text/json", "");
WiFiClient client = _SERVER.client();
_SERVER.sendContent("[");
for (int cnt = 0; true; ++cnt) {
File entry = dir.openNextFile();
if (!entry) {
break;
}
String output;
if (cnt > 0) {
output = ',';
}
output += "{\"type\":\"";
output += (entry.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\"";
output += entry.name();
output += "\"";
output += "}";
_SERVER.sendContent(output);
entry.close();
}
_SERVER.sendContent("]");
dir.close();
}
static void handleNotFound() {
if (hasSD && loadFromSdCard(_SERVER.uri())) {
return;
}
String message = "SDCARD Not Detected\n\n";
message += "URI: ";
message += _SERVER.uri();
message += "\nMethod: ";
message += (_SERVER.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += _SERVER.args();
message += "\n";
for (uint8_t i = 0; i < _SERVER.args(); i++) {
message += " NAME:" + _SERVER.argName(i) + "\n VALUE:" + _SERVER.arg(i) + "\n";
}
_SERVER.send(404, "text/plain", message);
DEBUG.print(message);
}
public:
static void setup(void) {
DEBUG.println("Setup WIFI AP mode");
WiFi.mode(WIFI_AP);
WiFi.softAP(_SSID);
WiFi.begin();
delay(500);
DEBUG.print("Started! IP address: ");
DEBUG.println(WiFi.softAPIP());
if (MDNS.begin(_SSID)) {
MDNS.addService("http", "tcp", 80);
DEBUG.println("MDNS responder started");
DEBUG.print("You can now connect to http://");
DEBUG.print(_SSID);
DEBUG.println(".local");
}
if (!firstTime) return;
_SERVER.on("/list", HTTP_GET, printDirectory);
_SERVER.on("/edit", HTTP_DELETE, handleDelete);
_SERVER.on("/edit", HTTP_PUT, handleCreate);
_SERVER.on("/edit", HTTP_POST, []() { returnOK(); }, handleFileUpload);
_SERVER.onNotFound(handleNotFound);
_SERVER.begin();
DEBUG.println("HTTP _SERVER started");
if (SD.begin()) {
DEBUG.println("SD Card initialized.");
hasSD = true;
}
firstTime = false;
}
static void loop(void) {
_SERVER.handleClient();
}
static void stop(void) {
WiFi.mode(WIFI_OFF);
WiFi.disconnect();
}
IPAddress getIP(void) {
return WiFi.softAPIP();
}
};
////////////////////////////////////////////////////////////////////////////////
// class DataFIFO: マルチスレッド対応リングバッファ(別スレッドで追加と取得OK)
// put(): データの追加
// get(): データの取得
// gets(): データの取得(複数アイテム対応)
////////////////////////////////////////////////////////////////////////////////
template <class DATA> class DataFIFO {
DATA *RING;
int SIZE;
int head;
int tail;
// FreeRTOS
SemaphoreHandle_t xMutex;
public:
DataFIFO(int N = 32, bool mutex = false) {
RING = new DATA[N];
SIZE = N;
head = tail = 0;
// FreeRTOS
xMutex = mutex? xSemaphoreCreateMutex(): NULL;
}
bool empty(void) { return head == tail; }
bool full(void) { return head == (tail + 1) % SIZE; }
int count(void) { return (tail - head + SIZE) % SIZE; }
void lock(void) { if (xMutex) xSemaphoreTake(xMutex,portMAX_DELAY); }
void unlock(void) { if (xMutex) xSemaphoreGive(xMutex); }
bool put(DATA *d) {
bool code = false;
lock();
if (full()) {
code = false;
}
else {
RING[tail] = *d;
tail = (tail + 1) % SIZE;
code = true;
}
unlock();
return code;
}
bool get(DATA *d) {
bool code = false;
lock();
if (empty()) {
code = false;
}
else {
*d = RING[head];
head = (head + 1) % SIZE;
code = true;
}
unlock();
return code;
}
bool gets(DATA **pData, int *pSize) {
bool code = false;
lock();
if (empty()) {
code = false;
}
else {
if (head < tail) {
*pData = &RING[head];
*pSize = (tail - head);
head = tail;
}
else {
*pData = &RING[head];
*pSize = (SIZE - head);
head = 0;
}
code = true;
}
unlock();
return code;
}
int gets(DATA *buf, int len) {
int code = -1;
lock();
if (empty()) {
code = 0;
}
else {
int N = min(len, count());
DATA* src = &RING[head];
DATA* dst = &buf[0];
if (head < tail) {
memcpy(dst,src,N*sizeof(DATA));
head += N;
}
else {
int n = min(N,SIZE-head);
memcpy(dst,src,n*sizeof(DATA));
if (n < N) {
src = &RING[0];
dst = &buf[n];
memcpy(dst,src,(N-n)*sizeof(DATA));
}
head = (head + N) % SIZE;
}
code = N;
}
unlock();
return code;
}
};
////////////////////////////////////////////////////////////////////////////////
// class DataFIFO: ログファイルの管理(SDカードへの遅延書き込み対応)
// open(): ログファイルの開始
// write(): ログデータの追加(受信スレッドで呼び出す)
// flush(): ログデータの保存
// close(): ログバイルの終了
////////////////////////////////////////////////////////////////////////////////
template <class DATA> class DataFILE {
static const int NCOL = sizeof(DATA)/sizeof(float);
DataFIFO<DATA>* FIFO;
DATA* BUFF;
int SIZE;
File FILE;
//const char** COLS;
// monitoring
bool OPENED;
unsigned int START, COUNT;
public:
//DataFILE(const char** NAME, int NBUF = 16) {
DataFILE(int NBUF = 16) {
FIFO = new DataFIFO<DATA>(NBUF,true);
BUFF = new DATA[NBUF];
SIZE = NBUF;
//COLS = NAME;
//
OPENED = false;
START = 0;
COUNT = 0;
}
bool open(const char *filepath) {
FILE = SD.open(filepath, FILE_WRITE);
if (FILE) {
START = millis();
COUNT = 0;
OPENED = true;
return true;
}
return false;
}
void close(void) {
OPENED = false;
if (FILE) {
DATA d;
while (FIFO->get(&d)) {
float *v = (float*)&d;
FILE.write((const uint8_t*)v, (size_t)sizeof(DATA));
}
FILE.close();
}
}
bool write(DATA *d) {
if (OPENED && FILE && !FIFO->full()) {
if (COUNT == 0) START = d->msec;
d->msec -= START;
//
COUNT++;
FIFO->put(d);
return true;
}
return false;
}
bool flush(int level = 0) {
if (OPENED && FILE && (FIFO->count() > level)) {
//unsigned long enter = micros();
int n;
while ((n = FIFO->gets(BUFF,SIZE)) > 0) {
FILE.write((const uint8_t*)BUFF, (size_t)(n*sizeof(DATA)));
}
//Serial.printf("flush: delta = %d usec\n", micros() - enter);
return true;
}
return false;
}
// for monitoring
bool isOpen(void) { return OPENED; }
int getCount(void) { return COUNT; }
int getDelta(void) { return millis() - START; }
int getFreq(void) { return 1000 * COUNT / getDelta(); }
#if 0
// for convert bin to csv
int toCSV(const char* binPath, const char* csvPath) {
if (FILE) return -1;
File bin = SD.open(binPath, FILE_READ);
File csv = SD.open(csvPath, FILE_WRITE);
int count = 0;
if (bin && csv) {
// header
for (int i=0; i<NCOL; i++) csv.printf("%s%s", COLS[i], i<NCOL-1? ",": "\n");
// content
size_t bsz = sizeof(DATA);
float dat[NCOL];
while (bin.read((uint8_t*)dat,bsz) == bsz) {
for (int i=0; i<NCOL; i++) csv.printf("%.3f%s", dat[i], i<NCOL-1? ",": "\n");
count++;
}
}
return count;
}
// for ploting
int compStats(const char* binPath,float MIN[NCOL], float MEAN[NCOL], float MAX[NCOL], float STD[NCOL], float CC[NCOL][NCOL]) {
if (FILE) return -1;
File file = SD.open(binPath,FILE_READ);
if (file == NULL) return -2;
float SUM[NCOL], VAR[NCOL][NCOL];
// (1) init
for (int i=0; i<NCOL; i++) {
if (MIN!=NULL) MIN[i] = 1e+9;
if (MAX!=NULL) MAX[i] = -1e+9;
SUM[i] = 0.0F;
for (int j=0; j<NCOL; j++) VAR[i][j] = 0.0F;
}
// (2) scan
float DAT[NCOL];
size_t bsz = sizeof(DATA);
int NUM = 0;
while (file.read((uint8_t*)DAT, bsz) == bsz) {
for (int i=0; i<NCOL; i++) {
if (MIN && DAT[i]<MIN[i]) MIN[i] = DAT[i];
if (MAX && DAT[i]>MAX[i]) MAX[i] = DAT[i];
SUM[i] += DAT[i];
for (int j=0; j<NCOL; j++) {
VAR[i][j] += DAT[i]*DAT[j];
}
}
NUM++;
}
// (3) stat
for (int i=0; i<NCOL; i++) {
if (MEAN) MEAN[i] = (SUM[i]/NUM);
if (STD) STD[i] = sqrt( (VAR[i][i]/NUM) - (SUM[i]/NUM)*(SUM[i]/NUM) );
}
for (int i=0; i<NCOL; i++) {
for (int j=0; j<NCOL; j++) {
if (CC && STD) CC[i][j] = STD[i]*STD[j]? (VAR[i][j]/NUM) / (STD[i]*STD[j]): 0.0;
}
}
return NUM;
}
#endif
};
#endif