-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32_k32_yoji_yoko.ino
611 lines (491 loc) · 15.4 KB
/
esp32_k32_yoji_yoko.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
/*
* Copyright(C) 2023 by Yukiya Ishioka
*/
#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#define TFT_CS 5
#define TFT_RST 48
#define TFT_DC 4
#define TFT_SCLK 7
#define TFT_MOSI 6
#define TFT_CTRL 45
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
#include "app_param.h"
/* フォントの縦方向のドット数 */
#define DEF_FONT_SIZE 32
/* フォント配列の設定 */
extern const unsigned char ILMH32XB_FNT[];
extern const unsigned char ILMZ32XB_FNT[];
extern const unsigned char yoji_data[];
#define DEF_FONT_A_VAR (unsigned char *)ILMH32XB_FNT
#define DEF_FONT_K_VAR (unsigned char *)ILMZ32XB_FNT
#define DEF_LCD_WIDTH 240
#define DEF_LCD_HIGH 320
/* 変数、配列 */
unsigned char yoji_buffer1[ 4 ][ DEF_FONT_HIGH ][DEF_FONT_WIDTH ];
unsigned char yoji_buffer2[ 4 ][ DEF_FONT_HIGH ][DEF_FONT_WIDTH ];
unsigned char tmp_buffer[ DEF_FONT_HIGH ][ DEF_FONT_WIDTH ];
int yoji_list[ DEF_YOJI_LST_MAX ];
int lcd_char_x;
int lcd_char_y;
int lcd_output_flag;
int lcd_output_color;
int disp_color;
int disp_color_bak;
int yoji_item_num;
int yoji_item_pos;
int yoji_item_pos_bef1;
int yoji_item_pos_bef2;
int yoji_pos;
int yoji_dir;
int yoji_bef_pos;
int yoji_bef_dir;
unsigned int yoji_code;
uint16_t lcd_color_tbl[] = {
ST77XX_RED,
ST77XX_GREEN,
//ST77XX_BLUE,
//ST77XX_MAGENTA,
ST77XX_CYAN,
ST77XX_YELLOW,
ST77XX_WHITE,
0x0000
};
#define DEF_LCD_COLOR_NUM 5
#define DR_REG_RNG_BASE 0x3ff75144
/*
* DR_REG_RNG
*/
uint32_t getESP32Random( void )
{
delay( 2 );
return READ_PERI_REG(DR_REG_RNG_BASE);
}
/*************************************************
*
*************************************************/
void y_fillScreen( uint16_t color )
{
int row;
int col;
tft.startWrite();
tft.setAddrWindow(0, 0, DEF_LCD_WIDTH, DEF_LCD_HIGH);
tft.endWrite();
for( row=0 ; row<DEF_LCD_HIGH ; row++ ) {
for( col=0 ; col<DEF_LCD_WIDTH ; col++ ) {
tft.pushColor( color );
}
}
}
/*************************************************
* font size ANK
*************************************************/
void y_setImage_a( uint16_t x, uint16_t y, uint16_t color, unsigned char *buff )
{
int row;
int col;
int wx = DEF_FONT_SIZE/2;
int wy = DEF_FONT_SIZE;
int tx;
int ty;
for( ty=0 ; ty<DEF_FONT_HIGH ; ty++ ) {
for( tx=0 ; tx<DEF_FONT_WIDTH/2 ; tx++ ) {
tmp_buffer[ tx ][ DEF_FONT_HIGH-1 - ty ] = *buff;
buff++;
}
}
buff = (unsigned char*)tmp_buffer;
tft.startWrite();
//tft.setAddrWindow( x, y, wx, wy );
tft.setAddrWindow( DEF_LCD_WIDTH-1-y-wy, x, wy, wx );
tft.endWrite();
for( row=0 ; row<wy ; row++ ) {
for( col=0 ; col<wx ; col++ ) {
if( *buff != 0 ) {
tft.pushColor( color );
} else {
tft.pushColor( ST77XX_BLACK );
}
buff++;
}
}
}
/*************************************************
* font size for KANJI
*************************************************/
void y_setImage_k( uint16_t x, uint16_t y, uint16_t color, unsigned char *buff )
{
int row;
int col;
int wx = DEF_FONT_SIZE;
int wy = DEF_FONT_SIZE;
int tx;
int ty;
for( ty=0 ; ty<DEF_FONT_HIGH ; ty++ ) {
for( tx=0 ; tx<DEF_FONT_WIDTH ; tx++ ) {
tmp_buffer[ tx ][ DEF_FONT_HIGH-1 - ty ] = *buff;
buff++;
}
}
buff = (unsigned char*)tmp_buffer;
tft.startWrite();
//tft.setAddrWindow( x, y, wx, wy );
tft.setAddrWindow( DEF_LCD_WIDTH-1-y-wy, x, wy, wx );
tft.endWrite();
for( row=0 ; row<wy ; row++ ) {
for( col=0 ; col<wx ; col++ ) {
if( *buff != 0 ) {
tft.pushColor( color );
} else {
tft.pushColor( ST77XX_BLACK );
}
buff++;
}
}
}
/*************************************************
*
*************************************************/
void y_output_char_a( unsigned char *buff )
{
int tmp_x;
int tmp_y;
tmp_x = lcd_char_x + DEF_FONT_SIZE/2;
if( tmp_x >= DEF_LCD_WIDTH ) {
lcd_char_x = 0;
tmp_y = lcd_char_y + DEF_FONT_SIZE + 4;
if( tmp_y >= DEF_LCD_HIGH ) {
lcd_output_flag = 1;
return;
}
lcd_char_y = tmp_y;
}
y_setImage_a( lcd_char_x, lcd_char_y, lcd_color_tbl[ lcd_output_color%DEF_LCD_COLOR_NUM ], buff );
lcd_char_x += DEF_FONT_SIZE/2;
}
/*************************************************
*
*************************************************/
void y_output_char_k( unsigned char *buff )
{
int tmp_x;
int tmp_y;
tmp_x = lcd_char_x + DEF_FONT_SIZE;
if( tmp_x >= DEF_LCD_WIDTH ) {
lcd_char_x = 0;
tmp_y = lcd_char_y + DEF_FONT_SIZE + 4;
if( tmp_y >= DEF_LCD_HIGH ) {
lcd_output_flag = 1;
return;
}
lcd_char_y = tmp_y;
}
y_setImage_k( lcd_char_x, lcd_char_y, lcd_color_tbl[ lcd_output_color%DEF_LCD_COLOR_NUM ], buff );
lcd_char_x += DEF_FONT_SIZE;
}
/*************************************************
* 1文字分のフォントデータを表示データ配列の指定位置へセット
*************************************************/
void set_font( uint8_t *font, uint8_t *buff, int width )
{
int i, j, k;
int row;
int w = (width/8); /* font width byte */
uint8_t pat;
/* row */
for( i=0 ; i<DEF_FONT_HIGH ; i++ ) {
row = DEF_FONT_WIDTH * i;
/* col */
for( j=0 ; j<w ; j++ ) {
pat = 0x80;
for( k=0 ; k<8 ; k++ ) {
if( (font[ i * w + j ] & pat) != 0 ) {
buff[ row + j*8 + k ] = 1;
}
pat >>= 1; /* bit shift */
}
}
}
}
void make_yoji_bitmap1( unsigned char *yoji )
{
unsigned int code;
int i;
unsigned char *fontdata;
memset( yoji_buffer1, 0x00, sizeof(yoji_buffer1) );
for( i=0 ; i<DEF_YOJI_MOJI ; i++ ) {
code = (unsigned int)(*yoji);
code = (code<<8) + *(yoji+1);
fontdata = read_fontx2_k( DEF_FONT_K_VAR, code );
set_font( fontdata, (uint8_t *)yoji_buffer1[i], DEF_FONT_WIDTH );
yoji += 2;
}
}
void make_yoji_bitmap2( unsigned char *yoji )
{
unsigned int code;
int i;
unsigned char *fontdata;
memset( yoji_buffer2, 0x00, sizeof(yoji_buffer2) );
for( i=0 ; i<DEF_YOJI_MOJI ; i++ ) {
code = (unsigned int)(*yoji);
code = (code<<8) + *(yoji+1);
fontdata = read_fontx2_k( DEF_FONT_K_VAR, code );
set_font( fontdata, (uint8_t *)yoji_buffer2[i], DEF_FONT_WIDTH );
yoji += 2;
}
}
void set_font_to_lcdimg( int x, int y, unsigned char *font, unsigned int color )
{
int loop;
unsigned int ctmp;
unsigned int r, g, b;
ctmp = lcd_color_tbl[color%DEF_LCD_COLOR_NUM];
for( loop=4 ; loop>=0 ; loop-- ) {
r = ((ctmp & 0xf800) >> loop) & 0xf800;
g = ((ctmp & 0x07e0) >> loop) & 0x07e0;
b = ((ctmp & 0x001f) >> loop) & 0x001f;
//lcd_set_image( x, y, 32, 32, font, r|g|b );
y_setImage_k( x, y, r|g|b, font );
delay( DEF_TIM_GRAD_DLY );
}
}
void clr_font_to_lcdimg( int x, int y, unsigned char *font, unsigned int color )
{
int loop;
unsigned int ctmp;
unsigned int r, g, b;
ctmp = lcd_color_tbl[ color%DEF_LCD_COLOR_NUM ];
for( loop=1 ; loop<=5 ; loop++ ) {
r = ((ctmp & 0xf800) >> loop) & 0xf800;
g = ((ctmp & 0x07e0) >> loop) & 0x07e0;
b = ((ctmp & 0x001f) >> loop) & 0x001f;
if( loop == 5 ) {
g = 0;
}
//lcd_set_image( x, y, 32, 32, font, r|g|b );
y_setImage_k( x, y, r|g|b, font );
delay( DEF_TIM_GRAD_DLY );
}
}
/*************************************************
* 半角文字コードからフォントデータの先頭アドレス取得
*************************************************/
unsigned char *read_fontx2_a( unsigned char *font, unsigned int code )
{
unsigned char *address = NULL ;
unsigned int fontbyte ;
fontbyte = (font[14] + 7) / 8 * font[15] ;
address = &font[17] + fontbyte * code ;
return address ;
}
/*************************************************
* 全角文字コードからフォントデータの先頭アドレス取得
*************************************************/
unsigned char *read_fontx2_k( unsigned char *font, unsigned int code )
{
unsigned char *address = NULL ;
unsigned char *tmp ;
unsigned int blknum, i, fontnum ;
unsigned int bstart, bend ;
unsigned int fontbyte ;
fontbyte = (font[14] + 7) / 8 * font[15] ;
fontnum = 0 ;
blknum = (unsigned int)font[17] * 4 ;
tmp = &font[18] ;
for( i=0 ; i<blknum ; i+=4 ) {
bstart = tmp[i] + ((unsigned int)tmp[i+1] << 8) ;
bend = tmp[i+2] + ((unsigned int)tmp[i+3] << 8) ;
if( code >= bstart && code <= bend ) {
address = tmp + (fontnum + (code - bstart)) * fontbyte + blknum ;
break ;
}
fontnum += (bend - bstart) + 1 ;
}
return address ;
}
int yoji_item_count( void )
{
int count = 0;
unsigned char *pnt = (unsigned char *)yoji_data;
while( *pnt != 0x00 ) {
count++;
pnt += 10;
}
return count;
}
int yoji_comp_list( unsigned int code, int pos )
{
int num;
int i;
unsigned char *pnt = (unsigned char *)yoji_data;
for( i=0 ; i<DEF_YOJI_LST_MAX ; i++ ) {
yoji_list[i] = 0;
}
pnt += pos * 2;
num = 0;
for( i=0 ; i<yoji_item_num ; i++ ) {
if( *pnt == ((code >> 8) & 0xff) && *(pnt+1) == (code & 0xff) ) {
yoji_list[ num++ ] = i;
if( num >= DEF_YOJI_LST_MAX ) {
break;
}
}
pnt += 10;
}
return num;
}
int pos_template_y[] = { 50, 86, 122, 158 };
int pos_template_x[] = { 80, 116, 152, 188 };
void task1( void )
{
unsigned char *yoji_table;
int num;
int i;
int match;
int pos;
int loop;
int tmp_yoji_item_pos;
yoji_pos = 0;
yoji_dir = 0;
yoji_bef_pos = 0;
yoji_bef_dir = 0;
yoji_code = 0x0000;
yoji_item_pos_bef1 = 99999;
yoji_item_pos_bef2 = 99999;
yoji_item_num = yoji_item_count();
Serial.printf( "yoji_item_num=%d", yoji_item_num );
yoji_item_pos = rand() % yoji_item_num;
Serial.printf( "yoji_item_pos=%d", yoji_item_pos );
disp_color = 0;
disp_color_bak = 0;
yoji_table = (unsigned char *)yoji_data + yoji_item_pos * 10;
/* set yoji */
make_yoji_bitmap1( yoji_table );
set_font_to_lcdimg( pos_template_x[0], pos_template_y[yoji_pos], (unsigned char *)yoji_buffer1[0], disp_color );
set_font_to_lcdimg( pos_template_x[1], pos_template_y[yoji_pos], (unsigned char *)yoji_buffer1[1], disp_color );
set_font_to_lcdimg( pos_template_x[2], pos_template_y[yoji_pos], (unsigned char *)yoji_buffer1[2], disp_color );
set_font_to_lcdimg( pos_template_x[3], pos_template_y[yoji_pos], (unsigned char *)yoji_buffer1[3], disp_color );
delay( DEF_TIM_YOJI_DLY );
while( true ) {
disp_color_bak = disp_color;
disp_color++;
if( lcd_color_tbl[disp_color] == 0x0000 ) {
disp_color = 0;
}
yoji_bef_pos = yoji_pos;
yoji_pos = rand() % DEF_YOJI_MOJI;
/* search yoji */
for( match=0 ; match <= 1 ; match++ ) {
for( i=0 ; i<DEF_YOJI_MOJI ; i++ ) {
yoji_table = (unsigned char *)yoji_data + yoji_item_pos * 10 + yoji_pos * 2;
yoji_code = (*yoji_table << 8) + *(yoji_table + 1);
/* make list and get list-num */
num = yoji_comp_list( yoji_code, yoji_bef_pos );
if( num == 0 ) {
Serial.printf( "yoji_pos=%d (match=%d num=%d) skip search.", yoji_pos, match, num );
yoji_pos++;
if( yoji_pos >= DEF_YOJI_MOJI ) {
yoji_pos = 0;
}
continue;
}
Serial.printf( "yoji_pos=%d (match=%d num=%d)", yoji_pos, match, num );
/* duplication check for previous data */
tmp_yoji_item_pos = yoji_item_pos;
for( loop=0 ; loop<5 ; loop++ ) {
pos = rand() % num;
yoji_item_pos = yoji_list[pos];
//if( (match > 0 && num == 1) ||
if( (match > 0) ||
(yoji_item_pos != yoji_item_pos_bef1 && yoji_item_pos != yoji_item_pos_bef2) ) {
match = 2;
break;
}
}
if( match == 2 ) {
break;
} else {
Serial.printf( "duplicated. new=%d bef1=%d bef2=%d",
yoji_item_pos, yoji_item_pos_bef1, yoji_item_pos_bef2 );
yoji_item_pos = tmp_yoji_item_pos;
yoji_pos++;
if( yoji_pos >= DEF_YOJI_MOJI ) {
yoji_pos = 0;
}
}
}
}
Serial.printf( "yoji_pos=%d yoji_item_pos=%d", yoji_pos, yoji_item_pos );
{
yoji_bef_dir = yoji_dir;
if( yoji_dir == 0 ) {
yoji_dir = 1;
} else {
yoji_dir = 0;
}
yoji_table = (unsigned char *)yoji_data + yoji_item_pos * 10;
/* set yoji */
if( yoji_dir == 0 ) {
make_yoji_bitmap1( yoji_table );
for( i=0 ; i<4 ; i++ ) {
set_font_to_lcdimg( pos_template_x[i], pos_template_y[yoji_pos], (unsigned char *)yoji_buffer1[i], disp_color );
}
} else {
make_yoji_bitmap2( yoji_table );
for( i=0 ; i<4 ; i++ ) {
set_font_to_lcdimg( pos_template_x[yoji_pos], pos_template_y[i], (unsigned char *)yoji_buffer2[i], disp_color );
}
}
delay( DEF_TIM_SET_DLY );
if( yoji_bef_dir == 0 ) {
for( i=0 ; i<DEF_YOJI_MOJI ; i++ ) {
if( i != yoji_pos ) {
clr_font_to_lcdimg( pos_template_x[i], pos_template_y[yoji_bef_pos], (unsigned char *)yoji_buffer1[i], disp_color_bak );
}
}
} else {
for( i=0 ; i<DEF_YOJI_MOJI ; i++ ) {
if( i != yoji_pos ) {
clr_font_to_lcdimg( pos_template_x[yoji_bef_pos], pos_template_y[i], (unsigned char *)yoji_buffer2[i], disp_color_bak );
}
}
}
yoji_item_pos_bef2 = yoji_item_pos_bef1;
yoji_item_pos_bef1 = yoji_item_pos;
}
delay( DEF_TIM_YOJI_DLY );
}
}
/*************************************************
* Arduinoの setup関数
*************************************************/
void setup( void )
{
/* シリアルの初期化 */
Serial.begin(115200);
Serial.println( "call setup()" );
lcd_char_x = 0;
lcd_char_y = 0;
lcd_output_flag = 0;
lcd_output_color = 0;
pinMode( TFT_CTRL, OUTPUT );
digitalWrite( TFT_CTRL, LOW );
tft.init(240, 320);
y_fillScreen(ST77XX_BLACK);
delay(500);
}
/*************************************************
* Arduinoの loop関数
*************************************************/
void loop( void )
{
Serial.println( "call mainTask()" );
srand( esp_random() );
task1();
delay( 100 );
}