You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

646 lines
21 KiB

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