-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHCuOLED.cpp
More file actions
896 lines (737 loc) · 22.6 KB
/
Copy pathHCuOLED.cpp
File metadata and controls
896 lines (737 loc) · 22.6 KB
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
/* FILE: HCuOLED.cpp
DATE: 14/01/22
VERSION: 1.0
AUTHOR: Andrew Davies
16/04/15 version 0.1: Original version
06/12/16 version 0.2: Added compatibility with ESP8266
Added support for uOLED displays in I2C mode
Added support for WeMos D1 mini OLED shield (see item HCWEMO0007)
Made speed improvement to erase function (thanks to vladyslav-savchenko)
22/05/17 version 0.3: Added support for 128x32 OLED display (see items HCMODU0118 & HCMODU0119)
02/11/18 version 0.4: Fixed issue which cause the integer value of 0 not to be printed.
06/01/19 version 0.5: Change unsigned int to uint16_t in font.h file to fix error when compiling for Wemos boards
18/05/19 version 0.6: Added Brightness() function to allow adjustment of displays brightness level
Removed initialisation code from Reset() and added a new Init() function to allow multiple displays to be reset from the same pin
Added command to pull CS pin high in the Init() function.
24/02/21 version 0.7: Add 2 additonal tiny fonts: sharpsharp_5pt & sharpsharp_6pt thanks to Chris Sharp for creating the fonts.
Also added different font spacing for each font.
22/03/21 version 0.8: Added GetPixel() function to get the current state of a pixel at a specified coordinate.
Updated font description in Fonts.h file from 'const unsigned int' to 'const short unsigned int' to remove compile error when compiling for WeMos D1
11/01/22 version 0.9: Updated Bitmap() and Cursor() functions to allow bitmaps and text to be printed partially off screen
14/01/22 version 1.0: Fixed bug that caused the Flip_H() & Flip_V() functions to not work.
Library header for SSD1307 and SH1106 based OLED displays. In particular this
library has been written for the following displays:
Hobby Components 0.96" uOLED displays (HCMODU0050 & HCMODU0052)
Hobby Components 1.3" uOLED displays (HCMODU0058 & HCMODU0059)
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/
#include "HCuOLED.h"
#include "Arduino.h"
#if !defined (ESP8266)
#include <avr/pgmspace.h>
#endif
#include "fonts.h"
/* Display output buffer */
byte DisplayBuffer[BUFFERCOLSIZE][8];
/* Variables and pointer used by shared static member functions */
int HCuOLED::_XPos;
int HCuOLED::_YPos;
byte HCuOLED::_DrawMode;
const byte *HCuOLED::_FontType;
const uint16_t *HCuOLED::_FontDescriptor;
byte HCuOLED::_FontHight;
byte HCuOLED::_FontSpacing;
/* I2C Constructor to initialise the GPIO and library */
HCuOLED::HCuOLED(byte DisplayType, byte I2C_Add, byte RST_DIO)
{
_RST = RST_DIO;
_I2C_Add = I2C_Add;
_Interface = INT_I2C;
_DisplayType = DisplayType;
pinMode(_RST, OUTPUT);
/* Set vertical and horizontal orientation of the display */
_V_Ori = 0;
_H_Ori = 0;
/* Set dimension and display area of the screen */
switch(_DisplayType)
{
case SSD1306:
_Res_Max_X = SSD1306_RES_X;
_GRAM_Col_Start = SSD1306_GRAM_COL_START;
_GRAM_Col_End = SSD1306_GRAM_COL_END;
_GRAM_Page_Start = SSD1306_GRAM_PAGE_START;
_GRAM_Page_End = SSD1306_GRAM_PAGE_END;
_RAM_Pages = SSD1306_GRAM_PAGE_END - SSD1306_GRAM_PAGE_START + 1;
break;
case SH1106:
_Res_Max_X = SH1106_RES_X;
_GRAM_Col_Start = SH1106_GRAM_COL_START;
_GRAM_Col_End = SH1106_GRAM_COL_END;
_GRAM_Page_Start = SH1106_GRAM_PAGE_START;
_GRAM_Page_End = SH1106_GRAM_PAGE_END;
_RAM_Pages = SH1106_GRAM_PAGE_END - SH1106_GRAM_PAGE_START + 1;;
break;
case WEMOS_D1_MINI_OLED:
_Res_Max_X = WEMOS_RES_X;
_GRAM_Col_Start = WEMOS_GRAM_COL_START;
_GRAM_Col_End = WEMOS_GRAM_COL_END;
_GRAM_Page_Start = WEMOS_GRAM_PAGE_START;
_GRAM_Page_End = WEMOS_GRAM_PAGE_END;
_RAM_Pages = WEMOS_GRAM_PAGE_END - WEMOS_GRAM_PAGE_START + 1;
break;
case SSD1306_128_32:
_Res_Max_X = SSD1306_128_32_RES_X;
_GRAM_Col_Start = SSD1306_128_32_GRAM_COL_START;
_GRAM_Col_End = SSD1306_128_32_GRAM_COL_END;
_GRAM_Page_Start = SSD1306_128_32_GRAM_PAGE_START;
_GRAM_Page_End = SSD1306_128_32_GRAM_PAGE_END;
_RAM_Pages = SSD1306_128_32_GRAM_PAGE_END - SSD1306_128_32_GRAM_PAGE_START + 1;
break;
}
/* Set text cursor to top corner */
Cursor(0, 0);
/* Set default font */
SetFont(Terminal_8pt);
/* Set default draw mode */
DrawMode(NORMAL);
}
/* SPI Constructor to initialise the GPIO and library */
HCuOLED::HCuOLED(byte DisplayType, byte SS_DIO, byte DC_DIO, byte RST_DIO = 0xFF)
{
/* Set the slave select (SS), Data/Command mode (DC) and reset (RST) pins to outputs */
_SS = SS_DIO;
_DC = DC_DIO;
_RST = RST_DIO;
_Interface = INT_SPI;
_DisplayType = DisplayType;
pinMode(_SS, OUTPUT);
pinMode(_DC, OUTPUT);
digitalWrite(_SS, HIGH);
if(_RST != 0xFF)
pinMode(_RST, OUTPUT);
/* Enable the hardware SPI interface */
SPI.begin();
SPI.setBitOrder(MSBFIRST);
/* Set vertical and horizontal orientation of the display */
_V_Ori = 0;
_H_Ori = 0;
/* Set dimension and display area of the screen */
switch(_DisplayType)
{
case SSD1306:
_Res_Max_X = SSD1306_RES_X;
_GRAM_Col_Start = SSD1306_GRAM_COL_START;
_GRAM_Col_End = SSD1306_GRAM_COL_END;
_GRAM_Page_Start = SSD1306_GRAM_PAGE_START;
_GRAM_Page_End = SSD1306_GRAM_PAGE_END;
_RAM_Pages = SSD1306_GRAM_PAGE_END - SSD1306_GRAM_PAGE_START + 1;
break;
case SH1106:
_Res_Max_X = SH1106_RES_X;
_GRAM_Col_Start = SH1106_GRAM_COL_START;
_GRAM_Col_End = SH1106_GRAM_COL_END;
_GRAM_Page_Start = SH1106_GRAM_PAGE_START;
_GRAM_Page_End = SH1106_GRAM_PAGE_END;
_RAM_Pages = SH1106_GRAM_PAGE_END - SH1106_GRAM_PAGE_START + 1;
break;
}
/* Set text cursor to top corner */
Cursor(0, 0);
/* Set default font */
SetFont(Terminal_8pt);
/* Set default draw mode */
DrawMode(NORMAL);
}
/* Writes the contents of the display buffer to the display */
void HCuOLED::Refresh(void)
{
byte ColIndex;
byte RowIndex;
byte Temp1, Temp2;
/* Only for displays with SSD1306 controller which support horizontal address mode */
if(_DisplayType == SSD1306 || _DisplayType == WEMOS_D1_MINI_OLED || _DisplayType == SSD1306_128_32)
{
/* set graphics ram start and end columns */
_Send_Command(SETCOLADDRESS);
_Send_Command(_GRAM_Col_Start);
_Send_Command(_GRAM_Col_End);
/* set graphics ram start and end pages */
_Send_Command(SETPAGEADDRESS);
_Send_Command(_GRAM_Page_Start);
_Send_Command(_GRAM_Page_End);
}
for (RowIndex = 0; RowIndex < _RAM_Pages; RowIndex++)
{
/* Only with displays with SH1106 controller that don't support horizontal address mode */
if( _DisplayType == SH1106)
{
_Send_Command(PAGEADD | RowIndex);
_Send_Command(COLLOWADD | _GRAM_Col_Start); /* Lower column address */
_Send_Command(COLHIGHADD | 0x0); /* Higher column address */
}
/* Via SPI interface */
if(_Interface == INT_SPI)
{
/* Write to display RAM */
_DisplayMode();
for (ColIndex = 0; ColIndex < _Res_Max_X; ColIndex++)
{
digitalWrite(_SS, LOW);
SPI.transfer(DisplayBuffer[ColIndex][RowIndex]);
digitalWrite(_SS, HIGH);
}
}else /* Via I2C interface */
{
/* Write to display RAM */
ColIndex = 0;
do
{
Wire.beginTransmission(_I2C_Add);
Wire.write(I2C_DATA); /* Data */
/* I2C buffer can only hold 32 bytes so write data to graphics memory in 32 byte chunks */
for(byte i = 0; i < 31 && ColIndex < _Res_Max_X; i++)
{
/* 128x32 screen is mapped to every other row so one 8 bit column has to be split
across two row pages. Not a very efficient way to memory map the screen !*/
if(_DisplayType == SSD1306_128_32)
{
Temp1 = DisplayBuffer[ColIndex][RowIndex >> 1];
Temp2 = 0;
if(RowIndex & 1)
{
if(Temp1 & 0b10000000)
Temp2 |= 0x80;
if(Temp1 & 0b01000000)
Temp2 |= 0x20;
if(Temp1 & 0b00100000)
Temp2 |= 0x08;
if(Temp1 & 0b00010000)
Temp2 |= 0x02;
}else
{
if(Temp1 & 0b00001000)
Temp2 |= 0x80;
if(Temp1 & 0b00000100)
Temp2 |= 0x20;
if(Temp1 & 0b00000010)
Temp2 |= 0x08;
if(Temp1 & 0b00000001)
Temp2 |= 0x02;
}
Wire.write(Temp2);
}else /* If its one of the other screens then just write one 8 bit column */
{
Wire.write(DisplayBuffer[ColIndex][RowIndex]);
}
ColIndex++;
}
Wire.endTransmission();
}while(ColIndex < _Res_Max_X);
}
}
}
/* Reset and initialise the module */
void HCuOLED::Reset(void)
{
/* Reset the module */
if(_RST != 0xFF)
{
digitalWrite(_RST, LOW);
delay(1);
digitalWrite(_RST, HIGH);
}
/* Wait 100mS for DC-DC to stabilise. This can probably be reduced */
delay(100);
Init();
}
void HCuOLED::Init(void)
{
_Send_Command(SETMUXRATIO); //Set MUX ratio
_Send_Command(0x3F);
_Send_Command(CHARGEPUMP); //Enable charge pump
_Send_Command(ENABLECHARGEPUMP);
/* Set memory addressing mode to horizontal addressing */
_Send_Command(MEMORYADDRESSMODE);
_Send_Command(HORIZONTALADDRESSMODE);
/* Clear the display buffer */
ClearBuffer();
/* Output the display buffer to clear the display RAM */
Refresh();
/* Turn display on */
_Send_Command(DISPLAYONADD | 1);
/* Wait 150mS for display to turn on */
delay(150);
/* Flip the display */
Flip_H();
Flip_V();
}
/* Sends a command byte to the display where:
Data is the byte of data to send to the command register */
void HCuOLED::_Send_Command(byte Data)
{
if(_Interface == INT_SPI) // Via SPI interface
{
digitalWrite(_DC, LOW); /* Write to command registers */
digitalWrite(_DC, LOW);
digitalWrite(_SS, LOW);
SPI.transfer(Data);
digitalWrite(_SS, HIGH);
}else // Via I2C interface
{
Wire.beginTransmission(_I2C_Add);
Wire.write(I2C_COMMAND);
Wire.write(Data);
Wire.endTransmission();
}
}
/* Clears the contents of the output buffer */
void HCuOLED::ClearBuffer(void)
{
byte ColIndex;
byte RowIndex;
for (RowIndex = 0; RowIndex < 8; RowIndex++)
{
for (ColIndex = 0; ColIndex < BUFFERCOLSIZE; ColIndex++)
{
DisplayBuffer[ColIndex][RowIndex] = 0x00;
}
}
}
/* Switch to command mode (SPI only) */
void HCuOLED::_CommandMode(void)
{
digitalWrite(_DC, LOW);
}
/* Switch to display mode (SPI only) */
void HCuOLED::_DisplayMode(void)
{
digitalWrite(_DC, HIGH);
}
/* Flip the horizontal orientation of the screen */
void HCuOLED::Flip_H(void)
{
_H_Ori = !_H_Ori;
/* Via SPI interface */
if(_Interface == INT_SPI)
{
_CommandMode();
digitalWrite(_SS, LOW);
if(_H_Ori)
{
SPI.transfer(SCANDIRECTIONADD | SCANDIRREVERSE);
}else
{
SPI.transfer(SCANDIRECTIONADD | SCANDIRNORMAL);
}
digitalWrite(_SS, HIGH);
}else /* Via I2C interface */
{
Wire.beginTransmission(_I2C_Add);
Wire.write(I2C_COMMAND); /* Command */
if(_H_Ori)
{
Wire.write(SCANDIRECTIONADD | SCANDIRREVERSE);
}else
{
Wire.write(SCANDIRECTIONADD | SCANDIRNORMAL);
}
Wire.endTransmission();
}
}
/* Flip the vertical orientation of the screen */
void HCuOLED::Flip_V(void)
{
_V_Ori = !_V_Ori;
/* Via SPI interface */
if(_Interface == INT_SPI)
{
_CommandMode();
digitalWrite(_SS, LOW);
if(_V_Ori)
{
SPI.transfer(SEGMENTMAPADD | SEGMENTMAPREVERSE);
}else
{
SPI.transfer(SEGMENTMAPADD | SEGMENTMAPNORMAL);
}
digitalWrite(_SS, HIGH);
}else /* Via I2C interface */
{
Wire.beginTransmission(_I2C_Add);
Wire.write(I2C_COMMAND); /* Command */
if(_V_Ori)
{
Wire.write(SEGMENTMAPADD | SEGMENTMAPREVERSE);
}else
{
Wire.write(SEGMENTMAPADD | SEGMENTMAPNORMAL);
}
Wire.endTransmission();
}
}
/* Sets the brightness of the display where:
Level is the brightness level of the display. Valid values for Level are
1 = Lowest brightness
255 = Highest brightness
*/
void HCuOLED::Brightness(uint8_t Level)
{
_Send_Command(SETCONTRAST);
_Send_Command(Level);
}
/* Write bitmap data to the LCD starting at the cursor location where:
Cols is the number byte columns to write to.
ByteRows is the number of rows to write to in 8 pixel chunks
BitmapData is an array containing the bitmap data to be written */
void HCuOLED::Bitmap(uint8_t Cols, uint8_t ByteRows, const uint8_t BitmapData[])
{
int XIndex;
int YIndex;
int BufRow;
int BufX;
unsigned int BitmapIndex;
/* Step through each 8 pixel row */
for (YIndex = 0; YIndex < ByteRows; YIndex++)
{
/* Step through each column */
for (XIndex = 0; XIndex < Cols; XIndex++)
{
BufX = XIndex + _XPos;
/* If column is beyond display area then don't bother writing to it*/
if(BufX >= 0 && BufX < BUFFERCOLSIZE)
{
BufRow = YIndex + (_YPos / 8);
BitmapIndex = (YIndex * Cols)+ XIndex;
/* If row is beyond the display area then don't bother writing to it */
if(BufRow >= 0 && BufRow < BUFFERROWSIZE)
{
if (_DrawMode == NORMAL)
{
DisplayBuffer[BufX][BufRow] |= pgm_read_byte_near(&BitmapData[BitmapIndex]) << (_YPos%8);
}else
{
DisplayBuffer[BufX][BufRow] ^= pgm_read_byte_near(&BitmapData[BitmapIndex]) << (_YPos%8);
}
/* If column data overlaps to 8 bit rows then write to the second row */
if(_YPos%8 && (BufRow +1) < BUFFERROWSIZE)
if (_DrawMode == NORMAL)
{
DisplayBuffer[BufX][BufRow+1] |= pgm_read_byte_near(&BitmapData[BitmapIndex]) >> (8 - (_YPos%8));
}else
{
DisplayBuffer[BufX][BufRow+1] ^= pgm_read_byte_near(&BitmapData[BitmapIndex]) >> (8 - (_YPos%8));
}
}
}
}
}
}
/* Write to a single pixel on the display where:
X is the x axis coordinate of the pixel
Y is the Y axis coordinate of the pixel */
void HCuOLED::Plot(uint8_t X, uint8_t Y)
{
byte row = Y / BUFFERROWSIZE;
if(X < BUFFERCOLSIZE && row < BUFFERROWSIZE)
if (_DrawMode == NORMAL)
{
DisplayBuffer[X][row] |= (0x01 << (Y % 8));
}else if(_DrawMode == INVERT)
{
DisplayBuffer[X][row] ^= (0x01 << (Y % 8));
}else if(_DrawMode == CLEAR)
{
DisplayBuffer[X][row] &= ~(0x01 << (Y % 8));
}
}
/* Get the current state of a pixel where:
X is the x axis coordinate of the pixel
Y is the Y axis coordinate of the pixel
Returns: A boolean value representing the state of the pixel at coordiate X,Y
*/
boolean HCuOLED::GetPixel(uint8_t X, uint8_t Y)
{
byte row = Y / BUFFERROWSIZE;
if(X < BUFFERCOLSIZE && row < BUFFERROWSIZE)
return DisplayBuffer[X][row] & (0x01 << (Y % 8));
return false;
}
/* Draw a line where:
X1 is the starting X axis coordinate of the line
Y1 is the starting Y axis coordinate of the line
X2 is the starting X axis coordinate of the line
Y2 is the starting Y axis coordinate of the line */
void HCuOLED::Line(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2)
{
double step;
int x, y;
/* If start coordinates are below and to the right of the end coordinate then flip them */
if((X2 <= X1 && Y2 <= Y1) || (X2 >= X1 && Y1 >= Y2) || (X2 >= X1 && Y1 >= Y2))
{
X2^=X1;
X1^=X2;
X2^=X1;
Y2^=Y1;
Y1^=Y2;
Y2^=Y1;
}
/* If X axis is wider than Y axis then step though X axis */
if(((X2-X1) >= (Y2-Y1)) || ((X1 - X2) >= (Y2-Y1)))
{
step = (double)(Y2-Y1) / (X2-X1);
if(X2 >= X1 && Y2 >= Y1)
{
for(x = X1; x <= X2; x++)
Plot(x, ((x-X1) * step) + Y1);
}else
{
for(x = X1; x >= X2; x--)
Plot(x, Y2 + ((x-X2) * step));
}
}else /* If Y axis is wider than X axis then step though Y axis */
{
step = (double)(X2-X1) / (Y2-Y1);
if(Y2 >= Y1 && X2 >= X1)
{
for(y = Y1; y <= Y2; y++)
Plot(((y-Y1) * step) + X1, y);
}else
{
for(y = Y2; y >= Y1; y--)
Plot(X2 + ((y-Y2) * step),y);
}
}
}
/* Draw a rectangle where:
X1 is the X axis coordinate of the first corner
Y1 is the Y axis coordinate of the first corner
X2 is the X axis coordinate of the opposite corner
Y2 is the Y axis coordinate of the opposite corner
FillMode is sets the drawing mode for the rectangle. Options are:
OUTLINE (Draws an outlined rectangle with no fill
SOLID (Draws a filled filled rectangle) */
void HCuOLED::Rect(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2, uint8_t FillMode)
{
byte y;
/* Draw the top and bottom borders */
Line(X1, Y1, X2, Y1);
Line(X1, Y2, X2, Y2);
/* If first corner is below second corner then flip the coordinates */
if(Y1 > Y2)
{
Y2^=Y1;
Y1^=Y2;
Y2^=Y1;
}
/* If rectangle is wider than two pixels set Y for drawing vertical borders */
if(Y2-Y1 > 1)
{
Y1++;
Y2--;
}
/* If box is solid then fill area between top and bottom border */
if(FillMode == SOLID)
{
for(y = Y1; y <= Y2; y++)
Line(X1, y, X2, y);
}else /* if not solid then just draw vertical borders */
{
Line(X1, Y1, X1, Y2);
Line(X2, Y1, X2, Y2);
}
}
/* Clears an area of the display buffer where:
X1 is the X axis coordinate of the first corner
Y1 is the Y axis coordinate of the first corner
X2 is the X axis coordinate of the opposite corner
Y2 is the Y axis coordinate of the opposite corner
Thanks to vladyslav-savchenko for improved version */
void HCuOLED::Erase(uint8_t X1, uint8_t Y1, uint8_t X2, uint8_t Y2)
{
byte temp = _DrawMode;
_DrawMode = CLEAR;
for (int column = X1; column <= X2; column ++)
for (int row = Y1; row <= Y2; row ++)
Plot(column, row);
_DrawMode = temp;
}
/* Sets the drawing mode for text and graphics where:
DrawMode sets the drawing mode. Options are:
NORMAL (Pixels are set to foreground colour)
INVERT (Pixels are set to the opposite of their current state)
CLEAR (Pixels are set to background colour) */
void HCuOLED::DrawMode(byte DrawMode)
{
_DrawMode = DrawMode;
}
/* Sets the location of the cursor for text and bitmap graphics where:
X and Y are the starting top left X & Y axis coordinates */
void HCuOLED::Cursor(int X, int Y)
{
_XPos = X;
_YPos = Y;
}
/* Print a text string to the LCD starting at the cursor coordinates where:
TextString[] is a string array containing the text to be displayed */
void HCuOLED::Print(char TextString[])
{
byte StringLength;
byte Index;
/* Finds length of string */
StringLength = strlen(TextString) - 1;
for (Index = 0; Index <= StringLength; Index++)
{
_WriteChar(TextString[Index] - 32);
}
}
/* Print a floating point number to the LCD starting at the cursor coordinates where:
value is the floating point value to display (max 10 digits including decimal places)
digits is the number of digits to crop the number to (including decimal places)
DecimalPlaces is the number of decimal places to display */
void HCuOLED::Print(float value, byte digits, byte DecimalPlaces)
{
char Buffer[10];
/* Clip the number of digits to 10 */
if (digits > 10)
digits = 10;
/* Convert the value to an character array */
dtostrf(value, digits, DecimalPlaces, Buffer);
/* Output the array to the display buffer */
Print(Buffer);
}
/* Print a signed integer number to the LCD at the current cursor
coordinates where:
Value is signed integer number of type long */
void HCuOLED::Print(long Value)
{
byte Digits[10];
int long Temp;
byte NumDigits = 0;
if(Value == 0)
_WriteChar(0x30 - 0x20);
else
{
/* Is the number negative ? */
if (Value < 0)
{
_WriteChar(13);
Temp = Value * -1;
}else
{
Temp = Value;
}
/* Store each digit in a byte array so that they
can be printed in reverse order */
while (Temp)
{
Digits[NumDigits] = Temp % 10;
Temp /= 10;
NumDigits++;
}
/* Print each digit */
while(NumDigits)
{
NumDigits--;
_WriteChar(Digits[NumDigits] + 16);
}
}
}
/* Print a signed integer number with decimal point to the LCD at
the current cursor coordinates where:
Value is signed integer number of type long
DecimalPlaces is the position of the decimal point */
void HCuOLED::Print(int long Value, byte DecimalPlaces)
{
byte Digits[10];
int long Temp;
byte NumDigits = 0;
/* Is the number negative ? */
if (Value < 0)
{
_WriteChar(13);
Temp = Value * -1;
}else
{
Temp = Value;
}
/* Store each digit in a byte array so that they
can be printed in reverse order */
while (Temp)
{
Digits[NumDigits] = Temp % 10;
Temp /= 10;
NumDigits++;
}
/* If the decimal point is at the beginning of the
number then pad it with a zero */
if(DecimalPlaces == NumDigits)
{
_WriteChar(16);
}
/* Print each digit */
while(NumDigits)
{
NumDigits--;
if (NumDigits + 1 == DecimalPlaces)
_WriteChar(14);
_WriteChar(Digits[NumDigits] + 16);
}
}
/* Write a single character or digit at the current cursor coordinate */
void HCuOLED::_WriteChar(char character)
{
const byte *FontByteAddress;
int FontWidth;
FontByteAddress = _FontType + pgm_read_word_near(_FontDescriptor + (character * 2) + 1);
FontWidth = pgm_read_word_near(_FontDescriptor + (character * 2));
Bitmap(FontWidth, _FontHight, FontByteAddress);
_XPos = _XPos + FontWidth + _FontSpacing;//2;
}
/* Sets the font to be used when writing text or numbers where:
*Font is the font type to be used. Options are:
Terminal_8pt (A small 8x8 fixed width font)
MedProp_11pt (A medium two row proportional font)
LCDLarge_24pt (A large 4 row LCD style font) */
void HCuOLED::SetFont(const byte *Font)
{
if(Font == Terminal_8pt)
{
_FontType = Terminal_8pt;
_FontHight = Terminal_8ptFontInfo.CharacterHeight;
_FontSpacing = Terminal_8ptFontInfo.CharacterSpacing;
_FontDescriptor = Terminal_8ptFontInfo.Descriptors;
}else if(Font == MedProp_11pt)
{
_FontType = MedProp_11pt;
_FontHight = MedProp_11ptFontInfo.CharacterHeight;
_FontSpacing = MedProp_11ptFontInfo.CharacterSpacing;
_FontDescriptor = MedProp_11ptFontInfo.Descriptors;
}else if(Font == LCDLarge_24pt )
{
_FontType = LCDLarge_24pt;
_FontHight = LCDLarge_24ptFontInfo.CharacterHeight;
_FontSpacing = LCDLarge_24ptFontInfo.CharacterSpacing;
_FontDescriptor = LCDLarge_24ptFontInfo.Descriptors;
}else if(Font == sharpsharp_6pt)
{
_FontType = sharpsharp_6pt;
_FontHight = sharpsharp_6ptFontInfo.CharacterHeight;
_FontSpacing = sharpsharp_6ptFontInfo.CharacterSpacing;
_FontDescriptor = sharpsharp_6ptFontInfo.Descriptors;
}else if(Font == sharpsharp_5pt)
{
_FontType = sharpsharp_5pt;
_FontHight = sharpsharp_5ptFontInfo.CharacterHeight;
_FontSpacing = sharpsharp_5ptFontInfo.CharacterSpacing;
_FontDescriptor = sharpsharp_5ptFontInfo.Descriptors;
}
}