forked from sumotoy/RA8875
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RA8875.cpp
2647 lines (2438 loc) · 81.2 KB
/
RA8875.cpp
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <SPI.h>
#include "RA8875.h"
#ifdef SPI_HAS_TRANSACTION
static SPISettings settings;
#endif
#if defined _SPI_HYPERDRIVE && (defined(__MK20DX128__) || defined(__MK20DX256__))
void RA8875::setMultipleRegisters(uint8_t reg[],uint8_t data[],uint8_t len) {
SPI.beginTransaction(settings);
for (uint8_t i=0;i<len;i++){
writecommand_cont(RA8875_CMDWRITE);
writecommand_last(reg[i]);
writecommand_cont(RA8875_DATAWRITE);
writecommand_last(data[i]);
}
SPI.endTransaction();
}
#endif
/**************************************************************************/
/*!
Contructor
CS: SPI SS pin
RST: Reset pin
altSCLK: alternate SCLK pin. If true = 14, otherwise 13
altMOSI: alternate MOSI pin. If true = 7, otherwise 11
altMISO: alternate MISO pin. If true = 8, otherwise 12
*/
/**************************************************************************/
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
RA8875::RA8875(const uint8_t CS,const uint8_t RST,const boolean altSCLK,const boolean altMOSI,const boolean altMISO){
/* if (altSCLK) SPI.setSCK(14);
if (altMOSI) SPI.setMOSI(7);
if (altMISO) SPI.setMISO(8); */
altMosiPin = altMOSI;
altMisoPin = altMISO;
altSclkPin = altSCLK;
_cs = CS;
_rst = 255;
if (_rst != 255) _rst = RST;
}
#else
#if defined(NEEDS_SET_MODULE)
RA8875::RA8875(const uint8_t module, const uint8_t RST) {
selectCS(module);
#else
/**************************************************************************/
/*!
Contructor
CS: SPI SS pin
RST: Reset pin (255 disable it)
*/
/**************************************************************************/
RA8875::RA8875(const uint8_t CS, const uint8_t RST) {
_cs = CS;
#endif
_rst = 255;
if (RST != 255) _rst = RST;
}
#endif
/**************************************************************************/
/*! PRIVATE
Helper, it will set CS pin accordly module selected
module: 0...3
*/
/**************************************************************************/
#if defined(NEEDS_SET_MODULE)
void RA8875::selectCS(uint8_t module) {
if (module > 3) module = 3;
switch(module){
case 0:
_cs = PA_3;
break;
case 1:
_cs = PF_3;
break;
case 2:
_cs = PB_5;
break;
case 3:
_cs = PD_1;
break;
}
SPImodule = module;
}
#endif
/**************************************************************************/
/*!
Initialize library and SPI
Parameter:
RA8875_480x272 (4.3" displays)
RA8875_800x480 (5" and 7" displays)
Adafruit_480x272 (4.3" Adafruit displays)
Adafruit_800x480 (5" and 7" Adafruit displays)
UPDATE! Some devices ONLY in Energia IDE needs an extra parameter!
module: sets the SPI interface (it depends from MCU). Default:0
*/
/**************************************************************************/
void RA8875::begin(const enum RA8875sizes s) {
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
if (altSclkPin) SPI.setSCK(14);
if (altMosiPin) SPI.setMOSI(7);
if (altMisoPin) SPI.setMISO(8);
#endif
_size = s;
uint8_t initIndex;
_size = s;
if (_size == RA8875_320x240) {//still not supported! Wait next version
_width = 320;
_height = 240;
initIndex = 0;
_maxLayers = 2;
} else if (_size == RA8875_480x272 || _size == Adafruit_480x272) {
_width = 480;
_height = 272;
initIndex = 1;
_maxLayers = 2;
} else if (_size == RA8875_640x480 || _size == Adafruit_640x480) {//still not supported! Wait next version
_width = 640;
_height = 480;
initIndex = 2;
_maxLayers = 1;
} else if (_size == RA8875_800x480 || _size == Adafruit_800x480) {
_width = 800;
_height = 480;
initIndex = 3;
_maxLayers = 1;
} else {
_width = 480;
_height = 272;
initIndex = 1;
_maxLayers = 2;
}
_currentLayer = 0;
_currentMode = GRAPHIC;
//_spiSpeed = MAXSPISPEED;
_cursorX = 0; _cursorY = 0;
_textWrap = true;
_textSize = X16;
_fontSpacing = 0;
_extFontRom = false;
_fontRomType = GT21L16T1W;
_fontRomCoding = GB2312;
_fontSource = INT;
_fontFullAlig = false;
_fontRotation = false;
_fontInterline = 0;
_fontFamily = STANDARD;
_textCursorStyle = BLINK;
_scrollXL = 0; _scrollXR = 0; _scrollYT = 0; _scrollYB = 0;
_useMultiLayers = false;//starts with one layer only
#if !defined(USE_EXTERNALTOUCH)
_touchPin = 255;
_clearTInt = false;
_touchEnabled = false;
_tsAdcMinX = 0; _tsAdcMinY = 0; _tsAdcMaxX = 1024; _tsAdcMaxY = 1024;
#endif
#if defined(USE_RA8875_KEYMATRIX)
_keyMatrixEnabled = false;
#endif
/* Display Configuration Register [0x20]
7: (Layer Setting Control) 0:one Layer, 1:two Layers
6,5,4: (na)
3: (Horizontal Scan Direction) 0: SEG0 to SEG(n-1), 1: SEG(n-1) to SEG0
2: (Vertical Scan direction) 0: COM0 to COM(n-1), 1: COM(n-1) to COM0
1,0: (na) */
_DPCRReg = 0b00000000;
/* Memory Write Control Register 0
7: 0(graphic mode), 1(textx mode)
6: 0(font-memory cursor not visible), 1(visible)
5: 0(normal), 1(blinking)
4: na
3-2: 00(LR,TB), 01(RL,TB), 10(TB,LR), 11(BT,LR)
1: 0(Auto Increase in write), 1(no)
0: 0(Auto Increase in read), 1(no) */
_MWCR0Reg = 0b00000000;
/* Font Control Register 0 [0x21]
7: 0(CGROM font is selected), 1(CGRAM font is selected)
6: na
5: 0(Internal CGROM [reg 0x2F to 00]), 1(External CGROM [0x2E reg, bit6,7 to 0)
4-2: na
1-0: 00(ISO/IEC 8859-1), 01(ISO/IEC 8859-2), 10(ISO/IEC 8859-3), 11(ISO/IEC 8859-4)*/
_FNCR0Reg = 0b00000000;
/* Font Control Register 1 [0x22]
7: 0(Full Alignment off), 1(Full Alignment on)
6: 0(no-trasparent), 1(trasparent)
5: na
4: 0(normal), 1(90degrees)
3-2: 00(x1), 01(x2), 10(x3), 11(x3) Horizontal font scale
1-0: 00(x1), 01(x2), 10(x3), 11(x3) Vertical font scale */
_FNCR1Reg = 0b00000000;
/* Font Write Type Setting Register [0x2E]
7-6: 00(16x16,8x16,nx16), 01(24x24,12x24,nx24), 1x(32x32,16x32, nx32)
5-0: 00...3F (font width off to 63 pixels)*/
_FWTSETReg = 0b00000000;
/* Serial Font ROM Setting [0x2F]
GT Serial Font ROM Select
7-5: 000(GT21L16TW/GT21H16T1W),001(GT30L16U2W),010(GT30L24T3Y/GT30H24T3Y),011(GT30L24M1Z),111(GT30L32S4W/GT30H32S4W)
FONT ROM Coding Setting
4-2: 000(GB2312),001(GB12345/GB18030),010(BIG5),011(UNICODE),100(ASCII),101(UNI-Japanese),110(JIS0208),111(Latin/Greek/Cyrillic/Arabic)
1-0: 00...11
bits ASCII Lat/Gr/Cyr Arabit
00 normal normal na
01 Arial var Wdth Pres Forms A
10 Roman na Pres Forms B
11 Bold na na */
_SFRSETReg = 0b00000000;
/* Interrupt Control Register1 [0xF0]
7,6,5: (na)
4: KEYSCAN Interrupt Enable Bit
3: DMA Interrupt Enable Bit
2: TOUCH Panel Interrupt Enable Bit
1: BTE Process Complete Interrupt Enable Bit
0:
When MCU-relative BTE operation is selected(*1) and BTE
Function is Enabled(REG[50h] Bit7 = 1), this bit is used to
Enable the BTE Interrupt for MCU R/W:
0 : Disable BTE interrupt for MCU R/W.
1 : Enable BTE interrupt for MCU R/W.
When the BTE Function is Disabled, this bit is used to
Enable the Interrupt of Font Write Function:
0 : Disable font write interrupt.
1 : Enable font write interrupt.
*/
_INTC1Reg = 0b00000000;
/* Touch Panel Control Register 0 [0x70]
7: 0(disable, 1:(enable)
6,5,4:TP Sample Time Adjusting (000...111)
3:Touch Panel Wakeup Enable 0(disable),1(enable)
2,1,0:ADC Clock Setting (000...111) set fixed to 010: (System CLK) / 4, 10Mhz Max! */
_TPCR0Reg = RA8875_TPCR0_WAIT_4096CLK | RA8875_TPCR0_WAKEDISABLE | RA8875_TPCR0_ADCCLK_DIV4;
SPI.begin();
#if defined _SPI_HYPERDRIVE && (defined(__MK20DX128__) || defined(__MK20DX256__))
if (SPI.pinIsChipSelect(_cs)) {
pcs_command = SPI.setCS(_cs);
} else {
pcs_command = 0;
return;
}
#else
#if !defined(ENERGIA)
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
#endif
#endif
if (_rst < 255){
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(5);
digitalWrite(_rst, LOW);
delay(20);
digitalWrite(_rst, HIGH);
delay(150);
}
#if defined(NEEDS_SET_MODULE)//energia specific
SPI.setModule(SPImodule);
#endif
#if defined(SPI_HAS_TRANSACTION) && defined(USESPITRANSACTIONS)
//_spiSpeed = MAXSPISPEED;//go back to full speed
settings = SPISettings(MAXSPISPEED, MSBFIRST, SPI_MODE0);
//SPI.begin();
#else//do not use SPItransactons
//SPI.begin();
#if defined(ENERGIA)
SPI.setClockDivider(SPI_SPEED_WRITE);//4Mhz (6.6Mhz Max)
delay(50);
#else
SPI.setClockDivider(SPI_CLOCK_DIV4);//4Mhz (6.6Mhz Max)
delay(50);
#endif
SPI.setDataMode(SPI_MODE0);
#endif
#if defined(ENERGIA)//dunno why but energia wants this here or not work!
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
#endif
initialize(initIndex);
}
/************************* Initialization *********************************/
/**************************************************************************/
/*!
PRIVATE
Hardware initialization of RA8875 and turn on
*/
/**************************************************************************/
void RA8875::initialize(uint8_t initIndex) {
if (_rst == 255) {//soft reset
writeCommand(RA8875_PWRR);
writeData(RA8875_PWRR_SOFTRESET);
writeData(RA8875_PWRR_NORMAL);
delay(200);
}
const uint8_t initStrings[4][15] = {
{0x0A,0x02,0x03,0x27,0x00,0x05,0x04,0x03,0xEF,0x00,0x05,0x00,0x0E,0x00,0x02},//0 -> 320x240 (to be fixed)
{0x10,0x02,0x82,0x3B,0x00,0x01,0x00,0x05,0x0F,0x01,0x02,0x00,0x07,0x00,0x09},//1 -> 480x272 (0x0A)
{0x0B,0x02,0x01,0x4F,0x05,0x0F,0x01,0x00,0xDF,0x10,0x0A,0x00,0x0E,0x00,0x01},//2 -> 640x480 (to be fixed)
{0x10,0x02,0x81,0x63,0x00,0x03,0x03,0x0B,0xDF,0x01,0x1F,0x00,0x16,0x00,0x01}// 3 -> 800x480 (0x0B)(to be fixed?)
};
writeReg(RA8875_PLLC1,initStrings[initIndex][0]);////PLL Control Register 1
delay(1);
writeReg(RA8875_PLLC2,initStrings[initIndex][1]);////PLL Control Register 2
delay(1);
writeReg(RA8875_PCSR,initStrings[initIndex][2]);//Pixel Clock Setting Register
delay(1);
writeReg(RA8875_SYSR,0x0C);//we are working ALWAYS at 65K color space!!!!
writeReg(RA8875_HDWR,initStrings[initIndex][3]);//LCD Horizontal Display Width Register
writeReg(RA8875_HNDFTR,initStrings[initIndex][4]);//Horizontal Non-Display Period Fine Tuning Option Register
writeReg(RA8875_HNDR,initStrings[initIndex][5]);////LCD Horizontal Non-Display Period Register
writeReg(RA8875_HSTR,initStrings[initIndex][6]);////HSYNC Start Position Register
writeReg(RA8875_HPWR,initStrings[initIndex][7]);////HSYNC Pulse Width Register
writeReg(RA8875_VDHR0,initStrings[initIndex][8]);////LCD Vertical Display Height Register0
writeReg(RA8875_VDHR1,initStrings[initIndex][9]);////LCD Vertical Display Height Register1
writeReg(RA8875_VNDR0,initStrings[initIndex][10]);////LCD Vertical Non-Display Period Register 0
writeReg(RA8875_VNDR1,initStrings[initIndex][11]);////LCD Vertical Non-Display Period Register 1
writeReg(RA8875_VSTR0,initStrings[initIndex][12]);////VSYNC Start Position Register 0
writeReg(RA8875_VSTR1,initStrings[initIndex][13]);////VSYNC Start Position Register 1
writeReg(RA8875_VPWR,initStrings[initIndex][14]);////VSYNC Pulse Width Register
setActiveWindow(0,(_width-1),0,(_height-1));//set the active winsow
clearMemory(true);//clear FULL memory
//end of hardware initialization
delay(10);
//now starts the first time setting up
displayOn(true);//turn On Display
if (_size == Adafruit_480x272 || _size == Adafruit_800x480 || _size == Adafruit_640x480) GPIOX(true);//only for adafruit stuff
PWMsetup(1,true, RA8875_PWM_CLK_DIV1024);//setup PWM ch 1 for backlight
PWMout(1,255);//turn on PWM1
setCursorBlinkRate(DEFAULTCURSORBLINKRATE);//set default blink rate
if (_textCursorStyle == BLINK) showCursor(false,BLINK); //set default text cursor type and turn off
setIntFontCoding(DEFAULTINTENCODING);//set default internal font encoding
setFont(INT); //set internal font use
setTextColor(RA8875_WHITE);//since the blackground it's black...
//now tft it's ready to go and in [Graphic mode]
}
/**************************************************************************/
/*!
Software Reset
*/
/**************************************************************************/
/* void RA8875::softReset(void) {
writeCommand(RA8875_PWRR);
writeData(RA8875_PWRR_SOFTRESET);
writeData(RA8875_PWRR_NORMAL);
delay(200);
}
*/
/**************************************************************************/
/*!
Clear memory
Parameters:
full: true(clear all memory), false(clear active window only)
*/
/**************************************************************************/
void RA8875::clearMemory(boolean full){
uint8_t temp = 0b00000000;
if (!full) temp |= (1 << 6);
temp |= (1 << 7);//enable start bit
writeReg(RA8875_MCLR,temp);
//_cursorX = _cursorY = 0;
waitBusy(0x80);
}
/**************************************************************************/
/*!
Set the Active Window
Parameters:
XL: Horizontal Left
XR: Horizontal Right
YT: Vertical TOP
YB: Vertical Bottom
*/
/**************************************************************************/
void RA8875::setActiveWindow(uint16_t XL,uint16_t XR ,uint16_t YT ,uint16_t YB){
if (XR >= _width) XR = _width-1;
if (YB >= _height) YB = _height-1;
// X
writeReg(RA8875_HSAW0,XL);
writeReg(RA8875_HSAW1,XL >> 8);
writeReg(RA8875_HEAW0,XR);
writeReg(RA8875_HEAW1,XR >> 8);
// Y
writeReg(RA8875_VSAW0,YT);
writeReg(RA8875_VSAW1,YT >> 8);
writeReg(RA8875_VEAW0,YB);
writeReg(RA8875_VEAW1,YB >> 8);
}
/**************************************************************************/
/*!
Return the max tft width.
Note that real size will start from 0
so you need to subtract 1!
*/
/**************************************************************************/
uint16_t RA8875::width(void) { return _width; }
/**************************************************************************/
/*!
Return the max tft height.
Note that real size will start from 0
so you need to subtract 1!
*/
/**************************************************************************/
uint16_t RA8875::height(void) { return _height; }
/************************* Text Mode ***********************************/
/**************************************************************************/
/*!
Change the mode between graphic and text
Parameters:
m: can be GRAPHIC or TEXT
*/
/**************************************************************************/
void RA8875::changeMode(enum RA8875modes m) {
writeCommand(RA8875_MWCR0);
if (m == GRAPHIC){
if (_currentMode == TEXT){//avoid useless consecutive calls
_MWCR0Reg &= ~(1 << 7);
_currentMode = GRAPHIC;
writeData(_MWCR0Reg);
}
} else {
if (_currentMode == GRAPHIC){//avoid useless consecutive calls
_MWCR0Reg |= (1 << 7);
_currentMode = TEXT;
writeData(_MWCR0Reg);
}
}
}
/**************************************************************************/
/*! Upload user custom cahr or symbol to CGRAM, max 255
Parameters:
symbol[]: an 8bit x 16 char in an array. Must be exact 16 bytes
address: 0...255 the address of the CGRAM where to store the char
*/
/**************************************************************************/
void RA8875::uploadUserChar(const uint8_t symbol[],uint8_t address) {
bool modeChanged = false;
if (_currentMode != GRAPHIC) {//was in text!
changeMode(GRAPHIC);
modeChanged = true;
}
writeReg(RA8875_CGSR,address);
writeTo(CGRAM);
writeCommand(RA8875_MRWC);
for (uint8_t i=0;i<16;i++){
writeData(symbol[i]);
}
if (modeChanged) changeMode(TEXT);
}
/**************************************************************************/
/*! Retrieve and print to screen the user custom char or symbol
User have to store a custom char before use this function
Parameters:
address: 0...255 the address of the CGRAM where char it's stored
wide:0 for single 8x16 char, if you have wider chars that use
more than a char slot they can be showed combined (see examples)
*/
/**************************************************************************/
void RA8875::showUserChar(uint8_t symbolAddrs,uint8_t wide) {
uint8_t oldRegState = _FNCR0Reg;
uint8_t i;
bitSet(oldRegState,7);//set to CGRAM
writeReg(RA8875_FNCR0,oldRegState);
//layers?
if (_useMultiLayers){
if (_currentLayer == 0){
writeTo(L1);
} else {
writeTo(L2);
}
} else {
writeTo(L1);
}
writeCommand(RA8875_MRWC);
writeData(symbolAddrs);
if (wide > 0){
for (i=1;i<=wide;i++){
writeData(symbolAddrs+i);
}
}
if (oldRegState != _FNCR0Reg) writeReg(RA8875_FNCR0,_FNCR0Reg);
}
/**************************************************************************/
/*!
Set internal Font Encoding
Parameters:
f:ISO_IEC_8859_1, ISO_IEC_8859_2, ISO_IEC_8859_3, ISO_IEC_8859_4
default:ISO_IEC_8859_1
*/
/**************************************************************************/
void RA8875::setIntFontCoding(enum RA8875fontCoding f) {
uint8_t temp = _FNCR0Reg;
temp &= ~((1<<1) | (1<<0));// Clear bits 1 and 0
switch (f){
case ISO_IEC_8859_1:
//do nothing
break;
case ISO_IEC_8859_2:
temp |= (1 << 0);
break;
case ISO_IEC_8859_3:
temp |= (1 << 1);
break;
case ISO_IEC_8859_4:
temp |= ((1<<1) | (1<<0));// Set bits 1 and 0
break;
default:
return;
}
_FNCR0Reg = temp;
writeReg(RA8875_FNCR0,_FNCR0Reg);
}
/**************************************************************************/
/*!
External Font Rom setup
This will not phisically change the register but should be called before setFont(EXT)!
You should use this values accordly Font ROM datasheet!
Parameters:
ert:ROM Type (GT21L16T1W, GT21H16T1W, GT23L16U2W, GT30H24T3Y, GT23L24T3Y, GT23L24M1Z, GT23L32S4W, GT30H32S4W)
erc:ROM Font Encoding (GB2312, GB12345, BIG5, UNICODE, ASCII, UNIJIS, JIS0208, LATIN)
erf:ROM Font Family (STANDARD, ARIAL, ROMAN, BOLD)
*/
/**************************************************************************/
void RA8875::setExternalFontRom(enum RA8875extRomType ert, enum RA8875extRomCoding erc, enum RA8875extRomFamily erf){
uint8_t temp = _SFRSETReg;//just to preserve the reg in case something wrong
switch(ert){ //type of rom
case GT21L16T1W:
case GT21H16T1W:
temp &= 0x1F;
break;
case GT23L16U2W:
temp &= 0x1F; temp |= 0x20;
break;
case GT23L24T3Y:
case GT30H24T3Y:
case ER3303_1://encoding GB12345
temp &= 0x1F; temp |= 0x40;
//erc = GB12345;//forced
break;
case GT23L24M1Z:
temp &= 0x1F; temp |= 0x60;
break;
case GT23L32S4W:
case GT30H32S4W:
temp &= 0x1F; temp |= 0x80;
break;
default:
_extFontRom = false;//wrong type, better avoid for future
return;//cannot continue, exit
}
_fontRomType = ert;
switch(erc){ //check rom font coding
case GB2312:
temp &= 0xE3;
break;
case GB12345:
temp &= 0xE3; temp |= 0x04;
break;
case BIG5:
temp &= 0xE3; temp |= 0x08;
break;
case UNICODE:
temp &= 0xE3; temp |= 0x0C;
break;
case ASCII:
temp &= 0xE3; temp |= 0x10;
break;
case UNIJIS:
temp &= 0xE3; temp |= 0x14;
break;
case JIS0208:
temp &= 0xE3; temp |= 0x18;
break;
case LATIN:
temp &= 0xE3; temp |= 0x1C;
break;
default:
_extFontRom = false;//wrong coding, better avoid for future
return;//cannot continue, exit
}
_fontRomCoding = erc;
_SFRSETReg = temp;
setExtFontFamily(erf,false);
_extFontRom = true;
//writeReg(RA8875_SFRSET,_SFRSETReg);//0x2F
//delay(4);
}
/**************************************************************************/
/*!
select the font family for the external Font Rom Chip
Parameters:
erf: STANDARD, ARIAL, ROMAN, BOLD
setReg:
true(send phisically the register, useful when you change
family after set setExternalFontRom)
false:(change only the register container, useful during config)
*/
/**************************************************************************/
void RA8875::setExtFontFamily(enum RA8875extRomFamily erf,boolean setReg) {
_fontFamily = erf;
switch(erf){ //check rom font family
case STANDARD:
_SFRSETReg &= 0xFC;
break;
case ARIAL:
_SFRSETReg &= 0xFC; _SFRSETReg |= 0x01;
break;
case ROMAN:
_SFRSETReg &= 0xFC; _SFRSETReg |= 0x02;
break;
case BOLD:
_SFRSETReg |= ((1<<1) | (1<<0)); // set bits 1 and 0
break;
default:
_fontFamily = STANDARD; _SFRSETReg &= 0xFC;
return;
}
if (setReg) writeReg(RA8875_SFRSET,_SFRSETReg);
}
/**************************************************************************/
/*!
choose from internal/external (if exist) Font Rom
Parameters:
s: Font source (INT,EXT)
*/
/**************************************************************************/
void RA8875::setFont(enum RA8875fontSource s) {
//enum RA8875fontCoding c
if (s == INT){
//check the font coding
if (_extFontRom) {
setFontSize(X16,false);
writeReg(RA8875_SFRSET,0b00000000);//_SFRSETReg
}
_FNCR0Reg &= ~((1<<7) | (1<<5));// Clear bits 7 and 5
writeReg(RA8875_FNCR0,_FNCR0Reg);
_fontSource = s;
delay(1);
} else {
if (_extFontRom){
_fontSource = s;
//now switch
_FNCR0Reg |= (1 << 5);
writeReg(RA8875_FNCR0,_FNCR0Reg);//0x21
delay(1);
writeReg(RA8875_SFCLR,0x02);//Serial Flash/ROM CLK frequency/2
setFontSize(X24,false);////X24 size
writeReg(RA8875_SFRSET,_SFRSETReg);//at this point should be already set
delay(4);
writeReg(RA8875_SROC,0x28);// 0x28 rom 0,24bit adrs,wave 3,1 byte dummy,font mode, single mode
delay(4);
} else {
setFont(INT);
}
}
}
/**************************************************************************/
/*!
Enable/Disable the Font Full Alignemet feature (default off)
Parameters:
align: true,false
*/
/**************************************************************************/
void RA8875::setFontFullAlign(boolean align) {
align == true ? _FNCR1Reg |= (1 << 7) : _FNCR1Reg &= ~(1 << 7);
writeReg(RA8875_FNCR1,_FNCR1Reg);
}
/**************************************************************************/
/*!
Enable/Disable 90" Font Rotation (default off)
Parameters:
rot: true,false
*/
/**************************************************************************/
void RA8875::setFontRotate(boolean rot) {
rot == true ? _FNCR1Reg |= (1 << 4) : _FNCR1Reg &= ~(1 << 4);
writeReg(RA8875_FNCR1,_FNCR1Reg);
}
/**************************************************************************/
/*!
Set distance between text lines (default off)
Parameters:
pix: 0...63 pixels
*/
/**************************************************************************/
void RA8875::setFontInterline(uint8_t pix){
if (pix > 0x3F) pix = 0x3F;
_fontInterline = pix;
//_FWTSETReg &= 0xC0;
//_FWTSETReg |= spc & 0x3F;
writeReg(RA8875_FLDR,_fontInterline);
}
/**************************************************************************/
/*!
Set the Text position for write Text only.
Parameters:
x:horizontal in pixels
y:vertical in pixels
*/
/**************************************************************************/
void RA8875::setCursor(uint16_t x, uint16_t y) {
if (!_textWrap){
if (x >= _width) x = _width-1;
if (y >= _height) y = _height-1;
}
_cursorX = x;
_cursorY = y;
#if defined _SPI_HYPERDRIVE && (defined(__MK20DX128__) || defined(__MK20DX256__))
uint8_t reg[] = {RA8875_F_CURXL,RA8875_F_CURXH,RA8875_F_CURYL,RA8875_F_CURYH};
uint8_t data[] = {(uint8_t)(x & 0xFF),(uint8_t)(x >> 8),(uint8_t)(y & 0xFF),(uint8_t)(y >> 8)};
setMultipleRegisters(reg,data,4);
#else
writeReg(RA8875_F_CURXL,(x & 0xFF));
writeReg(RA8875_F_CURXH,(x >> 8));
writeReg(RA8875_F_CURYL,(y & 0xFF));
writeReg(RA8875_F_CURYH,(y >> 8));
#endif
}
/**************************************************************************/
/*!
Update the library tracked _cursorX,_cursorX and give back
Parameters:
x*:horizontal pos in pixels
y*:vertical pos in pixels
USE: xxx.getCursor(&myX,&myY);
*/
/**************************************************************************/
void RA8875::getCursor(uint16_t *x, uint16_t *y) {
uint8_t t1,t2;
t1 = readReg(RA8875_F_CURXL);
t2 = readReg(RA8875_F_CURXH);
_cursorX = (t2 << 8) | (t1 & 0xFF);
t1 = readReg(RA8875_F_CURYL);
t2 = readReg(RA8875_F_CURYH);
_cursorY = (t2 << 8) | (t1 & 0xFF);
*x = _cursorX;
*y = _cursorY;
}
/**************************************************************************/
/*! Show/Hide text cursor
Parameters:
cur:(true/false) true:visible, false:not visible
c: cursor type (NORMAL, BLINK)
*/
/**************************************************************************/
void RA8875::showCursor(boolean cur,enum RA8875tcursor c){
if (c == BLINK){
_textCursorStyle = c;
_MWCR0Reg |= (1 << 5);
} else {
_textCursorStyle = NORMAL;
_MWCR0Reg &= ~(1 << 5);
}
bitWrite(_MWCR0Reg,6,cur);//set cursor visibility flag
writeReg(RA8875_MWCR0,_MWCR0Reg);
}
/**************************************************************************/
/*! Set cursor property blink and his rate
Parameters:
rate:blink speed (fast 0...255 slow)
*/
/**************************************************************************/
void RA8875::setCursorBlinkRate(uint8_t rate){
writeReg(RA8875_BTCR,rate);//set blink rate
}
/**************************************************************************/
/*!
set the text color and his background
Parameters:
fColor:16bit foreground color (text) RGB565
bColor:16bit background color RGB565
*/
/**************************************************************************/
void RA8875::setTextColor(uint16_t fColor, uint16_t bColor){
setForegroundColor(fColor);
setBackgroundColor(bColor);
_FNCR1Reg &= ~(1 << 6);
writeReg(RA8875_FNCR1,_FNCR1Reg);
}
/**************************************************************************/
/*!
set the text color w transparent background
Parameters:
fColor:16bit foreground color (text) RGB565
*/
/**************************************************************************/
void RA8875::setTextColor(uint16_t fColor){
setForegroundColor(fColor);
_FNCR1Reg |= (1 << 6);
writeReg(RA8875_FNCR1,_FNCR1Reg);
}
/**************************************************************************/
/*!
Set the Text size by it's multiple. normal should=0, max is 3 (x4)
Parameters:
scale:0..3 -> 0:normal, 1:x2, 2:x3, 3:x4
*/
/**************************************************************************/
void RA8875::setFontScale(uint8_t scale){
if (scale > 3) scale = 3;
_FNCR1Reg &= ~(0xF); // clear bits from 0 to 3
_FNCR1Reg |= scale << 2;
_FNCR1Reg |= scale;
writeReg(RA8875_FNCR1,_FNCR1Reg);
_textScale = scale;
}
/**************************************************************************/
/*!
Choose between 16x16(8x16) - 24x24(12x24) - 32x32(16x32)
for External Font ROM
Parameters:
ts:X16,X24,X32
halfSize:true/false (16x16 -> 8x16 and so on...)
*/
/**************************************************************************/
void RA8875::setFontSize(enum RA8875tsize ts,boolean halfSize){
switch(ts){
case X16:
_FWTSETReg &= 0x3F;
break;
case X24:
_FWTSETReg &= 0x3F; _FWTSETReg |= 0x40;
break;
case X32:
_FWTSETReg &= 0x3F; _FWTSETReg |= 0x80;
break;
default:
return;
}
_textSize = ts;
writeReg(RA8875_FWTSET,_FWTSETReg);
}
/**************************************************************************/
/*!
Choose space in pixels between chars
Parameters:
spc:0...63pix (default 0=off)
*/
/**************************************************************************/
void RA8875::setFontSpacing(uint8_t spc){//ok
if (spc > 0x3F) spc = 0x3F;
_fontSpacing = spc;
_FWTSETReg &= 0xC0;
_FWTSETReg |= spc & 0x3F;
writeReg(RA8875_FWTSET,_FWTSETReg);
}
/**************************************************************************/
/*! PRIVATE
This is the function that write text. Still in development
NOTE: It identify correctly (when I got it) println and nl & rt
*/
/**************************************************************************/
void RA8875::textWrite(const char* buffer, uint16_t len) {
bool goBack = false;
uint8_t start = 0;
uint16_t i,ny;
uint8_t t1,t2;
if (_currentMode == GRAPHIC){
changeMode(TEXT);
goBack = true;
}
if (len == 0) len = strlen(buffer);
if (len > 0 && ((buffer[0] == '\r') && (buffer[1] == '\n'))){//got a println?
//get current y
t1 = readReg(RA8875_F_CURYL);
t2 = readReg(RA8875_F_CURYH);
//calc new line y
ny = (t2 << 8) | (t1 & 0xFF);
//update y
ny = ny + (16 + (16*_textScale))+_fontInterline;//TODO??
setCursor(0,ny);
start = 2;
#if defined(ENERGIA)//oops! Energia 013 seems have a bug here! Should send a \r but only \n given!
} else if (len > 0 && ((buffer[0] == '\n'))){
//get current y
t1 = readReg(RA8875_F_CURYL);
t2 = readReg(RA8875_F_CURYH);
//calc new line y
ny = (t2 << 8) | (t1 & 0xFF);
//update y
ny = ny + (16 + (16*_textScale))+_fontInterline;//TODO??
setCursor(0,ny);
start = 1;
}
#else
}
#endif
writeCommand(RA8875_MRWC);
for (i=start;i<len;i++){
if (buffer[i] == '\n' || buffer[i] == '\r') {
//_cursor_y += textsize * 8;
//_cursor_x = 0;
} else {
writeData(buffer[i]);
waitBusy(0x80);
}
#if defined(__AVR__)
if (_textScale > 1) delay(1);
#elif defined(__arm__)
if (_textScale > 0) delay(1);//Teensy3
#endif
}
if (goBack) changeMode(GRAPHIC);
}
/**************************************************************************/
/*!
Sets set the foreground color using 16bit RGB565 color
Parameters:
color:16bit color RGB565
*/
/**************************************************************************/
void RA8875::setForegroundColor(uint16_t color){
#if defined _SPI_HYPERDRIVE && (defined(__MK20DX128__) || defined(__MK20DX256__))
uint8_t reg[] = {RA8875_FGCR0,RA8875_FGCR1,RA8875_FGCR2};
uint8_t data[] = {(uint8_t)((color & 0xF800) >> 11),(uint8_t)((color & 0x07E0) >> 5),(uint8_t)(color & 0x001F)};
setMultipleRegisters(reg,data,3);
#else
writeReg(RA8875_FGCR0,((color & 0xF800) >> 11));
writeReg(RA8875_FGCR1,((color & 0x07E0) >> 5));
writeReg(RA8875_FGCR2,(color & 0x001F));
#endif
}
/**************************************************************************/
/*!
Sets set the foreground color using 8bit R,G,B
Parameters:
R:8bit RED
G:8bit GREEN
B:8bit BLUE
*/
/**************************************************************************/
void RA8875::setForegroundColor(uint8_t R,uint8_t G,uint8_t B){
#if defined _SPI_HYPERDRIVE && (defined(__MK20DX128__) || defined(__MK20DX256__))
uint8_t reg[] = {RA8875_FGCR0,RA8875_FGCR1,RA8875_FGCR2};
uint8_t data[] = {R,G,B};
setMultipleRegisters(reg,data,3);
#else
writeReg(RA8875_FGCR0,R);
writeReg(RA8875_FGCR1,G);
writeReg(RA8875_FGCR2,B);
#endif
}
/**************************************************************************/
/*!
Sets set the background color using 16bit RGB565 color
Parameters:
color:16bit color RGB565
*/
/**************************************************************************/
void RA8875::setBackgroundColor(uint16_t color){
#if defined _SPI_HYPERDRIVE && (defined(__MK20DX128__) || defined(__MK20DX256__))
uint8_t reg[] = {RA8875_BGCR0,RA8875_BGCR1,RA8875_BGCR2};