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.

860 lines
27 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. #include "keyboard.h"
  22. // for SH1107: https://www.displayfuture.com/Display/datasheet/controller/SH1107.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 INVERT_DISPLAY 0xA7
  29. #define DISPLAY_ON 0xAF
  30. #define DISPLAY_OFF 0xAE
  31. #define NOP 0xE3
  32. // Scrolling Commands
  33. #define ACTIVATE_SCROLL 0x2F
  34. #define DEACTIVATE_SCROLL 0x2E
  35. #define SCROLL_RIGHT 0x26
  36. #define SCROLL_LEFT 0x27
  37. #define SCROLL_RIGHT_UP 0x29
  38. #define SCROLL_LEFT_UP 0x2A
  39. // Addressing Setting Commands
  40. #define MEMORY_MODE 0x20
  41. #define COLUMN_ADDR 0x21
  42. #define PAGE_ADDR 0x22
  43. #define PAM_SETCOLUMN_LSB 0x00
  44. #define PAM_SETCOLUMN_MSB 0x10
  45. #define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
  46. // Hardware Configuration Commands
  47. #define DISPLAY_START_LINE 0x40
  48. #define SEGMENT_REMAP 0xA0
  49. #define SEGMENT_REMAP_INV 0xA1
  50. #define MULTIPLEX_RATIO 0xA8
  51. #define COM_SCAN_INC 0xC0
  52. #define COM_SCAN_DEC 0xC8
  53. #define DISPLAY_OFFSET 0xD3
  54. #define COM_PINS 0xDA
  55. #define COM_PINS_SEQ 0x02
  56. #define COM_PINS_ALT 0x12
  57. #define COM_PINS_SEQ_LR 0x22
  58. #define COM_PINS_ALT_LR 0x32
  59. // Timing & Driving Commands
  60. #define DISPLAY_CLOCK 0xD5
  61. #define PRE_CHARGE_PERIOD 0xD9
  62. #define VCOM_DETECT 0xDB
  63. // Advance Graphic Commands
  64. #define FADE_BLINK 0x23
  65. #define ENABLE_FADE 0x20
  66. #define ENABLE_BLINK 0x30
  67. // Charge Pump Commands
  68. #define CHARGE_PUMP 0x8D
  69. // Commands specific to the SH1107 chip
  70. #define SH1107_DISPLAY_START_LINE 0xDC
  71. #define SH1107_MEMORY_MODE_PAGE 0x20
  72. #define SH1107_MEMORY_MODE_VERTICAL 0x21
  73. // Misc defines
  74. #ifndef OLED_BLOCK_COUNT
  75. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8)
  76. #endif
  77. #ifndef OLED_BLOCK_SIZE
  78. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
  79. #endif
  80. #define OLED_ALL_BLOCKS_MASK (((((OLED_BLOCK_TYPE)1 << (OLED_BLOCK_COUNT - 1)) - 1) << 1) | 1)
  81. #ifndef OLED_COM_PIN_COUNT
  82. # define OLED_COM_PIN_COUNT 128
  83. #endif
  84. #ifndef OLED_COM_PIN_OFFSET
  85. # define OLED_COM_PIN_OFFSET 0
  86. #endif
  87. // i2c defines
  88. #define I2C_CMD 0x00
  89. #define I2C_DATA 0x40
  90. #if defined(__AVR__)
  91. # define I2C_TRANSMIT_P(data) i2c_transmit_P((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT)
  92. #else // defined(__AVR__)
  93. # define I2C_TRANSMIT_P(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT)
  94. #endif // defined(__AVR__)
  95. #define I2C_TRANSMIT(data) i2c_transmit((OLED_DISPLAY_ADDRESS << 1), &data[0], sizeof(data), OLED_I2C_TIMEOUT)
  96. #define I2C_WRITE_REG(mode, data, size) i2c_writeReg((OLED_DISPLAY_ADDRESS << 1), mode, data, size, OLED_I2C_TIMEOUT)
  97. #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
  98. // Display buffer's is the same as the OLED memory layout
  99. // this is so we don't end up with rounding errors with
  100. // parts of the display unusable or don't get cleared correctly
  101. // and also allows for drawing & inverting
  102. uint8_t oled_buffer[OLED_MATRIX_SIZE];
  103. uint8_t *oled_cursor;
  104. OLED_BLOCK_TYPE oled_dirty = 0;
  105. bool oled_initialized = false;
  106. bool oled_active = false;
  107. bool oled_scrolling = false;
  108. bool oled_inverted = false;
  109. uint8_t oled_brightness = OLED_BRIGHTNESS;
  110. oled_rotation_t oled_rotation = 0;
  111. uint8_t oled_rotation_width = 0;
  112. uint8_t oled_scroll_speed = 0; // this holds the speed after being remapped to ssd1306 internal values
  113. uint8_t oled_scroll_start = 0;
  114. uint8_t oled_scroll_end = 7;
  115. #if OLED_TIMEOUT > 0
  116. uint32_t oled_timeout;
  117. #endif
  118. #if OLED_SCROLL_TIMEOUT > 0
  119. uint32_t oled_scroll_timeout;
  120. #endif
  121. #if OLED_UPDATE_INTERVAL > 0
  122. uint16_t oled_update_timeout;
  123. #endif
  124. // Internal variables to reduce math instructions
  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. for (uint16_t i = 0; i < length && status >= 0; i++) {
  131. status = i2c_write(pgm_read_byte((const char *)data++), timeout);
  132. if (status) break;
  133. }
  134. i2c_stop();
  135. return status;
  136. }
  137. #endif
  138. // Flips the rendering bits for a character at the current cursor position
  139. static void InvertCharacter(uint8_t *cursor) {
  140. const uint8_t *end = cursor + OLED_FONT_WIDTH;
  141. while (cursor < end) {
  142. *cursor = ~(*cursor);
  143. cursor++;
  144. }
  145. }
  146. bool oled_init(oled_rotation_t rotation) {
  147. #if defined(USE_I2C) && defined(SPLIT_KEYBOARD)
  148. if (!is_keyboard_master()) {
  149. return true;
  150. }
  151. #endif
  152. oled_rotation = oled_init_user(oled_init_kb(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. static const uint8_t PROGMEM display_setup1[] = {
  160. I2C_CMD,
  161. DISPLAY_OFF,
  162. DISPLAY_CLOCK,
  163. 0x80,
  164. MULTIPLEX_RATIO,
  165. OLED_DISPLAY_WIDTH - 1,
  166. SH1107_DISPLAY_START_LINE,
  167. 0x00,
  168. CHARGE_PUMP,
  169. 0x14,
  170. SH1107_MEMORY_MODE_PAGE,
  171. };
  172. if (I2C_TRANSMIT_P(display_setup1) != I2C_STATUS_SUCCESS) {
  173. print("oled_init cmd set 1 failed\n");
  174. return false;
  175. }
  176. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
  177. static const uint8_t PROGMEM display_normal[] = {
  178. I2C_CMD,
  179. SEGMENT_REMAP_INV,
  180. COM_SCAN_DEC,
  181. DISPLAY_OFFSET,
  182. OLED_COM_PIN_OFFSET,
  183. };
  184. if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
  185. print("oled_init cmd normal rotation failed\n");
  186. return false;
  187. }
  188. } else {
  189. static const uint8_t PROGMEM display_flipped[] = {
  190. I2C_CMD,
  191. SEGMENT_REMAP,
  192. COM_SCAN_INC,
  193. DISPLAY_OFFSET,
  194. (OLED_COM_PIN_COUNT - OLED_COM_PIN_OFFSET) % OLED_COM_PIN_COUNT,
  195. };
  196. if (I2C_TRANSMIT_P(display_flipped) != I2C_STATUS_SUCCESS) {
  197. print("display_flipped failed\n");
  198. return false;
  199. }
  200. }
  201. static const uint8_t PROGMEM display_setup2[] = {
  202. I2C_CMD, COM_PINS,
  203. OLED_COM_PINS,
  204. CONTRAST, OLED_BRIGHTNESS,
  205. PRE_CHARGE_PERIOD, 0x22,
  206. VCOM_DETECT, 0x35,
  207. DISPLAY_ALL_ON_RESUME,
  208. NORMAL_DISPLAY,
  209. DEACTIVATE_SCROLL,
  210. DISPLAY_ON
  211. };
  212. if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
  213. print("display_setup2 failed\n");
  214. return false;
  215. }
  216. #if OLED_TIMEOUT > 0
  217. oled_timeout = timer_read32() + OLED_TIMEOUT;
  218. #endif
  219. #if OLED_SCROLL_TIMEOUT > 0
  220. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  221. #endif
  222. oled_clear();
  223. oled_initialized = true;
  224. oled_active = true;
  225. oled_scrolling = false;
  226. return true;
  227. }
  228. __attribute__((weak)) oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
  229. return rotation;
  230. }
  231. __attribute__((weak)) oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  232. return rotation;
  233. }
  234. void oled_clear(void) {
  235. memset(oled_buffer, 0, sizeof(oled_buffer));
  236. oled_cursor = &oled_buffer[0];
  237. oled_dirty = OLED_ALL_BLOCKS_MASK;
  238. }
  239. static void calc_bounds(uint8_t update_start, uint8_t *cmd_array) {
  240. // Calculate commands to set memory addressing bounds.
  241. uint8_t start_page = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
  242. uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
  243. // Commands for Page Addressing Mode. Sets starting page and column; has no end bound.
  244. // Column value must be split into high and low nybble and sent as two commands.
  245. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  246. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  247. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  248. }
  249. static void calc_bounds_90(uint8_t update_start, uint8_t *cmd_array) {
  250. // Block numbering starts from the bottom left corner, going up and then to
  251. // the right. The controller needs the page and column numbers for the top
  252. // left and bottom right corners of that block.
  253. // Total number of pages across the screen height.
  254. const uint8_t height_in_pages = OLED_DISPLAY_HEIGHT / 8;
  255. // Difference of starting page numbers for adjacent blocks; may be 0 if
  256. // blocks are large enough to occupy one or more whole 8px columns.
  257. const uint8_t page_inc_per_block = OLED_BLOCK_SIZE % OLED_DISPLAY_HEIGHT / 8;
  258. // Top page number for a block which is at the bottom edge of the screen.
  259. const uint8_t bottom_block_top_page = (height_in_pages - page_inc_per_block) % height_in_pages;
  260. // Only the Page Addressing Mode is supported
  261. uint8_t start_page = bottom_block_top_page - (OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT / 8);
  262. uint8_t start_column = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
  263. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  264. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  265. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  266. }
  267. uint8_t crot(uint8_t a, int8_t n) {
  268. const uint8_t mask = 0x7;
  269. n &= mask;
  270. return a << n | a >> (-n & mask);
  271. }
  272. static void rotate_90(const uint8_t *src, uint8_t *dest) {
  273. for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
  274. uint8_t selector = (1 << i);
  275. for (uint8_t j = 0; j < 8; ++j) {
  276. dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
  277. }
  278. }
  279. }
  280. void oled_render(void) {
  281. // Do we have work to do?
  282. oled_dirty &= OLED_ALL_BLOCKS_MASK;
  283. if (!oled_dirty || !oled_initialized || oled_scrolling) {
  284. return;
  285. }
  286. // Turn on display if it is off
  287. oled_on();
  288. uint8_t update_start = 0;
  289. uint8_t num_processed = 0;
  290. while (oled_dirty && num_processed++ < OLED_UPDATE_PROCESS_LIMIT) { // render all dirty blocks (up to the configured limit)
  291. // Find next dirty block
  292. while (!(oled_dirty & ((OLED_BLOCK_TYPE)1 << update_start))) {
  293. ++update_start;
  294. }
  295. // Set column & page position
  296. static uint8_t display_start[] = {I2C_CMD, PAM_PAGE_ADDR, PAM_SETCOLUMN_LSB, PAM_SETCOLUMN_MSB};
  297. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  298. calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  299. } else {
  300. calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  301. }
  302. // Send column & page position
  303. if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
  304. print("oled_render offset command failed\n");
  305. return;
  306. }
  307. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  308. // Send render data chunk as is
  309. if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
  310. print("oled_render data failed\n");
  311. return;
  312. }
  313. } else {
  314. // Rotate the render chunks
  315. const static uint8_t source_map[] = OLED_SOURCE_MAP;
  316. const static uint8_t target_map[] = OLED_TARGET_MAP;
  317. static uint8_t temp_buffer[OLED_BLOCK_SIZE];
  318. memset(temp_buffer, 0, sizeof(temp_buffer));
  319. for (uint8_t i = 0; i < sizeof(source_map); ++i) {
  320. rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
  321. }
  322. // For SH1106 or SH1107 the data chunk must be split into separate pieces for each page
  323. const uint8_t columns_in_block = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8;
  324. const uint8_t num_pages = OLED_BLOCK_SIZE / columns_in_block;
  325. for (uint8_t i = 0; i < num_pages; ++i) {
  326. // Send column & page position for all pages except the first one
  327. if (i > 0) {
  328. display_start[1]++;
  329. if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
  330. print("oled_render offset command failed\n");
  331. return;
  332. }
  333. }
  334. // Send data for the page
  335. if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[columns_in_block * i], columns_in_block) != I2C_STATUS_SUCCESS) {
  336. print("oled_render90 data failed\n");
  337. return;
  338. }
  339. }
  340. }
  341. // Clear dirty flag
  342. oled_dirty &= ~((OLED_BLOCK_TYPE)1 << update_start);
  343. }
  344. }
  345. void oled_set_cursor(uint8_t col, uint8_t line) {
  346. uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
  347. // Out of bounds?
  348. if (index >= OLED_MATRIX_SIZE) {
  349. index = 0;
  350. }
  351. oled_cursor = &oled_buffer[index];
  352. }
  353. void oled_advance_page(bool clearPageRemainder) {
  354. uint16_t index = oled_cursor - &oled_buffer[0];
  355. uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
  356. if (clearPageRemainder) {
  357. // Remaining Char count
  358. remaining = remaining / OLED_FONT_WIDTH;
  359. // Write empty character until next line
  360. while (remaining--)
  361. oled_write_char(' ', false);
  362. } else {
  363. // Next page index out of bounds?
  364. if (index + remaining >= OLED_MATRIX_SIZE) {
  365. index = 0;
  366. remaining = 0;
  367. }
  368. oled_cursor = &oled_buffer[index + remaining];
  369. }
  370. }
  371. void oled_advance_char(void) {
  372. uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
  373. uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
  374. // Do we have enough space on the current line for the next character
  375. if (remainingSpace < OLED_FONT_WIDTH) {
  376. nextIndex += remainingSpace;
  377. }
  378. // Did we go out of bounds
  379. if (nextIndex >= OLED_MATRIX_SIZE) {
  380. nextIndex = 0;
  381. }
  382. // Update cursor position
  383. oled_cursor = &oled_buffer[nextIndex];
  384. }
  385. // Main handler that writes character data to the display buffer
  386. void oled_write_char(const char data, bool invert) {
  387. // Advance to the next line if newline
  388. if (data == '\n') {
  389. // Old source wrote ' ' until end of line...
  390. oled_advance_page(true);
  391. return;
  392. }
  393. if (data == '\r') {
  394. oled_advance_page(false);
  395. return;
  396. }
  397. // copy the current render buffer to check for dirty after
  398. static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
  399. memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
  400. _Static_assert(sizeof(font) >= ((OLED_FONT_END + 1 - OLED_FONT_START) * OLED_FONT_WIDTH), "OLED_FONT_END references outside array");
  401. // set the reder buffer data
  402. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  403. if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
  404. memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
  405. } else {
  406. const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
  407. memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
  408. }
  409. // Invert if needed
  410. if (invert) {
  411. InvertCharacter(oled_cursor);
  412. }
  413. // Dirty check
  414. if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
  415. uint16_t index = oled_cursor - &oled_buffer[0];
  416. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  417. // Edgecase check if the written data spans the 2 chunks
  418. oled_dirty |= ((OLED_BLOCK_TYPE)1 << ((index + OLED_FONT_WIDTH - 1) / OLED_BLOCK_SIZE));
  419. }
  420. // Finally move to the next char
  421. oled_advance_char();
  422. }
  423. void oled_write(const char *data, bool invert) {
  424. const char *end = data + strlen(data);
  425. while (data < end) {
  426. oled_write_char(*data, invert);
  427. data++;
  428. }
  429. }
  430. void oled_write_ln(const char *data, bool invert) {
  431. oled_write(data, invert);
  432. oled_advance_page(true);
  433. }
  434. void oled_pan(bool left) {
  435. uint16_t i = 0;
  436. for (uint16_t y = 0; y < OLED_DISPLAY_HEIGHT / 8; y++) {
  437. if (left) {
  438. for (uint16_t x = 0; x < OLED_DISPLAY_WIDTH - 1; x++) {
  439. i = y * OLED_DISPLAY_WIDTH + x;
  440. oled_buffer[i] = oled_buffer[i + 1];
  441. }
  442. } else {
  443. for (uint16_t x = OLED_DISPLAY_WIDTH - 1; x > 0; x--) {
  444. i = y * OLED_DISPLAY_WIDTH + x;
  445. oled_buffer[i] = oled_buffer[i - 1];
  446. }
  447. }
  448. }
  449. oled_dirty = OLED_ALL_BLOCKS_MASK;
  450. }
  451. void oled_pan_section(bool left, uint16_t y_start, uint16_t y_end, uint16_t x_start, uint16_t x_end) {
  452. uint16_t i = 0;
  453. for (uint16_t y = y_start; y < y_end; y++) {
  454. if (left) {
  455. for (uint16_t x = x_start; x < x_end - 1; x++) {
  456. i = y * OLED_DISPLAY_WIDTH + x;
  457. oled_buffer[i] = oled_buffer[i + 1];
  458. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  459. }
  460. } else {
  461. for (uint16_t x = x_end - 1; x > 0; x--) {
  462. i = y * OLED_DISPLAY_WIDTH + x;
  463. oled_buffer[i] = oled_buffer[i - 1];
  464. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  465. }
  466. }
  467. }
  468. }
  469. oled_buffer_reader_t oled_read_raw(uint16_t start_index) {
  470. if (start_index > OLED_MATRIX_SIZE) start_index = OLED_MATRIX_SIZE;
  471. oled_buffer_reader_t ret_reader;
  472. ret_reader.current_element = &oled_buffer[start_index];
  473. ret_reader.remaining_element_count = OLED_MATRIX_SIZE - start_index;
  474. return ret_reader;
  475. }
  476. void oled_write_raw_byte(const char data, uint16_t index) {
  477. if (index > OLED_MATRIX_SIZE) index = OLED_MATRIX_SIZE;
  478. if (oled_buffer[index] == data) return;
  479. oled_buffer[index] = data;
  480. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  481. }
  482. void oled_write_raw(const char *data, uint16_t size) {
  483. uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
  484. if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
  485. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  486. uint8_t c = *data++;
  487. if (oled_buffer[i] == c) continue;
  488. oled_buffer[i] = c;
  489. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  490. }
  491. }
  492. void oled_write_pixel(uint8_t x, uint8_t y, bool on) {
  493. if (x >= oled_rotation_width) {
  494. return;
  495. }
  496. uint16_t index = x + (y / 8) * oled_rotation_width;
  497. if (index >= OLED_MATRIX_SIZE) {
  498. return;
  499. }
  500. uint8_t data = oled_buffer[index];
  501. if (on) {
  502. data |= (1 << (y % 8));
  503. } else {
  504. data &= ~(1 << (y % 8));
  505. }
  506. if (oled_buffer[index] != data) {
  507. oled_buffer[index] = data;
  508. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  509. }
  510. }
  511. #if defined(__AVR__)
  512. void oled_write_P(const char *data, bool invert) {
  513. uint8_t c = pgm_read_byte(data);
  514. while (c != 0) {
  515. oled_write_char(c, invert);
  516. c = pgm_read_byte(++data);
  517. }
  518. }
  519. void oled_write_ln_P(const char *data, bool invert) {
  520. oled_write_P(data, invert);
  521. oled_advance_page(true);
  522. }
  523. void oled_write_raw_P(const char *data, uint16_t size) {
  524. uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
  525. if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
  526. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  527. uint8_t c = pgm_read_byte(data++);
  528. if (oled_buffer[i] == c) continue;
  529. oled_buffer[i] = c;
  530. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  531. }
  532. }
  533. #endif // defined(__AVR__)
  534. bool oled_on(void) {
  535. if (!oled_initialized) {
  536. return oled_active;
  537. }
  538. #if OLED_TIMEOUT > 0
  539. oled_timeout = timer_read32() + OLED_TIMEOUT;
  540. #endif
  541. static const uint8_t PROGMEM display_on[] =
  542. #ifdef OLED_FADE_OUT
  543. {I2C_CMD, FADE_BLINK, 0x00};
  544. #else
  545. {I2C_CMD, DISPLAY_ON};
  546. #endif
  547. if (!oled_active) {
  548. if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
  549. print("oled_on cmd failed\n");
  550. return oled_active;
  551. }
  552. oled_active = true;
  553. }
  554. return oled_active;
  555. }
  556. bool oled_off(void) {
  557. if (!oled_initialized) {
  558. return !oled_active;
  559. }
  560. static const uint8_t PROGMEM display_off[] =
  561. #ifdef OLED_FADE_OUT
  562. {I2C_CMD, FADE_BLINK, ENABLE_FADE | OLED_FADE_OUT_INTERVAL};
  563. #else
  564. {I2C_CMD, DISPLAY_OFF};
  565. #endif
  566. if (oled_active) {
  567. if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
  568. print("oled_off cmd failed\n");
  569. return oled_active;
  570. }
  571. oled_active = false;
  572. }
  573. return !oled_active;
  574. }
  575. bool is_oled_on(void) {
  576. return oled_active;
  577. }
  578. uint8_t oled_set_brightness(uint8_t level) {
  579. if (!oled_initialized) {
  580. return oled_brightness;
  581. }
  582. uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level};
  583. if (oled_brightness != level) {
  584. if (I2C_TRANSMIT(set_contrast) != I2C_STATUS_SUCCESS) {
  585. print("set_brightness cmd failed\n");
  586. return oled_brightness;
  587. }
  588. oled_brightness = level;
  589. }
  590. return oled_brightness;
  591. }
  592. uint8_t oled_get_brightness(void) {
  593. return oled_brightness;
  594. }
  595. // Set the specific 8 lines rows of the screen to scroll.
  596. // 0 is the default for start, and 7 for end, which is the entire
  597. // height of the screen. For 128x32 screens, rows 4-7 are not used.
  598. void oled_scroll_set_area(uint8_t start_line, uint8_t end_line) {
  599. oled_scroll_start = start_line;
  600. oled_scroll_end = end_line;
  601. }
  602. void oled_scroll_set_speed(uint8_t speed) {
  603. // Sets the speed for scrolling... does not take effect
  604. // until scrolling is either started or restarted
  605. // the ssd1306 supports 8 speeds
  606. // FrameRate2 speed = 7
  607. // FrameRate3 speed = 4
  608. // FrameRate4 speed = 5
  609. // FrameRate5 speed = 0
  610. // FrameRate25 speed = 6
  611. // FrameRate64 speed = 1
  612. // FrameRate128 speed = 2
  613. // FrameRate256 speed = 3
  614. // for ease of use these are remaped here to be in order
  615. static const uint8_t scroll_remap[8] = {7, 4, 5, 0, 6, 1, 2, 3};
  616. oled_scroll_speed = scroll_remap[speed];
  617. }
  618. bool oled_scroll_right(void) {
  619. if (!oled_initialized) {
  620. return oled_scrolling;
  621. }
  622. // Dont enable scrolling if we need to update the display
  623. // This prevents scrolling of bad data from starting the scroll too early after init
  624. if (!oled_dirty && !oled_scrolling) {
  625. uint8_t display_scroll_right[] = {I2C_CMD, SCROLL_RIGHT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
  626. if (I2C_TRANSMIT(display_scroll_right) != I2C_STATUS_SUCCESS) {
  627. print("oled_scroll_right cmd failed\n");
  628. return oled_scrolling;
  629. }
  630. oled_scrolling = true;
  631. }
  632. return oled_scrolling;
  633. }
  634. bool oled_scroll_left(void) {
  635. if (!oled_initialized) {
  636. return oled_scrolling;
  637. }
  638. // Dont enable scrolling if we need to update the display
  639. // This prevents scrolling of bad data from starting the scroll too early after init
  640. if (!oled_dirty && !oled_scrolling) {
  641. uint8_t display_scroll_left[] = {I2C_CMD, SCROLL_LEFT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
  642. if (I2C_TRANSMIT(display_scroll_left) != I2C_STATUS_SUCCESS) {
  643. print("oled_scroll_left cmd failed\n");
  644. return oled_scrolling;
  645. }
  646. oled_scrolling = true;
  647. }
  648. return oled_scrolling;
  649. }
  650. bool oled_scroll_off(void) {
  651. if (!oled_initialized) {
  652. return !oled_scrolling;
  653. }
  654. if (oled_scrolling) {
  655. static const uint8_t PROGMEM display_scroll_off[] = {I2C_CMD, DEACTIVATE_SCROLL};
  656. if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
  657. print("oled_scroll_off cmd failed\n");
  658. return oled_scrolling;
  659. }
  660. oled_scrolling = false;
  661. oled_dirty = OLED_ALL_BLOCKS_MASK;
  662. }
  663. return !oled_scrolling;
  664. }
  665. bool is_oled_scrolling(void) {
  666. return oled_scrolling;
  667. }
  668. bool oled_invert(bool invert) {
  669. if (!oled_initialized) {
  670. return oled_inverted;
  671. }
  672. if (invert && !oled_inverted) {
  673. static const uint8_t PROGMEM display_inverted[] = {I2C_CMD, INVERT_DISPLAY};
  674. if (I2C_TRANSMIT_P(display_inverted) != I2C_STATUS_SUCCESS) {
  675. print("oled_invert cmd failed\n");
  676. return oled_inverted;
  677. }
  678. oled_inverted = true;
  679. } else if (!invert && oled_inverted) {
  680. static const uint8_t PROGMEM display_normal[] = {I2C_CMD, NORMAL_DISPLAY};
  681. if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
  682. print("oled_invert cmd failed\n");
  683. return oled_inverted;
  684. }
  685. oled_inverted = false;
  686. }
  687. return oled_inverted;
  688. }
  689. uint8_t oled_max_chars(void) {
  690. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  691. return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
  692. }
  693. return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
  694. }
  695. uint8_t oled_max_lines(void) {
  696. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  697. return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
  698. }
  699. return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
  700. }
  701. void oled_task(void) {
  702. if (!oled_initialized) {
  703. return;
  704. }
  705. #if OLED_UPDATE_INTERVAL > 0
  706. if (timer_elapsed(oled_update_timeout) >= OLED_UPDATE_INTERVAL) {
  707. oled_update_timeout = timer_read();
  708. oled_set_cursor(0, 0);
  709. oled_task_kb();
  710. }
  711. #else
  712. oled_set_cursor(0, 0);
  713. oled_task_kbr();
  714. #endif
  715. #if OLED_SCROLL_TIMEOUT > 0
  716. if (oled_dirty && oled_scrolling) {
  717. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  718. oled_scroll_off();
  719. }
  720. #endif
  721. // Smart render system, no need to check for dirty
  722. oled_render();
  723. // Display timeout check
  724. #if OLED_TIMEOUT > 0
  725. if (oled_active && timer_expired32(timer_read32(), oled_timeout)) {
  726. oled_off();
  727. }
  728. #endif
  729. #if OLED_SCROLL_TIMEOUT > 0
  730. if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) {
  731. # ifdef OLED_SCROLL_TIMEOUT_RIGHT
  732. oled_scroll_right();
  733. # else
  734. oled_scroll_left();
  735. # endif
  736. }
  737. #endif
  738. }
  739. __attribute__((weak)) bool oled_task_kb(void) {
  740. return oled_task_user();
  741. }
  742. __attribute__((weak)) bool oled_task_user(void) {
  743. return true;
  744. }