-
Notifications
You must be signed in to change notification settings - Fork 2
/
SSD1306_OLED.c
2715 lines (2534 loc) · 89.3 KB
/
SSD1306_OLED.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
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
/*
* MIT License
Copyright (c) 2017 DeeplyEmbedded
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* SSD1306_OLED.c
*
* Created on : Sep 26, 2017
* Author : Vinay Divakar
* Description : SSD1306 OLED Driver, Graphics API's.
* Website : www.deeplyembedded.org
*/
/* Lib Includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include "I2C.h"
#include "SSD1306_OLED.h"
#include "gfxfont.h"
/* Enable or Disable DEBUG Prints */
//#define SSD1306_DBG
/* MACROS */
#define SWAP(x,y) {short temp; temp = x; x = y; y = temp;}
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#define pgm_read_word(addr) (*(const unsigned long *)(addr))
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
#define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
/* static Variables */
static unsigned char _rotation = 0,textsize = 0;
static short _width = SSD1306_LCDWIDTH;
static short _height = SSD1306_LCDHEIGHT;
static short cursor_x = 0, cursor_y = 0, textcolor = 0, textbgcolor = 0;
static bool _cp437 = false, wrap = true;
/* static struct objects */
static GFXfontPtr gfxFont;
/* Externs - I2C.c */
extern I2C_DeviceT I2C_DEV_1;
/* Chunk Buffer */
static unsigned char chunk[17] = {0};
/* Memory buffer for displaying data on LCD - This is an Apple - Fruit */
static unsigned char screen[DISPLAY_BUFF_SIZE] ={0};
/* Static Functions */
static void transfer();
static void drawFastVLine(short x, short y,short h, short color);
static void writeFastVLine(short x, short y, short h, short color);
static void drawFastHLine(short x, short y,short w, short color);
static void writeFastHLine(short x, short y, short w, short color);
static short print(const char *buffer, short size);
// Standard ASCII 5x7 font
static const unsigned char ssd1306_font5x7[] = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x1C, 0x57, 0x7D, 0x57, 0x1C,
0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
0x00, 0x18, 0x3C, 0x18, 0x00,
0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
0x00, 0x18, 0x24, 0x18, 0x00,
0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
0x30, 0x48, 0x3A, 0x06, 0x0E,
0x26, 0x29, 0x79, 0x29, 0x26,
0x40, 0x7F, 0x05, 0x05, 0x07,
0x40, 0x7F, 0x05, 0x25, 0x3F,
0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
0x7F, 0x3E, 0x1C, 0x1C, 0x08,
0x08, 0x1C, 0x1C, 0x3E, 0x7F,
0x14, 0x22, 0x7F, 0x22, 0x14,
0x5F, 0x5F, 0x00, 0x5F, 0x5F,
0x06, 0x09, 0x7F, 0x01, 0x7F,
0x00, 0x66, 0x89, 0x95, 0x6A,
0x60, 0x60, 0x60, 0x60, 0x60,
0x94, 0xA2, 0xFF, 0xA2, 0x94,
0x08, 0x04, 0x7E, 0x04, 0x08,
0x10, 0x20, 0x7E, 0x20, 0x10,
0x08, 0x08, 0x2A, 0x1C, 0x08,
0x08, 0x1C, 0x2A, 0x08, 0x08,
0x1E, 0x10, 0x10, 0x10, 0x10,
0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
0x30, 0x38, 0x3E, 0x38, 0x30,
0x06, 0x0E, 0x3E, 0x0E, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14,
0x24, 0x2A, 0x7F, 0x2A, 0x12,
0x23, 0x13, 0x08, 0x64, 0x62,
0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x08, 0x07, 0x03, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
0x08, 0x08, 0x3E, 0x08, 0x08,
0x00, 0x80, 0x70, 0x30, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08,
0x00, 0x00, 0x60, 0x60, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02,
0x3E, 0x51, 0x49, 0x45, 0x3E,
0x00, 0x42, 0x7F, 0x40, 0x00,
0x72, 0x49, 0x49, 0x49, 0x46,
0x21, 0x41, 0x49, 0x4D, 0x33,
0x18, 0x14, 0x12, 0x7F, 0x10,
0x27, 0x45, 0x45, 0x45, 0x39,
0x3C, 0x4A, 0x49, 0x49, 0x31,
0x41, 0x21, 0x11, 0x09, 0x07,
0x36, 0x49, 0x49, 0x49, 0x36,
0x46, 0x49, 0x49, 0x29, 0x1E,
0x00, 0x00, 0x14, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41,
0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x41, 0x22, 0x14, 0x08,
0x02, 0x01, 0x59, 0x09, 0x06,
0x3E, 0x41, 0x5D, 0x59, 0x4E,
0x7C, 0x12, 0x11, 0x12, 0x7C,
0x7F, 0x49, 0x49, 0x49, 0x36,
0x3E, 0x41, 0x41, 0x41, 0x22,
0x7F, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x49, 0x49, 0x49, 0x41,
0x7F, 0x09, 0x09, 0x09, 0x01,
0x3E, 0x41, 0x41, 0x51, 0x73,
0x7F, 0x08, 0x08, 0x08, 0x7F,
0x00, 0x41, 0x7F, 0x41, 0x00,
0x20, 0x40, 0x41, 0x3F, 0x01,
0x7F, 0x08, 0x14, 0x22, 0x41,
0x7F, 0x40, 0x40, 0x40, 0x40,
0x7F, 0x02, 0x1C, 0x02, 0x7F,
0x7F, 0x04, 0x08, 0x10, 0x7F,
0x3E, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x09, 0x09, 0x09, 0x06,
0x3E, 0x41, 0x51, 0x21, 0x5E,
0x7F, 0x09, 0x19, 0x29, 0x46,
0x26, 0x49, 0x49, 0x49, 0x32,
0x03, 0x01, 0x7F, 0x01, 0x03,
0x3F, 0x40, 0x40, 0x40, 0x3F,
0x1F, 0x20, 0x40, 0x20, 0x1F,
0x3F, 0x40, 0x38, 0x40, 0x3F,
0x63, 0x14, 0x08, 0x14, 0x63,
0x03, 0x04, 0x78, 0x04, 0x03,
0x61, 0x59, 0x49, 0x4D, 0x43,
0x00, 0x7F, 0x41, 0x41, 0x41,
0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x41, 0x41, 0x41, 0x7F,
0x04, 0x02, 0x01, 0x02, 0x04,
0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x03, 0x07, 0x08, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40,
0x7F, 0x28, 0x44, 0x44, 0x38,
0x38, 0x44, 0x44, 0x44, 0x28,
0x38, 0x44, 0x44, 0x28, 0x7F,
0x38, 0x54, 0x54, 0x54, 0x18,
0x00, 0x08, 0x7E, 0x09, 0x02,
0x18, 0xA4, 0xA4, 0x9C, 0x78,
0x7F, 0x08, 0x04, 0x04, 0x78,
0x00, 0x44, 0x7D, 0x40, 0x00,
0x20, 0x40, 0x40, 0x3D, 0x00,
0x7F, 0x10, 0x28, 0x44, 0x00,
0x00, 0x41, 0x7F, 0x40, 0x00,
0x7C, 0x04, 0x78, 0x04, 0x78,
0x7C, 0x08, 0x04, 0x04, 0x78,
0x38, 0x44, 0x44, 0x44, 0x38,
0xFC, 0x18, 0x24, 0x24, 0x18,
0x18, 0x24, 0x24, 0x18, 0xFC,
0x7C, 0x08, 0x04, 0x04, 0x08,
0x48, 0x54, 0x54, 0x54, 0x24,
0x04, 0x04, 0x3F, 0x44, 0x24,
0x3C, 0x40, 0x40, 0x20, 0x7C,
0x1C, 0x20, 0x40, 0x20, 0x1C,
0x3C, 0x40, 0x30, 0x40, 0x3C,
0x44, 0x28, 0x10, 0x28, 0x44,
0x4C, 0x90, 0x90, 0x90, 0x7C,
0x44, 0x64, 0x54, 0x4C, 0x44,
0x00, 0x08, 0x36, 0x41, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02,
0x3C, 0x26, 0x23, 0x26, 0x3C,
0x1E, 0xA1, 0xA1, 0x61, 0x12,
0x3A, 0x40, 0x40, 0x20, 0x7A,
0x38, 0x54, 0x54, 0x55, 0x59,
0x21, 0x55, 0x55, 0x79, 0x41,
0x22, 0x54, 0x54, 0x78, 0x42, // a-umlaut
0x21, 0x55, 0x54, 0x78, 0x40,
0x20, 0x54, 0x55, 0x79, 0x40,
0x0C, 0x1E, 0x52, 0x72, 0x12,
0x39, 0x55, 0x55, 0x55, 0x59,
0x39, 0x54, 0x54, 0x54, 0x59,
0x39, 0x55, 0x54, 0x54, 0x58,
0x00, 0x00, 0x45, 0x7C, 0x41,
0x00, 0x02, 0x45, 0x7D, 0x42,
0x00, 0x01, 0x45, 0x7C, 0x40,
0x7D, 0x12, 0x11, 0x12, 0x7D, // A-umlaut
0xF0, 0x28, 0x25, 0x28, 0xF0,
0x7C, 0x54, 0x55, 0x45, 0x00,
0x20, 0x54, 0x54, 0x7C, 0x54,
0x7C, 0x0A, 0x09, 0x7F, 0x49,
0x32, 0x49, 0x49, 0x49, 0x32,
0x3A, 0x44, 0x44, 0x44, 0x3A, // o-umlaut
0x32, 0x4A, 0x48, 0x48, 0x30,
0x3A, 0x41, 0x41, 0x21, 0x7A,
0x3A, 0x42, 0x40, 0x20, 0x78,
0x00, 0x9D, 0xA0, 0xA0, 0x7D,
0x3D, 0x42, 0x42, 0x42, 0x3D, // O-umlaut
0x3D, 0x40, 0x40, 0x40, 0x3D,
0x3C, 0x24, 0xFF, 0x24, 0x24,
0x48, 0x7E, 0x49, 0x43, 0x66,
0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
0xFF, 0x09, 0x29, 0xF6, 0x20,
0xC0, 0x88, 0x7E, 0x09, 0x03,
0x20, 0x54, 0x54, 0x79, 0x41,
0x00, 0x00, 0x44, 0x7D, 0x41,
0x30, 0x48, 0x48, 0x4A, 0x32,
0x38, 0x40, 0x40, 0x22, 0x7A,
0x00, 0x7A, 0x0A, 0x0A, 0x72,
0x7D, 0x0D, 0x19, 0x31, 0x7D,
0x26, 0x29, 0x29, 0x2F, 0x28,
0x26, 0x29, 0x29, 0x29, 0x26,
0x30, 0x48, 0x4D, 0x40, 0x20,
0x38, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x38,
0x2F, 0x10, 0xC8, 0xAC, 0xBA,
0x2F, 0x10, 0x28, 0x34, 0xFA,
0x00, 0x00, 0x7B, 0x00, 0x00,
0x08, 0x14, 0x2A, 0x14, 0x22,
0x22, 0x14, 0x2A, 0x14, 0x08,
0x55, 0x00, 0x55, 0x00, 0x55, // #176 (25% block) missing in old code
0xAA, 0x55, 0xAA, 0x55, 0xAA, // 50% block
0xFF, 0x55, 0xFF, 0x55, 0xFF, // 75% block
0x00, 0x00, 0x00, 0xFF, 0x00,
0x10, 0x10, 0x10, 0xFF, 0x00,
0x14, 0x14, 0x14, 0xFF, 0x00,
0x10, 0x10, 0xFF, 0x00, 0xFF,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x14, 0x14, 0x14, 0xFC, 0x00,
0x14, 0x14, 0xF7, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x14, 0x14, 0xF4, 0x04, 0xFC,
0x14, 0x14, 0x17, 0x10, 0x1F,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0x1F, 0x00,
0x10, 0x10, 0x10, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x1F, 0x10,
0x10, 0x10, 0x10, 0x1F, 0x10,
0x10, 0x10, 0x10, 0xF0, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0xFF, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x14,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x1F, 0x10, 0x17,
0x00, 0x00, 0xFC, 0x04, 0xF4,
0x14, 0x14, 0x17, 0x10, 0x17,
0x14, 0x14, 0xF4, 0x04, 0xF4,
0x00, 0x00, 0xFF, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0xF7, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x17, 0x14,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0xF4, 0x14,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x00, 0x00, 0x1F, 0x10, 0x1F,
0x00, 0x00, 0x00, 0x1F, 0x14,
0x00, 0x00, 0x00, 0xFC, 0x14,
0x00, 0x00, 0xF0, 0x10, 0xF0,
0x10, 0x10, 0xFF, 0x10, 0xFF,
0x14, 0x14, 0x14, 0xFF, 0x14,
0x10, 0x10, 0x10, 0x1F, 0x00,
0x00, 0x00, 0x00, 0xF0, 0x10,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0xFF,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x38, 0x44, 0x44, 0x38, 0x44,
0xFC, 0x4A, 0x4A, 0x4A, 0x34, // sharp-s or beta
0x7E, 0x02, 0x02, 0x06, 0x06,
0x02, 0x7E, 0x02, 0x7E, 0x02,
0x63, 0x55, 0x49, 0x41, 0x63,
0x38, 0x44, 0x44, 0x3C, 0x04,
0x40, 0x7E, 0x20, 0x1E, 0x20,
0x06, 0x02, 0x7E, 0x02, 0x02,
0x99, 0xA5, 0xE7, 0xA5, 0x99,
0x1C, 0x2A, 0x49, 0x2A, 0x1C,
0x4C, 0x72, 0x01, 0x72, 0x4C,
0x30, 0x4A, 0x4D, 0x4D, 0x30,
0x30, 0x48, 0x78, 0x48, 0x30,
0xBC, 0x62, 0x5A, 0x46, 0x3D,
0x3E, 0x49, 0x49, 0x49, 0x00,
0x7E, 0x01, 0x01, 0x01, 0x7E,
0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
0x44, 0x44, 0x5F, 0x44, 0x44,
0x40, 0x51, 0x4A, 0x44, 0x40,
0x40, 0x44, 0x4A, 0x51, 0x40,
0x00, 0x00, 0xFF, 0x01, 0x03,
0xE0, 0x80, 0xFF, 0x00, 0x00,
0x08, 0x08, 0x6B, 0x6B, 0x08,
0x36, 0x12, 0x36, 0x24, 0x36,
0x06, 0x0F, 0x09, 0x0F, 0x06,
0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x00, 0x10, 0x10, 0x00,
0x30, 0x40, 0xFF, 0x01, 0x01,
0x00, 0x1F, 0x01, 0x01, 0x1E,
0x00, 0x19, 0x1D, 0x17, 0x12,
0x00, 0x3C, 0x3C, 0x3C, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00 // #255 NBSP
};
/****************************************************************
* Function Name : clearDisplay
* Description : Clear the display memory buffer
* Returns : NONE.
* Params : NONE.
****************************************************************/
void clearDisplay()
{
memset(screen, 0x00, DISPLAY_BUFF_SIZE);
}
/****************************************************************
* Function Name : display_Init_seq
* Description : Performs SSD1306 OLED Initialization Sequence
* Returns : NONE.
* Params : NONE.
****************************************************************/
void display_Init_seq()
{
/* Add the reset code, If needed */
/* Send display OFF command */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_DISPLAY_OFF) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display OFF Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display OFF Command Failed\r\n");
#endif
exit(1);
}
/* Set display clock frequency */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_DISP_CLK) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display CLK Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display CLK Command Failed\r\n");
#endif
exit(1);
}
/* Send display CLK command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_DISPCLK_DIV) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display CLK Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display CLK Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display multiplex */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_MULTIPLEX) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display MULT Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display MULT Command Failed\r\n");
#endif
exit(1);
}
/* Send display MULT command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_MULT_DAT) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display MULT Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display MULT Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display OFFSET */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_DISP_OFFSET) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display OFFSET Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display OFFSET Command Failed\r\n");
#endif
exit(1);
}
/* Send display OFFSET command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_DISP_OFFSET_VAL) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display OFFSET Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display OFFSET Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display START LINE - Check this command if something weird happens */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_DISP_START_LINE) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display START LINE Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display START LINE Command Failed\r\n");
#endif
exit(1);
}
/* Enable CHARGEPUMP*/
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_CONFIG_CHARGE_PUMP) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display CHARGEPUMP Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display CHARGEPUMP Command Failed\r\n");
#endif
exit(1);
}
/* Send display CHARGEPUMP command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_CHARGE_PUMP_EN) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display CHARGEPUMP Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display CHARGEPUMP Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display MEMORYMODE */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_MEM_ADDR_MODE) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display MEMORYMODE Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display MEMORYMODE Command Failed\r\n");
#endif
exit(1);
}
/* Send display HORIZONTAL MEMORY ADDR MODE command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_HOR_MM) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display HORIZONTAL MEMORY ADDR MODE Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display HORIZONTAL MEMORY ADDR MODE Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display SEG_REMAP */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SEG_REMAP) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display SEG_REMAP Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display SEG_REMAP Command Failed\r\n");
#endif
exit(1);
}
/* Set display DIR */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_COMSCANDEC) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display DIR Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display DIR Command Failed\r\n");
#endif
exit(1);
}
/* Set display COM */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_COMPINS) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display COM Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display COM Command Failed\r\n");
#endif
exit(1);
}
/* Send display COM command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_CONFIG_COM_PINS) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display COM Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display COM Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display CONTRAST */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_CONTRAST) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display CONTRAST Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display CONTRAST Command Failed\r\n");
#endif
exit(1);
}
/* Send display CONTRAST command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_CONTRAST_VAL) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display CONTRAST Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display CONTRAST Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display PRECHARGE */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_PRECHARGE) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display PRECHARGE Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display PRECHARGE Command Failed\r\n");
#endif
exit(1);
}
/* Send display PRECHARGE command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_PRECHARGE_VAL) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display PRECHARGE Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display PRECHARGE Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display VCOMH */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_VCOMDETECT) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display VCOMH Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display VCOMH Command Failed\r\n");
#endif
exit(1);
}
/* Send display VCOMH command parameter */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_VCOMH_VAL) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display VCOMH Command Parameter Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display VCOMH Command Parameter Failed\r\n");
#endif
exit(1);
}
/* Set display ALL-ON */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_DISPLAYALLON_RESUME) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display ALL-ON Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display ALL-ON Command Failed\r\n");
#endif
exit(1);
}
/* Set display to NORMAL-DISPLAY */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_NORMAL_DISPLAY) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display NORMAL-DISPLAY Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display NORMAL-DISPLAY Command Failed\r\n");
#endif
exit(1);
}
/* Set display to DEACTIVATE_SCROLL */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_DEACTIVATE_SCROLL) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display DEACTIVATE_SCROLL Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display DEACTIVATE_SCROLL Command Failed\r\n");
#endif
exit(1);
}
/* Set display to TURN-ON */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_DISPLAYON) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display TURN-ON Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display TURN-ON Command Failed\r\n");
#endif
exit(1);
}
}
/****************************************************************
* Function Name : transfer
* Description : Transfer the frame buffer onto the display
* Returns : NONE.
* Params : NONE.
****************************************************************/
void transfer()
{
short loop_1 = 0, loop_2 = 0;
short index = 0x00;
for (loop_1 = 0; loop_1 < 1024; loop_1++)
{
chunk[0] = 0x40;
for(loop_2 = 1; loop_2 < 17; loop_2++)
chunk[loop_2] = screen[index++];
if(i2c_multiple_writes(I2C_DEV_1.fd_i2c, 17, chunk) == 17)
{
#ifdef SSD1306_DBG
printf("Chunk written to RAM - Completed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Chunk written to RAM - Failed\r\n");
#endif
exit(1);
}
memset(chunk,0x00,17);
if(index == 1024)
break;
}
}
/****************************************************************
* Function Name : Display
* Description : 1. Resets the column and page addresses.
* 2. Displays the contents of the memory buffer.
* Returns : NONE.
* Params : NONE.
* Note : Each new form can be preceded by a clearDisplay.
****************************************************************/
void Display()
{
Init_Col_PG_addrs(SSD1306_COL_START_ADDR,SSD1306_COL_END_ADDR,
SSD1306_PG_START_ADDR,SSD1306_PG_END_ADDR);
transfer();
}
/****************************************************************
* Function Name : Init_Col_PG_addrs
* Description : Sets the column and page, start and
* end addresses.
* Returns : NONE.
* Params : @col_start_addr: Column start address
* @col_end_addr: Column end address
* @pg_start_addr: Page start address
* @pg_end_addr: Page end address
****************************************************************/
void Init_Col_PG_addrs(unsigned char col_start_addr, unsigned char col_end_addr,
unsigned char pg_start_addr, unsigned char pg_end_addr)
{
/* Send COLMN address setting command */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_SET_COL_ADDR) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display COLMN Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display COLMN Command Failed\r\n");
#endif
exit(1);
}
/* Set COLMN start address */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, col_start_addr) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display COLMN Start Address param Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display COLMN Start Address param Failed\r\n");
#endif
exit(1);
}
/* Set COLMN end address */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, col_end_addr) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display COLMN End Address param Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display COLMN End Address param Failed\r\n");
#endif
exit(1);
}
/* Send PAGE address setting command */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_PAGEADDR) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display PAGE Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display PAGE Command Failed\r\n");
#endif
exit(1);
}
/* Set PAGE start address */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, pg_start_addr) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display PAGE Start Address param Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display PAGE Start Address param Failed\r\n");
#endif
exit(1);
}
/* Set PAGE end address */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, pg_end_addr) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display PAGE End Address param Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display PAGE End Address param Failed\r\n");
#endif
exit(1);
}
}
/****************************************************************
* Function Name : setRotation
* Description : Set the display rotation
* Returns : NONE.
* Params : @x: Display rotation parameter
****************************************************************/
void setRotation(unsigned char x)
{
_rotation = x & 3;
switch(_rotation)
{
case 0:
case 2:
_width = SSD1306_LCDWIDTH;
_height = SSD1306_LCDHEIGHT;
break;
case 1:
case 3:
_width = SSD1306_LCDHEIGHT;
_height = SSD1306_LCDWIDTH;
break;
}
}
/****************************************************************
* Function Name : startscrollright
* Description : Activate a right handed scroll for rows start
* through stop
* Returns : NONE.
* Params : @start: Start location
* @stop: Stop location
* HINT. : the display is 16 rows tall. To scroll the whole
* display, run: display.scrollright(0x00, 0x0F)
****************************************************************/
void startscrollright(unsigned char start, unsigned char stop)
{
/* Send SCROLL horizontal right command */
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, SSD1306_RIGHT_HORIZONTAL_SCROLL) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("Display HORIZONTAL SCROLL RIGHT Command Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("Display HORIZONTAL SCROLL RIGHT Command Failed\r\n");
#endif
exit(1);
}
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, 0x00) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("HORI_SR Param_1 Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("HORI_SR Param_1 Failed\r\n");
#endif
exit(1);
}
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, start) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("HORI_SR Param_2 Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("HORI_SR Param_2 Passed\r\n");
#endif
exit(1);
}
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, 0x00) == I2C_TWO_BYTES)
{
#ifdef SSD1306_DBG
printf("HORI_SR Param_3 Passed\r\n");
#endif
}
else
{
#ifdef SSD1306_DBG
printf("HORI_SR Param_3 Failed\r\n");
#endif
exit(1);
}
if(i2c_write_register(I2C_DEV_1.fd_i2c, SSD1306_CNTRL_CMD, stop) == I2C_TWO_BYTES)