oled_driver.c 17 KB

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