-
Notifications
You must be signed in to change notification settings - Fork 0
/
HGSC256128_SSD1322.cpp
1393 lines (1201 loc) · 38.7 KB
/
HGSC256128_SSD1322.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
/**
* This is an example for the Newhaven NHD-3.12-25664UCY2 OLED based on SSD1322 drivers
* The NHD-3.12-25664UCY2 is sold through Digikey and Mouser
*
* Details in
* data sheet (http://www.newhavendisplay.com/specs/NHD-3.12-25664UCY2.pdf)
* app note (http://www.newhavendisplay.com/app_notes/SSD1322.pdf)
*
* Based on Adafruit SSD1306 driver (https://github.com/adafruit/Adafruit_SSD1306)
* for which the original header is left below:
*/
/*********************************************************************
This is a library for the 256 x 64 pixel 16 color gray scale OLEDs
based on SSD1322 drivers
These displays use SPI to communicate, 4 or 5 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/
#include <stdlib.h>
#include "Adafruit_GFX.h"
#include "HGSC256128_SSD1322.h"
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
#ifdef LOAD_GLCD
#include "glcdfont.c"
#endif
#ifdef LOAD_FONT2
#include "Font16.h"
#endif
#ifdef LOAD_FONT4
#include "Font32.h"
#endif
#ifdef LOAD_FONT6
#include "Font64.h"
#endif
#ifdef LOAD_FONT7
#include "Font7s.h"
#endif
#ifdef LOAD_FONT8
#include "Font10.h"
#endif
// the memory buffer for the LCD
static uint8_t buffer[SSD1322_LCDHEIGHT * SSD1322_LCDWIDTH / (8 / SSD1322_BITS_PER_PIXEL)] = { 0x00 };
// the most basic function, set a single pixel
void HGSC256128_SSD1322::drawPixel(int16_t x, int16_t y, uint16_t gscale)
{
// Serial.print("x=");
// Serial.println(x);
// Serial.print("y=");
// Serial.println(y);
// check rotation, move pixel around if necessary
switch (getRotation())
{
case 1:
_swap_int16_t(x, y);
x = WIDTH - x - 1;
break;
case 2:
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
break;
case 3:
_swap_int16_t(x, y);
y = HEIGHT - y - 1;
break;
}
if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
return;
// Serial.print("x2=");
// Serial.println(x);
// Serial.print("y2=");
// Serial.println(y);
//4 bits per pixel
register uint8_t mask = ((x % 2) ? gscale : gscale << 4);
register uint8_t *pBuf = &buffer[(x >> 1) + (y * (SSD1322_LCDWIDTH / 2))];
register uint8_t b1 = *pBuf;
b1 &= (x % 2) ? 0xF0 : 0x0F; // cleardown nibble to be replaced
// write our value in
*pBuf++ = b1 | mask;
}
HGSC256128_SSD1322::HGSC256128_SSD1322(int8_t SID, int8_t SCLK, int8_t DC,
int8_t RST, int8_t CS1, int8_t CS2) :
Adafruit_GFX(SSD1322_LCDWIDTH, SSD1322_LCDHEIGHT)
{
cs1 = CS1;
cs2 = CS2;
rst = RST;
dc = DC;
sclk = SCLK;
sid = SID;
hwSPI = false;
}
// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset
HGSC256128_SSD1322::HGSC256128_SSD1322(int8_t DC, int8_t RST, int8_t CS1, int8_t CS2) :
Adafruit_GFX(SSD1322_LCDWIDTH, SSD1322_LCDHEIGHT)
{
dc = DC;
rst = RST;
cs1 = CS1;
cs2 = CS2;
hwSPI = true;
}
/* ------------------------------------------------------------
------------------------------------------------------------ */
void HGSC256128_SSD1322::begin()
{
// set pin directions
if (sid != -1)
{
pinMode(dc, OUTPUT);
pinMode(cs1, OUTPUT);
pinMode(cs2, OUTPUT);
if (hwSPI)
{
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider (SPI_CLOCK_DIV4); // 72/4 = 18 MHz (freq STM32C8T6 72MHz)
}
}
digitalWrite(cs1, HIGH);
digitalWrite(cs2, HIGH);
//digitalWrite(rst, HIGH);
digitalWrite(cs1, LOW);
delay(15);
digitalWrite(rst, LOW);
delay(1);
digitalWrite(rst, HIGH);
delay(150);
init(cs1);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
init(cs2);
digitalWrite(cs2, HIGH);
}
void HGSC256128_SSD1322::init(int8_t cs_set)
{
//#ifdef SSD1322_256_64
ssd1322_command(SSD1322_SETCOMMANDLOCK);// 0xFD
ssd1322_data(0x12);// Unlock OLED driver IC
ssd1322_command(SSD1322_DISPLAYOFF);// 0xAE
ssd1322_command(SSD1322_SETCLOCKDIVIDER);// 0xB3
ssd1322_data(0x91);// 0xB3
ssd1322_command(SSD1322_SETMUXRATIO);// 0xCA
ssd1322_data(0x3F);// duty = 1/64
ssd1322_command(SSD1322_SETDISPLAYOFFSET);// 0xA2
ssd1322_data(0x00);
ssd1322_command(SSD1322_SETSTARTLINE);// 0xA1
ssd1322_data(0x00);
ssd1322_command(SSD1322_SETREMAP);// 0xA0
if(cs_set == cs1)
ssd1322_data(0x04);//Horizontal address increment,Disable Column Address Re-map,Enable Nibble Re-map,Scan from COM[N-1] to COM0,Disable COM Split Odd Even
else if(cs_set == cs2)
ssd1322_data(0x16);//Horizontal address increment,Disable Column Address Re-map,Enable Nibble Re-map,Scan from COM[N-1] to COM0,Disable COM Split Odd Even
ssd1322_data(0x11);//Enable Dual COM mode
ssd1322_command(SSD1322_SETGPIO);// 0xB5
ssd1322_data(0x00);// Disable GPIO Pins Input
ssd1322_command(SSD1322_FUNCTIONSEL);// 0xAB
ssd1322_data(0x01);// selection external vdd
ssd1322_command(SSD1322_DISPLAYENHANCE);// 0xB4
ssd1322_data(0xA0);// enables the external VSL
ssd1322_data(0xFD);// 0xfFD,Enhanced low GS display quality;default is 0xb5(normal),
ssd1322_command(SSD1322_SETCONTRASTCURRENT);// 0xC1
ssd1322_data(0xFF);// 0xFF - default is 0x7f
ssd1322_command(SSD1322_MASTERCURRENTCONTROL);// 0xC7
ssd1322_data(0x0F);// default is 0x0F
// Set grayscale
ssd1322_command(SSD1322_SELECTDEFAULTGRAYSCALE); // 0xB9
ssd1322_command(SSD1322_SETPHASELENGTH);// 0xB1
ssd1322_data(0xE2);// default is 0x74
ssd1322_command(SSD1322_DISPLAYENHANCEB);// 0xD1
ssd1322_data(0x82);// Reserved;default is 0xa2(normal)
ssd1322_data(0x20);//
ssd1322_command(SSD1322_SETPRECHARGEVOLTAGE);// 0xBB
ssd1322_data(0x1F);// 0.6xVcc
ssd1322_command(SSD1322_SETSECONDPRECHARGEPERIOD);// 0xB6
ssd1322_data(0x08);// default
// Set the gray value;
ssd1322_command(0xB8);
ssd1322_data(0x0c);
ssd1322_data(0x18);
ssd1322_data(0x24);
ssd1322_data(0x30);
ssd1322_data(0x3c);
ssd1322_data(0x48);
ssd1322_data(0x54);
ssd1322_data(0x60);
ssd1322_data(0x6c);
ssd1322_data(0x78);
ssd1322_data(0x84);
ssd1322_data(0x90);
ssd1322_data(0x9c);
ssd1322_data(0xa8);
ssd1322_data(0xb4);
ssd1322_command(SSD1322_SETVCOMH);// 0xBE
ssd1322_data(0x07);// 0.86xVcc;default is 0x04
ssd1322_command(SSD1322_NORMALDISPLAY);// 0xA6
ssd1322_command(SSD1322_EXITPARTIALDISPLAY);// 0xA9
//#endif
//Clear down image ram before opening display
fill(0x00);
ssd1322_command(SSD1322_DISPLAYON);// 0xAF
}
void HGSC256128_SSD1322::invertDisplay(bool enable)
{
if (enable)
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_INVERSEDISPLAY);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_INVERSEDISPLAY);
digitalWrite(cs2, HIGH);
}
else
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_NORMALDISPLAY);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_NORMALDISPLAY);
digitalWrite(cs2, HIGH);
}
}
// startscrollright
// Activate a right handed scroll for rows start through stop
// Hint, the display is 16 rows tall. To scroll the whole display, run:
// display.scrollright(0x00, 0x0F)
void HGSC256128_SSD1322::startscrollright(uint8_t start, uint8_t stop)
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_RIGHT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X00);
ssd1322_command(0XFF);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_RIGHT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X00);
ssd1322_command(0XFF);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs2, HIGH);
}
// startscrollleft
// Activate a right handed scroll for rows start through stop
// Hint, the display is 16 rows tall. To scroll the whole display, run:
// display.scrollright(0x00, 0x0F)
void HGSC256128_SSD1322::startscrollleft(uint8_t start, uint8_t stop)
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_LEFT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X00);
ssd1322_command(0XFF);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_LEFT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X00);
ssd1322_command(0XFF);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs2, HIGH);
}
// startscrolldiagright
// Activate a diagonal scroll for rows start through stop
// Hint, the display is 16 rows tall. To scroll the whole display, run:
// display.scrollright(0x00, 0x0F)
void HGSC256128_SSD1322::startscrolldiagright(uint8_t start, uint8_t stop)
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_SET_VERTICAL_SCROLL_AREA);
ssd1322_command(0X00);
ssd1322_command(SSD1322_LCDHEIGHT);
ssd1322_command(SSD1322_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X01);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_SET_VERTICAL_SCROLL_AREA);
ssd1322_command(0X00);
ssd1322_command(SSD1322_LCDHEIGHT);
ssd1322_command(SSD1322_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X01);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs2, HIGH);
}
// startscrolldiagleft
// Activate a diagonal scroll for rows start through stop
// Hint, the display is 16 rows tall. To scroll the whole display, run:
// display.scrollright(0x00, 0x0F)
void HGSC256128_SSD1322::startscrolldiagleft(uint8_t start, uint8_t stop)
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_SET_VERTICAL_SCROLL_AREA);
ssd1322_command(0X00);
ssd1322_command(SSD1322_LCDHEIGHT);
ssd1322_command(SSD1322_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X01);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_SET_VERTICAL_SCROLL_AREA);
ssd1322_command(0X00);
ssd1322_command(SSD1322_LCDHEIGHT);
ssd1322_command(SSD1322_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL);
ssd1322_command(0X00);
ssd1322_command(start);
ssd1322_command(0X00);
ssd1322_command(stop);
ssd1322_command(0X01);
ssd1322_command(SSD1322_ACTIVATE_SCROLL);
digitalWrite(cs2, HIGH);
}
void HGSC256128_SSD1322::stopscroll(void)
{
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_DEACTIVATE_SCROLL);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_DEACTIVATE_SCROLL);
digitalWrite(cs2, HIGH);
}
// Dim the display
// dim = true: display is dimmed
// dim = false: display is normal
void HGSC256128_SSD1322::dim(boolean dim)
{
uint8_t contrast;
if (dim)
{
contrast = 0; // Dimmed display
}
// else {
// if (_vccstate == SSD1322_EXTERNALVCC) {
// contrast = 0x9F;
// } else {
// contrast = 0xCF;
// }
// }
// the range of contrast to too small to be really useful
// it is useful to dim the display
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_SETCONTRASTCURRENT);
ssd1322_command(contrast);
digitalWrite(cs1, HIGH);
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_SETCONTRASTCURRENT);
ssd1322_command(contrast);
digitalWrite(cs2, HIGH);
}
void HGSC256128_SSD1322::ssd1322_command(uint8_t c)
{
if (sid != -1)
{
// SPI
digitalWrite(dc, LOW);
fastSPIwrite(c);
}
}
void HGSC256128_SSD1322::ssd1322_data(uint8_t c)
{
if (sid != -1)
{
// SPI
digitalWrite(dc, HIGH);
fastSPIwrite(c);
}
}
void HGSC256128_SSD1322::ssd1322_dataBytes(uint8_t *buf, uint32_t size)
{
if (sid != -1)
{
// SPI
digitalWrite(dc, HIGH);
fastSPIwriteBytes(buf, size);
}
}
void HGSC256128_SSD1322::display()
{
//Screan 1 ------------------------------------------------------------------
digitalWrite(cs1, LOW);
ssd1322_command(SSD1322_SETCOLUMNADDR);
ssd1322_data(MIN_SEG);
ssd1322_data(MAX_SEG);
ssd1322_command(SSD1322_SETROWADDR);
ssd1322_data(0);
ssd1322_data(63);
ssd1322_command(SSD1322_WRITERAM);
register uint16_t bufSize = (SSD1322_LCDHEIGHT * SSD1322_LCDWIDTH / (8 / SSD1322_BITS_PER_PIXEL)) / 2; // bytes
register uint8_t *pBuf = buffer;
// Write as quick as possible 64 bits at a time
ssd1322_dataBytes(pBuf, bufSize);
digitalWrite(cs1, HIGH);
//screan 2 ------------------------------------------------------------------
digitalWrite(cs2, LOW);
ssd1322_command(SSD1322_SETCOLUMNADDR);
ssd1322_data(MIN_SEG);
ssd1322_data(MAX_SEG);
ssd1322_command(SSD1322_SETROWADDR);
ssd1322_data(0);
ssd1322_data(63);
ssd1322_command(SSD1322_WRITERAM);
pBuf = buffer + bufSize;
// Write as quick as possible 64 bits at a time
ssd1322_dataBytes(pBuf, bufSize);
digitalWrite(cs2, HIGH);
}
// clear everything
void HGSC256128_SSD1322::clearDisplay(void)
{
memset(buffer, 0, (SSD1322_LCDHEIGHT * SSD1322_LCDWIDTH / (8 / SSD1322_BITS_PER_PIXEL)));
}
inline void HGSC256128_SSD1322::fastSPIwrite(uint8_t d)
{
if (hwSPI)
{
(void) SPI.transfer(d);
}
else
{
for (uint8_t bit = 0x80; bit; bit >>= 1)
{
*clkport &= ~clkpinmask;
if (d & bit)
*mosiport |= mosipinmask;
else
*mosiport &= ~mosipinmask;
*clkport |= clkpinmask;
}
}
//*csport |= cspinmask;
}
inline void HGSC256128_SSD1322::fastSPIwriteBytes(uint8_t *data, uint32_t const size)
{
for (uint32_t ii = 0; ii < size; ii++)
{
SPI.transfer(data[ii]);
}
}
void HGSC256128_SSD1322::drawFastHLine(int16_t x, int16_t y, int16_t w,
uint16_t color)
{
boolean bSwap = false;
switch (rotation)
{
case 0:
// 0 degree rotation, do nothing
break;
case 1:
// 90 degree rotation, swap x & y for rotation, then invert x
bSwap = true;
_swap_int16_t(x, y)
;
x = WIDTH - x - 1;
break;
case 2:
// 180 degree rotation, invert x and y - then shift y around for height.
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
x -= (w - 1);
break;
case 3:
// 270 degree rotation, swap x & y for rotation, then invert y and adjust y for w (not to become h)
bSwap = true;
_swap_int16_t(x, y)
;
y = HEIGHT - y - 1;
y -= (w - 1);
break;
}
if (bSwap)
{
drawFastVLineInternal(x, y, w, color);
}
else
{
drawFastHLineInternal(x, y, w, color);
}
}
void HGSC256128_SSD1322::drawFastHLineInternal(int16_t x, int16_t y, int16_t w,
uint16_t color)
{
// Do bounds/limit checks
if (y < 0 || y >= HEIGHT)
{
return;
}
// make sure we don't try to draw below 0
if (x < 0)
{
w += x;
x = 0;
}
// make sure we don't go off the edge of the display
if ((x + w) > WIDTH)
{
w = (WIDTH - x);
}
// if our width is now negative, punt
if (w <= 0)
{
return;
}
// set up the pointer for movement through the buffer
// adjust the buffer pointer for the current row
register uint8_t *pBuf = buffer;
pBuf += (x >> 1) + (y * (SSD1322_LCDWIDTH / 2));
register uint8_t oddmask = color;
register uint8_t evenmask = (color << 4);
register uint8_t fullmask = (color << 4) + color;
uint8_t byteLen = w / 2;
if (((x % 2) == 0) && ((w % 2) == 0)) // Start at even and length is even
{
while (byteLen--)
{
*pBuf++ = fullmask;
}
return;
}
if (((x % 2) == 1) && ((w % 2) == 1)) // Start at odd and length is odd
{
register uint8_t b1 = *pBuf;
b1 &= (x % 2) ? 0xF0 : 0x0F; // cleardown nibble to be replaced
// write our value in
*pBuf++ = b1 | oddmask;
while (byteLen--)
{
*pBuf++ = fullmask;
}
return;
}
if (((x % 2) == 0) && ((w % 2) == 1)) // Start at even and length is odd
{
while (byteLen--)
{
*pBuf++ = fullmask;
}
register uint8_t b1 = *pBuf;
b1 &= 0x0F; // cleardown nibble to be replaced
// write our value in
*pBuf++ = b1 | evenmask;
return;
}
if (((x % 2) == 1) && ((w % 2) == 0)) // Start at odd and length is even
{
register uint8_t b1 = *pBuf;
b1 &= (x % 2) ? 0xF0 : 0x0F; // cleardown nibble to be replaced
// write our value in
*pBuf++ = b1 | oddmask;
while (byteLen--)
{
*pBuf++ = fullmask;
}
b1 = *pBuf;
b1 &= 0x0F; // cleardown nibble to be replaced
// write our value in
*pBuf++ = b1 | evenmask;
return;
}
}
void HGSC256128_SSD1322::drawFastVLine(int16_t x, int16_t y, int16_t h,
uint16_t color)
{
bool bSwap = false;
switch (rotation)
{
case 0:
break;
case 1:
// 90 degree rotation, swap x & y for rotation, then invert x and adjust x for h (now to become w)
bSwap = true;
_swap_int16_t(x, y)
;
x = WIDTH - x - 1;
x -= (h - 1);
break;
case 2:
// 180 degree rotation, invert x and y - then shift y around for height.
x = WIDTH - x - 1;
y = HEIGHT - y - 1;
y -= (h - 1);
break;
case 3:
// 270 degree rotation, swap x & y for rotation, then invert y
bSwap = true;
_swap_int16_t(x, y)
;
y = HEIGHT - y - 1;
break;
}
if (bSwap)
{
drawFastHLineInternal(x, y, h, color);
}
else
{
drawFastVLineInternal(x, y, h, color);
}
}
void HGSC256128_SSD1322::drawFastVLineInternal(int16_t x, int16_t __y,
int16_t __h, uint16_t color)
{
// do nothing if we're off the left or right side of the screen
if (x < 0 || x >= WIDTH)
{
return;
}
// make sure we don't try to draw below 0
if (__y < 0)
{
// __y is negative, this will subtract enough from __h to account for __y being 0
__h += __y;
__y = 0;
}
// make sure we don't go past the height of the display
if ((__y + __h) > HEIGHT)
{
__h = (HEIGHT - __y);
}
// if our height is now negative, punt
if (__h <= 0)
{
return;
}
// this display doesn't need ints for coordinates, use local byte registers for faster juggling
register uint8_t y = __y;
register uint8_t h = __h;
// set up the pointer for fast movement through the buffer
register uint8_t *pBuf = buffer;
// adjust the buffer pointer for the current row
pBuf += (x >> 1) + (y * (SSD1322_LCDWIDTH / 2));
register uint8_t mask = ((x % 2) ? color : color << 4);
while (h--)
{
register uint8_t b1 = *pBuf;
b1 &= (x % 2) ? 0xF0 : 0x0F; // cleardown nibble to be replaced
// write our value in
*pBuf = b1 | mask;
// adjust the buffer forward to next row worth of data
pBuf += SSD1322_LCDWIDTH / 2;
};
}
/**
* Fill the display with the specified colour by setting
* every pixel to the colour.
* @param colour - fill the display with this colour.
*/
void HGSC256128_SSD1322::fill(uint8_t colour)
{
uint8_t x, y;
ssd1322_command(SSD1322_SETCOLUMNADDR);
ssd1322_data(MIN_SEG);
ssd1322_data(MAX_SEG);
ssd1322_command(SSD1322_SETROWADDR);
ssd1322_data(0);
ssd1322_data(63);
colour = (colour & 0x0F) | (colour << 4);
ssd1322_command(SSD1322_WRITERAM);
for(y = 0; y < 64; y++)
{
for(x = 0; x < 64; x++)
{
ssd1322_data(colour);
ssd1322_data(colour);
}
}
delay(0);
}
/***************************************************************************************
** Function name: fastDrawBitmap
** Descriptions: draw a bitmap fast. Right now limits are bitmap must be mutiple
** of 8 bits in width.
***************************************************************************************/
void HGSC256128_SSD1322::fastDrawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint8_t color)
{
// do nothing if we're off the left or right side of the screen
if (x < 0 || x >= WIDTH)
{
return;
}
// TODO - NEEDS SOME WORK TO HANDLE XPOS that is not multiple of 8 bits
// calc start pos in the buffer
register uint8_t *pBuf = &buffer[(x >> 1) + (y * (SSD1322_LCDWIDTH / 2))];
register uint8_t wInBytes = w >> 1; // Divide by 2, as 2 pixels per byte (4 bits per pixel)
uint16_t bitPos = 0;
// loop the height
for (int lh = 0; lh < h; lh++)
{
// loop the width
for (int lw = 0; lw < wInBytes; lw++)
{
*pBuf++ = pgm_read_byte((uint8_t *)bitmap + bitPos++);
}
pBuf += (SSD1322_LCDWIDTH / 2) - wInBytes; // Move buffer position to next row
}
}
/***************************************************************************************
** Function name: drawUnicode
** Descriptions: draw a unicode
***************************************************************************************/
int HGSC256128_SSD1322::drawUnicode(unsigned int uniCode, int x, int y, int size)
{
//Serial.println("drawUnicode:E");
if (size) uniCode -= 32;
uint8_t width = 0;
uint8_t height = 0;
uint32_t flash_address = 0;
int8_t gap = 0;
// if (size == 1) {
// flash_address = pgm_read_word(&chrtbl_f8[uniCode]);
// width = pgm_read_byte((uint8_t *)widtbl_f8+uniCode);
// height = chr_hgt_f8;
// gap = 1;
// }
// in calls to pgm_read_dword(), compiler will warn about strict-aliasing,
// 'cause chrtbl_* are uint8_t[] instead of uint32_t[]
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#ifdef LOAD_FONT2
if (size == 2)
{
flash_address = pgm_read_dword(&chrtbl_f16[uniCode]);
width = pgm_read_byte((uint8_t *)widtbl_f16 + uniCode);
height = chr_hgt_f16;
gap = 1;
}
#endif
// if (size == 3) {
// flash_address = pgm_read_word(&chrtbl_f24[uniCode]);
// width = pgm_read_byte((uint8_t *)widtbl_f24+uniCode);
// height = chr_hgt_f24;
// gap = 0;
// }
#ifdef LOAD_FONT4
if (size == 4)
{
flash_address = pgm_read_dword(&chrtbl_f32[uniCode]);
width = pgm_read_byte((uint8_t *)widtbl_f32 + uniCode);
height = chr_hgt_f32;
gap = -3;
}
#endif
// if (size == 5) {
// flash_address = pgm_read_word(&chrtbl_f48[uniCode]);
// width = pgm_read_byte((uint8_t *)widtbl_f48+uniCode);
// height = chr_hgt_f48;
// gap = -3;
// }
#ifdef LOAD_FONT6
if (size == 6)
{
flash_address = pgm_read_dword(&chrtbl_f64[uniCode]);
width = pgm_read_byte((uint8_t *)widtbl_f64 + uniCode);
height = chr_hgt_f64;
gap = -3;
}
#endif
#ifdef LOAD_FONT7
if (size == 7)
{
flash_address = pgm_read_dword(&chrtbl_f7s[uniCode]);
width = pgm_read_byte((uint8_t *)widtbl_f7s + uniCode);
height = chr_hgt_f7s;
gap = 2;
}
#endif
#ifdef LOAD_FONT8
if (size == 8)
{
flash_address = pgm_read_dword(&chrtbl_F10[uniCode]);
width = pgm_read_byte((uint8_t *)widtbl_F10 + uniCode);
height = chr_hgt_F10;
gap = gap_F10;
}
#endif
#pragma GCC diagnostic pop
if (!flash_address)
{
return 0;
}
int w = (width + 7) / 8;
register int pX = 0;
register int pY = y;
byte line = 0;
for(register int i = 0; i < height; i++)
{
if (textcolor != textbgcolor)
{
if (textsize == 1)
{
drawFastHLine(x, pY, width + gap, textbgcolor);
}
else
{
fillRect(x, pY, (width + gap)*textsize, textsize, textbgcolor);
}
}
for (register int k = 0; k < w; k++)
{
line = pgm_read_byte((uint8_t *)flash_address + w * i + k);
if(line)
{
if (textsize == 1)
{
pX = x + k * 8;
//Serial.print("pX=");
//Serial.println(pX);
if(line & 0x80) drawPixel(pX, pY, textcolor);
if(line & 0x40) drawPixel(pX + 1, pY, textcolor);
if(line & 0x20) drawPixel(pX + 2, pY, textcolor);
if(line & 0x10) drawPixel(pX + 3, pY, textcolor);
if(line & 0x8) drawPixel(pX + 4, pY, textcolor);
if(line & 0x4) drawPixel(pX + 5, pY, textcolor);
if(line & 0x2) drawPixel(pX + 6, pY, textcolor);
if(line & 0x1) drawPixel(pX + 7, pY, textcolor);
}
else
{
pX = x + k * 8 * textsize;
if(line & 0x80) fillRect(pX, pY, textsize, textsize, textcolor);
if(line & 0x40) fillRect(pX + textsize, pY, textsize, textsize, textcolor);
if(line & 0x20) fillRect(pX + 2 * textsize, pY, textsize, textsize, textcolor);
if(line & 0x10) fillRect(pX + 3 * textsize, pY, textsize, textsize, textcolor);
if(line & 0x8) fillRect(pX + 4 * textsize, pY, textsize, textsize, textcolor);
if(line & 0x4) fillRect(pX + 5 * textsize, pY, textsize, textsize, textcolor);
if(line & 0x2) fillRect(pX + 6 * textsize, pY, textsize, textsize, textcolor);
if(line & 0x1) fillRect(pX + 7 * textsize, pY, textsize, textsize, textcolor);
}
}
}