-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStickDisp.ino
629 lines (536 loc) · 13.7 KB
/
StickDisp.ino
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
/**
* @file StickDisp.ino
* @author a.amg
* @brief M5stickC plus 用簡易表示端末化プログラム
* @version 0.3
* @date 2022-10-22
*
* @copyright Copyright (c) 2022
*
*/
#include <M5StickCPlus.h>
#include <time.h>
TFT_eSprite sprite = TFT_eSprite(&M5.Lcd);
hw_timer_t *wdtimer = NULL;
#define TIMEOUT 1000
#define CMDSTR_MAX 32
#define LCDCONTENT_MAXSCENE 3
#define LCDCONTENT_VALLINES 4
#define VERSION_STRING "0.3.0"
#define LEDMODE_OFF 0
#define LEDMODE_ON 1
#define LEDMODE_ONESHOT 2
typedef struct tLCDCONTENT {
int scene;
struct tm dttime;
uint8_t led;
int debug;
uint16_t clk_color;
String valuestr[LCDCONTENT_VALLINES][2];
String toppgstr[2];
} LCDCONTENT;
static LCDCONTENT lcdinfo;
typedef struct tBEEPCTRL {
int tone;
int count;
} BEEPCTRL;
static BEEPCTRL beepctrl;
void LCDC_init()
{
lcdinfo.scene = 0;
lcdinfo.toppgstr[0] = "";
lcdinfo.toppgstr[1] = "";
lcdinfo.dttime.tm_sec = 0;
lcdinfo.led = 0;
lcdinfo.debug = 0;
lcdinfo.clk_color = WHITE;
M5.Lcd.setRotation(1);
sprite.setColorDepth(16);
sprite.createSprite(M5.Lcd.width(), M5.Lcd.height());
for(int i = 0; i < LCDCONTENT_VALLINES; i++)
{
lcdinfo.valuestr[i][0] = "Label";
lcdinfo.valuestr[i][1] = "Value";
}
}
/**
* @brief 時計画面の描画
*/
void LCDC_show_scene0()
{
sprite.fillRect(0,0,M5.Lcd.width(), M5.Lcd.height(), BLACK);
// Current time
sprite.setTextColor(WHITE, BLACK);
sprite.setTextFont(4); // 26pixel font
sprite.setCursor(0,0);
sprite.printf("%04d/%02d/%02d", lcdinfo.dttime.tm_year + 1900, lcdinfo.dttime.tm_mon + 1, lcdinfo.dttime.tm_mday);
sprite.setTextColor(lcdinfo.clk_color, BLACK);
sprite.setTextFont(7); // 48pixel font
sprite.setCursor(0,30);
sprite.printf("%02d:%02d:%02d", lcdinfo.dttime.tm_hour, lcdinfo.dttime.tm_min, lcdinfo.dttime.tm_sec);
sprite.setTextColor(WHITE, BLACK);
sprite.setTextFont(4); // 26pixel font
sprite.setTextSize(1);
sprite.setCursor(0, 84);
sprite.print(lcdinfo.toppgstr[0]);
sprite.setCursor(0, 110);
sprite.print(lcdinfo.toppgstr[1]);
sprite.pushSprite(0, 0);
}
/**
* @brief テーブル画面の描画
*/
void LCDC_show_scene1()
{
const int LBLWIDTH = 80;
int i, x, y;
sprite.fillRect(0,0,M5.Lcd.width(), M5.Lcd.height(), NAVY);
sprite.setTextFont(4); // 26pix font
sprite.setTextSize(1);
sprite.setTextColor(WHITE, NAVY);
x = 4;
for(i = 0; i < LCDCONTENT_VALLINES; i++)
{
y = i * 30 + 4;
sprite.setCursor(x,y);
sprite.print(lcdinfo.valuestr[i][0]);
}
x = LBLWIDTH +4;
for(i = 0; i < LCDCONTENT_VALLINES; i++)
{
y = i * 30 + 4;
sprite.setCursor(x,y);
sprite.print(lcdinfo.valuestr[i][1]);
}
// 枠線
for(i = 0; i <= LCDCONTENT_VALLINES; i++)
{
y = i * 30;
sprite.drawLine(0, y, M5.Lcd.width(), y, DARKGREY);
}
sprite.drawLine(LBLWIDTH, 0, LBLWIDTH, y, DARKGREY);
sprite.pushSprite(0, 0);
}
/**
* @brief シャットダウン画面の描画
*/
void LCDC_show_scene2()
{
sprite.fillRect(0,0,M5.Lcd.width(), M5.Lcd.height(), MAROON);
sprite.setTextFont(4); // 26pix font
sprite.setTextSize(1);
sprite.setTextColor(WHITE, MAROON);
sprite.setCursor(0,M5.Lcd.height()/2 - 13);
sprite.printf("Shutdown?");
sprite.pushSprite(0, 0);
}
/**
* @brief LCDの表示更新. 100ms毎に呼ばれる.
*/
void LCDC_show()
{
switch(lcdinfo.scene)
{
case 0:
LCDC_show_scene0();
break;
case 1:
LCDC_show_scene1();
break;
case 2:
LCDC_show_scene2();
break;
default:
LCDC_show_scene0();
break;
}
// LED点灯状態の更新
if( lcdinfo.led != LEDMODE_OFF )
{
digitalWrite(GPIO_NUM_10, LOW); // LOWで点灯
}
else
{
digitalWrite(GPIO_NUM_10, HIGH);
}
// oneshot の場合は次のタイミング(100ms後)では消灯
if( lcdinfo.led == LEDMODE_ONESHOT)
{
lcdinfo.led = LEDMODE_OFF;
}
}
/**
* @brief RTC読み出し
* @param tm 時刻情報格納先
*/
void rtc_read(struct tm *tm)
{
RTC_DateTypeDef DateStruct;
RTC_DateTypeDef DateStruct2;
RTC_TimeTypeDef TimeStruct;
do{
M5.Rtc.GetData(&DateStruct);
M5.Rtc.GetTime(&TimeStruct);
M5.Rtc.GetData(&DateStruct2);
}while( DateStruct.Date != DateStruct2.Date );
tm->tm_year = DateStruct.Year - 1900;
tm->tm_mon = DateStruct.Month - 1;
tm->tm_mday = DateStruct.Date;
tm->tm_hour = TimeStruct.Hours;
tm->tm_min = TimeStruct.Minutes;
tm->tm_sec = TimeStruct.Seconds;
}
/**
* @brief RTC読み出し.時刻が進むまで待つ.
* @param tm 時刻情報格納先
*/
void rtc_read_step(struct tm *tm)
{
struct tm told;
rtc_read(&told);
do{
rtc_read(tm);
if( told.tm_sec == tm->tm_sec )
{
delay(10);
}
}while(told.tm_sec == tm->tm_sec);
}
/**
* @brief RTC書き込み
* @param tm 時刻情報
*/
void rtc_write(struct tm *tm)
{
RTC_DateTypeDef DateStruct;
RTC_TimeTypeDef TimeStruct;
DateStruct.Year = tm->tm_year + 1900;
DateStruct.Month = tm->tm_mon + 1;
DateStruct.Date = tm->tm_mday;
TimeStruct.Hours = tm->tm_hour;
TimeStruct.Minutes = tm->tm_min;
TimeStruct.Seconds = tm->tm_sec;
M5.Rtc.SetData(&DateStruct);
M5.Rtc.SetTime(&TimeStruct);
}
void IRAM_ATTR resetModule()
{
ets_printf("Reboot\n");
esp_restart();
}
/**
* @brief セットアップ
*/
void setup()
{
// Initialize the M5Stack object
M5.begin();
beepctrl.tone = 1000;
beepctrl.count = 0;
M5.Beep.tone(1000);
M5.Beep.mute();
LCDC_init();
// LEDの制御 LOWを出力すると点灯する
pinMode(GPIO_NUM_10, OUTPUT);
digitalWrite(GPIO_NUM_10, HIGH);
// シリアルポートのレート
Serial.begin(115200);
// RTCから初期値の読み出し
rtc_read(&lcdinfo.dttime);
LCDC_show();
wdtimer = timerBegin(0, 80, true);
timerAttachInterrupt(wdtimer, &resetModule, true);
timerAlarmWrite(wdtimer, TIMEOUT * 1000, false);
timerAlarmEnable(wdtimer);
}
/**
* @brief コマンド文字列の確認.
* @param buf コマンド文字列
* @return 1:正常,0:異常
*/
int serial_isvalid(const uint8_t *buf)
{
int i;
int rtn;
rtn = 1;
for( i = 0; i < CMDSTR_MAX; i++)
{
if( buf[i] == 0 )
{
rtn = 1;
break;
}
if(buf[i] < ' ' || buf[i] > '~')
{
rtn = 0;
break;
}
}
return rtn;
}
/**
* @brief hex文字から整数へ変換.
*
* @param c
* @return 変換した整数(0-15).失敗した場合は-1
*/
inline int hexchar2dec(char c)
{
if( c >= '0' && c <= '9' )
{
return c - '0';
}
if( c >= 'A' && c <= 'F' )
{
return c - 'A' + 10;
}
if( c >= 'a' && c <= 'f' )
{
return c - 'a' + 10;
}
return -1;
}
/**
* @brief コマンドを1つ処理する
* @param buf コマンド文字列
* @return 0:正常
*/
int serial_cmd(const uint8_t *buf)
{
struct tm tm;
int i, scn;
int l,c,n;
int cr, cb, cg;
if( serial_isvalid(buf) == 0 )
{
return 1;
}
switch( buf[0] )
{
// Read RTC
case 'r':
rtc_read(&tm);
Serial.printf("$r%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
Serial.printf("%02d%02d%02d\r\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
break;
// 秒が更新される瞬間まで待って返答する read
case 'R':
rtc_read_step(&tm);
Serial.printf("$R%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
Serial.printf("%02d%02d%02d\r\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
break;
// Write RTC
case 'w':
for(i = 1; i<= 14; i++)
{
if( buf[i] < '0' || buf[i] > '9' )
{
return 1;
}
}
tm.tm_year = (buf[1] - '0') * 1000
+ (buf[2] - '0') * 100
+ (buf[3] - '0') * 10
+ (buf[4] - '0') - 1900;
tm.tm_mon = (buf[5] - '0') * 10 + (buf[6] - '0') - 1;
tm.tm_mday = (buf[7] - '0') * 10 + (buf[8] - '0');
tm.tm_hour = (buf[9] - '0') * 10 + (buf[10] - '0');
tm.tm_min = (buf[11] - '0') * 10 + (buf[12] - '0');
tm.tm_sec = (buf[13] - '0') * 10 + (buf[14] - '0');
rtc_write(&tm);
break;
// 時刻画面に表示する文字列
case 't':
n = buf[1] - '0';
if( n < 0 || n > 1 )
{
return 1;
}
lcdinfo.toppgstr[n] = (const char*)&buf[2];
break;
// 時刻の文字の色
// cRGB で,R,G,Bはそれぞれ0-Fの値.M5StickのRGBはビット数が565なので,変換が必要.
case 'c':
cr = hexchar2dec(buf[1]) * 15;
cg = hexchar2dec(buf[2]) * 15;
cb = hexchar2dec(buf[3]) * 15;
if( cr >= 0 && cb >= 0 && cg >= 0 )
{
lcdinfo.clk_color = M5.lcd.color565(cr, cg, cb);
}
break;
// Power off
case 'p':
M5.Axp.PowerOff();
break;
// LED
case 'l':
if( buf[1] == '1' )
{
lcdinfo.led = LEDMODE_ON;
}
else if (buf[1] == '2')
{
lcdinfo.led = LEDMODE_ONESHOT;
}
else
{
lcdinfo.led = LEDMODE_OFF;
}
break;
// Scene選択
case 's':
scn = buf[1] - '0';
if( scn < 0 || scn >= LCDCONTENT_MAXSCENE )
{
return 1;
}
lcdinfo.scene = scn;
break;
// 汎用文字列
// v10abcdefg という形式で,1が行,0が列を表す.0列目がラベルに相当.
case 'v':
l = buf[1] - '0';
c = buf[2] - '0';
if( l < 0 || l >= LCDCONTENT_VALLINES )
{
return 1;
}
if( c < 0 || c > 1 )
{
return 1;
}
lcdinfo.valuestr[l][c] = (const char*)&buf[3];
break;
// バージョン取得
case 'V':
Serial.printf("$Vstickdisp " VERSION_STRING "\r\n");
break;
default:
// Error
return 1;
}
return 0;
}
/**
* @brief シリアルから入ってきた文字列の処理.
*
* @param c 入力された文字
* @return 0:正常
*/
int serial_parse(uint8_t c)
{
static int rxstate = 0;
static int rxptr = 0;
static uint8_t rxbuf[CMDSTR_MAX];
int rtn;
if(c == 0 )
{
rxstate = 0;
rxptr = 0;
return 0;
}
rtn = 0;
switch(rxstate)
{
case 0:
if(c == '$')
{
rxptr = 0;
rxstate = 1;
}
break;
case 1:
if(c == '\r' || c == '\n' )
{
rxbuf[rxptr] = 0;
rtn = serial_cmd(rxbuf);
rxstate = 0;
}
else
{
rxbuf[rxptr++] = c;
if( rxptr >= sizeof(rxbuf) ) // overflow
{
rxstate = 0;
rxptr = 0;
rtn = 1;
}
}
break;
default:
rxstate = 0;
break;
}
return rtn;
}
/**
* @brief ビープ音制御のためのポーリング用関数
*
*/
void beep_poll()
{
if( beepctrl.count > 0 )
{
beepctrl.count--;
}
else
{
M5.Beep.mute();
}
}
/**
* @brief ピっという音を鳴らす
*
* @param tone 音の高さ
* @param count 鳴らす時間 (10ms単位)
*/
void beep_trigger(int tone, int count)
{
beepctrl.tone = tone;
beepctrl.count = count;
M5.Beep.tone(beepctrl.tone);
}
/**
* @brief 10ms周期で回るループ
*
*/
void loop()
{
uint8_t rxc;
static uint8_t updatecount = 0;
timerWrite(wdtimer, 0);
M5.update();
while( Serial.available() > 0 )
{
rxc = Serial.read();
serial_parse(rxc);
}
if( M5.BtnA.wasPressed() )
{
Serial.printf("$b%dA\r\n", lcdinfo.scene);
beep_trigger(1000, 10);
}
if( M5.BtnB.wasPressed() )
{
Serial.printf("$b%dB\r\n", lcdinfo.scene);
lcdinfo.scene++;
if( lcdinfo.scene >= LCDCONTENT_MAXSCENE )
{
lcdinfo.scene = 0;
}
}
beep_poll();
// 画面は100ms毎に更新
if( updatecount == 0)
{
rtc_read(&lcdinfo.dttime);
LCDC_show();
updatecount = 10;
}
else
{
updatecount--;
}
delay(10);
}