oled_driver.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 OLED_TIMEOUT > 0
  102. uint32_t oled_timeout;
  103. #endif
  104. #if OLED_SCROLL_TIMEOUT > 0
  105. uint32_t oled_scroll_timeout;
  106. #endif
  107. // Internal variables to reduce math instructions
  108. #if defined(__AVR__)
  109. // identical to i2c_transmit, but for PROGMEM since all initialization is in PROGMEM arrays currently
  110. // probably should move this into i2c_master...
  111. static i2c_status_t i2c_transmit_P(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
  112. i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
  113. for (uint16_t i = 0; i < length && status >= 0; i++) {
  114. status = i2c_write(pgm_read_byte((const char*)data++), timeout);
  115. if (status) break;
  116. }
  117. i2c_stop();
  118. return status;
  119. }
  120. #endif
  121. // Flips the rendering bits for a character at the current cursor position
  122. static void InvertCharacter(uint8_t *cursor)
  123. {
  124. const uint8_t *end = cursor + OLED_FONT_WIDTH;
  125. while (cursor < end) {
  126. *cursor = ~(*cursor);
  127. cursor++;
  128. }
  129. }
  130. bool oled_init(uint8_t rotation) {
  131. oled_rotation = oled_init_user(rotation);
  132. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  133. oled_rotation_width = OLED_DISPLAY_WIDTH;
  134. } else {
  135. oled_rotation_width = OLED_DISPLAY_HEIGHT;
  136. }
  137. i2c_init();
  138. static const uint8_t PROGMEM display_setup1[] = {
  139. I2C_CMD,
  140. DISPLAY_OFF,
  141. DISPLAY_CLOCK, 0x80,
  142. MULTIPLEX_RATIO, OLED_DISPLAY_HEIGHT - 1,
  143. DISPLAY_OFFSET, 0x00,
  144. DISPLAY_START_LINE | 0x00,
  145. CHARGE_PUMP, 0x14,
  146. #if (OLED_IC != OLED_IC_SH1106)
  147. // MEMORY_MODE is unsupported on SH1106 (Page Addressing only)
  148. MEMORY_MODE, 0x00, // Horizontal addressing mode
  149. #endif
  150. };
  151. if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) {
  152. print("oled_init cmd set 1 failed\n");
  153. return false;
  154. }
  155. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
  156. static const uint8_t PROGMEM display_normal[] = {
  157. I2C_CMD,
  158. SEGMENT_REMAP_INV,
  159. COM_SCAN_DEC };
  160. if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
  161. print("oled_init cmd normal rotation failed\n");
  162. return false;
  163. }
  164. } else {
  165. static const uint8_t PROGMEM display_flipped[] = {
  166. I2C_CMD,
  167. SEGMENT_REMAP,
  168. COM_SCAN_INC };
  169. if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) {
  170. print("display_flipped failed\n");
  171. return false;
  172. }
  173. }
  174. static const uint8_t PROGMEM display_setup2[] = {
  175. I2C_CMD,
  176. COM_PINS, OLED_COM_PINS,
  177. CONTRAST, 0x8F,
  178. PRE_CHARGE_PERIOD, 0xF1,
  179. VCOM_DETECT, 0x40,
  180. DISPLAY_ALL_ON_RESUME,
  181. NORMAL_DISPLAY,
  182. DEACTIVATE_SCROLL,
  183. DISPLAY_ON };
  184. if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
  185. print("display_setup2 failed\n");
  186. return false;
  187. }
  188. #if OLED_TIMEOUT > 0
  189. oled_timeout = timer_read32() + OLED_TIMEOUT;
  190. #endif
  191. #if OLED_SCROLL_TIMEOUT > 0
  192. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  193. #endif
  194. oled_clear();
  195. oled_initialized = true;
  196. oled_active = true;
  197. oled_scrolling = false;
  198. return true;
  199. }
  200. __attribute__((weak))
  201. oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  202. return rotation;
  203. }
  204. void oled_clear(void) {
  205. memset(oled_buffer, 0, sizeof(oled_buffer));
  206. oled_cursor = &oled_buffer[0];
  207. oled_dirty = -1; // -1 will be max value as long as display_dirty is unsigned type
  208. }
  209. static void calc_bounds(uint8_t update_start, uint8_t* cmd_array)
  210. {
  211. // Calculate commands to set memory addressing bounds.
  212. uint8_t start_page = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
  213. uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
  214. #if (OLED_IC == OLED_IC_SH1106)
  215. // Commands for Page Addressing Mode. Sets starting page and column; has no end bound.
  216. // Column value must be split into high and low nybble and sent as two commands.
  217. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  218. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  219. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  220. cmd_array[3] = NOP;
  221. cmd_array[4] = NOP;
  222. cmd_array[5] = NOP;
  223. #else
  224. // Commands for use in Horizontal Addressing mode.
  225. cmd_array[1] = start_column;
  226. cmd_array[4] = start_page;
  227. cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1];
  228. cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1;
  229. #endif
  230. }
  231. static void calc_bounds_90(uint8_t update_start, uint8_t* cmd_array)
  232. {
  233. cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
  234. cmd_array[4] = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT;
  235. cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1];;
  236. cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8;
  237. }
  238. uint8_t crot(uint8_t a, int8_t n)
  239. {
  240. const uint8_t mask = 0x7;
  241. n &= mask;
  242. return a << n | a >> (-n & mask);
  243. }
  244. static void rotate_90(const uint8_t* src, uint8_t* dest)
  245. {
  246. for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
  247. uint8_t selector = (1 << i);
  248. for (uint8_t j = 0; j < 8; ++j) {
  249. dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
  250. }
  251. }
  252. }
  253. void oled_render(void) {
  254. // Do we have work to do?
  255. if (!oled_dirty || oled_scrolling) {
  256. return;
  257. }
  258. // Find first dirty block
  259. uint8_t update_start = 0;
  260. while (!(oled_dirty & (1 << update_start))) { ++update_start; }
  261. // Set column & page position
  262. static uint8_t display_start[] = {
  263. I2C_CMD,
  264. COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1,
  265. PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1 };
  266. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  267. calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  268. } else {
  269. calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  270. }
  271. // Send column & page position
  272. if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
  273. print("oled_render offset command failed\n");
  274. return;
  275. }
  276. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  277. // Send render data chunk as is
  278. if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
  279. print("oled_render data failed\n");
  280. return;
  281. }
  282. } else {
  283. // Rotate the render chunks
  284. const static uint8_t source_map[] = OLED_SOURCE_MAP;
  285. const static uint8_t target_map[] = OLED_TARGET_MAP;
  286. static uint8_t temp_buffer[OLED_BLOCK_SIZE];
  287. memset(temp_buffer, 0, sizeof(temp_buffer));
  288. for(uint8_t i = 0; i < sizeof(source_map); ++i) {
  289. rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
  290. }
  291. // Send render data chunk after rotating
  292. if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[0], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
  293. print("oled_render90 data failed\n");
  294. return;
  295. }
  296. }
  297. // Turn on display if it is off
  298. oled_on();
  299. // Clear dirty flag
  300. oled_dirty &= ~(1 << update_start);
  301. }
  302. void oled_set_cursor(uint8_t col, uint8_t line) {
  303. uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
  304. // Out of bounds?
  305. if (index >= OLED_MATRIX_SIZE) {
  306. index = 0;
  307. }
  308. oled_cursor = &oled_buffer[index];
  309. }
  310. void oled_advance_page(bool clearPageRemainder) {
  311. uint16_t index = oled_cursor - &oled_buffer[0];
  312. uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
  313. if (clearPageRemainder) {
  314. // Remaining Char count
  315. remaining = remaining / OLED_FONT_WIDTH;
  316. // Write empty character until next line
  317. while (remaining--)
  318. oled_write_char(' ', false);
  319. } else {
  320. // Next page index out of bounds?
  321. if (index + remaining >= OLED_MATRIX_SIZE) {
  322. index = 0;
  323. remaining = 0;
  324. }
  325. oled_cursor = &oled_buffer[index + remaining];
  326. }
  327. }
  328. void oled_advance_char(void) {
  329. uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
  330. uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
  331. // Do we have enough space on the current line for the next character
  332. if (remainingSpace < OLED_FONT_WIDTH) {
  333. nextIndex += remainingSpace;
  334. }
  335. // Did we go out of bounds
  336. if (nextIndex >= OLED_MATRIX_SIZE) {
  337. nextIndex = 0;
  338. }
  339. // Update cursor position
  340. oled_cursor = &oled_buffer[nextIndex];
  341. }
  342. // Main handler that writes character data to the display buffer
  343. void oled_write_char(const char data, bool invert) {
  344. // Advance to the next line if newline
  345. if (data == '\n') {
  346. // Old source wrote ' ' until end of line...
  347. oled_advance_page(true);
  348. return;
  349. }
  350. if (data == '\r') {
  351. oled_advance_page(false);
  352. return;
  353. }
  354. // copy the current render buffer to check for dirty after
  355. static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
  356. memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
  357. // set the reder buffer data
  358. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  359. if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
  360. memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
  361. } else {
  362. const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
  363. memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
  364. }
  365. // Invert if needed
  366. if (invert) {
  367. InvertCharacter(oled_cursor);
  368. }
  369. // Dirty check
  370. if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
  371. uint16_t index = oled_cursor - &oled_buffer[0];
  372. oled_dirty |= (1 << (index / OLED_BLOCK_SIZE));
  373. // Edgecase check if the written data spans the 2 chunks
  374. oled_dirty |= (1 << ((index + OLED_FONT_WIDTH) / OLED_BLOCK_SIZE));
  375. }
  376. // Finally move to the next char
  377. oled_advance_char();
  378. }
  379. void oled_write(const char *data, bool invert) {
  380. const char *end = data + strlen(data);
  381. while (data < end) {
  382. oled_write_char(*data, invert);
  383. data++;
  384. }
  385. }
  386. void oled_write_ln(const char *data, bool invert) {
  387. oled_write(data, invert);
  388. oled_advance_page(true);
  389. }
  390. #if defined(__AVR__)
  391. void oled_write_P(const char *data, bool invert) {
  392. uint8_t c = pgm_read_byte(data);
  393. while (c != 0) {
  394. oled_write_char(c, invert);
  395. c = pgm_read_byte(++data);
  396. }
  397. }
  398. void oled_write_ln_P(const char *data, bool invert) {
  399. oled_write_P(data, invert);
  400. oled_advance_page(true);
  401. }
  402. #endif // defined(__AVR__)
  403. bool oled_on(void) {
  404. #if OLED_TIMEOUT > 0
  405. oled_timeout = timer_read32() + OLED_TIMEOUT;
  406. #endif
  407. static const uint8_t PROGMEM display_on[] = { I2C_CMD, DISPLAY_ON };
  408. if (!oled_active) {
  409. if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
  410. print("oled_on cmd failed\n");
  411. return oled_active;
  412. }
  413. oled_active = true;
  414. }
  415. return oled_active;
  416. }
  417. bool oled_off(void) {
  418. static const uint8_t PROGMEM display_off[] = { I2C_CMD, DISPLAY_OFF };
  419. if (oled_active) {
  420. if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
  421. print("oled_off cmd failed\n");
  422. return oled_active;
  423. }
  424. oled_active = false;
  425. }
  426. return !oled_active;
  427. }
  428. bool oled_scroll_right(void) {
  429. // Dont enable scrolling if we need to update the display
  430. // This prevents scrolling of bad data from starting the scroll too early after init
  431. if (!oled_dirty && !oled_scrolling) {
  432. static const uint8_t PROGMEM display_scroll_right[] = {
  433. I2C_CMD, SCROLL_RIGHT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL };
  434. if (I2C_TRANSMIT_P(display_scroll_right) != I2C_STATUS_SUCCESS) {
  435. print("oled_scroll_right cmd failed\n");
  436. return oled_scrolling;
  437. }
  438. oled_scrolling = true;
  439. }
  440. return oled_scrolling;
  441. }
  442. bool oled_scroll_left(void) {
  443. // Dont enable scrolling if we need to update the display
  444. // This prevents scrolling of bad data from starting the scroll too early after init
  445. if (!oled_dirty && !oled_scrolling) {
  446. static const uint8_t PROGMEM display_scroll_left[] = {
  447. I2C_CMD, SCROLL_LEFT, 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, ACTIVATE_SCROLL };
  448. if (I2C_TRANSMIT_P(display_scroll_left) != I2C_STATUS_SUCCESS) {
  449. print("oled_scroll_left cmd failed\n");
  450. return oled_scrolling;
  451. }
  452. oled_scrolling = true;
  453. }
  454. return oled_scrolling;
  455. }
  456. bool oled_scroll_off(void) {
  457. if (oled_scrolling) {
  458. static const uint8_t PROGMEM display_scroll_off[] = { I2C_CMD, DEACTIVATE_SCROLL };
  459. if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
  460. print("oled_scroll_off cmd failed\n");
  461. return oled_scrolling;
  462. }
  463. oled_scrolling = false;
  464. oled_dirty = -1;
  465. }
  466. return !oled_scrolling;
  467. }
  468. uint8_t oled_max_chars(void) {
  469. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  470. return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
  471. }
  472. return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
  473. }
  474. uint8_t oled_max_lines(void) {
  475. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  476. return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
  477. }
  478. return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
  479. }
  480. void oled_task(void) {
  481. if (!oled_initialized) {
  482. return;
  483. }
  484. oled_set_cursor(0, 0);
  485. oled_task_user();
  486. #if OLED_SCROLL_TIMEOUT > 0
  487. if (oled_dirty && oled_scrolling) {
  488. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  489. oled_scroll_off();
  490. }
  491. #endif
  492. // Smart render system, no need to check for dirty
  493. oled_render();
  494. // Display timeout check
  495. #if OLED_TIMEOUT > 0
  496. if (oled_active && timer_expired32(timer_read32(), oled_timeout)) {
  497. oled_off();
  498. }
  499. #endif
  500. #if OLED_SCROLL_TIMEOUT > 0
  501. if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) {
  502. #ifdef OLED_SCROLL_TIMEOUT_RIGHT
  503. oled_scroll_right();
  504. #else
  505. oled_scroll_left();
  506. #endif
  507. }
  508. #endif
  509. }
  510. __attribute__((weak))
  511. void oled_task_user(void) {
  512. }