qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

oled_driver.c (18209B)


      1 /*
      2 Copyright 2019 Ryan Caltabiano <https://github.com/XScorpion2>
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along 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 #if defined(__AVR__)
     26 #    include <avr/io.h>
     27 #    include <avr/pgmspace.h>
     28 #elif defined(ESP8266)
     29 #    include <pgmspace.h>
     30 #else  // defined(ESP8266)
     31 #    define PROGMEM
     32 #    define memcpy_P(des, src, len) memcpy(des, src, len)
     33 #endif  // defined(__AVR__)
     34 
     35 // Used commands from spec sheet: https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf
     36 // for SH1106: https://www.velleman.eu/downloads/29/infosheets/sh1106_datasheet.pdf
     37 
     38 // Fundamental Commands
     39 #define CONTRAST 0x81
     40 #define DISPLAY_ALL_ON 0xA5
     41 #define DISPLAY_ALL_ON_RESUME 0xA4
     42 #define NORMAL_DISPLAY 0xA6
     43 #define DISPLAY_ON 0xAF
     44 #define DISPLAY_OFF 0xAE
     45 #define NOP 0xE3
     46 
     47 // Scrolling Commands
     48 #define ACTIVATE_SCROLL 0x2F
     49 #define DEACTIVATE_SCROLL 0x2E
     50 #define SCROLL_RIGHT 0x26
     51 #define SCROLL_LEFT 0x27
     52 #define SCROLL_RIGHT_UP 0x29
     53 #define SCROLL_LEFT_UP 0x2A
     54 
     55 // Addressing Setting Commands
     56 #define MEMORY_MODE 0x20
     57 #define COLUMN_ADDR 0x21
     58 #define PAGE_ADDR 0x22
     59 #define PAM_SETCOLUMN_LSB 0x00
     60 #define PAM_SETCOLUMN_MSB 0x10
     61 #define PAM_PAGE_ADDR 0xB0  // 0xb0 -- 0xb7
     62 
     63 // Hardware Configuration Commands
     64 #define DISPLAY_START_LINE 0x40
     65 #define SEGMENT_REMAP 0xA0
     66 #define SEGMENT_REMAP_INV 0xA1
     67 #define MULTIPLEX_RATIO 0xA8
     68 #define COM_SCAN_INC 0xC0
     69 #define COM_SCAN_DEC 0xC8
     70 #define DISPLAY_OFFSET 0xD3
     71 #define COM_PINS 0xDA
     72 #define COM_PINS_SEQ 0x02
     73 #define COM_PINS_ALT 0x12
     74 #define COM_PINS_SEQ_LR 0x22
     75 #define COM_PINS_ALT_LR 0x32
     76 
     77 // Timing & Driving Commands
     78 #define DISPLAY_CLOCK 0xD5
     79 #define PRE_CHARGE_PERIOD 0xD9
     80 #define VCOM_DETECT 0xDB
     81 
     82 // Charge Pump Commands
     83 #define CHARGE_PUMP 0x8D
     84 
     85 // Misc defines
     86 #define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8)
     87 #define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
     88 
     89 // i2c defines
     90 #define I2C_CMD 0x00
     91 #define I2C_DATA 0x40
     92 #if defined(__AVR__)
     93 // already defined on ARM
     94 #    define I2C_TIMEOUT 100
     95 #    define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
     96 #else  // defined(__AVR__)
     97 #    define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
     98 #endif  // defined(__AVR__)
     99 #define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), I2C_TIMEOUT)
    100 #define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, I2C_TIMEOUT)
    101 
    102 #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
    103 
    104 // Display buffer's is the same as the OLED memory layout
    105 // this is so we don't end up with rounding errors with
    106 // parts of the display unusable or don't get cleared correctly
    107 // and also allows for drawing & inverting
    108 uint8_t         oled_buffer[OLED_MATRIX_SIZE];
    109 uint8_t *       oled_cursor;
    110 OLED_BLOCK_TYPE oled_dirty          = 0;
    111 bool            oled_initialized    = false;
    112 bool            oled_active         = false;
    113 bool            oled_scrolling      = false;
    114 uint8_t         oled_rotation       = 0;
    115 uint8_t         oled_rotation_width = 0;
    116 #if OLED_TIMEOUT > 0
    117 uint32_t oled_timeout;
    118 #endif
    119 #if OLED_SCROLL_TIMEOUT > 0
    120 uint32_t oled_scroll_timeout;
    121 #endif
    122 
    123 // Internal variables to reduce math instructions
    124 
    125 #if defined(__AVR__)
    126 // identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently
    127 // probably should move this into i2c_master...
    128 static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t *data, uint16_t length, uint16_t timeout) {
    129     i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
    130 
    131     for (uint16_t i = 0; i < length && status >= 0; i++) {
    132         status = i2c_write(pgm_read_byte((const char *)data++), timeout);
    133         if (status) break;
    134     }
    135 
    136     i2c_stop();
    137 
    138     return status;
    139 }
    140 #endif
    141 
    142 // Flips the rendering bits for a character at the current cursor position
    143 static void InvertCharacter(uint8_t *cursor) {
    144     const uint8_t *end = cursor + OLED_FONT_WIDTH;
    145     while (cursor < end) {
    146         *cursor = ~(*cursor);
    147         cursor++;
    148     }
    149 }
    150 
    151 bool oled_init(uint8_t rotation) {
    152     oled_rotation = oled_init_user(rotation);
    153     if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
    154         oled_rotation_width = OLED_DISPLAY_WIDTH;
    155     } else {
    156         oled_rotation_width = OLED_DISPLAY_HEIGHT;
    157     }
    158     i2c_init();
    159 
    160     static const uint8_t PROGMEM display_setup1[] = {
    161         I2C_CMD,
    162         DISPLAY_OFF,
    163         DISPLAY_CLOCK,
    164         0x80,
    165         MULTIPLEX_RATIO,
    166         OLED_DISPLAY_HEIGHT - 1,
    167         DISPLAY_OFFSET,
    168         0x00,
    169         DISPLAY_START_LINE | 0x00,
    170         CHARGE_PUMP,
    171         0x14,
    172 #if (OLED_IC != OLED_IC_SH1106)
    173         // MEMORY_MODE is unsupported on SH1106 (Page Addressing only)
    174         MEMORY_MODE,
    175         0x00,  // Horizontal addressing mode
    176 #endif
    177     };
    178     if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) {
    179         print("oled_init cmd set 1 failed\n");
    180         return false;
    181     }
    182 
    183     if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
    184         static const uint8_t PROGMEM display_normal[] = {I2C_CMD, SEGMENT_REMAP_INV, COM_SCAN_DEC};
    185         if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
    186             print("oled_init cmd normal rotation failed\n");
    187             return false;
    188         }
    189     } else {
    190         static const uint8_t PROGMEM display_flipped[] = {I2C_CMD, SEGMENT_REMAP, COM_SCAN_INC};
    191         if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) {
    192             print("display_flipped failed\n");
    193             return false;
    194         }
    195     }
    196 
    197     static const uint8_t PROGMEM display_setup2[] = {I2C_CMD, COM_PINS, OLED_COM_PINS, CONTRAST, 0x8F, PRE_CHARGE_PERIOD, 0xF1, VCOM_DETECT, 0x40, DISPLAY_ALL_ON_RESUME, NORMAL_DISPLAY, DEACTIVATE_SCROLL, DISPLAY_ON};
    198     if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
    199         print("display_setup2 failed\n");
    200         return false;
    201     }
    202 
    203 #if OLED_TIMEOUT > 0
    204     oled_timeout = timer_read32() + OLED_TIMEOUT;
    205 #endif
    206 #if OLED_SCROLL_TIMEOUT > 0
    207     oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
    208 #endif
    209 
    210     oled_clear();
    211     oled_initialized = true;
    212     oled_active      = true;
    213     oled_scrolling   = false;
    214     return true;
    215 }
    216 
    217 __attribute__((weak)) oled_rotation_t oled_init_user(oled_rotation_t rotation) { return rotation; }
    218 
    219 void oled_clear(void) {
    220     memset(oled_buffer, 0, sizeof(oled_buffer));
    221     oled_cursor = &oled_buffer[0];
    222     oled_dirty  = -1;  // -1 will be max value as long as display_dirty is unsigned type
    223 }
    224 
    225 static void calc_bounds(uint8_t update_start, uint8_t *cmd_array) {
    226     // Calculate commands to set memory addressing bounds.
    227     uint8_t start_page   = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
    228     uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
    229 #if (OLED_IC == OLED_IC_SH1106)
    230     // Commands for Page Addressing Mode. Sets starting page and column; has no end bound.
    231     // Column value must be split into high and low nybble and sent as two commands.
    232     cmd_array[0] = PAM_PAGE_ADDR | start_page;
    233     cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
    234     cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
    235     cmd_array[3] = NOP;
    236     cmd_array[4] = NOP;
    237     cmd_array[5] = NOP;
    238 #else
    239     // Commands for use in Horizontal Addressing mode.
    240     cmd_array[1] = start_column;
    241     cmd_array[4] = start_page;
    242     cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1];
    243     cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1;
    244 #endif
    245 }
    246 
    247 static void calc_bounds_90(uint8_t update_start, uint8_t *cmd_array) {
    248     cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
    249     cmd_array[4] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT;
    250     cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1];
    251     ;
    252     cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8;
    253 }
    254 
    255 uint8_t crot(uint8_t a, int8_t n) {
    256     const uint8_t mask = 0x7;
    257     n &= mask;
    258     return a << n | a >> (-n & mask);
    259 }
    260 
    261 static void rotate_90(const uint8_t *src, uint8_t *dest) {
    262     for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
    263         uint8_t selector = (1 << i);
    264         for (uint8_t j = 0; j < 8; ++j) {
    265             dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
    266         }
    267     }
    268 }
    269 
    270 void oled_render(void) {
    271     // Do we have work to do?
    272     if (!oled_dirty || oled_scrolling) {
    273         return;
    274     }
    275 
    276     // Find first dirty block
    277     uint8_t update_start = 0;
    278     while (!(oled_dirty & (1 << update_start))) {
    279         ++update_start;
    280     }
    281 
    282     // Set column & page position
    283     static uint8_t display_start[] = {I2C_CMD, COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1, PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1};
    284     if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
    285         calc_bounds(update_start, &display_start[1]);  // Offset from I2C_CMD byte at the start
    286     } else {
    287         calc_bounds_90(update_start, &display_start[1]);  // Offset from I2C_CMD byte at the start
    288     }
    289 
    290     // Send column & page position
    291     if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
    292         print("oled_render offset command failed\n");
    293         return;
    294     }
    295 
    296     if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
    297         // Send render data chunk as is
    298         if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
    299             print("oled_render data failed\n");
    300             return;
    301         }
    302     } else {
    303         // Rotate the render chunks
    304         const static uint8_t source_map[] = OLED_SOURCE_MAP;
    305         const static uint8_t target_map[] = OLED_TARGET_MAP;
    306 
    307         static uint8_t temp_buffer[OLED_BLOCK_SIZE];
    308         memset(temp_buffer, 0, sizeof(temp_buffer));
    309         for (uint8_t i = 0; i < sizeof(source_map); ++i) {
    310             rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
    311         }
    312 
    313         // Send render data chunk after rotating
    314         if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[0], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
    315             print("oled_render90 data failed\n");
    316             return;
    317         }
    318     }
    319 
    320     // Turn on display if it is off
    321     oled_on();
    322 
    323     // Clear dirty flag
    324     oled_dirty &= ~(1 << update_start);
    325 }
    326 
    327 void oled_set_cursor(uint8_t col, uint8_t line) {
    328     uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
    329 
    330     // Out of bounds?
    331     if (index >= OLED_MATRIX_SIZE) {
    332         index = 0;
    333     }
    334 
    335     oled_cursor = &oled_buffer[index];
    336 }
    337 
    338 void oled_advance_page(bool clearPageRemainder) {
    339     uint16_t index     = oled_cursor - &oled_buffer[0];
    340     uint8_t  remaining = oled_rotation_width - (index % oled_rotation_width);
    341 
    342     if (clearPageRemainder) {
    343         // Remaining Char count
    344         remaining = remaining / OLED_FONT_WIDTH;
    345 
    346         // Write empty character until next line
    347         while (remaining--) oled_write_char(' ', false);
    348     } else {
    349         // Next page index out of bounds?
    350         if (index + remaining >= OLED_MATRIX_SIZE) {
    351             index     = 0;
    352             remaining = 0;
    353         }
    354 
    355         oled_cursor = &oled_buffer[index + remaining];
    356     }
    357 }
    358 
    359 void oled_advance_char(void) {
    360     uint16_t nextIndex      = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
    361     uint8_t  remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
    362 
    363     // Do we have enough space on the current line for the next character
    364     if (remainingSpace < OLED_FONT_WIDTH) {
    365         nextIndex += remainingSpace;
    366     }
    367 
    368     // Did we go out of bounds
    369     if (nextIndex >= OLED_MATRIX_SIZE) {
    370         nextIndex = 0;
    371     }
    372 
    373     // Update cursor position
    374     oled_cursor = &oled_buffer[nextIndex];
    375 }
    376 
    377 // Main handler that writes character data to the display buffer
    378 void oled_write_char(const char data, bool invert) {
    379     // Advance to the next line if newline
    380     if (data == '\n') {
    381         // Old source wrote ' ' until end of line...
    382         oled_advance_page(true);
    383         return;
    384     }
    385 
    386     if (data == '\r') {
    387         oled_advance_page(false);
    388         return;
    389     }
    390 
    391     // copy the current render buffer to check for dirty after
    392     static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
    393     memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
    394 
    395     // set the reder buffer data
    396     uint8_t cast_data = (uint8_t)data;  // font based on unsigned type for index
    397     if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
    398         memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
    399     } else {
    400         const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
    401         memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
    402     }
    403 
    404     // Invert if needed
    405     if (invert) {
    406         InvertCharacter(oled_cursor);
    407     }
    408 
    409     // Dirty check
    410     if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
    411         uint16_t index = oled_cursor - &oled_buffer[0];
    412         oled_dirty |= (1 << (index / OLED_BLOCK_SIZE));
    413         // Edgecase check if the written data spans the 2 chunks
    414         oled_dirty |= (1 << ((index + OLED_FONT_WIDTH) / OLED_BLOCK_SIZE));
    415     }
    416 
    417     // Finally move to the next char
    418     oled_advance_char();
    419 }
    420 
    421 void oled_write(const char *data, bool invert) {
    422     const char *end = data + strlen(data);
    423     while (data < end) {
    424         oled_write_char(*data, invert);
    425         data++;
    426     }
    427 }
    428 
    429 void oled_write_ln(const char *data, bool invert) {
    430     oled_write(data, invert);
    431     oled_advance_page(true);
    432 }
    433 
    434 #if defined(__AVR__)
    435 void oled_write_P(const char *data, bool invert) {
    436     uint8_t c = pgm_read_byte(data);
    437     while (c != 0) {
    438         oled_write_char(c, invert);
    439         c = pgm_read_byte(++data);
    440     }
    441 }
    442 
    443 void oled_write_ln_P(const char *data, bool invert) {
    444     oled_write_P(data, invert);
    445     oled_advance_page(true);
    446 }
    447 #endif  // defined(__AVR__)
    448 
    449 bool oled_on(void) {
    450 #if OLED_TIMEOUT > 0
    451     oled_timeout = timer_read32() + OLED_TIMEOUT;
    452 #endif
    453 
    454     static const uint8_t PROGMEM display_on[] = {I2C_CMD, DISPLAY_ON};
    455     if (!oled_active) {
    456         if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
    457             print("oled_on cmd failed\n");
    458             return oled_active;
    459         }
    460         oled_active = true;
    461     }
    462     return oled_active;
    463 }
    464 
    465 bool oled_off(void) {
    466     static const uint8_t PROGMEM display_off[] = {I2C_CMD, DISPLAY_OFF};
    467     if (oled_active) {
    468         if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
    469             print("oled_off cmd failed\n");
    470             return oled_active;
    471         }
    472         oled_active = false;
    473     }
    474     return !oled_active;
    475 }
    476 
    477 bool oled_scroll_right(void) {
    478     // Dont enable scrolling if we need to update the display
    479     // This prevents scrolling of bad data from starting the scroll too early after init
    480     if (!oled_dirty && !oled_scrolling) {
    481         static const uint8_t PROGMEM display_scroll_right[] = {I2C_CMD, SCROLL_RIGHT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL};
    482         if (I2C_TRANSMIT_P(display_scroll_right) != I2C_STATUS_SUCCESS) {
    483             print("oled_scroll_right cmd failed\n");
    484             return oled_scrolling;
    485         }
    486         oled_scrolling = true;
    487     }
    488     return oled_scrolling;
    489 }
    490 
    491 bool oled_scroll_left(void) {
    492     // Dont enable scrolling if we need to update the display
    493     // This prevents scrolling of bad data from starting the scroll too early after init
    494     if (!oled_dirty && !oled_scrolling) {
    495         static const uint8_t PROGMEM display_scroll_left[] = {I2C_CMD, SCROLL_LEFT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL};
    496         if (I2C_TRANSMIT_P(display_scroll_left) != I2C_STATUS_SUCCESS) {
    497             print("oled_scroll_left cmd failed\n");
    498             return oled_scrolling;
    499         }
    500         oled_scrolling = true;
    501     }
    502     return oled_scrolling;
    503 }
    504 
    505 bool oled_scroll_off(void) {
    506     if (oled_scrolling) {
    507         static const uint8_t PROGMEM display_scroll_off[] = {I2C_CMD, DEACTIVATE_SCROLL};
    508         if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
    509             print("oled_scroll_off cmd failed\n");
    510             return oled_scrolling;
    511         }
    512         oled_scrolling = false;
    513         oled_dirty     = -1;
    514     }
    515     return !oled_scrolling;
    516 }
    517 
    518 uint8_t oled_max_chars(void) {
    519     if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
    520         return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
    521     }
    522     return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
    523 }
    524 
    525 uint8_t oled_max_lines(void) {
    526     if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
    527         return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
    528     }
    529     return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
    530 }
    531 
    532 void oled_task(void) {
    533     if (!oled_initialized) {
    534         return;
    535     }
    536 
    537     oled_set_cursor(0, 0);
    538 
    539     oled_task_user();
    540 
    541 #if OLED_SCROLL_TIMEOUT > 0
    542     if (oled_dirty && oled_scrolling) {
    543         oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
    544         oled_scroll_off();
    545     }
    546 #endif
    547 
    548     // Smart render system, no need to check for dirty
    549     oled_render();
    550 
    551     // Display timeout check
    552 #if OLED_TIMEOUT > 0
    553     if (oled_active && timer_expired32(timer_read32(), oled_timeout)) {
    554         oled_off();
    555     }
    556 #endif
    557 
    558 #if OLED_SCROLL_TIMEOUT > 0
    559     if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) {
    560 #    ifdef OLED_SCROLL_TIMEOUT_RIGHT
    561         oled_scroll_right();
    562 #    else
    563         oled_scroll_left();
    564 #    endif
    565     }
    566 #endif
    567 }
    568 
    569 __attribute__((weak)) void oled_task_user(void) {}