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.

847 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) { return rotation; }
  229. __attribute__((weak)) oled_rotation_t oled_init_user(oled_rotation_t rotation) { return rotation; }
  230. void oled_clear(void) {
  231. memset(oled_buffer, 0, sizeof(oled_buffer));
  232. oled_cursor = &oled_buffer[0];
  233. oled_dirty = OLED_ALL_BLOCKS_MASK;
  234. }
  235. static void calc_bounds(uint8_t update_start, uint8_t *cmd_array) {
  236. // Calculate commands to set memory addressing bounds.
  237. uint8_t start_page = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
  238. uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
  239. // Commands for Page Addressing Mode. Sets starting page and column; has no end bound.
  240. // Column value must be split into high and low nybble and sent as two commands.
  241. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  242. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  243. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  244. }
  245. static void calc_bounds_90(uint8_t update_start, uint8_t *cmd_array) {
  246. // Block numbering starts from the bottom left corner, going up and then to
  247. // the right. The controller needs the page and column numbers for the top
  248. // left and bottom right corners of that block.
  249. // Total number of pages across the screen height.
  250. const uint8_t height_in_pages = OLED_DISPLAY_HEIGHT / 8;
  251. // Difference of starting page numbers for adjacent blocks; may be 0 if
  252. // blocks are large enough to occupy one or more whole 8px columns.
  253. const uint8_t page_inc_per_block = OLED_BLOCK_SIZE % OLED_DISPLAY_HEIGHT / 8;
  254. // Top page number for a block which is at the bottom edge of the screen.
  255. const uint8_t bottom_block_top_page = (height_in_pages - page_inc_per_block) % height_in_pages;
  256. // Only the Page Addressing Mode is supported
  257. uint8_t start_page = bottom_block_top_page - (OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT / 8);
  258. uint8_t start_column = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
  259. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  260. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  261. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  262. }
  263. uint8_t crot(uint8_t a, int8_t n) {
  264. const uint8_t mask = 0x7;
  265. n &= mask;
  266. return a << n | a >> (-n & mask);
  267. }
  268. static void rotate_90(const uint8_t *src, uint8_t *dest) {
  269. for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
  270. uint8_t selector = (1 << i);
  271. for (uint8_t j = 0; j < 8; ++j) {
  272. dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
  273. }
  274. }
  275. }
  276. void oled_render(void) {
  277. if (!oled_initialized) {
  278. return;
  279. }
  280. // Do we have work to do?
  281. oled_dirty &= OLED_ALL_BLOCKS_MASK;
  282. if (!oled_dirty || oled_scrolling) {
  283. return;
  284. }
  285. // Find first dirty block
  286. uint8_t update_start = 0;
  287. while (!(oled_dirty & ((OLED_BLOCK_TYPE)1 << update_start))) {
  288. ++update_start;
  289. }
  290. // Set column & page position
  291. static uint8_t display_start[] = {I2C_CMD, PAM_PAGE_ADDR, PAM_SETCOLUMN_LSB, PAM_SETCOLUMN_MSB};
  292. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  293. calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  294. } else {
  295. calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  296. }
  297. // Send column & page position
  298. if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
  299. print("oled_render offset command failed\n");
  300. return;
  301. }
  302. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  303. // Send render data chunk as is
  304. if (I2C_WRITE_REG(I2C_DATA, &oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE) != I2C_STATUS_SUCCESS) {
  305. print("oled_render data failed\n");
  306. return;
  307. }
  308. } else {
  309. // Rotate the render chunks
  310. const static uint8_t source_map[] = OLED_SOURCE_MAP;
  311. const static uint8_t target_map[] = OLED_TARGET_MAP;
  312. static uint8_t temp_buffer[OLED_BLOCK_SIZE];
  313. memset(temp_buffer, 0, sizeof(temp_buffer));
  314. for (uint8_t i = 0; i < sizeof(source_map); ++i) {
  315. rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
  316. }
  317. // For SH1106 or SH1107 the data chunk must be split into separate pieces for each page
  318. const uint8_t columns_in_block = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8;
  319. const uint8_t num_pages = OLED_BLOCK_SIZE / columns_in_block;
  320. for (uint8_t i = 0; i < num_pages; ++i) {
  321. // Send column & page position for all pages except the first one
  322. if (i > 0) {
  323. display_start[1]++;
  324. if (I2C_TRANSMIT(display_start) != I2C_STATUS_SUCCESS) {
  325. print("oled_render offset command failed\n");
  326. return;
  327. }
  328. }
  329. // Send data for the page
  330. if (I2C_WRITE_REG(I2C_DATA, &temp_buffer[columns_in_block * i], columns_in_block) != I2C_STATUS_SUCCESS) {
  331. print("oled_render90 data failed\n");
  332. return;
  333. }
  334. }
  335. }
  336. // Turn on display if it is off
  337. oled_on();
  338. // Clear dirty flag
  339. oled_dirty &= ~((OLED_BLOCK_TYPE)1 << update_start);
  340. }
  341. void oled_set_cursor(uint8_t col, uint8_t line) {
  342. uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
  343. // Out of bounds?
  344. if (index >= OLED_MATRIX_SIZE) {
  345. index = 0;
  346. }
  347. oled_cursor = &oled_buffer[index];
  348. }
  349. void oled_advance_page(bool clearPageRemainder) {
  350. uint16_t index = oled_cursor - &oled_buffer[0];
  351. uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
  352. if (clearPageRemainder) {
  353. // Remaining Char count
  354. remaining = remaining / OLED_FONT_WIDTH;
  355. // Write empty character until next line
  356. while (remaining--) oled_write_char(' ', false);
  357. } else {
  358. // Next page index out of bounds?
  359. if (index + remaining >= OLED_MATRIX_SIZE) {
  360. index = 0;
  361. remaining = 0;
  362. }
  363. oled_cursor = &oled_buffer[index + remaining];
  364. }
  365. }
  366. void oled_advance_char(void) {
  367. uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
  368. uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
  369. // Do we have enough space on the current line for the next character
  370. if (remainingSpace < OLED_FONT_WIDTH) {
  371. nextIndex += remainingSpace;
  372. }
  373. // Did we go out of bounds
  374. if (nextIndex >= OLED_MATRIX_SIZE) {
  375. nextIndex = 0;
  376. }
  377. // Update cursor position
  378. oled_cursor = &oled_buffer[nextIndex];
  379. }
  380. // Main handler that writes character data to the display buffer
  381. void oled_write_char(const char data, bool invert) {
  382. // Advance to the next line if newline
  383. if (data == '\n') {
  384. // Old source wrote ' ' until end of line...
  385. oled_advance_page(true);
  386. return;
  387. }
  388. if (data == '\r') {
  389. oled_advance_page(false);
  390. return;
  391. }
  392. // copy the current render buffer to check for dirty after
  393. static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
  394. memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
  395. _Static_assert(sizeof(font) >= ((OLED_FONT_END + 1 - OLED_FONT_START) * OLED_FONT_WIDTH), "OLED_FONT_END references outside array");
  396. // set the reder buffer data
  397. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  398. if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
  399. memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
  400. } else {
  401. const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
  402. memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
  403. }
  404. // Invert if needed
  405. if (invert) {
  406. InvertCharacter(oled_cursor);
  407. }
  408. // Dirty check
  409. if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
  410. uint16_t index = oled_cursor - &oled_buffer[0];
  411. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  412. // Edgecase check if the written data spans the 2 chunks
  413. oled_dirty |= ((OLED_BLOCK_TYPE)1 << ((index + OLED_FONT_WIDTH - 1) / OLED_BLOCK_SIZE));
  414. }
  415. // Finally move to the next char
  416. oled_advance_char();
  417. }
  418. void oled_write(const char *data, bool invert) {
  419. const char *end = data + strlen(data);
  420. while (data < end) {
  421. oled_write_char(*data, invert);
  422. data++;
  423. }
  424. }
  425. void oled_write_ln(const char *data, bool invert) {
  426. oled_write(data, invert);
  427. oled_advance_page(true);
  428. }
  429. void oled_pan(bool left) {
  430. uint16_t i = 0;
  431. for (uint16_t y = 0; y < OLED_DISPLAY_HEIGHT / 8; y++) {
  432. if (left) {
  433. for (uint16_t x = 0; x < OLED_DISPLAY_WIDTH - 1; x++) {
  434. i = y * OLED_DISPLAY_WIDTH + x;
  435. oled_buffer[i] = oled_buffer[i + 1];
  436. }
  437. } else {
  438. for (uint16_t x = OLED_DISPLAY_WIDTH - 1; x > 0; x--) {
  439. i = y * OLED_DISPLAY_WIDTH + x;
  440. oled_buffer[i] = oled_buffer[i - 1];
  441. }
  442. }
  443. }
  444. oled_dirty = OLED_ALL_BLOCKS_MASK;
  445. }
  446. void oled_pan_section(bool left, uint16_t y_start, uint16_t y_end, uint16_t x_start, uint16_t x_end) {
  447. uint16_t i = 0;
  448. for (uint16_t y = y_start; y < y_end; y++) {
  449. if (left) {
  450. for (uint16_t x = x_start; x < x_end - 1; x++) {
  451. i = y * OLED_DISPLAY_WIDTH + x;
  452. oled_buffer[i] = oled_buffer[i + 1];
  453. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  454. }
  455. } else {
  456. for (uint16_t x = x_end - 1; x > 0; x--) {
  457. i = y * OLED_DISPLAY_WIDTH + x;
  458. oled_buffer[i] = oled_buffer[i - 1];
  459. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  460. }
  461. }
  462. }
  463. }
  464. oled_buffer_reader_t oled_read_raw(uint16_t start_index) {
  465. if (start_index > OLED_MATRIX_SIZE) start_index = OLED_MATRIX_SIZE;
  466. oled_buffer_reader_t ret_reader;
  467. ret_reader.current_element = &oled_buffer[start_index];
  468. ret_reader.remaining_element_count = OLED_MATRIX_SIZE - start_index;
  469. return ret_reader;
  470. }
  471. void oled_write_raw_byte(const char data, uint16_t index) {
  472. if (index > OLED_MATRIX_SIZE) index = OLED_MATRIX_SIZE;
  473. if (oled_buffer[index] == data) return;
  474. oled_buffer[index] = data;
  475. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  476. }
  477. void oled_write_raw(const char *data, uint16_t size) {
  478. uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
  479. if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
  480. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  481. uint8_t c = *data++;
  482. if (oled_buffer[i] == c) continue;
  483. oled_buffer[i] = c;
  484. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  485. }
  486. }
  487. void oled_write_pixel(uint8_t x, uint8_t y, bool on) {
  488. if (x >= oled_rotation_width) {
  489. return;
  490. }
  491. uint16_t index = x + (y / 8) * oled_rotation_width;
  492. if (index >= OLED_MATRIX_SIZE) {
  493. return;
  494. }
  495. uint8_t data = oled_buffer[index];
  496. if (on) {
  497. data |= (1 << (y % 8));
  498. } else {
  499. data &= ~(1 << (y % 8));
  500. }
  501. if (oled_buffer[index] != data) {
  502. oled_buffer[index] = data;
  503. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  504. }
  505. }
  506. #if defined(__AVR__)
  507. void oled_write_P(const char *data, bool invert) {
  508. uint8_t c = pgm_read_byte(data);
  509. while (c != 0) {
  510. oled_write_char(c, invert);
  511. c = pgm_read_byte(++data);
  512. }
  513. }
  514. void oled_write_ln_P(const char *data, bool invert) {
  515. oled_write_P(data, invert);
  516. oled_advance_page(true);
  517. }
  518. void oled_write_raw_P(const char *data, uint16_t size) {
  519. uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
  520. if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
  521. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  522. uint8_t c = pgm_read_byte(data++);
  523. if (oled_buffer[i] == c) continue;
  524. oled_buffer[i] = c;
  525. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  526. }
  527. }
  528. #endif // defined(__AVR__)
  529. bool oled_on(void) {
  530. if (!oled_initialized) {
  531. return oled_active;
  532. }
  533. #if OLED_TIMEOUT > 0
  534. oled_timeout = timer_read32() + OLED_TIMEOUT;
  535. #endif
  536. static const uint8_t PROGMEM display_on[] =
  537. #ifdef OLED_FADE_OUT
  538. {I2C_CMD, FADE_BLINK, 0x00};
  539. #else
  540. {I2C_CMD, DISPLAY_ON};
  541. #endif
  542. if (!oled_active) {
  543. if (I2C_TRANSMIT_P(display_on) != I2C_STATUS_SUCCESS) {
  544. print("oled_on cmd failed\n");
  545. return oled_active;
  546. }
  547. oled_active = true;
  548. }
  549. return oled_active;
  550. }
  551. bool oled_off(void) {
  552. if (!oled_initialized) {
  553. return !oled_active;
  554. }
  555. static const uint8_t PROGMEM display_off[] =
  556. #ifdef OLED_FADE_OUT
  557. {I2C_CMD, FADE_BLINK, ENABLE_FADE | OLED_FADE_OUT_INTERVAL};
  558. #else
  559. {I2C_CMD, DISPLAY_OFF};
  560. #endif
  561. if (oled_active) {
  562. if (I2C_TRANSMIT_P(display_off) != I2C_STATUS_SUCCESS) {
  563. print("oled_off cmd failed\n");
  564. return oled_active;
  565. }
  566. oled_active = false;
  567. }
  568. return !oled_active;
  569. }
  570. bool is_oled_on(void) { return oled_active; }
  571. uint8_t oled_set_brightness(uint8_t level) {
  572. if (!oled_initialized) {
  573. return oled_brightness;
  574. }
  575. uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level};
  576. if (oled_brightness != level) {
  577. if (I2C_TRANSMIT(set_contrast) != I2C_STATUS_SUCCESS) {
  578. print("set_brightness cmd failed\n");
  579. return oled_brightness;
  580. }
  581. oled_brightness = level;
  582. }
  583. return oled_brightness;
  584. }
  585. uint8_t oled_get_brightness(void) { return oled_brightness; }
  586. // Set the specific 8 lines rows of the screen to scroll.
  587. // 0 is the default for start, and 7 for end, which is the entire
  588. // height of the screen. For 128x32 screens, rows 4-7 are not used.
  589. void oled_scroll_set_area(uint8_t start_line, uint8_t end_line) {
  590. oled_scroll_start = start_line;
  591. oled_scroll_end = end_line;
  592. }
  593. void oled_scroll_set_speed(uint8_t speed) {
  594. // Sets the speed for scrolling... does not take effect
  595. // until scrolling is either started or restarted
  596. // the ssd1306 supports 8 speeds
  597. // FrameRate2 speed = 7
  598. // FrameRate3 speed = 4
  599. // FrameRate4 speed = 5
  600. // FrameRate5 speed = 0
  601. // FrameRate25 speed = 6
  602. // FrameRate64 speed = 1
  603. // FrameRate128 speed = 2
  604. // FrameRate256 speed = 3
  605. // for ease of use these are remaped here to be in order
  606. static const uint8_t scroll_remap[8] = {7, 4, 5, 0, 6, 1, 2, 3};
  607. oled_scroll_speed = scroll_remap[speed];
  608. }
  609. bool oled_scroll_right(void) {
  610. if (!oled_initialized) {
  611. return oled_scrolling;
  612. }
  613. // Dont enable scrolling if we need to update the display
  614. // This prevents scrolling of bad data from starting the scroll too early after init
  615. if (!oled_dirty && !oled_scrolling) {
  616. uint8_t display_scroll_right[] = {I2C_CMD, SCROLL_RIGHT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
  617. if (I2C_TRANSMIT(display_scroll_right) != I2C_STATUS_SUCCESS) {
  618. print("oled_scroll_right cmd failed\n");
  619. return oled_scrolling;
  620. }
  621. oled_scrolling = true;
  622. }
  623. return oled_scrolling;
  624. }
  625. bool oled_scroll_left(void) {
  626. if (!oled_initialized) {
  627. return oled_scrolling;
  628. }
  629. // Dont enable scrolling if we need to update the display
  630. // This prevents scrolling of bad data from starting the scroll too early after init
  631. if (!oled_dirty && !oled_scrolling) {
  632. uint8_t display_scroll_left[] = {I2C_CMD, SCROLL_LEFT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
  633. if (I2C_TRANSMIT(display_scroll_left) != I2C_STATUS_SUCCESS) {
  634. print("oled_scroll_left cmd failed\n");
  635. return oled_scrolling;
  636. }
  637. oled_scrolling = true;
  638. }
  639. return oled_scrolling;
  640. }
  641. bool oled_scroll_off(void) {
  642. if (!oled_initialized) {
  643. return !oled_scrolling;
  644. }
  645. if (oled_scrolling) {
  646. static const uint8_t PROGMEM display_scroll_off[] = {I2C_CMD, DEACTIVATE_SCROLL};
  647. if (I2C_TRANSMIT_P(display_scroll_off) != I2C_STATUS_SUCCESS) {
  648. print("oled_scroll_off cmd failed\n");
  649. return oled_scrolling;
  650. }
  651. oled_scrolling = false;
  652. oled_dirty = OLED_ALL_BLOCKS_MASK;
  653. }
  654. return !oled_scrolling;
  655. }
  656. bool is_oled_scrolling(void) { return oled_scrolling; }
  657. bool oled_invert(bool invert) {
  658. if (!oled_initialized) {
  659. return oled_inverted;
  660. }
  661. if (invert && !oled_inverted) {
  662. static const uint8_t PROGMEM display_inverted[] = {I2C_CMD, INVERT_DISPLAY};
  663. if (I2C_TRANSMIT_P(display_inverted) != I2C_STATUS_SUCCESS) {
  664. print("oled_invert cmd failed\n");
  665. return oled_inverted;
  666. }
  667. oled_inverted = true;
  668. } else if (!invert && oled_inverted) {
  669. static const uint8_t PROGMEM display_normal[] = {I2C_CMD, NORMAL_DISPLAY};
  670. if (I2C_TRANSMIT_P(display_normal) != I2C_STATUS_SUCCESS) {
  671. print("oled_invert cmd failed\n");
  672. return oled_inverted;
  673. }
  674. oled_inverted = false;
  675. }
  676. return oled_inverted;
  677. }
  678. uint8_t oled_max_chars(void) {
  679. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  680. return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
  681. }
  682. return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
  683. }
  684. uint8_t oled_max_lines(void) {
  685. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  686. return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
  687. }
  688. return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
  689. }
  690. void oled_task(void) {
  691. if (!oled_initialized) {
  692. return;
  693. }
  694. #if OLED_UPDATE_INTERVAL > 0
  695. if (timer_elapsed(oled_update_timeout) >= OLED_UPDATE_INTERVAL) {
  696. oled_update_timeout = timer_read();
  697. oled_set_cursor(0, 0);
  698. oled_task_kb();
  699. }
  700. #else
  701. oled_set_cursor(0, 0);
  702. oled_task_kbr();
  703. #endif
  704. #if OLED_SCROLL_TIMEOUT > 0
  705. if (oled_dirty && oled_scrolling) {
  706. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  707. oled_scroll_off();
  708. }
  709. #endif
  710. // Smart render system, no need to check for dirty
  711. oled_render();
  712. // Display timeout check
  713. #if OLED_TIMEOUT > 0
  714. if (oled_active && timer_expired32(timer_read32(), oled_timeout)) {
  715. oled_off();
  716. }
  717. #endif
  718. #if OLED_SCROLL_TIMEOUT > 0
  719. if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) {
  720. # ifdef OLED_SCROLL_TIMEOUT_RIGHT
  721. oled_scroll_right();
  722. # else
  723. oled_scroll_left();
  724. # endif
  725. }
  726. #endif
  727. }
  728. __attribute__((weak)) bool oled_task_kb(void) { return oled_task_user(); }
  729. __attribute__((weak)) bool oled_task_user(void) { return true; }