aboutsummaryrefslogtreecommitdiff
path: root/drivers/oled/ssd1306_sh1106.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/oled/ssd1306_sh1106.c')
-rw-r--r--drivers/oled/ssd1306_sh1106.c777
1 files changed, 777 insertions, 0 deletions
diff --git a/drivers/oled/ssd1306_sh1106.c b/drivers/oled/ssd1306_sh1106.c
new file mode 100644
index 000000000..7d4197890
--- /dev/null
+++ b/drivers/oled/ssd1306_sh1106.c
@@ -0,0 +1,777 @@
1/*
2Copyright 2019 Ryan Caltabiano <https://github.com/XScorpion2>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include "i2c_master.h"
18#include "oled_driver.h"
19#include OLED_FONT_H
20#include "timer.h"
21#include "print.h"
22
23#include <string.h>
24
25#include "progmem.h"
26
27#include "keyboard.h"
28
29// Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
30// for SH1106: https://www.velleman.eu/downloads/29/infosheets/sh1106_datasheet.pdf
31
32// Fundamental Commands
33#define CONTRAST 0x81
34#define DISPLAY_ALL_ON 0xA5
35#define DISPLAY_ALL_ON_RESUME 0xA4
36#define NORMAL_DISPLAY 0xA6
37#define INVERT_DISPLAY 0xA7
38#define DISPLAY_ON 0xAF
39#define DISPLAY_OFF 0xAE
40#define NOP 0xE3
41
42// Scrolling Commands
43#define ACTIVATE_SCROLL 0x2F
44#define DEACTIVATE_SCROLL 0x2E
45#define SCROLL_RIGHT 0x26
46#define SCROLL_LEFT 0x27
47#define SCROLL_RIGHT_UP 0x29
48#define SCROLL_LEFT_UP 0x2A
49
50// Addressing Setting Commands
51#define MEMORY_MODE 0x20
52#define COLUMN_ADDR 0x21
53#define PAGE_ADDR 0x22
54#define PAM_SETCOLUMN_LSB 0x00
55#define PAM_SETCOLUMN_MSB 0x10
56#define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
57
58// Hardware Configuration Commands
59#define DISPLAY_START_LINE 0x40
60#define SEGMENT_REMAP 0xA0
61#define SEGMENT_REMAP_INV 0xA1
62#define MULTIPLEX_RATIO 0xA8
63#define COM_SCAN_INC 0xC0
64#define COM_SCAN_DEC 0xC8
65#define DISPLAY_OFFSET 0xD3
66#define COM_PINS 0xDA
67#define COM_PINS_SEQ 0x02
68#define COM_PINS_ALT 0x12
69#define COM_PINS_SEQ_LR 0x22
70#define COM_PINS_ALT_LR 0x32
71
72// Timing & Driving Commands
73#define DISPLAY_CLOCK 0xD5
74#define PRE_CHARGE_PERIOD 0xD9
75#define VCOM_DETECT 0xDB
76
77// Advance Graphic Commands
78#define FADE_BLINK 0x23
79#define ENABLE_FADE 0x20
80#define ENABLE_BLINK 0x30
81
82// Charge Pump Commands
83#define CHARGE_PUMP 0x8D
84
85// Misc defines
86#ifndef OLED_BLOCK_COUNT
87# define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8)
88#endif
89#ifndef OLED_BLOCK_SIZE
90# define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
91#endif
92
93#define OLED_ALL_BLOCKS_MASK (((((OLED_BLOCK_TYPE)1 << (OLED_BLOCK_COUNT - 1)) - 1) << 1) | 1)
94
95// i2c defines
96#define I2C_CMD 0x00
97#define I2C_DATA 0x40
98#if defined(__AVR__)
99# define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT)
100#else // defined(__AVR__)
101# define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT)
102#endif // defined(__AVR__)
103#define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT)
104#define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, OLED_I2C_TIMEOUT)
105
106#define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
107
108// Display buffer's is the same as the OLED memory layout
109// this is so we don't end up with rounding errors with
110// parts of the display unusable or don't get cleared correctly
111// and also allows for drawing & inverting
112uint8_t oled_buffer[OLED_MATRIX_SIZE];
113uint8_t * oled_cursor;
114OLED_BLOCK_TYPE oled_dirty = 0;
115bool oled_initialized = false;
116bool oled_active = false;
117bool oled_scrolling = false;
118bool oled_inverted = false;
119uint8_t oled_brightness = OLED_BRIGHTNESS;
120oled_rotation_t oled_rotation = 0;
121uint8_t oled_rotation_width = 0;
122uint8_t oled_scroll_speed = 0; // this holds the speed after being remapped to ssd1306 internal values
123uint8_t oled_scroll_start = 0;
124uint8_t oled_scroll_end = 7;
125#if OLED_TIMEOUT > 0
126uint32_t oled_timeout;
127#endif
128#if OLED_SCROLL_TIMEOUT > 0
129uint32_t oled_scroll_timeout;
130#endif
131#if OLED_UPDATE_INTERVAL > 0
132uint16_t oled_update_timeout;
133#endif
134
135// Internal variables to reduce math instructions
136
137#if defined(__AVR__)
138// identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently
139// probably should move this into i2c_master...
140static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t *data, uint16_t length, uint16_t timeout) {
141 i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
142
143 for (uint16_t i = 0; i < length && status >= 0; i++) {
144 status = i2c_write(pgm_read_byte((const char *)data++), timeout);
145 if (status) break;
146 }
147
148 i2c_stop();
149
150 return status;
151}
152#endif
153
154// Flips the rendering bits for a character at the current cursor position
155static void InvertCharacter(uint8_t *cursor) {
156 const uint8_t *end = cursor + OLED_FONT_WIDTH;
157 while (cursor < end) {
158 *cursor = ~(*cursor);
159 cursor++;
160 }
161}
162
163bool oled_init(oled_rotation_t rotation) {
164#if defined(USE_I2C) && defined(SPLIT_KEYBOARD)
165 if (!is_keyboard_master()) {
166 return true;
167 }
168#endif
169
170 oled_rotation = oled_init_user(rotation);
171 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
172 oled_rotation_width = OLED_DISPLAY_WIDTH;
173 } else {
174 oled_rotation_width = OLED_DISPLAY_HEIGHT;
175 }
176 i2c_init();
177
178 static const uint8_t PROGMEM display_setup1[] = {
179 I2C_CMD,
180 DISPLAY_OFF,
181 DISPLAY_CLOCK,
182 0x80,
183 MULTIPLEX_RATIO,
184 OLED_DISPLAY_HEIGHT - 1,
185 DISPLAY_OFFSET,
186 0x00,
187 DISPLAY_START_LINE | 0x00,
188 CHARGE_PUMP,
189 0x14,
190#if (OLED_IC != OLED_IC_SH1106)
191 // MEMORY_MODE is unsupported on SH1106 (Page Addressing only)
192 MEMORY_MODE,
193 0x00, // Horizontal addressing mode
194#endif
195 };
196 if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) {
197 print("oled_init cmd set 1 failed\n");
198 return false;
199 }
200
201 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
202 static const uint8_t PROGMEM display_normal[] = {I2C_CMD, SEGMENT_REMAP_INV, COM_SCAN_DEC};
203 if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
204 print("oled_init cmd normal rotation failed\n");
205 return false;
206 }
207 } else {
208 static const uint8_t PROGMEM display_flipped[] = {I2C_CMD, SEGMENT_REMAP, COM_SCAN_INC};
209 if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) {
210 print("display_flipped failed\n");
211 return false;
212 }
213 }
214
215 static const uint8_t PROGMEM display_setup2[] = {I2C_CMD, COM_PINS, OLED_COM_PINS, CONTRAST, OLED_BRIGHTNESS, PRE_CHARGE_PERIOD, 0xF1, VCOM_DETECT, 0x20, DISPLAY_ALL_ON_RESUME, NORMAL_DISPLAY, DEACTIVATE_SCROLL, DISPLAY_ON};
216 if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
217 print("display_setup2 failed\n");
218 return false;
219 }
220
221#if OLED_TIMEOUT > 0
222 oled_timeout = timer_read32() + OLED_TIMEOUT;
223#endif
224#if OLED_SCROLL_TIMEOUT > 0
225 oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
226#endif
227
228 oled_clear();
229 oled_initialized = true;
230 oled_active = true;
231 oled_scrolling = false;
232 return true;
233}
234
235__attribute__((weak)) oled_rotation_t oled_init_user(oled_rotation_t rotation) { return rotation; }
236
237void oled_clear(void) {
238 memset(oled_buffer, 0, sizeof(oled_buffer));
239 oled_cursor = &oled_buffer[0];
240 oled_dirty = OLED_ALL_BLOCKS_MASK;
241}
242
243static void calc_bounds(uint8_t update_start, uint8_t *cmd_array) {
244 // Calculate commands to set memory addressing bounds.
245 uint8_t start_page = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
246 uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
247#if (OLED_IC == OLED_IC_SH1106)
248 // Commands for Page Addressing Mode. Sets starting page and column; has no end bound.
249 // Column value must be split into high and low nybble and sent as two commands.
250 cmd_array[0] = PAM_PAGE_ADDR | start_page;
251 cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
252 cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
253 cmd_array[3] = NOP;
254 cmd_array[4] = NOP;
255 cmd_array[5] = NOP;
256#else
257 // Commands for use in Horizontal Addressing mode.
258 cmd_array[1] = start_column;
259 cmd_array[4] = start_page;
260 cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1];
261 cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1;
262#endif
263}
264
265static void calc_bounds_90(uint8_t update_start, uint8_t *cmd_array) {
266 cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
267 cmd_array[4] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT;
268 cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1];
269 ;
270 cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8;
271}
272
273uint8_t crot(uint8_t a, int8_t n) {
274 const uint8_t mask = 0x7;
275 n &= mask;
276 return a << n | a >> (-n & mask);
277}
278
279static void rotate_90(const uint8_t *src, uint8_t *dest) {
280 for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
281 uint8_t selector = (1 << i);
282 for (uint8_t j = 0; j < 8; ++j) {
283 dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
284 }
285 }
286}
287
288void oled_render(void) {
289 if (!oled_initialized) {
290 return;
291 }
292
293 // Do we have work to do?
294 oled_dirty &= OLED_ALL_BLOCKS_MASK;
295 if (!oled_dirty || oled_scrolling) {
296 return;
297 }
298
299 // Find first dirty block
300 uint8_t update_start = 0;
301 while (!(oled_dirty & ((OLED_BLOCK_TYPE)1 << update_start))) {
302 ++update_start;
303 }
304
305 // Set column & page position
306 static uint8_t display_start[] = {I2C_CMD, COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1, PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1};
307 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
308 calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
309 } else {
310 calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
311 }
312
313 // Send column & page position
314 if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
315 print("oled_render offset command failed\n");
316 return;
317 }
318
319 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
320 // Send render data chunk as is
321 if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
322 print("oled_render data failed\n");
323 return;
324 }
325 } else {
326 // Rotate the render chunks
327 const static uint8_t source_map[] = OLED_SOURCE_MAP;
328 const static uint8_t target_map[] = OLED_TARGET_MAP;
329
330 static uint8_t temp_buffer[OLED_BLOCK_SIZE];
331 memset(temp_buffer, 0, sizeof(temp_buffer));
332 for (uint8_t i = 0; i < sizeof(source_map); ++i) {
333 rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
334 }
335
336 // Send render data chunk after rotating
337 if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[0], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
338 print("oled_render90 data failed\n");
339 return;
340 }
341 }
342
343 // Turn on display if it is off
344 oled_on();
345
346 // Clear dirty flag
347 oled_dirty &= ~((OLED_BLOCK_TYPE)1 << update_start);
348}
349
350void oled_set_cursor(uint8_t col, uint8_t line) {
351 uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
352
353 // Out of bounds?
354 if (index >= OLED_MATRIX_SIZE) {
355 index = 0;
356 }
357
358 oled_cursor = &oled_buffer[index];
359}
360
361void oled_advance_page(bool clearPageRemainder) {
362 uint16_t index = oled_cursor - &oled_buffer[0];
363 uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
364
365 if (clearPageRemainder) {
366 // Remaining Char count
367 remaining = remaining / OLED_FONT_WIDTH;
368
369 // Write empty character until next line
370 while (remaining--) oled_write_char(' ', false);
371 } else {
372 // Next page index out of bounds?
373 if (index + remaining >= OLED_MATRIX_SIZE) {
374 index = 0;
375 remaining = 0;
376 }
377
378 oled_cursor = &oled_buffer[index + remaining];
379 }
380}
381
382void oled_advance_char(void) {
383 uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
384 uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
385
386 // Do we have enough space on the current line for the next character
387 if (remainingSpace < OLED_FONT_WIDTH) {
388 nextIndex += remainingSpace;
389 }
390
391 // Did we go out of bounds
392 if (nextIndex >= OLED_MATRIX_SIZE) {
393 nextIndex = 0;
394 }
395
396 // Update cursor position
397 oled_cursor = &oled_buffer[nextIndex];
398}
399
400// Main handler that writes character data to the display buffer
401void oled_write_char(const char data, bool invert) {
402 // Advance to the next line if newline
403 if (data == '\n') {
404 // Old source wrote ' ' until end of line...
405 oled_advance_page(true);
406 return;
407 }
408
409 if (data == '\r') {
410 oled_advance_page(false);
411 return;
412 }
413
414 // copy the current render buffer to check for dirty after
415 static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
416 memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
417
418 _Static_assert(sizeof(font) >= ((OLED_FONT_END + 1 - OLED_FONT_START) * OLED_FONT_WIDTH), "OLED_FONT_END references outside array");
419
420 // set the reder buffer data
421 uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
422 if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
423 memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
424 } else {
425 const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
426 memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
427 }
428
429 // Invert if needed
430 if (invert) {
431 InvertCharacter(oled_cursor);
432 }
433
434 // Dirty check
435 if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
436 uint16_t index = oled_cursor - &oled_buffer[0];
437 oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
438 // Edgecase check if the written data spans the 2 chunks
439 oled_dirty |= ((OLED_BLOCK_TYPE)1 << ((index + OLED_FONT_WIDTH - 1) / OLED_BLOCK_SIZE));
440 }
441
442 // Finally move to the next char
443 oled_advance_char();
444}
445
446void oled_write(const char *data, bool invert) {
447 const char *end = data + strlen(data);
448 while (data < end) {
449 oled_write_char(*data, invert);
450 data++;
451 }
452}
453
454void oled_write_ln(const char *data, bool invert) {
455 oled_write(data, invert);
456 oled_advance_page(true);
457}
458
459void oled_pan(bool left) {
460 uint16_t i = 0;
461 for (uint16_t y = 0; y < OLED_DISPLAY_HEIGHT / 8; y++) {
462 if (left) {
463 for (uint16_t x = 0; x < OLED_DISPLAY_WIDTH - 1; x++) {
464 i = y * OLED_DISPLAY_WIDTH + x;
465 oled_buffer[i] = oled_buffer[i + 1];
466 }
467 } else {
468 for (uint16_t x = OLED_DISPLAY_WIDTH - 1; x > 0; x--) {
469 i = y * OLED_DISPLAY_WIDTH + x;
470 oled_buffer[i] = oled_buffer[i - 1];
471 }
472 }
473 }
474 oled_dirty = OLED_ALL_BLOCKS_MASK;
475}
476
477oled_buffer_reader_t oled_read_raw(uint16_t start_index) {
478 if (start_index > OLED_MATRIX_SIZE) start_index = OLED_MATRIX_SIZE;
479 oled_buffer_reader_t ret_reader;
480 ret_reader.current_element = &oled_buffer[start_index];
481 ret_reader.remaining_element_count = OLED_MATRIX_SIZE - start_index;
482 return ret_reader;
483}
484
485void oled_write_raw_byte(const char data, uint16_t index) {
486 if (index > OLED_MATRIX_SIZE) index = OLED_MATRIX_SIZE;
487 if (oled_buffer[index] == data) return;
488 oled_buffer[index] = data;
489 oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
490}
491
492void oled_write_raw(const char *data, uint16_t size) {
493 uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
494 if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
495 for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
496 uint8_t c = *data++;
497 if (oled_buffer[i] == c) continue;
498 oled_buffer[i] = c;
499 oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
500 }
501}
502
503void oled_write_pixel(uint8_t x, uint8_t y, bool on) {
504 if (x >= oled_rotation_width) {
505 return;
506 }
507 uint16_t index = x + (y / 8) * oled_rotation_width;
508 if (index >= OLED_MATRIX_SIZE) {
509 return;
510 }
511 uint8_t data = oled_buffer[index];
512 if (on) {
513 data |= (1 << (y % 8));
514 } else {
515 data &= ~(1 << (y % 8));
516 }
517 if (oled_buffer[index] != data) {
518 oled_buffer[index] = data;
519 oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
520 }
521}
522
523#if defined(__AVR__)
524void oled_write_P(const char *data, bool invert) {
525 uint8_t c = pgm_read_byte(data);
526 while (c != 0) {
527 oled_write_char(c, invert);
528 c = pgm_read_byte(++data);
529 }
530}
531
532void oled_write_ln_P(const char *data, bool invert) {
533 oled_write_P(data, invert);
534 oled_advance_page(true);
535}
536
537void oled_write_raw_P(const char *data, uint16_t size) {
538 uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
539 if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
540 for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
541 uint8_t c = pgm_read_byte(data++);
542 if (oled_buffer[i] == c) continue;
543 oled_buffer[i] = c;
544 oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
545 }
546}
547#endif // defined(__AVR__)
548
549bool oled_on(void) {
550 if (!oled_initialized) {
551 return oled_active;
552 }
553
554#if OLED_TIMEOUT > 0
555 oled_timeout = timer_read32() + OLED_TIMEOUT;
556#endif
557
558 static const uint8_t PROGMEM display_on[] =
559#ifdef OLED_FADE_OUT
560 {I2C_CMD, FADE_BLINK, 0x00};
561#else
562 {I2C_CMD, DISPLAY_ON};
563#endif
564
565 if (!oled_active) {
566 if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
567 print("oled_on cmd failed\n");
568 return oled_active;
569 }
570 oled_active = true;
571 }
572 return oled_active;
573}
574
575bool oled_off(void) {
576 if (!oled_initialized) {
577 return !oled_active;
578 }
579
580 static const uint8_t PROGMEM display_off[] =
581#ifdef OLED_FADE_OUT
582 {I2C_CMD, FADE_BLINK, ENABLE_FADE | OLED_FADE_OUT_INTERVAL};
583#else
584 {I2C_CMD, DISPLAY_OFF};
585#endif
586
587 if (oled_active) {
588 if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
589 print("oled_off cmd failed\n");
590 return oled_active;
591 }
592 oled_active = false;
593 }
594 return !oled_active;
595}
596
597bool is_oled_on(void) { return oled_active; }
598
599uint8_t oled_set_brightness(uint8_t level) {
600 if (!oled_initialized) {
601 return oled_brightness;
602 }
603
604 uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level};
605 if (oled_brightness != level) {
606 if (I2C_TRANSMIT(set_contrast) != I2C_STATUS_SUCCESS) {
607 print("set_brightness cmd failed\n");
608 return oled_brightness;
609 }
610 oled_brightness = level;
611 }
612 return oled_brightness;
613}
614
615uint8_t oled_get_brightness(void) { return oled_brightness; }
616
617// Set the specific 8 lines rows of the screen to scroll.
618// 0 is the default for start, and 7 for end, which is the entire
619// height of the screen. For 128x32 screens, rows 4-7 are not used.
620void oled_scroll_set_area(uint8_t start_line, uint8_t end_line) {
621 oled_scroll_start = start_line;
622 oled_scroll_end = end_line;
623}
624
625void oled_scroll_set_speed(uint8_t speed) {
626 // Sets the speed for scrolling... does not take effect
627 // until scrolling is either started or restarted
628 // the ssd1306 supports 8 speeds
629 // FrameRate2 speed = 7
630 // FrameRate3 speed = 4
631 // FrameRate4 speed = 5
632 // FrameRate5 speed = 0
633 // FrameRate25 speed = 6
634 // FrameRate64 speed = 1
635 // FrameRate128 speed = 2
636 // FrameRate256 speed = 3
637 // for ease of use these are remaped here to be in order
638 static const uint8_t scroll_remap[8] = {7, 4, 5, 0, 6, 1, 2, 3};
639 oled_scroll_speed = scroll_remap[speed];
640}
641
642bool oled_scroll_right(void) {
643 if (!oled_initialized) {
644 return oled_scrolling;
645 }
646
647 // Dont enable scrolling if we need to update the display
648 // This prevents scrolling of bad data from starting the scroll too early after init
649 if (!oled_dirty && !oled_scrolling) {
650 uint8_t display_scroll_right[] = {I2C_CMD, SCROLL_RIGHT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
651 if (I2C_TRANSMIT(display_scroll_right) != I2C_STATUS_SUCCESS) {
652 print("oled_scroll_right cmd failed\n");
653 return oled_scrolling;
654 }
655 oled_scrolling = true;
656 }
657 return oled_scrolling;
658}
659
660bool oled_scroll_left(void) {
661 if (!oled_initialized) {
662 return oled_scrolling;
663 }
664
665 // Dont enable scrolling if we need to update the display
666 // This prevents scrolling of bad data from starting the scroll too early after init
667 if (!oled_dirty && !oled_scrolling) {
668 uint8_t display_scroll_left[] = {I2C_CMD, SCROLL_LEFT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
669 if (I2C_TRANSMIT(display_scroll_left) != I2C_STATUS_SUCCESS) {
670 print("oled_scroll_left cmd failed\n");
671 return oled_scrolling;
672 }
673 oled_scrolling = true;
674 }
675 return oled_scrolling;
676}
677
678bool oled_scroll_off(void) {
679 if (!oled_initialized) {
680 return !oled_scrolling;
681 }
682
683 if (oled_scrolling) {
684 static const uint8_t PROGMEM display_scroll_off[] = {I2C_CMD, DEACTIVATE_SCROLL};
685 if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
686 print("oled_scroll_off cmd failed\n");
687 return oled_scrolling;
688 }
689 oled_scrolling = false;
690 oled_dirty = OLED_ALL_BLOCKS_MASK;
691 }
692 return !oled_scrolling;
693}
694
695bool oled_invert(bool invert) {
696 if (!oled_initialized) {
697 return oled_inverted;
698 }
699
700 if (invert && !oled_inverted) {
701 static const uint8_t PROGMEM display_inverted[] = {I2C_CMD, INVERT_DISPLAY};
702 if (I2C_TRANSMIT_P(display_inverted) != I2C_STATUS_SUCCESS) {
703 print("oled_invert cmd failed\n");
704 return oled_inverted;
705 }
706 oled_inverted = true;
707 } else if (!invert && oled_inverted) {
708 static const uint8_t PROGMEM display_normal[] = {I2C_CMD, NORMAL_DISPLAY};
709 if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
710 print("oled_invert cmd failed\n");
711 return oled_inverted;
712 }
713 oled_inverted = false;
714 }
715
716 return oled_inverted;
717}
718
719uint8_t oled_max_chars(void) {
720 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
721 return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
722 }
723 return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
724}
725
726uint8_t oled_max_lines(void) {
727 if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
728 return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
729 }
730 return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
731}
732
733void oled_task(void) {
734 if (!oled_initialized) {
735 return;
736 }
737
738#if OLED_UPDATE_INTERVAL > 0
739 if (timer_elapsed(oled_update_timeout) >= OLED_UPDATE_INTERVAL) {
740 oled_update_timeout = timer_read();
741 oled_set_cursor(0, 0);
742 oled_task_user();
743 }
744#else
745 oled_set_cursor(0, 0);
746 oled_task_user();
747#endif
748
749#if OLED_SCROLL_TIMEOUT > 0
750 if (oled_dirty && oled_scrolling) {
751 oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
752 oled_scroll_off();
753 }
754#endif
755
756 // Smart render system, no need to check for dirty
757 oled_render();
758
759 // Display timeout check
760#if OLED_TIMEOUT > 0
761 if (oled_active && timer_expired32(timer_read32(), oled_timeout)) {
762 oled_off();
763 }
764#endif
765
766#if OLED_SCROLL_TIMEOUT > 0
767 if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) {
768# ifdef OLED_SCROLL_TIMEOUT_RIGHT
769 oled_scroll_right();
770# else
771 oled_scroll_left();
772# endif
773 }
774#endif
775}
776
777__attribute__((weak)) void oled_task_user(void) {}