oled_driver.c 18 KB

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