forked from leaflabs/libmaple
-
Notifications
You must be signed in to change notification settings - Fork 6
/
OLED.cpp
434 lines (352 loc) · 14.3 KB
/
OLED.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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "wirish.h"
#include "OLED.h"
#define LCD_DC_GPIO 31
#define LCD_CS_GPIO 33
#define LCD_PWR_GPIO 16
#define LCD_RES_GPIO 17
#define BPP 2 /* 2 bytes per pixel */
#define NOTE(x) Serial1.print(__FILE__); Serial1.print(":"); Serial1.print(__LINE__); Serial1.print(" "); Serial1.println(x);
#define RGB16(r, b, g) ((((r)<<11L)&0x1fL) | (((g)<<5L)&0x3fL) | (((b)<<0L)&0x1fL))
static HardwareSPI *spi;
static void
write_c(unsigned char out_command)
{
digitalWrite(LCD_DC_GPIO, 0);
spi->write(out_command);
delayMicroseconds(70);
}
static void
write_d_stream(void *data, unsigned int count)
{
digitalWrite(LCD_DC_GPIO, 1);
spi->write((uint8 *)data, count);
delayMicroseconds(70);
}
static void
write_d(unsigned char out_data)
{
digitalWrite(LCD_DC_GPIO, 1);
spi->write(out_data);
delayMicroseconds(70);
}
static void
platform_init(void)
{
spi = new HardwareSPI(2);
spi->begin(SPI_9MHZ, MSBFIRST, SPI_MODE_3);
/* Set the data/command pin to be a GPIO */
pinMode(LCD_DC_GPIO, OUTPUT);
digitalWrite(LCD_DC_GPIO, 0);
/* Set chip-select to be a GPIO */
pinMode(LCD_CS_GPIO, OUTPUT);
digitalWrite(LCD_CS_GPIO, 0);
/* Turn the display on */
pinMode(LCD_PWR_GPIO, OUTPUT);
digitalWrite(LCD_PWR_GPIO, 1);
delayMicroseconds(2000); /* Documentation says at least 1ms */
/* Reset the display */
pinMode(LCD_RES_GPIO, OUTPUT);
digitalWrite(LCD_RES_GPIO, 0);
delayMicroseconds(20); /* Documentation says at least 2us */
digitalWrite(LCD_RES_GPIO, 1);
}
//***************************************************
//--------------------------------------
#define Max_Column 0x7f // 128-1
#define Max_Row 0x7f // 128-1
#define Brightness 0x0F
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Instruction Setting
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
static void Set_Column_Address(unsigned char a, unsigned char b)
{
write_c(0x15); // Set Column Address
write_d(a); // Default => 0x00 (Start Address)
write_d(b); // Default => 0x7F (End Address)
}
static void Set_Row_Address(unsigned char a, unsigned char b)
{
write_c(0x75); // Set Row Address
write_d(a); // Default => 0x00 (Start Address)
write_d(b); // Default => 0x7F (End Address)
}
//=========================================================
// Reset GDRAM position
//=========================================================
static void Home(void)
{
Set_Column_Address(0x00, Max_Column);
Set_Row_Address(0x00, Max_Row);
}
static void Set_Remap_Format(unsigned char d)
{
write_c(0xA0); // Set Re-Map / Color Depth
write_d(d); // Default => 0x40
// Horizontal Address Increment
// Column Address 0 Mapped to SEG0
// Color Sequence: A => B => C
// Scan from COM0 to COM[N-1]
// Disable COM Split Odd Even
// 65,536 Colors
}
static void Set_Start_Line(unsigned char d)
{
write_c(0xA1); // Set Vertical Scroll by RAM
write_d(d); // Default => 0x00
}
static void Set_Display_Offset(unsigned char d)
{
write_c(0xA2); // Set Vertical Scroll by Row
write_d(d); // Default => 0x60
}
static void Set_Display_Mode(unsigned char d)
{
write_c(0xA4|d); // Set Display Mode
// Default => 0xA6
// 0xA4 (0x00) => Entire Display Off, All Pixels Turn Off
// 0xA5 (0x01) => Entire Display On, All Pixels Turn On at GS Level 63
// 0xA6 (0x02) => Normal Display
// 0xA7 (0x03) => Inverse Display
}
static void Set_Function_Selection(unsigned char d)
{
write_c(0xAB); // Function Selection
write_d(d); // Default => 0x01
// Enable Internal VDD Regulator
// Select 8-bit Parallel Interface
}
static void Set_Display_On()
{
write_c(0xAF);
}
static void Set_Display_Off()
{
write_c(0xAE);
}
static void Set_Phase_Length(unsigned char d)
{
write_c(0xB1); // Phase 1 (Reset) & Phase 2 (Pre-Charge) Period Adjustment
write_d(d); // Default => 0x82 (8 Display Clocks [Phase 2] / 5 Display Clocks [Phase 1])
// D[3:0] => Phase 1 Period in 5~31 Display Clocks
// D[7:4] => Phase 2 Period in 3~15 Display Clocks
}
static void Set_Display_Enhancement(unsigned char d)
{
write_c(0xB2); // Display Enhancement
write_d(d); // Default => 0x00 (Normal)
write_d(0x00);
write_d(0x00);
}
static void Set_Display_Clock(unsigned char d)
{
write_c(0xB3); // Set Display Clock Divider / Oscillator Frequency
write_d(d); // Default => 0x00
// A[3:0] => Display Clock Divider
// A[7:4] => Oscillator Frequency
}
static void Set_VSL(unsigned char d)
{
write_c(0xB4); // Set Segment Low Voltage
write_d(0xA0|d); // Default => 0xA0
// 0xA0 (0x00) => Enable External VSL
// 0xA2 (0x02) => Enable Internal VSL (Kept VSL Pin N.C.)
write_d(0xB5);
write_d(0x55);
}
static void Set_GPIO(unsigned char d)
{
write_c(0xB5); // General Purpose IO
write_d(d); // Default => 0x0A (GPIO Pins output Low Level.)
}
static void Set_Precharge_Period(unsigned char d)
{
write_c(0xB6); // Set Second Pre-Charge Period
write_d(d); // Default => 0x08 (8 Display Clocks)
}
static void Set_Precharge_Voltage(unsigned char d)
{
write_c(0xBB); // Set Pre-Charge Voltage Level
write_d(d); // Default => 0x17 (0.50*VCC)
}
static void Set_VCOMH(unsigned char d)
{
write_c(0xBE); // Set COM Deselect Voltage Level
write_d(d); // Default => 0x05 (0.82*VCC)
}
//=========================================================
// Clear OLED GDRAM
//=========================================================
static void CLS(void)
{
unsigned int i;
Home();
write_c(0x5C); // Enable MCU to Read from RAM
for (i=1; i<=((Max_Column+1)*(Max_Row+1)) * 2;i++)
write_d(0x7f);
}
static void Set_Contrast_Color(unsigned char a, unsigned char b, unsigned char c)
{
write_c(0xC1); // Set Contrast Current for Color A, B, C
write_d(a); // Default => 0x8A (Color A)
write_d(b); // Default => 0x51 (Color B)
write_d(c); // Default => 0x8A (Color C)
}
static void Set_Master_Current(unsigned char d)
{
write_c(0xC7); // Master Contrast Current Control
write_d(d); // Default => 0x0F (Maximum)
}
static void Set_Multiplex_Ratio(unsigned char d)
{
write_c(0xCA); // Set Multiplex Ratio
write_d(d); // Default => 0x7F (1/128 Duty)
}
static void Set_Command_Lock(unsigned char d)
{
write_c(0xFD); // Set Command Lock
write_d(d); // Default => 0x12
// 0x12 => Driver IC interface is unlocked from entering command.
// 0x16 => All Commands are locked except 0xFD.
// 0xB0 => Command 0xA2, 0xB1, 0xB3, 0xBB & 0xBE are inaccessible.
// 0xB1 => All Commands are accessible.
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Gray Scale Table Setting (Full Screen)
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
static void Set_Gray_Scale_Table()
{
write_c(0xB8);
write_d(0x02); // Gray Scale Level 1
write_d(0x03); // Gray Scale Level 2
write_d(0x04); // Gray Scale Level 3
write_d(0x05); // Gray Scale Level 4
write_d(0x06); // Gray Scale Level 5
write_d(0x07); // Gray Scale Level 6
write_d(0x08); // Gray Scale Level 7
write_d(0x09); // Gray Scale Level 8
write_d(0x0A); // Gray Scale Level 9
write_d(0x0B); // Gray Scale Level 10
write_d(0x0C); // Gray Scale Level 11
write_d(0x0D); // Gray Scale Level 12
write_d(0x0E); // Gray Scale Level 13
write_d(0x0F); // Gray Scale Level 14
write_d(0x10); // Gray Scale Level 15
write_d(0x11); // Gray Scale Level 16
write_d(0x12); // Gray Scale Level 17
write_d(0x13); // Gray Scale Level 18
write_d(0x15); // Gray Scale Level 19
write_d(0x17); // Gray Scale Level 20
write_d(0x19); // Gray Scale Level 21
write_d(0x1B); // Gray Scale Level 22
write_d(0x1D); // Gray Scale Level 23
write_d(0x1F); // Gray Scale Level 24
write_d(0x21); // Gray Scale Level 25
write_d(0x23); // Gray Scale Level 26
write_d(0x25); // Gray Scale Level 27
write_d(0x27); // Gray Scale Level 28
write_d(0x2A); // Gray Scale Level 29
write_d(0x2D); // Gray Scale Level 30
write_d(0x30); // Gray Scale Level 31
write_d(0x33); // Gray Scale Level 32
write_d(0x36); // Gray Scale Level 33
write_d(0x39); // Gray Scale Level 34
write_d(0x3C); // Gray Scale Level 35
write_d(0x3F); // Gray Scale Level 36
write_d(0x42); // Gray Scale Level 37
write_d(0x45); // Gray Scale Level 38
write_d(0x48); // Gray Scale Level 39
write_d(0x4C); // Gray Scale Level 40
write_d(0x50); // Gray Scale Level 41
write_d(0x54); // Gray Scale Level 42
write_d(0x58); // Gray Scale Level 43
write_d(0x5C); // Gray Scale Level 44
write_d(0x60); // Gray Scale Level 45
write_d(0x64); // Gray Scale Level 46
write_d(0x68); // Gray Scale Level 47
write_d(0x6C); // Gray Scale Level 48
write_d(0x70); // Gray Scale Level 49
write_d(0x74); // Gray Scale Level 50
write_d(0x78); // Gray Scale Level 51
write_d(0x7D); // Gray Scale Level 52
write_d(0x82); // Gray Scale Level 53
write_d(0x87); // Gray Scale Level 54
write_d(0x8C); // Gray Scale Level 55
write_d(0x91); // Gray Scale Level 56
write_d(0x96); // Gray Scale Level 57
write_d(0x9B); // Gray Scale Level 58
write_d(0xA0); // Gray Scale Level 59
write_d(0xA5); // Gray Scale Level 60
write_d(0xAA); // Gray Scale Level 61
write_d(0xAF); // Gray Scale Level 62
write_d(0xB4); // Gray Scale Level 63
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Initialization
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void oled_init(void)
{
platform_init();
//==============================
Set_Command_Lock(0x12); // Unlock Driver IC (0x12/0x16/0xB0/0xB1)
Set_Command_Lock(0xB1); // Unlock All Commands (0x12/0x16/0xB0/0xB1)
Set_Display_Off();
Set_Display_Clock(0xF1); // Set Clock as 90 Frames/Sec
Set_Multiplex_Ratio(0x7F); // 1/128 Duty (0x0F~0x7F)
Set_Display_Offset(0x00); // Shift Mapping RAM Counter (0x00~0x7F)
Set_Start_Line(0x00); // Set Mapping RAM Display Start Line (0x00~0x7F)
Set_Remap_Format(0x74); // Set Horizontal Address Increment
// Column Address 0 Mapped to SEG0
// Color Sequence D[15:0]=[RRRRR:GGGGGG:BBBBB]
// Scan from COM127 to COM0
// Enable COM Split Odd Even
// 65,536 Colors Mode (0x74)
// * 262,144 Colors Mode (0xB4)
Set_GPIO(0x00); // Disable GPIO Pins Input
//Set_Function_Selection(0x00); // Disable Internal VDD Regulator
// Select 8-bit Parallel Interface
Set_VSL(0xA0); // Enable External VSL
Set_Contrast_Color(0xC8,0x80,0xC8); // Set Contrast of Color A (Red)
// Set Contrast of Color B (Green)
// Set Contrast of Color C (Blue)
Set_Master_Current(Brightness); // Set Scale Factor of Segment Output Current Control
Set_Gray_Scale_Table(); // Set Pulse Width for Gray Scale Table
Set_Phase_Length(0x32); // Set Phase 1 as 5 Clocks & Phase 2 as 3 Clocks
Set_Precharge_Voltage(0x17); // Set Pre-Charge Voltage Level as 0.50*VCC
Set_Display_Enhancement(0xA4); // Enhance Display Performance
Set_Precharge_Period(0x01); // Set Second Pre-Charge Period as 1 Clock
Set_VCOMH(0x05); // Set Common Pins Deselect Voltage Level as 0.82*VCC
Set_Display_Mode(0x02); // Normal Display Mode (0x00/0x01/0x02/0x03)
CLS(); // Clear Screen
delayMicroseconds(1000);
Set_Display_On();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Initialization
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void oled_deinit(void)
{
Serial1.println("Turning display off...");
//==============================
Set_Display_Off();
digitalWrite(LCD_PWR_GPIO, 0); // cuts power to the display
delay(250); // give it 250ms to discharge, hard wait; prevent issues with switch bounce
}
void oled_draw_rect(uint8 x, uint8 y, uint8 w, uint8 h, uint8 *data)
{
Set_Column_Address(x, x+w-1);
Set_Row_Address(y, y+h-1);
write_c(0x5c);
write_d_stream(data, w*h*BPP);
}
void oled_blank(void)
{
Set_Display_Mode(0);
}
void oled_unblank(void)
{
Set_Display_Mode(2);
}