oled_driver.c 23 KB

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