forked from nopnop2002/Raspberry-ili9340spi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ili9340.c
837 lines (733 loc) · 19 KB
/
ili9340.c
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// ili9340.c
// Used by bcm2835 library.(Mike McCauley)
//
// Original is http://imagewriteriij.blogspot.jp/2014/01/raspberry-pi-9-lcd-1.html
// LCD test program 20130103 by ImageWriter
//
// [Pin connection]
// ILI9340-SPI Rpi(pin)
// ------------------------
// MISO------------MISO(21)
// LED---220ohm----3.3V( 1)
// SCK-------------SCLK(23)
// MOSI------------MOSI(19)
// D/C-------------IO02( 3) LOW = 0 = COMMAND
// RES-------------IO03( 5) LOW = 0 = RESET
// CS--------------CS0 (24) LOW = 0 = Chip Select
// GND-------------0V ( 6)
// VCC-------------3.3V( 1)
// ------------------------
//
// [SPI settings]
// ORDER : MSB First
// MODE : Mode0
// CLOCK : 31.25MHz on Rpi/Rpi2, 50MHz on RPI3
// CS : CS0
// CS_POL : LOW
//
//
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#ifdef WPI
#include <wiringPi.h>
#include <wiringPiSPI.h>
#endif
#ifdef BCM
#include <bcm2835.h>
#endif
#include "ili9340.h"
#define D_C 22 // GPIO22=Pin#15
#define RES 17 // GPIO17=Pin#11
#define C_S 8 // GPIO8=Pin#24
#define BeepPin 19 // GPIO19
#define LED 18 // GPIO18
#define _DEBUG_ 0
uint16_t _FONT_DIRECTION_;
uint16_t _FONT_FILL_;
uint16_t _FONT_FILL_COLOR_;
uint16_t _FONT_UNDER_LINE_;
uint16_t _FONT_UNDER_LINE_COLOR_;
int _width;
int _height;
int _offsetx;
int _offsety;
#ifdef WPI
// Write Command 8Bit
// D/C=LOW then,write command(8bit)
void lcdWriteCommandByte(uint8_t c){
digitalWrite(D_C, LOW);
wiringPiSPIDataRW(0, &c, 1);
}
// Write Data 8Bit
// D/C=HIGH then,write data(8bit)
void lcdWriteDataByte(uint8_t c){
digitalWrite(D_C, HIGH);
wiringPiSPIDataRW(0, &c, 1);
}
// Write Data 16Bit
void lcdWriteDataWord(uint16_t w){
uint8_t hi,lo;
hi = (w >> 8) & 0xFF;
lo = w & 0xFF;
lcdWriteDataByte(hi);
lcdWriteDataByte(lo);
}
// Write Tow Data 8Bit
void lcdWriteAddr(uint8_t addr1, uint8_t addr2){
uint8_t byte[4];
byte[0] = (addr1 >> 8) & 0xFF;
byte[1] = addr1 & 0xFF;
byte[2] = (addr2 >> 8) & 0xFF;
byte[3] = addr2 & 0xFF;
digitalWrite(D_C, HIGH);
wiringPiSPIDataRW(0, byte, 4);
}
// Write Data 16Bit
void lcdWriteColor(uint16_t color, uint16_t size) {
uint8_t byte[1024];
int index=0;
int i;
for(i=0;i<size;i++) {
byte[index++] = (color >> 8) & 0xFF;
byte[index++] = color & 0xFF;
}
digitalWrite(D_C, HIGH);
wiringPiSPIDataRW(0, byte, size*2);
}
#endif
#ifdef BCM
// Write Command
// D/C=LOW then,write command(8bit)
void lcdWriteCommandByte(uint8_t c){
bcm2835_gpio_write(D_C, LOW);
bcm2835_spi_transfer(c);
}
// Write Data 8Bit
// D/C=HIGH then,write data(8bit)
void lcdWriteDataByte(uint8_t c){
bcm2835_gpio_write(D_C, HIGH);
bcm2835_spi_transfer(c);
}
// Write Data 16Bit
void lcdWriteDataWord(uint16_t w){
bcm2835_gpio_write(D_C, HIGH);
bcm2835_spi_write(w);
}
// Write Tow Data 8Bit
void lcdWriteAddr(uint8_t addr1, uint8_t addr2){
bcm2835_gpio_write(D_C, HIGH);
bcm2835_spi_write(addr1);
bcm2835_spi_write(addr2);
}
// Write Data 16Bit
void lcdWriteColor(uint16_t color, uint16_t size) {
bcm2835_gpio_write(D_C, HIGH);
int i;
for(i=0;i<size;i++) bcm2835_spi_write(color);
}
#endif
#ifdef WPI
// SPI interfase initialize
// MSB,mode0,clock=8,cs0=low
void lcdInit(int width, int height, int offsetx, int offsety){
_width = width;
_height = height;
_offsetx = offsetx;
_offsety = offsety;
if (wiringPiSetupGpio() == -1) {
printf("wiringPiSetup Error\n");
return;
}
wiringPiSPISetup(0, 16000000);
// wiringPiSPISetup(0, 32000000);
_FONT_DIRECTION_ = DIRECTION0;
_FONT_FILL_ = false;
_FONT_UNDER_LINE_ = false;
}
void lcdReset(void){
pinMode(D_C, OUTPUT);
pinMode(RES, OUTPUT);
pinMode(C_S, OUTPUT);
digitalWrite(D_C, HIGH);
digitalWrite(C_S, LOW);
digitalWrite(RES, LOW);
delay(100);
digitalWrite(RES, HIGH);
delay(100);
}
#endif
#ifdef BCM
// SPI interfase initialize
// MSB,mode0,clock=8,cs0=low
void lcdInit(int width, int height, int offsetx, int offsety){
_width = width;
_height = height;
_offsetx = offsetx;
_offsety = offsety;
if (bcm2835_init() == -1) {
printf("bmc2835_init Error\n");
return;
}
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_8);
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
// Send a byte to the slave and simultaneously read a byte back from the slave
// If you tie MISO to MOSI, you should read back what was sent
_FONT_DIRECTION_ = DIRECTION0;
_FONT_FILL_ = false;
_FONT_UNDER_LINE_ = false;
}
// TFT Reset
void lcdReset(void){
bcm2835_gpio_fsel(D_C,BCM2835_GPIO_FSEL_OUTP); // D/C
bcm2835_gpio_fsel(RES,BCM2835_GPIO_FSEL_OUTP); // Reset
bcm2835_gpio_fsel(LED,BCM2835_GPIO_FSEL_OUTP); // LED
bcm2835_gpio_fsel(BeepPin,BCM2835_GPIO_FSEL_OUTP); // Beeper
//bcm2835_gpio_write(D_C, HIGH);
bcm2835_gpio_write(RES, LOW);
bcm2835_delay(100);
bcm2835_gpio_write(RES, HIGH);
bcm2835_delay(100);
}
#endif
// TFT initialize
void lcdSetup(void){
lcdWriteCommandByte(0xC0); //Power Control 1
lcdWriteDataByte(0x23);
lcdWriteCommandByte(0xC1); //Power Control 2
lcdWriteDataByte(0x10);
lcdWriteCommandByte(0xC5); //VCOM Control 1
lcdWriteDataByte(0x3e);
lcdWriteDataByte(0x28);
lcdWriteCommandByte(0xC7); //VCOM Control 2
lcdWriteDataByte(0x86);
lcdWriteCommandByte(0x36); //Memory Access Control
lcdWriteDataByte(0x48); //Left bottom start
lcdWriteCommandByte(0x3A); //Pixel Format Set
lcdWriteDataByte(0x55); //65K color: 16-bit/pixel
lcdWriteCommandByte(0x20); //Display Inversion OFF
lcdWriteCommandByte(0xB1); //Frame Rate Control
lcdWriteDataByte(0x00);
lcdWriteDataByte(0x18);
lcdWriteCommandByte(0xB6); //Display Function Control
lcdWriteDataByte(0x08);
lcdWriteDataByte(0xA2);
lcdWriteDataByte(0x27);
lcdWriteDataByte(0x00);
lcdWriteCommandByte(0xF2); //3Gamma Function Disable
lcdWriteDataByte(0x00);
lcdWriteCommandByte(0x26); //Gamma Set
lcdWriteDataByte(0x01);
lcdWriteCommandByte(0xE0); //Positive Gamma Correction
lcdWriteDataByte(0x0F);
lcdWriteDataByte(0x31);
lcdWriteDataByte(0x2B);
lcdWriteDataByte(0x0C);
lcdWriteDataByte(0x0E);
lcdWriteDataByte(0x08);
lcdWriteDataByte(0x4E);
lcdWriteDataByte(0xF1);
lcdWriteDataByte(0x37);
lcdWriteDataByte(0x07);
lcdWriteDataByte(0x10);
lcdWriteDataByte(0x03);
lcdWriteDataByte(0x0E);
lcdWriteDataByte(0x09);
lcdWriteDataByte(0x00);
lcdWriteCommandByte(0XE1); //Negative Gamma Correction
lcdWriteDataByte(0x00);
lcdWriteDataByte(0x0E);
lcdWriteDataByte(0x14);
lcdWriteDataByte(0x03);
lcdWriteDataByte(0x11);
lcdWriteDataByte(0x07);
lcdWriteDataByte(0x31);
lcdWriteDataByte(0xC1);
lcdWriteDataByte(0x48);
lcdWriteDataByte(0x08);
lcdWriteDataByte(0x0F);
lcdWriteDataByte(0x0C);
lcdWriteDataByte(0x31);
lcdWriteDataByte(0x36);
lcdWriteDataByte(0x0F);
lcdWriteCommandByte(0x11); //Sleep Out
#ifdef BCM
bcm2835_delay(200);
#endif
#ifdef WPI
delay(200);
#endif
lcdWriteCommandByte(0x29); //Display ON
}
// Draw pixel
// x:X coordinate
// y:Y coordinate
// color:color
void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color){
if (x >= _width) return;
if (y >= _height) return;
uint16_t _x = x + _offsetx;
uint16_t _y = y + _offsety;
lcdWriteCommandByte(0x2A); // set column(x) address
lcdWriteDataWord(_x);
lcdWriteDataWord(_x);
lcdWriteCommandByte(0x2B); // set Page(y) address
lcdWriteDataWord(_y);
lcdWriteDataWord(_y);
lcdWriteCommandByte(0x2C); // Memory Write
lcdWriteDataWord(color);
}
// Draw rectangule of filling
// x1:Start X coordinate
// y1:Start Y coordinate
// x2:End X coordinate
// y2:End Y coordinate
// color:color
void lcdDrawFillRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
int i,j;
if (x1 >= _width) return;
if (x2 >= _width) x2=_width-1;
if (y1 >= _height) return;
if (y2 >= _height) y2=_height-1;
uint16_t _x1 = x1 + _offsetx;
uint16_t _x2 = x2 + _offsetx;
uint16_t _y1 = y1 + _offsety;
uint16_t _y2 = y2 + _offsety;
lcdWriteCommandByte(0x2A); // set column(x) address
lcdWriteDataWord(_x1);
lcdWriteDataWord(_x2);
//lcdWriteAddr(_x1, _x2); // Don't work
lcdWriteCommandByte(0x2B); // set Page(y) address
lcdWriteDataWord(_y1);
lcdWriteDataWord(_y2);
//lcdWriteAddr(_y1, _y2); // Don't work
lcdWriteCommandByte(0x2C); // Memory Write
for(i=x1;i<=x2;i++){
uint16_t size = y2-y1+1;
lcdWriteColor(color, size);
#if 0
for(j=y1;j<=y2;j++){
lcdWriteDataWord(color);
}
#endif
}
}
// Display Off
void lcdDisplayOff(void) {
lcdWriteCommandByte(0x28); //Display OFF
}
// Display On
void lcdDisplayOn(void) {
lcdWriteCommandByte(0x29); //Display ON
}
// Display Inversion On
void lcdInversionOn(void) {
lcdWriteCommandByte(0x21); //Display Inversion ON
}
// Fill screen
// color:color
void lcdFillScreen(uint16_t color) {
lcdDrawFillRect(0, 0, _width-1, _height-1, color);
}
// Draw line
// x1:Start X coordinate
// y1:Start Y coordinate
// x2:End X coordinate
// y2:End Y coordinate
// color:color
void lcdDrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
int i;
int dx,dy;
int sx,sy;
int E;
/* distance between two points */
dx = ( x2 > x1 ) ? x2 - x1 : x1 - x2;
dy = ( y2 > y1 ) ? y2 - y1 : y1 - y2;
/* direction of two point */
sx = ( x2 > x1 ) ? 1 : -1;
sy = ( y2 > y1 ) ? 1 : -1;
/* inclination < 1 */
if ( dx > dy ) {
E = -dx;
for ( i = 0 ; i <= dx ; i++ ) {
lcdDrawPixel( x1, y1, color );
x1 += sx;
E += 2 * dy;
if ( E >= 0 ) {
y1 += sy;
E -= 2 * dx;
}
}
/* inclination >= 1 */
} else {
E = -dy;
for ( i = 0 ; i <= dy ; i++ ) {
lcdDrawPixel( x1, y1, color );
y1 += sy;
E += 2 * dx;
if ( E >= 0 ) {
x1 += sx;
E -= 2 * dy;
}
}
}
}
// Draw rectangule
// x1:Start X coordinate
// y1:Start Y coordinate
// x2:End X coordinate
// y2:End Y coordinate
// color:color
void lcdDrawRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
lcdDrawLine(x1,y1,x2,y1,color);
lcdDrawLine(x2,y1,x2,y2,color);
lcdDrawLine(x2,y2,x1,y2,color);
lcdDrawLine(x1,y2,x1,y1,color);
}
// Draw round
// x0:Central X coordinate
// y0:Central Y coordinate
// r:radius
// color:color
void lcdDrawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
int x;
int y;
int err;
int old_err;
x=0;
y=-r;
err=2-2*r;
do{
lcdDrawPixel(x0-x,y0+y,color);
lcdDrawPixel(x0-y,y0-x,color);
lcdDrawPixel(x0+x,y0-y,color);
lcdDrawPixel(x0+y,y0+x,color);
if ((old_err=err)<=x) err+=++x*2+1;
if (old_err>y || err>x) err+=++y*2+1;
} while(y<0);
}
// Draw round of filling
// x0:Central X coordinate
// y0:Central Y coordinate
// r:radius
// color:color
void lcdDrawFillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
int x;
int y;
int err;
int old_err;
int ChangeX;
x=0;
y=-r;
err=2-2*r;
ChangeX=1;
do{
if(ChangeX) {
lcdDrawLine(x0-x,y0-y,x0-x,y0+y,color);
lcdDrawLine(x0+x,y0-y,x0+x,y0+y,color);
} // if
ChangeX=(old_err=err)<=x;
if (ChangeX) err+=++x*2+1;
if (old_err>y || err>x) err+=++y*2+1;
} while(y<=0);
}
// Draw rectangule with round corner
// x1:Start X coordinate
// y1:Start Y coordinate
// x2:End X coordinate
// y2:End Y coordinate
// r:radius
// color:color
void lcdDrawRoundRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t r, uint16_t color) {
int x;
int y;
int err;
int old_err;
unsigned char temp;
if(x1>x2) {
temp=x1; x1=x2; x2=temp;
}
if(y1>y2) {
temp=y1; y1=y2; y2=temp;
}
if (x2 - x1 < r) return; // Add 20190517
if (y2 - y1 < r) return; // Add 20190517
x=0;
y=-r;
err=2-2*r;
do{
if(x) {
lcdDrawPixel(x1+r-x,y1+r+y,color);
lcdDrawPixel(x2-r+x,y1+r+y,color);
lcdDrawPixel(x1+r-x,y2-r-y,color);
lcdDrawPixel(x2-r+x,y2-r-y,color);
} // if
if ((old_err=err)<=x) err+=++x*2+1;
if (old_err>y || err>x) err+=++y*2+1;
} while(y<0);
lcdDrawLine(x1+r,y1 ,x2-r,y1 ,color);
lcdDrawLine(x1+r,y2 ,x2-r,y2 ,color);
lcdDrawLine(x1 ,y1+r,x1 ,y2-r,color);
lcdDrawLine(x2 ,y1+r,x2 ,y2-r,color);
}
// Draw arrow
// x1:Start X coordinate
// y1:Start Y coordinate
// x2:End X coordinate
// y2:End Y coordinate
// w:Width of the botom
// color:color
// Thanks http://k-hiura.cocolog-nifty.com/blog/2010/11/post-2a62.html
void lcdDrawArrow(uint16_t x0,uint16_t y0,uint16_t x1,uint16_t y1,uint16_t w,uint16_t color) {
double Vx= x1 - x0;
double Vy= y1 - y0;
double v = sqrt(Vx*Vx+Vy*Vy);
// printf("v=%f\n",v);
double Ux= Vx/v;
double Uy= Vy/v;
uint16_t L[2],R[2];
L[0]= x1 - Uy*w - Ux*v;
L[1]= y1 + Ux*w - Uy*v;
R[0]= x1 + Uy*w - Ux*v;
R[1]= y1 - Ux*w - Uy*v;
// printf("L=%d-%d R=%d-%d\n",L[0],L[1],R[0],R[1]);
// lcdDrawLine(x0,y0,x1,y1,color);
lcdDrawLine(x1,y1,L[0],L[1],color);
lcdDrawLine(x1,y1,R[0],R[1],color);
lcdDrawLine(L[0],L[1],R[0],R[1],color);
}
// Draw arrow of filling
// x1:Start X coordinate
// y1:Start Y coordinate
// x2:End X coordinate
// y2:End Y coordinate
// w:Width of the botom
// color:color
void lcdDrawFillArrow(uint16_t x0,uint16_t y0,uint16_t x1,uint16_t y1,uint16_t w,uint16_t color) {
double Vx= x1 - x0;
double Vy= y1 - y0;
double v = sqrt(Vx*Vx+Vy*Vy);
// printf("v=%f\n",v);
double Ux= Vx/v;
double Uy= Vy/v;
uint16_t L[2],R[2];
L[0]= x1 - Uy*w - Ux*v;
L[1]= y1 + Ux*w - Uy*v;
R[0]= x1 + Uy*w - Ux*v;
R[1]= y1 - Ux*w - Uy*v;
// printf("L=%d-%d R=%d-%d\n",L[0],L[1],R[0],R[1]);
lcdDrawLine(x0,y0,x1,y1,color);
lcdDrawLine(x1,y1,L[0],L[1],color);
lcdDrawLine(x1,y1,R[0],R[1],color);
lcdDrawLine(L[0],L[1],R[0],R[1],color);
int ww;
for(ww=w-1;ww>0;ww--) {
L[0]= x1 - Uy*ww - Ux*v;
L[1]= y1 + Ux*ww - Uy*v;
R[0]= x1 + Uy*ww - Ux*v;
R[1]= y1 - Ux*ww - Uy*v;
// printf("Fill>L=%d-%d R=%d-%d\n",L[0],L[1],R[0],R[1]);
lcdDrawLine(x1,y1,L[0],L[1],color);
lcdDrawLine(x1,y1,R[0],R[1],color);
}
}
// RGB565 conversion
// RGB565 is R(5)+G(6)+B(5)=16bit color format.
// Bit image "RRRRRGGGGGGBBBBB"
uint16_t rgb565_conv(uint16_t r,uint16_t g,uint16_t b) {
return (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3));
}
// Draw SJIS character
// x:X coordinate
// y:Y coordinate
// sjis: SJIS code
// color:color
int lcdDrawSJISChar(FontxFile *fx, uint16_t x,uint16_t y,uint16_t sjis,uint16_t color) {
uint16_t xx,yy,bit,ofs;
unsigned char fonts[128]; // font pattern
unsigned char pw, ph;
int h,w;
uint16_t mask;
bool rc;
if(_DEBUG_)printf("_FONT_DIRECTION_=%d\n",_FONT_DIRECTION_);
// sjis = UTF2SJIS(utf8);
//if(_DEBUG_)printf("sjis=%04x\n",sjis);
rc = GetFontx(fx, sjis, fonts, &pw, &ph); // SJIS -> Font pattern
if(_DEBUG_)printf("GetFontx rc=%d pw=%d ph=%d\n",rc,pw,ph);
if (!rc) return 0;
uint16_t xd1, yd1;
uint16_t xd2, yd2;
uint16_t xss, yss;
uint16_t xsd, ysd;
int next;
uint16_t x0 = 0;
uint16_t x1 = 0;
uint16_t y0 = 0;
uint16_t y1 = 0;
if (_FONT_DIRECTION_ == 0) {
xd1 = +1;
yd1 = -1;
xd2 = 0;
yd2 = 0;
xss = x;
yss = y + (ph - 1);
xsd = 1;
ysd = 0;
next = x + pw;
x0 = x;
y0 = y;
x1 = x + (pw-1);
y1 = y + (ph-1);
} else if (_FONT_DIRECTION_ == 2) {
xd1 = -1;
yd1 = +1;
xd2 = 0;
yd2 = 0;
xss = x;
yss = y - (ph + 1);
xsd = 1;
ysd = 0;
next = x - pw;
x0 = x - (pw-1);
y0 = y - (ph-1);
x1 = x;
y1 = y;
} else if (_FONT_DIRECTION_ == 1) {
xd1 = 0;
yd1 = 0;
xd2 = -1;
yd2 = -1;
xss = x + (ph - 1); // Bug Fix
yss = y;
xsd = 0;
ysd = 1;
next = y - pw;
x0 = x;
y0 = y - (pw-1);
x1 = x + (ph-1);
y1 = y;
} else if (_FONT_DIRECTION_ == 3) {
xd1 = 0;
yd1 = 0;
xd2 = +1;
yd2 = +1;
xss = x - (ph - 1); // Bug Fix
yss = y;
xsd = 0;
ysd = 1;
next = y + pw;
x0 = x - (ph-1);
y0 = y;
x1 = x;
y1 = y + (pw-1);
}
if (_FONT_FILL_) lcdDrawFillRect(x0, y0, x1, y1, _FONT_FILL_COLOR_);
int bits;
if(_DEBUG_)printf("xss=%d yss=%d\n",xss,yss);
ofs = 0;
yy = yss;
xx = xss;
for(h=0;h<ph;h++) {
if(xsd) xx = xss;
if(ysd) yy = yss;
// for(w=0;w<(pw/8);w++) {
bits = pw;
for(w=0;w<((pw+4)/8);w++) {
mask = 0x80;
for(bit=0;bit<8;bit++) {
bits--;
if (bits < 0) continue;
//if(_DEBUG_)printf("xx=%d yy=%d mask=%02x fonts[%d]=%02x\n",xx,yy,mask,ofs,fonts[ofs]);
if (fonts[ofs] & mask) {
lcdDrawPixel(xx,yy,color);
} else {
//if (_FONT_FILL_) lcdDrawPixel(xx,yy,_FONT_FILL_COLOR_);
}
if (h == (ph-2) && _FONT_UNDER_LINE_)
lcdDrawPixel(xx,yy,_FONT_UNDER_LINE_COLOR_);
if (h == (ph-1) && _FONT_UNDER_LINE_)
lcdDrawPixel(xx,yy,_FONT_UNDER_LINE_COLOR_);
xx = xx + xd1;
yy = yy + yd2;
mask = mask >> 1;
}
ofs++;
}
yy = yy + yd1;
xx = xx + xd2;
}
if (next < 0) next = 0;
return next;
}
// Draw UTF8 character
// x:X coordinate
// y:Y coordinate
// utf8: UTF8 code
// color:color
int lcdDrawUTF8Char(FontxFile *fx, uint16_t x,uint16_t y,uint8_t *utf8,uint16_t color) {
uint16_t sjis[1];
sjis[0] = UTF2SJIS(utf8);
if(_DEBUG_)printf("sjis=%04x\n",sjis[0]);
return lcdDrawSJISChar(fx,x,y,sjis[0],color);
}
// Draw UTF8 string
// x:X coordinate
// y:Y coordinate
// utfs: UTF8 string
// color:color
int lcdDrawUTF8String(FontxFile *fx, uint16_t x,uint16_t y,unsigned char *utfs,uint16_t color) {
if(_DEBUG_)printf("lcdDrawUTF8String start x=%d y=%d\n",x,y);
int i;
int spos;
uint16_t sjis[64];
spos = String2SJIS(utfs, strlen((char *)utfs), sjis, 64);
if(_DEBUG_)printf("spos=%d\n",spos);
for(i=0;i<spos;i++) {
if(_DEBUG_)printf("sjis[%d]=%x y=%d\n",i,sjis[i],y);
if (_FONT_DIRECTION_ == 0)
x=lcdDrawSJISChar(fx,x,y,sjis[i],color);
if (_FONT_DIRECTION_ == 1)
y=lcdDrawSJISChar(fx,x,y,sjis[i],color);
if (_FONT_DIRECTION_ == 2)
x=lcdDrawSJISChar(fx,x,y,sjis[i],color);
if (_FONT_DIRECTION_ == 3)
y=lcdDrawSJISChar(fx,x,y,sjis[i],color);
}
if(_DEBUG_)printf("lcdDrawUTF8String end x=%d y=%d\n",x,y);
if (_FONT_DIRECTION_ == 0) return x;
if (_FONT_DIRECTION_ == 2) return x;
if (_FONT_DIRECTION_ == 1) return y;
if (_FONT_DIRECTION_ == 3) return y;
return 0;
}
// Set font direction
// dir:Direction
void lcdSetFontDirection(uint16_t dir) {
_FONT_DIRECTION_ = dir;
}
// Set font filling
// color:fill color
void lcdSetFontFill(uint16_t color) {
_FONT_FILL_ = true;
_FONT_FILL_COLOR_ = color;
}
// UnSet font filling
void lcdUnsetFontFill(void) {
_FONT_FILL_ = false;
}
// Set font underline
// color:frame color
void lcdSetFontUnderLine(uint16_t color) {
_FONT_UNDER_LINE_ = true;
_FONT_UNDER_LINE_COLOR_ = color;
}
// UnSet font filling
void lcdUnsetFontUnderLine(void) {
_FONT_UNDER_LINE_ = false;
}