oled_driver.c 31 KB

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