-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFAL9664BFB2.ino
1147 lines (1091 loc) · 31 KB
/
CFAL9664BFB2.ino
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
//
// Sketch written for Seeeduino v4.2 set to 3.3v (important!)
// Seeduino is an Arduino Uno clone, with built-in 3.3v support.
//
// CRYSTALFONTZ CFAL9664B-F-B1 or CFAL9664B-F-B2 96x64 COLOR 0.95" OLED
//
// This code uses the 4-wire SPI mode of the display. Actually only 3 wires
// since MISO is unused.
//
// https://www.crystalfontz.com/product/cfal9664bfb1
// https://www.crystalfontz.com/product/cfal9664bfb2
//
// video: https://www.youtube.com/watch?v=
//
// 2018 - 07 - 02 Brent A. Crosby
// 2018 - 10 - 08 Trevin Jorgenson
//===========================================================================
//This is free and unencumbered software released into the public domain.
//
//Anyone is free to copy, modify, publish, use, compile, sell, or
//distribute this software, either in source code form or as a compiled
//binary, for any purpose, commercial or non-commercial, and by any
//means.
//
//In jurisdictions that recognize copyright laws, the author or authors
//of this software dedicate any and all copyright interest in the
//software to the public domain. We make this dedication for the benefit
//of the public at large and to the detriment of our heirs and
//successors. We intend this dedication to be an overt act of
//relinquishment in perpetuity of all present and future rights to this
//software under copyright law.
//
//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 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.
//
//For more information, please refer to <http://unlicense.org/>
//============================================================================
//
// Display is Crystalfontz CFAL9664B-F-B1 or CFAL9664B-F-B2
// https://www.crystalfontz.com/product/cfal9664bfb1
// https://www.crystalfontz.com/product/cfal9664bfb2
//
// The controller is a SSD1331
// https://www.crystalfontz.com/controllers/SolomonSystech/SSD1331/
//
// Set Display Type:
#define CFAL9664BFB1 (0)
#define CFAL9664BFB2 (1)
// The two varaints have different current requirements.
//============================================================================
//Set BUILD_SD to enable a slide show from the uSD card to the OLED.
//Files need to be Windows 96x64 24-bit BMP, file size will be
//exactly 18,486 bytes.
#define BUILD_SD (0)
//============================================================================
#include <avr/io.h>
#include <SPI.h>
// C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src\SPI.cpp
// C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src\SPI.h
#include <util/delay.h>
#include <avr/pgmspace.h>
#if BUILD_SD
#include <SD.h>
// C:\Program Files (x86)\Arduino\libraries\SD\src\SD.cpp
// C:\Program Files (x86)\Arduino\libraries\SD\src\SD.h
/* You might want to make these changes to the SD.cpp library
function SDClass::begin() to allow multiple calls
//ref: https://github.com/arduino-libraries/SD/issues/5
// ----> Line Added to beginning of SDClass::begin
if (root.isOpen()) root.close(); // allows repeated calls
// <---- Line Added
And this change to make speed reasonable:
// return card.init(SPI_HALF_SPEED, csPin) &&
// volume.init(card) &&
// root.openRoot(volume);
//
// to:
//
// return card.init(SPI_FULL_SPEED, csPin) &&
// volume.init(card) &&
// root.openRoot(volume);
*/
#endif
//============================================================================
// CFAL9664B-F-Bx on CFA-10083 Carrier Board
// OLED SPI & control lines
// ARD | Port | OLED | Color
// -----------+------+---------------------|--------
// #6/D6 | PD6 | OLED_RESET_NOT | Purple
// #7/D7 | PD7 | VCC_EN | Blue
// #8/D8 | PB0 | OLED_RS | Yellow
// #9/D9 | PB1 | OLED_CS_NOT | Grey
// #10/D10 | PB2 | SD_CS_NOT | Grey
// #11/D11 | PB3 | MOSI (hardware SPI) | Green
// #12/D12 | PB4 | MISO (hardware SPI) | Blue
// #13/D13 | PB5 | SCK (hardware SPI) | White
//============================================================================
#define SD_CS 10
#define CLR_RESET (PORTD &= ~(0x40))
#define SET_RESET (PORTD |= (0x40))
#define DISABLE_VCC (PORTD &= ~(0x80))
#define ENABLE_VCC (PORTD |= (0x80))
#define CLR_RS (PORTB &= ~(0x01))
#define SET_RS (PORTB |= (0x01))
#define CLR_CS (PORTB &= ~(0x02))
#define SET_CS (PORTB |= (0x02))
#define CLR_SD_CS (PORTB &= ~(0x04))
#define SET_SD_CS (PORTB |= (0x04))
#define CLR_MOSI (PORTB &= ~(0x08))
#define SET_MOSI (PORTB |= (0x08))
#define CLR_SCK (PORTB &= ~(0x20))
#define SET_SCK (PORTB |= (0x20))
//============================================================================
void SPI_sendCommand(uint8_t command)
{
// Select the OLED's command register
CLR_RS;
// Select the OLED controller
CLR_CS;
//Send the command via hardware SPI, wait for all bits to clock out:
SPI.transfer(command);
// Deselect the OLED controller
SET_CS;
}
//----------------------------------------------------------------------------
void SPI_sendData(uint8_t data)
{
// Select the OLED's data register
SET_RS;
// Select the OLED controller
CLR_CS;
//Send the command via hardware SPI, wait for all bits to clock out:
SPI.transfer(data);
// Deselect the OLED controller
SET_CS;
}
//============================================================================
// Defines for the SSD1331 registers.
// ref: https://www.crystalfontz.com/controllers/SolomonSystech/SSD1331/
#define SSD1331_0x15_Column_Address (0x15)
#define SSD1331_0x21_Draw_Line (0x21)
#define SSD1331_0x22_Draw_Rectangle (0x22)
#define SSD1331_0x23_Copy (0x23)
#define SSD1331_0x24_Dim_Window (0x24)
#define SSD1331_0x25_Clear_Window (0x25)
#define SSD1331_0x26_Fill_Copy_Options (0x26)
#define SSD1331_0x27_Scrolling_Options (0x27)
#define SSD1331_0x2E_Scrolling_Stop (0x2E)
#define SSD1331_0x2F_Scrolling_Start (0x2F)
#define SSD1331_0x75_Row_Address (0x75)
#define SSD1331_0x81_Contrast_A_Blue (0x81)
#define SSD1331_0x82_Contrast_B_Green (0x82)
#define SSD1331_0x83_Contrast_C_Red (0x83)
#define SSD1331_0x87_Master_Current (0x87)
#define SSD1331_0x8A_Second_Precharge (0x8A)
#define SSD1331_0xA0_Remap_Data_Format (0xA0)
#define SSD1331_0xA1_Start_Line (0xA1)
#define SSD1331_0xA2_Vertical_Offset (0xA2)
#define SSD1331_0xA4_Mode_Normal (0xA4)
#define SSD1331_0xA5_Mode_All_On (0xA5)
#define SSD1331_0xA6_Mode_All_Off (0xA6)
#define SSD1331_0xA7_Mode_Inverse (0xA7)
#define SSD1331_0xA8_Multiplex_Ratio (0xA8)
#define SSD1331_0xAB_Dim_Mode_Setting (0xAB)
#define SSD1331_0xAD_Master_Configuration (0xAD)
#define SSD1331_0x8F_Param_Not_External_VCC (0x8F)
#define SSD1331_0x8E_Param_Set_External_VCC (0x8E)
#define SSD1331_0xAC_Display_On_Dim (0xAC)
#define SSD1331_0xAE_Display_Off_Sleep (0xAE)
#define SSD1331_0xAF_Display_On_Normal (0xAF)
#define SSD1331_0xB0_Power_Save_Mode (0xB0)
#define SSD1331_0x1A_Param_Yes_Power_Save (0x1A)
#define SSD1331_0x0B_Param_No_Power_Save (0x0B)
#define SSD1331_0xB1_Phase_1_2_Period (0xB1)
#define SSD1331_0xB3_Clock_Divide_Frequency (0xB3)
#define SSD1331_0xB8_Gamma_Table (0xB8)
#define SSD1331_0xB9_Linear_Gamma (0xB9)
#define SSD1331_0xBB_Precharge_Voltage (0xBB)
#define SSD1331_0xBE_VCOMH_Voltage (0xBE)
#define SSD1331_0xFD_Lock_Unlock (0xFD)
#define SSD1331_0x12_Param_Unlock (0x12)
#define SSD1331_0x16_Param_Lock (0x16)
//============================================================================
void Initialize_OLED(void)
{
//Reset the OLED controller by hardware
CLR_RESET;
delay(1);
SET_RESET;
delay(100);
//Make sure the chip is not in "lock" mode.
SPI_sendCommand(SSD1331_0xFD_Lock_Unlock);
SPI_sendCommand(SSD1331_0x12_Param_Unlock);
//Turn the display off during setup
SPI_sendCommand(SSD1331_0xAE_Display_Off_Sleep);
//Set normal display mode (not all on, all off or inverted)
SPI_sendCommand(SSD1331_0xA4_Mode_Normal);
//Set column address range 0-95
SPI_sendCommand(SSD1331_0x15_Column_Address);
SPI_sendCommand(0); //column address start
SPI_sendCommand(95); //column address end
//Set row address range 0-63
SPI_sendCommand(SSD1331_0x75_Row_Address);
SPI_sendCommand(0); //row address start
SPI_sendCommand(63); //row address end
//Set the master current control, based on the version of OLED
SPI_sendCommand(SSD1331_0x87_Master_Current);
//Old "0x0E"
#if CFAL9664BFB1
SPI_sendCommand(6); // 6/16 (60uA of 160uA)
#else //CFAL9664BFB2
SPI_sendCommand(9); // 9/16 (90uA of 160uA)
#endif
//From BM7 current sweeps + maths
#if CFAL9664BFB1
//Set current level/fraction for each color channel
#if 1
//Target 100 cd/m*m (normal)
SPI_sendCommand(SSD1331_0x81_Contrast_A_Blue);
SPI_sendCommand(184);
SPI_sendCommand(SSD1331_0x82_Contrast_B_Green);
SPI_sendCommand(145);
SPI_sendCommand(SSD1331_0x83_Contrast_C_Red);
SPI_sendCommand(176);
#else
//Target 115 cd/m*m (brighter, may reduce life)
SPI_sendCommand(SSD1331_0x81_Contrast_A_Blue);
SPI_sendCommand(218);
SPI_sendCommand(SSD1331_0x82_Contrast_B_Green);
SPI_sendCommand(169);
SPI_sendCommand(SSD1331_0x83_Contrast_C_Red);
SPI_sendCommand(226);
#endif
#else //CFAL9664BFB2
//Set current level/fraction for each color channel
#if 1
//Target 100 cd/m*m (normal)
SPI_sendCommand(SSD1331_0x81_Contrast_A_Blue);
SPI_sendCommand(85);
SPI_sendCommand(SSD1331_0x82_Contrast_B_Green);
SPI_sendCommand(46);
SPI_sendCommand(SSD1331_0x83_Contrast_C_Red);
SPI_sendCommand(158);
#else
//Target 115 cd/m*m (brighter, may reduce life)
SPI_sendCommand(SSD1331_0x81_Contrast_A_Blue);
SPI_sendCommand(102);
SPI_sendCommand(SSD1331_0x82_Contrast_B_Green);
SPI_sendCommand(59);
SPI_sendCommand(SSD1331_0x83_Contrast_C_Red);
SPI_sendCommand(199);
#endif
#endif
//Set pre-charge drive strength/slope
SPI_sendCommand(SSD1331_0x8A_Second_Precharge);
SPI_sendCommand(0x61); //A=Blue
SPI_sendCommand(0x8b); //required token
SPI_sendCommand(0x62); //B=Green
SPI_sendCommand(0x8c); //required token
SPI_sendCommand(0x63); //C=Red
//Configure the controller for our panel
SPI_sendCommand(SSD1331_0xA0_Remap_Data_Format);
SPI_sendCommand(0x70); //check this
// 0111 0000
// ffsv wmhi
// |||| ||||--*0 = Horizontal Increment
// |||| ||| 1 = Vertical Increment
// |||| |||---*0 = Com order is 0 -> 95
// |||| || 1 = Com order is 95 -> 0
// |||| ||----*0 = Normal Order SA,SB,SC
// |||| || 1 = Reverse Order SC,SB,SA
// |||| |-----*0 = No Left/Right COM swap
// |||| 1 = Swap Left/Right COM
// ||||------- 0 = Scan COM0 -> COM63
// ||| *1 = Scan COM63 -> COM0
// |||-------- 0 = COM not split
// || *1 = COM split odd/even
// ||---------*00 = 256 color format
// 01 = 65K color format #1
// 10 = 65K color format #2
// 11 = reserved
//Start at line 0
SPI_sendCommand(SSD1331_0xA1_Start_Line);
SPI_sendCommand(0);
//No vertical offset
SPI_sendCommand(SSD1331_0xA2_Vertical_Offset);
SPI_sendCommand(0);
//Mux ratio = vertical resolution = 64
SPI_sendCommand(SSD1331_0xA8_Multiplex_Ratio);
SPI_sendCommand(63);
//Reset Master configuration
SPI_sendCommand(SSD1331_0xAD_Master_Configuration);
SPI_sendCommand(SSD1331_0x8F_Param_Not_External_VCC);
//Enable power save mode
SPI_sendCommand(SSD1331_0xB0_Power_Save_Mode);
SPI_sendCommand(SSD1331_0x1A_Param_Yes_Power_Save);
//Set the periods for our OLED
SPI_sendCommand(SSD1331_0xB1_Phase_1_2_Period);
SPI_sendCommand(0xF1); // Phase 2 period Phase 1 period
// 1111 0001
// tttt oooo
// |||| ||||-- 1 to 15 = Phase 1 period in DCLK
// ||||------- 1 to 15 = Phase 2 period in DCLK
//Set Display Clock Divide Ratio/Oscillator Frequency
SPI_sendCommand(SSD1331_0xB3_Clock_Divide_Frequency);
SPI_sendCommand(0xD0);
// 1101 0000
// ffff dddd
// |||| ||||-- Divide 0 => /1, 15 => /16
// ||||------- Frequency (See Figure 28 in SSD1331 datasheet)
// Table shows ~800KHz for 2.7v
// Original comment is "0.97MHz"
//Set pre-charge voltage level
SPI_sendCommand(SSD1331_0xBB_Precharge_Voltage);
//0x3E => 0.5 * Vcc (See Figure 28 in SSD1331 datasheet)
SPI_sendCommand(0x3E);
//Set COM deselect voltage level (VCOMH)
SPI_sendCommand(SSD1331_0xBE_VCOMH_Voltage);
// 0011 1110
// --VV VVV-
// |||| ||||-- reserved = 0
// |||| |||--- Voltage Code
// || 00000 = 0.44 * Vcc (0x00)
// || 01000 = 0.52 * Vcc (0x10)
// || 10000 = 0.61 * Vcc (0x20)
// || 11000 = 0.71 * Vcc (0x30)
// || 11111 = 0.83 * Vcc (0x3E)
// ||--------- reserved = 0
#if CFAL9664BFB1
// 0.83 * Vcc (0x3E)
SPI_sendCommand(0x3E);
#else //CFAL9664BFB2
// 0.71 * Vcc (0x30)
SPI_sendCommand(0x30);
#endif
#if 0
//Set default linear gamma / gray scale table
SPI_sendCommand(SSD1331_0xB9_Linear_Gamma);
#else
//Set custom gamma / gray scale table
//Same curve is used for all channels
//This table is gamma 2.25
//See Gamma_Calculations.xlsx
static const uint8_t gamma[32] PROGMEM = {
0x01,0x01,0x01,0x02,0x02,0x04,0x05,0x07,
0x09,0x0B,0x0D,0x10,0x13,0x16,0x1A,0x1E,
0x22,0x26,0x2B,0x2F,0x34,0x3A,0x3F,0x45,
0x4B,0x52,0x58,0x5F,0x66,0x6E,0x75,0x7D };
//Send gamma table
SPI_sendCommand(SSD1331_0xB8_Gamma_Table);
uint8_t
i;
//Send 32 bytes from the table
for (i = 0; i < 32; i++)
{
SPI_sendCommand(pgm_read_byte(&gamma[i]));
}
#endif
//Set Master configuration to select external VCC
SPI_sendCommand(SSD1331_0xAD_Master_Configuration);
SPI_sendCommand(SSD1331_0x8E_Param_Set_External_VCC);
//As a more reasonable default, set the recta
SPI_sendCommand(SSD1331_0x26_Fill_Copy_Options);
// 0x00 Draw rectangle outline only
// 0x01 Draw rectangle outline and fill
SPI_sendCommand(0x01);
//Clear the screen to 0x00
SPI_sendCommand(SSD1331_0x25_Clear_Window);
SPI_sendCommand(0); //X0
SPI_sendCommand(0); //Y0
SPI_sendCommand(95); //X0
SPI_sendCommand(63); //Y0
//Let it finish.
delay(1);
//Turn the display on
SPI_sendCommand(SSD1331_0xAF_Display_On_Normal);
}
//============================================================================
void putPixel(uint8_t x, uint8_t y, uint8_t r, uint8_t g, uint8_t b)
{
SPI_sendCommand(SSD1331_0x21_Draw_Line);
SPI_sendCommand(x);
SPI_sendCommand(y);
SPI_sendCommand(x);
SPI_sendCommand(y);
//Map 0-255 (8-bit) colors to 0-63 (6-bit)
SPI_sendCommand(r >> 2);
SPI_sendCommand(g >> 2);
SPI_sendCommand(b >> 2);
}
//============================================================================
// From: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
void drawCircle(uint16_t x0, uint16_t y0, uint16_t radius, uint16_t R, uint16_t G, uint16_t B)
{
uint16_t x = radius;
uint16_t y = 0;
int16_t radiusError = 1 - (int16_t)x;
while (x >= y)
{
//11 O'Clock
putPixel(x0 - y, y0 + x, R, G, B);
//1 O'Clock
putPixel(x0 + y, y0 + x, R, G, B);
//10 O'Clock
putPixel(x0 - x, y0 + y, R, G, B);
//2 O'Clock
putPixel(x0 + x, y0 + y, R, G, B);
//8 O'Clock
putPixel(x0 - x, y0 - y, R, G, B);
//4 O'Clock
putPixel(x0 + x, y0 - y, R, G, B);
//7 O'Clock
putPixel(x0 - y, y0 - x, R, G, B);
//5 O'Clock
putPixel(x0 + y, y0 - x, R, G, B);
y++;
if (radiusError < 0)
radiusError += (int16_t)(2 * y + 1);
else
{
x--;
radiusError += 2 * (((int16_t)y - (int16_t)x) + 1);
}
}
}
//============================================================================
void drawLine(uint8_t x0, uint8_t y0,
uint8_t x1, uint8_t y1,
uint8_t r, uint8_t g, uint8_t b)
{
SPI_sendCommand(SSD1331_0x21_Draw_Line);
SPI_sendCommand(x0);
SPI_sendCommand(y0);
SPI_sendCommand(x1);
SPI_sendCommand(y1);
//Map 0-255 (8-bit) colors to 0-63 (6-bit)
SPI_sendCommand(r >> 2);
SPI_sendCommand(g >> 2);
SPI_sendCommand(b >> 2);
}
//============================================================================
void drawRectangle(uint8_t x0, uint8_t y0,
uint8_t x1, uint8_t y1,
uint8_t r_line, uint8_t g_line, uint8_t b_line,
uint8_t r_fill, uint8_t g_fill, uint8_t b_fill)
{
uint8_t
i;
//Sort so we are left to right
if (x1 < x0)
{
i = x0;
x0 = x1;
x1 = i;
}
//Sort so we are bottom to top
if (y1 < y0)
{
i = y0;
y0 = y1;
y1 = i;
}
//This is a hardware version, it appears to need the coordiantes
//sorted, and some undocumented delay.
SPI_sendCommand(SSD1331_0x22_Draw_Rectangle);
SPI_sendCommand(x0); //X0
SPI_sendCommand(y0); //Y0
SPI_sendCommand(x1); //X1
SPI_sendCommand(y1); //Y1
//Outline
SPI_sendCommand(r_line >> 2);
SPI_sendCommand(g_line >> 2);
SPI_sendCommand(b_line >> 2);
//Fill
SPI_sendCommand(r_fill >> 2);
SPI_sendCommand(g_fill >> 2);
SPI_sendCommand(b_fill >> 2);
//Give the command some time to complete. Empirical.
//270 fails 275 works 350 should have some margin
delayMicroseconds(350);
}
//============================================================================
void fillOLED(uint8_t r, uint8_t g, uint8_t b)
{
drawRectangle(0, 0,
95, 63,
r, g, b,
r, g, b);
}
//============================================================================
//Gradient Bar fill
void gammaGradient()
{
uint8_t
x;
uint8_t
c;
for (x = 0; x <= 95; x++)
{
//map 0-255 color levels onto 0-95 pixels
c = (((uint16_t)x * (uint16_t)256) + (uint16_t)128) / (uint16_t)95;
//Red
drawLine(x, 0,
x, 20,
c, 0, 0);
//Green
drawLine(x, 21,
x, 42,
0, c, 0);
//Blue
drawLine(x, 43,
x, 63,
0, 0, c);
}
}
//============================================================================
// Sets the active window to the entire display (0,0) to (95,63), and the
// current position to 0,0.
void displayHome()
{
SPI_sendCommand(SSD1331_0x15_Column_Address);
// 00000000: A[6:0] sets the column start address from 0-95, RESET=00d.
SPI_sendCommand(0x00);
// 01011111: B[6:0] sets the column end address from 0-95, RESET=95d.
SPI_sendCommand(0x5F);
SPI_sendCommand(SSD1331_0x75_Row_Address);
// 00000000: A[5:0] sets the row start address from 0-63, RESET=00d.
SPI_sendCommand(0x00);
// 00111111: B[5:0] sets the row end address from 0-63, RESET=63d.
SPI_sendCommand(0x3F);
}
//============================================================================
//Fill screen with vertical color bars using memory writes.
void displayColorBars()
{
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
displayHome();
int
row;
int
col;
for (row = 0; row < 64; row++)
{
for (col = 0; col < 96; col++)
{
if (col > 84)
{
SPI_sendData(BLACK >> 8);
SPI_sendData(BLACK & 0x00FF);
}
else if (col > 72)
{
SPI_sendData(BLUE >> 8);
SPI_sendData(BLUE & 0x00FF);
}
else if (col > 60)
{
SPI_sendData(RED >> 8);
SPI_sendData(RED & 0x00FF);
}
else if (col > 48)
{
SPI_sendData(GREEN >> 8);
SPI_sendData(GREEN & 0x00FF);
}
else if (col > 36)
{
SPI_sendData(CYAN >> 8);
SPI_sendData(CYAN & 0x00FF);
}
else if (col > 24)
{
SPI_sendData(MAGENTA >> 8);
SPI_sendData(MAGENTA & 0x00FF);
}
else if (col > 12)
{
SPI_sendData(YELLOW >> 8);
SPI_sendData(YELLOW & 0x00FF);
}
else
{
SPI_sendData(WHITE >> 8);
SPI_sendData(WHITE & 0x00FF);
}
}
}
}
//============================================================================
//Shows an image from flash, so we can show an image even if there is no
//uSD card installed.
//16-bit-per-pixel image
#include "massey.h"
void showTractor()
{
uint16_t
index;
index = 0;
uint8_t
x;
uint8_t
y;
displayHome();
for (y = 0; y <= 63; y++)
{
for (x = 0; x <= 95; x++)
{
SPI_sendData(pgm_read_byte(&massey[index]));
index++;
SPI_sendData(pgm_read_byte(&massey[index]));
index++;
}
}
}
//============================================================================
void fillDemo()
{
uint8_t
i;
//Fill Demo
for (i = 0; i < 2; i++)
{
fillOLED(0x00, 0x00, 0x00);
delay(250);
fillOLED(0x00, 0x00, 0xFF);
delay(250);
fillOLED(0x00, 0xFF, 0x00);
delay(250);
fillOLED(0x00, 0xFF, 0xFF);
delay(250);
fillOLED(0xFF, 0x00, 0x00);
delay(250);
fillOLED(0xFF, 0x00, 0xFF);
delay(250);
fillOLED(0xFF, 0xFF, 0x00);
delay(250);
fillOLED(0xFF, 0xFF, 0xFF);
delay(250);
}
}
//============================================================================
void cheesyLineDemo(void)
{
#define MAX_X (95)
#define MAX_Y (63)
#define CTR_X (48)
#define CTR_Y (32)
//Hardware lines spin way too fast.
#define SLOW (2)
uint8_t
x;
uint8_t
y;
uint8_t
r;
uint8_t
g;
uint8_t
b;
uint8_t
i;
r = 0xff;
g = 0x00;
b = 0x80;
for (i = 0; i < 10; i++)
{
for (x = 0; x <= MAX_X; x++)
{
drawLine(CTR_X, CTR_Y,
x, 0,
r++, g--, b += 2);
delay(SLOW);
}
for (y = 0; y <= MAX_Y; y++)
{
drawLine(CTR_X, CTR_Y,
MAX_X, y,
r++, g += 4, b += 2);
delay(SLOW);
}
for (x = MAX_X; 0 != x; x--)
{
drawLine(CTR_X, CTR_Y,
x, MAX_Y,
r -= 3, g -= 2, b -= 1);
delay(SLOW);
}
for (y = MAX_Y; 0 != y; y--)
{
drawLine(CTR_X, CTR_Y,
0, y,
r + -3, g--, b++);
delay(SLOW);
}
}
}
//============================================================================
void checkerDemo(void)
{
//8x8 checkerboard demo
uint8_t
x;
uint8_t
y;
uint8_t
i;
uint8_t
r;
uint8_t
g;
uint8_t
b;
//Some seed colors
r = 17;
g = 34;
b = 68;
for (i = 0; i < 20; i++)
{
//Write a 8x8 checkerboard
for (x = 0; x <= 11; x++)
{
for (y = 0; y <= 7; y++)
{
if (((x & 0x01) && !(y & 0x01)) || (!(x & 0x01) && (y & 0x01)))
{
drawRectangle((x << 3), (y << 3),
(x << 3) + 7, (y << 3) + 7,
0, 0, 0,
0, 0, 0);
}
else
{
//Come up with some kind of colors
r = 0xFF - ((x + i) << 4);
g = 0xFF - ((x << 5) + (i << 3));
b = 0xFF - (y << 6);
r++;
g--;
b += 2;
drawRectangle((x << 3), (y << 3),
(x << 3) + 7, (y << 3) + 7,
r, g, b,
r, g, b);
}
}
delay(5);
}
}
}
//============================================================================
void circleDemo(void)
{
uint8_t
x;
uint8_t
i;
uint8_t
r;
uint8_t
g;
uint8_t
b;
fillOLED(0x00, 0x00, 0x00);
r = 0xff;
g = 0x00;
b = 0x80;
for (i = 0; i < 80; i++)
{
for (x = 4; x < 92; x += 3)
{
uint8_t
rad;
if (x < 48)
{
rad = (x << 1) / 3;
}
else
{
rad = ((96 - x) << 1) / 3;
}
drawCircle(x,
(x << 1) / 3,
rad,
r + -6, g -= 4, b += 11);
}
}
}
//============================================================================
#if BUILD_SD
#if(1)
// This function transfers data, in one stream. Slightly
// optimized to do index operations during SPI transfers.
// 312uS ~ 319uS
void SPI_send_pixels(uint8_t pixel_count, uint8_t *data_ptr)
{
uint8_t
r;
uint8_t
g;
uint8_t
b;
uint8_t
first_half;
uint8_t
second_half;
// Select the OLED's data register
SET_RS;
// Select the OLED controller
CLR_CS;
//Load the first pixel. BMPs BGR format
b = *data_ptr;
data_ptr++;
g = *data_ptr;
data_ptr++;
r = *data_ptr;
data_ptr++;
//The display takes two bytes (565) RRRRR GGGGGG BBBBB
//to show one pixel.
first_half = (r & 0xF8) | (g >> 5);
second_half = ((g << 3) & 0xE0) | (b >> 3);
while (pixel_count)
{
//Send the first half of this pixel out
SPDR = first_half;
//Load the next pixel while that is transmitting
b = *data_ptr;
data_ptr++;
g = *data_ptr;
data_ptr++;
r = *data_ptr;
data_ptr++;
//Calculate the next first half while that is transmitting
// ~1.9368us -0.1256 us = 1.8112uS
first_half = (r & 0xF8) | (g >> 5);
//This still seems to be needed, but it should exit more quickly.
while (!(SPSR & _BV(SPIF)));
//Send the second half of the this pixel out
SPDR = second_half;
//Calculate the next second half
second_half = ((g << 3) & 0xE0) | (b >> 3);
//Done with this pixel
pixel_count--;
//This still seems to be needed, but it should exit more quickly.
while (!(SPSR & _BV(SPIF)));
}
//Wait for the final transfer to complete before we bang on CS.
while (!(SPSR & _BV(SPIF)));
// Deselect the OLED controller
SET_CS;
}
#else
// Simple. Slower
// This function transfers data, in one stream.
// 660uS ~ 667uS
void SPI_send_pixels(uint8_t pixel_count, uint8_t *data_ptr)
{
uint8_t
r;
uint8_t
g;
uint8_t
b;
// SPI_sendCommand(LD7138_0x0C_DATA_WRITE_READ);
while (pixel_count)
{
//Load the first pixel. BMPs BGR format
b = *data_ptr;
data_ptr++;
g = *data_ptr;
data_ptr++;
r = *data_ptr;
data_ptr++;
//The display takes two bytes (565) RRRRR GGGGGG BBBBB
//two show one pixel.
//Calculate the first byte
//RRRR RGGG
SPI_sendData((r & 0xF8) | (g >> 5));
//Calculate the second byte
//GGGB BBBB
SPI_sendData(((g << 3) & 0xE0) | (b >> 3));
//Done with this pixel
pixel_count--;
}
}
#endif
//----------------------------------------------------------------------------
void show_BMPs_in_root(void)
{
File
root_dir;
root_dir = SD.open("/");
if (0 == root_dir)
{
Serial.println("show_BMPs_in_root: Can't open \"root\"");
return;
}
File
bmp_file;
while (1)
{
bmp_file = root_dir.openNextFile();
if (0 == bmp_file)
{
// no more files, break out of while()
// root_dir will be closed below.
break;
}
//Skip directories (what about volume name?)
if (0 == bmp_file.isDirectory())
{
//The file name must include ".BMP"
if (0 != strstr(bmp_file.name(), ".BMP"))
{
//The BMP should be exactly 18486 bits long
//(this is correct for 96x64, 24-bit, 54 byte header)
if (18484 <= bmp_file.size() <= 18490)
{
//Jump over BMP header. BMP must be 96x64 24-bit
bmp_file.seek(54);
#if (0)
// Simple and very slow
uint8_t
x;
uint8_t
y;
for (y = 0; y <= 63; y++)
{
for (x = 0; x <= 95; x++)
{
uint8_t
r;
uint8_t
g;
uint8_t
b;
//Read in the RGB triple for this pixel (BMPs are BGR format)
bmp_file.read(&b, 1);
bmp_file.read(&g, 1);
bmp_file.read(&r, 1);
//Plot the pixel
putPixel(x, y, r, g, b);