oled_driver.c 23 KB

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