forked from winneymj/ESP8266_SSD1322
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSSD1322.cpp
1655 lines (1418 loc) · 42.1 KB
/
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
*********************************************************************/
#ifndef ESP8266 //Added for compatibility with ESP8266 board
#include <avr/pgmspace.h>
#endif
#if !defined(__SAM3X8E__) && !defined(ESP8266) && !defined(ARDUINO_ARCH_ARC32)
#include <util/delay.h>
#endif
#include <stdlib.h>
#include "Adafruit_GFX.h"
#include "ESP8266_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 ESP8266_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);
#ifdef SSD1322_256_64_4 // 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;
#endif
#ifdef SSD1322_256_64_1 // 1 bit per pixel
register uint8_t *pBuf = &buffer[(x >> 3) + (y * (SSD1322_LCDWIDTH / 8))];
switch (gscale)
{
case WHITE: *pBuf |= (0x80 >> (x%8)); break;
case BLACK: *pBuf &= ~(0x80 >> (x%8)); break;
case INVERSE: *pBuf ^= (0x80 >> (x%8)); break;
}
#endif
}
ESP8266_SSD1322::ESP8266_SSD1322(int8_t SID, int8_t SCLK, int8_t DC,
int8_t RST, int8_t CS) :
Adafruit_GFX(SSD1322_LCDWIDTH, SSD1322_LCDHEIGHT) {
cs = CS;
rst = RST;
dc = DC;
sclk = SCLK;
sid = SID;
hwSPI = false;
}
// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset
ESP8266_SSD1322::ESP8266_SSD1322(int8_t DC, int8_t RST, int8_t CS) :
Adafruit_GFX(SSD1322_LCDWIDTH, SSD1322_LCDHEIGHT) {
dc = DC;
rst = RST;
cs = CS;
hwSPI = true;
}
// initializer for I2C - we only indicate the reset pin!
ESP8266_SSD1322::ESP8266_SSD1322(int8_t reset) :
Adafruit_GFX(SSD1322_LCDWIDTH, SSD1322_LCDHEIGHT) {
sclk = dc = cs = sid = -1;
rst = reset;
}
/* ------------------------------------------------------------
------------------------------------------------------------ */
void ESP8266_SSD1322::begin(uint8_t i2caddr, bool reset) {
_i2caddr = i2caddr;
// set pin directions
if (sid != -1) {
pinMode(dc, OUTPUT);
pinMode(cs, OUTPUT);
if (hwSPI) {
SPI.begin();
SPI.setClockDivider (SPI_CLOCK_DIV2); // 26/2 = 13 MHz (freq ESP8266 26 MHz)
}
}
if (reset && rst)
{
// Setup reset pin direction (used by both SPI and I2C)
pinMode(rst, OUTPUT);
// bring out of reset
digitalWrite(rst, HIGH);
delay(100);
// bring reset low
digitalWrite(rst, LOW);
delay(400);
// bring out of reset
digitalWrite(rst, HIGH);
}
//#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
ssd1322_data(0x14);//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
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 ESP8266_SSD1322::invertDisplay(uint8_t i) {
if (i) {
ssd1322_command(SSD1322_INVERSEDISPLAY);
} else {
ssd1322_command(SSD1322_NORMALDISPLAY);
}
}
// 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 ESP8266_SSD1322::startscrollright(uint8_t start, uint8_t stop) {
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);
}
// 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 ESP8266_SSD1322::startscrollleft(uint8_t start, uint8_t stop) {
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);
}
// 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 ESP8266_SSD1322::startscrolldiagright(uint8_t start, uint8_t stop) {
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);
}
// 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 ESP8266_SSD1322::startscrolldiagleft(uint8_t start, uint8_t stop) {
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);
}
void ESP8266_SSD1322::stopscroll(void) {
ssd1322_command(SSD1322_DEACTIVATE_SCROLL);
}
// Dim the display
// dim = true: display is dimmed
// dim = false: display is normal
void ESP8266_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
ssd1322_command(SSD1322_SETCONTRASTCURRENT);
ssd1322_command(contrast);
}
void ESP8266_SSD1322::ssd1322_command(uint8_t c) {
if (sid != -1) {
// SPI
digitalWrite(cs, HIGH);
digitalWrite(dc, LOW);
digitalWrite(cs, LOW);
fastSPIwrite(c);
digitalWrite(cs, HIGH);
}
}
void ESP8266_SSD1322::ssd1322_data(uint8_t c) {
if (sid != -1) {
// SPI
digitalWrite(cs, HIGH);
digitalWrite(dc, HIGH);
digitalWrite(cs, LOW);
fastSPIwrite(c);
digitalWrite(cs, HIGH);
}
}
void ESP8266_SSD1322::ssd1322_dataBytes(uint8_t *buf, uint32_t size) {
if (sid != -1) {
// SPI
digitalWrite(cs, HIGH);
digitalWrite(dc, HIGH);
digitalWrite(cs, LOW);
fastSPIwriteBytes(buf, size);
digitalWrite(cs, HIGH);
}
}
void ESP8266_SSD1322::display() {
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)); // bytes
register uint8_t *pBuf = buffer;
#ifdef SSD1322_256_64_4
// Write as quick as possible 64 bits at a time
ssd1322_dataBytes(pBuf, bufSize);
#endif
#ifdef SSD1322_256_64_1
uint16_t srcIndex = 0;
while (srcIndex < bufSize)
{
uint8_t destIndex = 0;
uint8_t destArray[64] = {0};
while (destIndex < 64)
{
uint8_t mask = 0x80;
while (mask > 0)
{
// upper nibble
destArray[destIndex] |= (pBuf[srcIndex] & mask) ? 0xf0 : 0x00;
//shift mask to next bit, but this goes into lower nibble.
mask >>= 1;
destArray[destIndex] |= (pBuf[srcIndex] & mask) ? 0x0f : 0x00;
destIndex++;
mask >>= 1;
}
srcIndex++;
}
// Send to display here.
ssd1322_dataBytes(destArray, 64);
}
#endif
}
// clear everything
void ESP8266_SSD1322::clearDisplay(void) {
memset(buffer, 0, (SSD1322_LCDHEIGHT * SSD1322_LCDWIDTH / (8 / SSD1322_BITS_PER_PIXEL)));
}
inline void ESP8266_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 ESP8266_SSD1322::fastSPIwriteBytes(uint8_t * data, uint32_t const size) {
#ifdef ESP8266
SPI.writeBytes(data, size);
#else
for (uint32_t ii = 0; ii < size; ii++) {
SPI.transfer(data[ii]);
}
#endif
}
void ESP8266_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 ESP8266_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
#ifdef SSD1322_256_64_4
// 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;
}
#endif
#ifdef SSD1322_256_64_1
register uint8_t *pBuf = &buffer[(x >> 3) + (y * (SSD1322_LCDWIDTH / 8))];
// do the first partial byte, if necessary - this requires some masking
register uint8_t mod = (x % 8);
//Serial.println("** START ***");
//Serial.print("mod=");
//Serial.println(mod);
if (mod)
{
// mask off the high n bits we want to set
mod = 8-mod;
//Serial.print("mod=");
//Serial.println(mod);
// note - lookup table results in a nearly 10% performance improvement in fill* functions
// register uint8_t mask = ~(0xFF >> (mod));
static uint8_t premask[8] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };
register uint8_t mask = premask[mod];
// adjust the mask if we're not going to reach the end of this byte
if( w < mod)
{
//Serial.println("here 2");
mask &= (0XFF << (mod - w));
//Serial.print("mask=");
//Serial.println((int)mask);
}
switch (color)
{
case WHITE: *pBuf |= mask; break;
case BLACK: *pBuf &= ~mask; break;
case INVERSE: *pBuf ^= mask; break;
}
// fast exit if we're done here!
if(w < mod)
{
return;
}
w -= mod;
// adjust the buffer forward
pBuf++;
}
// write solid bytes while we can - effectively doing 8 rows at a time
if (w >= 8)
{
if (color == INVERSE)
{ // separate copy of the code so we don't impact performance of the black/white write version with an extra comparison per loop
do
{
*pBuf=~(*pBuf);
// adjust the buffer forward
pBuf++;
// adjust h & y (there's got to be a faster way for me to do this, but this should still help a fair bit for now)
w -= 8;
} while(w >= 8);
}
else
{
//Serial.println("here 1");
// store a local value to work with
register uint8_t val = (color == WHITE) ? 255 : 0;
do
{
//Serial.print("w=");
//Serial.println(w);
// write our value in
*pBuf = val;
// adjust the buffer forward
pBuf++;
// adjust h & y (there's got to be a faster way for me to do this, but this should still help a fair bit for now)
w -= 8;
} while(w >= 8);
}
}
// now do the final partial byte, if necessary
if (w)
{
mod = w % 8;
//Serial.print("w%8=");
//Serial.println(mod);
// this time we want to mask the low bits of the byte, vs the high bits we did above
// register uint8_t mask = (1 << mod) - 1;
// note - lookup table results in a nearly 10% performance improvement in fill* functions
static uint8_t postmask[8] = {0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
register uint8_t mask = postmask[mod];
switch (color)
{
case WHITE: *pBuf |= mask; break;
case BLACK: *pBuf &= ~mask; break;
case INVERSE: *pBuf ^= mask; break;
}
}
#endif
}
void ESP8266_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 ESP8266_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;
#ifdef SSD1322_256_64_4
// 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;
};
#endif
#ifdef SSD1322_256_64_1
register uint8_t *pBuf = &buffer[(x >> 3) + (y * (SSD1322_LCDWIDTH / 8))];
register uint8_t mod = (x % 8);
static uint8_t postmask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
register uint8_t mask = postmask[mod];
while (h--)
{
switch (color)
{
case WHITE: *pBuf |= mask; break;
case BLACK: *pBuf &= ~mask; break;
case INVERSE: *pBuf ^= mask; break;
}
// adjust the buffer forward to next row worth of data
pBuf += SSD1322_LCDWIDTH / 8;
}
#endif
}
/**
* Fill the display with the specified colour by setting
* every pixel to the colour.
* @param colour - fill the display with this colour.
*/
void ESP8266_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);
}
#ifdef SSD1322_256_64_1
void ESP8266_SSD1322::fastDrawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint8_t color)
{
//Serial.println("-------fastDrawBitmap----------");
// do nothing if we're off the left or right side of the screen
if ((x + w) < 0 || x >= SSD1322_LCDWIDTH)
{
//cout << "all off left/right" << endl;
return;
}
// Do nothing if off top or bottom
if (y + h < 0 || y >= SSD1322_LCDHEIGHT)
{
//cout << "all off top/bottom" << endl;
return;
}
register int8_t xDiv8 = (x / 8);
register int8_t wDiv8 = (w / 8);
//Serial.print("xDiv8=");
//Serial.println(xDiv8);
// calc start pos in the buffer
register uint8_t *pBuf = &buffer[xDiv8 + (y * (SSD1322_LCDWIDTH / 8))];
// Divide by 8, as 8 pixels per byte (1 bit per pixel) unless this not, then need to add 1 extra byte
register uint8_t wInBytes = ((w % 8) > 0) ? wDiv8 + 1 : wDiv8;
register uint8_t wStartByte = (xDiv8 < 0 ? abs(xDiv8) : 0);
register uint16_t hInRows = min(SSD1322_LCDHEIGHT - y, h);
register uint16_t bytePos = 0;
pBuf += wStartByte; // Move start of buffer up if X < 0
// wInBytes -= wStartByte;
register int16_t mod = x % 8;
register uint8_t bmap = 0;
register uint8_t mask;
// bytePos += wStartByte;
//Serial.print("wInBytes=");
//Serial.println(wInBytes);
//Serial.print("wStartByte=");
//Serial.println(wStartByte);
//Serial.print("mod=");
//Serial.println(mod);
//Serial.print("pBuf=");
//Serial.println(pBuf - buffer);
if (x < 0) // x is less than zero
{
//Serial.print("wInBytes=");
//Serial.println(wInBytes);
//Serial.print("wStartByte=");
//Serial.println(wStartByte);
//Serial.print("mod=");
//Serial.println(mod);
//Serial.print("pBuf=");
//Serial.println(pBuf - buffer);
bytePos += wStartByte;
mod = abs(mod);
//Serial.print("mod now=");
//Serial.println(mod);
// loop the height
for (int lh = 0; lh < hInRows; lh++)
{
register uint8_t shftedOut = 0;
// loop the width
for (int lw = 0; lw < wInBytes; lw++)
{
//Serial.print("lw=");
//Serial.println(lw);
if (lw >= wStartByte)
{
//Serial.println("lw >= wStartByte");
//Serial.print("pBuf=");
//Serial.println(pBuf - buffer);
//Serial.print("bytePos=");
//Serial.println(bytePos);
// Get byte from bitmap to display
bmap = pgm_read_byte(bitmap + bytePos++);
// Shift the byte to display at correct x position.
mask = bmap << mod;
if (lw != (wInBytes - 1))
{
//Serial.println("dont do last");
bmap = pgm_read_byte(bitmap + bytePos); // Get next byte in image
shftedOut = bmap >> (8-mod);
// Move in the bytes from the shifted out of previous
mask |= shftedOut;
}
// Display this image byte
switch (color)
{
case WHITE: *pBuf++ |= mask; break;
case BLACK: *pBuf++ &= ~mask; break;
case INVERSE: *pBuf++ ^= mask; break;
}
}
}//for (int lw = 0; lw < wInBytes; lw++)
//Serial.println("Row done");
pBuf += (SSD1322_LCDWIDTH / 8) - wInBytes + wStartByte; // Move buffer position to next row
bytePos += wStartByte;
}//for (int lh = 0; lh < hInRows; lh++)
}
else
{
if (mod != 0)
{
//Serial.print("---- GREATER THAN ZERO ----");
// loop the height
for (int lh = 0; lh < hInRows; lh++)
{
bool rowTerminated = false;
register uint8_t shftedOut = 0;
// loop the width
for (int lw = 0; lw < wInBytes; lw++)
{
//Serial.print("pBuf=");
//Serial.println(pBuf - buffer);
//Serial.print("bytePos=");
//Serial.println(bytePos);
// Get byte from bitmap to display
bmap = pgm_read_byte(bitmap + bytePos++);
// Shift the byte to display at correct x position.
mask = bmap >> mod;
// Move in the bytes from the shifted out of previous
mask |= shftedOut;
//Serial.print("bmap=");
//Serial.println(bmap);
//Serial.print("mask=");
//Serial.println(mask);
// Display this image byte
switch (color)
{
case WHITE: *pBuf++ |= mask; break;
case BLACK: *pBuf++ &= ~mask; break;
case INVERSE: *pBuf++ ^= mask; break;
}
// Somehow look at pBuf and see if this is now gone off the screen