oled_driver.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. gpio_write_pin_low(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. gpio_write_pin_low(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_transmit_P((OLED_DISPLAY_ADDRESS << 1), data, size, OLED_I2C_TIMEOUT);
  202. return (status == I2C_STATUS_SUCCESS);
  203. # endif
  204. #else
  205. return oled_send_cmd(data, size);
  206. #endif
  207. }
  208. __attribute__((weak)) bool oled_send_data(const uint8_t *data, uint16_t size) {
  209. #if defined(OLED_TRANSPORT_SPI)
  210. if (!spi_start(OLED_CS_PIN, false, OLED_SPI_MODE, OLED_SPI_DIVISOR)) {
  211. return false;
  212. }
  213. // Data Mode
  214. gpio_write_pin_high(OLED_DC_PIN);
  215. // Send the commands
  216. if (spi_transmit(data, size) != SPI_STATUS_SUCCESS) {
  217. spi_stop();
  218. return false;
  219. }
  220. spi_stop();
  221. return true;
  222. #elif defined(OLED_TRANSPORT_I2C)
  223. i2c_status_t status = i2c_write_register((OLED_DISPLAY_ADDRESS << 1), I2C_DATA, data, size, OLED_I2C_TIMEOUT);
  224. return (status == I2C_STATUS_SUCCESS);
  225. #endif
  226. }
  227. __attribute__((weak)) void oled_driver_init(void) {
  228. #if defined(OLED_TRANSPORT_SPI)
  229. spi_init();
  230. gpio_set_pin_output(OLED_CS_PIN);
  231. gpio_write_pin_high(OLED_CS_PIN);
  232. gpio_set_pin_output(OLED_DC_PIN);
  233. gpio_write_pin_low(OLED_DC_PIN);
  234. # ifdef OLED_RST_PIN
  235. /* Reset device */
  236. gpio_set_pin_output(OLED_RST_PIN);
  237. gpio_write_pin_low(OLED_RST_PIN);
  238. wait_ms(20);
  239. gpio_write_pin_high(OLED_RST_PIN);
  240. wait_ms(20);
  241. # endif
  242. #elif defined(OLED_TRANSPORT_I2C)
  243. i2c_init();
  244. #endif
  245. }
  246. // Flips the rendering bits for a character at the current cursor position
  247. static void InvertCharacter(uint8_t *cursor) {
  248. const uint8_t *end = cursor + OLED_FONT_WIDTH;
  249. while (cursor < end) {
  250. *cursor = ~(*cursor);
  251. cursor++;
  252. }
  253. }
  254. bool oled_init(oled_rotation_t rotation) {
  255. #if defined(USE_I2C) && defined(SPLIT_KEYBOARD) && defined(OLED_TRANSPORT_I2C)
  256. if (!is_keyboard_master()) {
  257. return true;
  258. }
  259. #endif
  260. oled_rotation = oled_init_user(oled_init_kb(rotation));
  261. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  262. oled_rotation_width = OLED_DISPLAY_WIDTH;
  263. } else {
  264. oled_rotation_width = OLED_DISPLAY_HEIGHT;
  265. }
  266. oled_driver_init();
  267. static const uint8_t PROGMEM display_setup1[] = {
  268. I2C_CMD,
  269. DISPLAY_OFF,
  270. DISPLAY_CLOCK,
  271. OLED_DISPLAY_CLOCK,
  272. MULTIPLEX_RATIO,
  273. #if OLED_IC_COM_PINS_ARE_COLUMNS
  274. OLED_DISPLAY_WIDTH - 1,
  275. #else
  276. OLED_DISPLAY_HEIGHT - 1,
  277. #endif
  278. #if OLED_IC == OLED_IC_SH1107
  279. SH1107_DISPLAY_START_LINE,
  280. 0x00,
  281. #else
  282. DISPLAY_START_LINE | 0x00,
  283. #endif
  284. CHARGE_PUMP,
  285. 0x14,
  286. #if OLED_IC_HAS_HORIZONTAL_MODE
  287. // MEMORY_MODE is unsupported on SH1106 (Page Addressing only)
  288. MEMORY_MODE,
  289. 0x00, // Horizontal addressing mode
  290. #elif OLED_IC == OLED_IC_SH1107
  291. // Page addressing mode
  292. SH1107_MEMORY_MODE_PAGE,
  293. #endif
  294. };
  295. if (!oled_send_cmd_P(display_setup1, ARRAY_SIZE(display_setup1))) {
  296. print("oled_init cmd set 1 failed\n");
  297. return false;
  298. }
  299. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_180)) {
  300. static const uint8_t PROGMEM display_normal[] = {
  301. I2C_CMD, SEGMENT_REMAP_INV, COM_SCAN_DEC, DISPLAY_OFFSET, OLED_COM_PIN_OFFSET,
  302. };
  303. if (!oled_send_cmd_P(display_normal, ARRAY_SIZE(display_normal))) {
  304. print("oled_init cmd normal rotation failed\n");
  305. return false;
  306. }
  307. } else {
  308. static const uint8_t PROGMEM display_flipped[] = {
  309. I2C_CMD, SEGMENT_REMAP, COM_SCAN_INC, DISPLAY_OFFSET, (OLED_COM_PIN_COUNT - OLED_COM_PIN_OFFSET) % OLED_COM_PIN_COUNT,
  310. };
  311. if (!oled_send_cmd_P(display_flipped, ARRAY_SIZE(display_flipped))) {
  312. print("display_flipped failed\n");
  313. return false;
  314. }
  315. }
  316. 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};
  317. if (!oled_send_cmd_P(display_setup2, ARRAY_SIZE(display_setup2))) {
  318. print("display_setup2 failed\n");
  319. return false;
  320. }
  321. #if OLED_TIMEOUT > 0
  322. oled_timeout = timer_read32() + OLED_TIMEOUT;
  323. #endif
  324. #if OLED_SCROLL_TIMEOUT > 0
  325. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  326. #endif
  327. oled_clear();
  328. oled_initialized = true;
  329. oled_active = true;
  330. oled_scrolling = false;
  331. return true;
  332. }
  333. __attribute__((weak)) oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
  334. return rotation;
  335. }
  336. __attribute__((weak)) oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  337. return rotation;
  338. }
  339. void oled_clear(void) {
  340. memset(oled_buffer, 0, sizeof(oled_buffer));
  341. oled_cursor = &oled_buffer[0];
  342. oled_dirty = OLED_ALL_BLOCKS_MASK;
  343. }
  344. static void calc_bounds(uint8_t update_start, uint8_t *cmd_array) {
  345. // Calculate commands to set memory addressing bounds.
  346. uint8_t start_page = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_WIDTH;
  347. uint8_t start_column = OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_WIDTH;
  348. #if !OLED_IC_HAS_HORIZONTAL_MODE
  349. // Commands for Page Addressing Mode. Sets starting page and column; has no end bound.
  350. // Column value must be split into high and low nybble and sent as two commands.
  351. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  352. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  353. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  354. #else
  355. // Commands for use in Horizontal Addressing mode.
  356. cmd_array[1] = start_column + OLED_COLUMN_OFFSET;
  357. cmd_array[4] = start_page;
  358. cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) % OLED_DISPLAY_WIDTH + cmd_array[1];
  359. cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_WIDTH - 1) / OLED_DISPLAY_WIDTH - 1 + cmd_array[4];
  360. #endif
  361. }
  362. static void calc_bounds_90(uint8_t update_start, uint8_t *cmd_array) {
  363. // Block numbering starts from the bottom left corner, going up and then to
  364. // the right. The controller needs the page and column numbers for the top
  365. // left and bottom right corners of that block.
  366. // Total number of pages across the screen height.
  367. const uint8_t height_in_pages = OLED_DISPLAY_HEIGHT / 8;
  368. // Difference of starting page numbers for adjacent blocks; may be 0 if
  369. // blocks are large enough to occupy one or more whole 8px columns.
  370. const uint8_t page_inc_per_block = OLED_BLOCK_SIZE % OLED_DISPLAY_HEIGHT / 8;
  371. // Top page number for a block which is at the bottom edge of the screen.
  372. const uint8_t bottom_block_top_page = (height_in_pages - page_inc_per_block) % height_in_pages;
  373. #if !OLED_IC_HAS_HORIZONTAL_MODE
  374. // Only the Page Addressing Mode is supported
  375. uint8_t start_page = bottom_block_top_page - (OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT / 8);
  376. uint8_t start_column = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8;
  377. cmd_array[0] = PAM_PAGE_ADDR | start_page;
  378. cmd_array[1] = PAM_SETCOLUMN_LSB | ((OLED_COLUMN_OFFSET + start_column) & 0x0f);
  379. cmd_array[2] = PAM_SETCOLUMN_MSB | ((OLED_COLUMN_OFFSET + start_column) >> 4 & 0x0f);
  380. #else
  381. cmd_array[1] = OLED_BLOCK_SIZE * update_start / OLED_DISPLAY_HEIGHT * 8 + OLED_COLUMN_OFFSET;
  382. cmd_array[4] = bottom_block_top_page - (OLED_BLOCK_SIZE * update_start % OLED_DISPLAY_HEIGHT / 8);
  383. cmd_array[2] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8 - 1 + cmd_array[1];
  384. cmd_array[5] = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) % OLED_DISPLAY_HEIGHT / 8 + cmd_array[4];
  385. #endif
  386. }
  387. uint8_t crot(uint8_t a, int8_t n) {
  388. const uint8_t mask = 0x7;
  389. n &= mask;
  390. return a << n | a >> (-n & mask);
  391. }
  392. static void rotate_90(const uint8_t *src, uint8_t *dest) {
  393. for (uint8_t i = 0, shift = 7; i < 8; ++i, --shift) {
  394. uint8_t selector = (1 << i);
  395. for (uint8_t j = 0; j < 8; ++j) {
  396. dest[i] |= crot(src[j] & selector, shift - (int8_t)j);
  397. }
  398. }
  399. }
  400. void oled_render_dirty(bool all) {
  401. // Do we have work to do?
  402. oled_dirty &= OLED_ALL_BLOCKS_MASK;
  403. if (!oled_dirty || !oled_initialized || oled_scrolling) {
  404. return;
  405. }
  406. // Turn on display if it is off
  407. oled_on();
  408. uint8_t update_start = 0;
  409. uint8_t num_processed = 0;
  410. while (oled_dirty && (num_processed++ < OLED_UPDATE_PROCESS_LIMIT || all)) { // render all dirty blocks (up to the configured limit)
  411. // Find next dirty block
  412. while (!(oled_dirty & ((OLED_BLOCK_TYPE)1 << update_start))) {
  413. ++update_start;
  414. }
  415. // Set column & page position
  416. #if OLED_IC_HAS_HORIZONTAL_MODE
  417. static uint8_t display_start[] = {I2C_CMD, COLUMN_ADDR, 0, OLED_DISPLAY_WIDTH - 1, PAGE_ADDR, 0, OLED_DISPLAY_HEIGHT / 8 - 1};
  418. #else
  419. static uint8_t display_start[] = {I2C_CMD, PAM_PAGE_ADDR, PAM_SETCOLUMN_LSB, PAM_SETCOLUMN_MSB};
  420. #endif
  421. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  422. calc_bounds(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  423. } else {
  424. calc_bounds_90(update_start, &display_start[1]); // Offset from I2C_CMD byte at the start
  425. }
  426. // Send column & page position
  427. if (!oled_send_cmd(display_start, ARRAY_SIZE(display_start))) {
  428. print("oled_render offset command failed\n");
  429. return;
  430. }
  431. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  432. // Send render data chunk as is
  433. if (!oled_send_data(&oled_buffer[OLED_BLOCK_SIZE * update_start], OLED_BLOCK_SIZE)) {
  434. print("oled_render data failed\n");
  435. return;
  436. }
  437. } else {
  438. // Rotate the render chunks
  439. const static uint8_t source_map[] = OLED_SOURCE_MAP;
  440. const static uint8_t target_map[] = OLED_TARGET_MAP;
  441. static uint8_t temp_buffer[OLED_BLOCK_SIZE];
  442. memset(temp_buffer, 0, sizeof(temp_buffer));
  443. for (uint8_t i = 0; i < sizeof(source_map); ++i) {
  444. rotate_90(&oled_buffer[OLED_BLOCK_SIZE * update_start + source_map[i]], &temp_buffer[target_map[i]]);
  445. }
  446. #if OLED_IC_HAS_HORIZONTAL_MODE
  447. // Send render data chunk after rotating
  448. if (!oled_send_data(&temp_buffer[0], OLED_BLOCK_SIZE)) {
  449. print("oled_render90 data failed\n");
  450. return;
  451. }
  452. #else
  453. // For SH1106 or SH1107 the data chunk must be split into separate pieces for each page
  454. const uint8_t columns_in_block = (OLED_BLOCK_SIZE + OLED_DISPLAY_HEIGHT - 1) / OLED_DISPLAY_HEIGHT * 8;
  455. const uint8_t num_pages = OLED_BLOCK_SIZE / columns_in_block;
  456. for (uint8_t i = 0; i < num_pages; ++i) {
  457. // Send column & page position for all pages except the first one
  458. if (i > 0) {
  459. display_start[1]++;
  460. if (!oled_send_cmd(display_start, ARRAY_SIZE(display_start))) {
  461. print("oled_render offset command failed\n");
  462. return;
  463. }
  464. }
  465. // Send data for the page
  466. if (!oled_send_data(&temp_buffer[columns_in_block * i], columns_in_block)) {
  467. print("oled_render90 data failed\n");
  468. return;
  469. }
  470. }
  471. #endif
  472. }
  473. // Clear dirty flag of just rendered block
  474. oled_dirty &= ~((OLED_BLOCK_TYPE)1 << update_start);
  475. }
  476. }
  477. void oled_set_cursor(uint8_t col, uint8_t line) {
  478. uint16_t index = line * oled_rotation_width + col * OLED_FONT_WIDTH;
  479. // Out of bounds?
  480. if (index >= OLED_MATRIX_SIZE) {
  481. index = 0;
  482. }
  483. oled_cursor = &oled_buffer[index];
  484. }
  485. void oled_advance_page(bool clearPageRemainder) {
  486. uint16_t index = oled_cursor - &oled_buffer[0];
  487. uint8_t remaining = oled_rotation_width - (index % oled_rotation_width);
  488. if (clearPageRemainder) {
  489. // Remaining Char count
  490. remaining = remaining / OLED_FONT_WIDTH;
  491. // Write empty character until next line
  492. while (remaining--)
  493. oled_write_char(' ', false);
  494. } else {
  495. // Next page index out of bounds?
  496. if (index + remaining >= OLED_MATRIX_SIZE) {
  497. index = 0;
  498. remaining = 0;
  499. }
  500. oled_cursor = &oled_buffer[index + remaining];
  501. }
  502. }
  503. void oled_advance_char(void) {
  504. uint16_t nextIndex = oled_cursor - &oled_buffer[0] + OLED_FONT_WIDTH;
  505. uint8_t remainingSpace = oled_rotation_width - (nextIndex % oled_rotation_width);
  506. // Do we have enough space on the current line for the next character
  507. if (remainingSpace < OLED_FONT_WIDTH) {
  508. nextIndex += remainingSpace;
  509. }
  510. // Did we go out of bounds
  511. if (nextIndex >= OLED_MATRIX_SIZE) {
  512. nextIndex = 0;
  513. }
  514. // Update cursor position
  515. oled_cursor = &oled_buffer[nextIndex];
  516. }
  517. // Main handler that writes character data to the display buffer
  518. void oled_write_char(const char data, bool invert) {
  519. // Advance to the next line if newline
  520. if (data == '\n') {
  521. // Old source wrote ' ' until end of line...
  522. oled_advance_page(true);
  523. return;
  524. }
  525. if (data == '\r') {
  526. oled_advance_page(false);
  527. return;
  528. }
  529. // copy the current render buffer to check for dirty after
  530. static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
  531. memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);
  532. _Static_assert(sizeof(font) >= ((OLED_FONT_END + 1 - OLED_FONT_START) * OLED_FONT_WIDTH), "OLED_FONT_END references outside array");
  533. // set the reder buffer data
  534. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  535. if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
  536. memset(oled_cursor, 0x00, OLED_FONT_WIDTH);
  537. } else {
  538. const uint8_t *glyph = &font[(cast_data - OLED_FONT_START) * OLED_FONT_WIDTH];
  539. memcpy_P(oled_cursor, glyph, OLED_FONT_WIDTH);
  540. }
  541. // Invert if needed
  542. if (invert) {
  543. InvertCharacter(oled_cursor);
  544. }
  545. // Dirty check
  546. if (memcmp(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH)) {
  547. uint16_t index = oled_cursor - &oled_buffer[0];
  548. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  549. // Edgecase check if the written data spans the 2 chunks
  550. oled_dirty |= ((OLED_BLOCK_TYPE)1 << ((index + OLED_FONT_WIDTH - 1) / OLED_BLOCK_SIZE));
  551. }
  552. // Finally move to the next char
  553. oled_advance_char();
  554. }
  555. void oled_write(const char *data, bool invert) {
  556. const char *end = data + strlen(data);
  557. while (data < end) {
  558. oled_write_char(*data, invert);
  559. data++;
  560. }
  561. }
  562. void oled_write_ln(const char *data, bool invert) {
  563. oled_write(data, invert);
  564. oled_advance_page(true);
  565. }
  566. void oled_pan(bool left) {
  567. uint16_t i = 0;
  568. for (uint16_t y = 0; y < OLED_DISPLAY_HEIGHT / 8; y++) {
  569. if (left) {
  570. for (uint16_t x = 0; x < OLED_DISPLAY_WIDTH - 1; x++) {
  571. i = y * OLED_DISPLAY_WIDTH + x;
  572. oled_buffer[i] = oled_buffer[i + 1];
  573. }
  574. } else {
  575. for (uint16_t x = OLED_DISPLAY_WIDTH - 1; x > 0; x--) {
  576. i = y * OLED_DISPLAY_WIDTH + x;
  577. oled_buffer[i] = oled_buffer[i - 1];
  578. }
  579. }
  580. }
  581. oled_dirty = OLED_ALL_BLOCKS_MASK;
  582. }
  583. oled_buffer_reader_t oled_read_raw(uint16_t start_index) {
  584. if (start_index > OLED_MATRIX_SIZE) start_index = OLED_MATRIX_SIZE;
  585. oled_buffer_reader_t ret_reader;
  586. ret_reader.current_element = &oled_buffer[start_index];
  587. ret_reader.remaining_element_count = OLED_MATRIX_SIZE - start_index;
  588. return ret_reader;
  589. }
  590. void oled_write_raw_byte(const char data, uint16_t index) {
  591. if (index > OLED_MATRIX_SIZE) index = OLED_MATRIX_SIZE;
  592. if (oled_buffer[index] == data) return;
  593. oled_buffer[index] = data;
  594. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  595. }
  596. void oled_write_raw(const char *data, uint16_t size) {
  597. uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
  598. if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
  599. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  600. uint8_t c = *data++;
  601. if (oled_buffer[i] == c) continue;
  602. oled_buffer[i] = c;
  603. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  604. }
  605. }
  606. void oled_write_pixel(uint8_t x, uint8_t y, bool on) {
  607. if (x >= oled_rotation_width) {
  608. return;
  609. }
  610. uint16_t index = x + (y / 8) * oled_rotation_width;
  611. if (index >= OLED_MATRIX_SIZE) {
  612. return;
  613. }
  614. uint8_t data = oled_buffer[index];
  615. if (on) {
  616. data |= (1 << (y % 8));
  617. } else {
  618. data &= ~(1 << (y % 8));
  619. }
  620. if (oled_buffer[index] != data) {
  621. oled_buffer[index] = data;
  622. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (index / OLED_BLOCK_SIZE));
  623. }
  624. }
  625. #if defined(__AVR__)
  626. void oled_write_P(const char *data, bool invert) {
  627. uint8_t c = pgm_read_byte(data);
  628. while (c != 0) {
  629. oled_write_char(c, invert);
  630. c = pgm_read_byte(++data);
  631. }
  632. }
  633. void oled_write_ln_P(const char *data, bool invert) {
  634. oled_write_P(data, invert);
  635. oled_advance_page(true);
  636. }
  637. void oled_write_raw_P(const char *data, uint16_t size) {
  638. uint16_t cursor_start_index = oled_cursor - &oled_buffer[0];
  639. if ((size + cursor_start_index) > OLED_MATRIX_SIZE) size = OLED_MATRIX_SIZE - cursor_start_index;
  640. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  641. uint8_t c = pgm_read_byte(data++);
  642. if (oled_buffer[i] == c) continue;
  643. oled_buffer[i] = c;
  644. oled_dirty |= ((OLED_BLOCK_TYPE)1 << (i / OLED_BLOCK_SIZE));
  645. }
  646. }
  647. #endif // defined(__AVR__)
  648. bool oled_on(void) {
  649. if (!oled_initialized) {
  650. return oled_active;
  651. }
  652. #if OLED_TIMEOUT > 0
  653. oled_timeout = timer_read32() + OLED_TIMEOUT;
  654. #endif
  655. static const uint8_t PROGMEM display_on[] =
  656. #ifdef OLED_FADE_OUT
  657. {I2C_CMD, FADE_BLINK, 0x00};
  658. #else
  659. {I2C_CMD, DISPLAY_ON};
  660. #endif
  661. if (!oled_active) {
  662. if (!oled_send_cmd_P(display_on, ARRAY_SIZE(display_on))) {
  663. print("oled_on cmd failed\n");
  664. return oled_active;
  665. }
  666. oled_active = true;
  667. }
  668. return oled_active;
  669. }
  670. bool oled_off(void) {
  671. if (!oled_initialized) {
  672. return !oled_active;
  673. }
  674. static const uint8_t PROGMEM display_off[] =
  675. #ifdef OLED_FADE_OUT
  676. {I2C_CMD, FADE_BLINK, ENABLE_FADE | OLED_FADE_OUT_INTERVAL};
  677. #else
  678. {I2C_CMD, DISPLAY_OFF};
  679. #endif
  680. if (oled_active) {
  681. if (!oled_send_cmd_P(display_off, ARRAY_SIZE(display_off))) {
  682. print("oled_off cmd failed\n");
  683. return oled_active;
  684. }
  685. oled_active = false;
  686. }
  687. return !oled_active;
  688. }
  689. bool is_oled_on(void) {
  690. return oled_active;
  691. }
  692. uint8_t oled_set_brightness(uint8_t level) {
  693. if (!oled_initialized) {
  694. return oled_brightness;
  695. }
  696. uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level};
  697. if (oled_brightness != level) {
  698. if (!oled_send_cmd(set_contrast, ARRAY_SIZE(set_contrast))) {
  699. print("set_brightness cmd failed\n");
  700. return oled_brightness;
  701. }
  702. oled_brightness = level;
  703. }
  704. return oled_brightness;
  705. }
  706. uint8_t oled_get_brightness(void) {
  707. return oled_brightness;
  708. }
  709. // Set the specific 8 lines rows of the screen to scroll.
  710. // 0 is the default for start, and 7 for end, which is the entire
  711. // height of the screen. For 128x32 screens, rows 4-7 are not used.
  712. void oled_scroll_set_area(uint8_t start_line, uint8_t end_line) {
  713. oled_scroll_start = start_line;
  714. oled_scroll_end = end_line;
  715. }
  716. void oled_scroll_set_speed(uint8_t speed) {
  717. // Sets the speed for scrolling... does not take effect
  718. // until scrolling is either started or restarted
  719. // the ssd1306 supports 8 speeds
  720. // FrameRate2 speed = 7
  721. // FrameRate3 speed = 4
  722. // FrameRate4 speed = 5
  723. // FrameRate5 speed = 0
  724. // FrameRate25 speed = 6
  725. // FrameRate64 speed = 1
  726. // FrameRate128 speed = 2
  727. // FrameRate256 speed = 3
  728. // for ease of use these are remaped here to be in order
  729. static const uint8_t scroll_remap[8] = {7, 4, 5, 0, 6, 1, 2, 3};
  730. oled_scroll_speed = scroll_remap[speed];
  731. }
  732. bool oled_scroll_right(void) {
  733. if (!oled_initialized) {
  734. return oled_scrolling;
  735. }
  736. // Dont enable scrolling if we need to update the display
  737. // This prevents scrolling of bad data from starting the scroll too early after init
  738. if (!oled_dirty && !oled_scrolling) {
  739. uint8_t display_scroll_right[] = {I2C_CMD, SCROLL_RIGHT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
  740. if (!oled_send_cmd(display_scroll_right, ARRAY_SIZE(display_scroll_right))) {
  741. print("oled_scroll_right cmd failed\n");
  742. return oled_scrolling;
  743. }
  744. oled_scrolling = true;
  745. }
  746. return oled_scrolling;
  747. }
  748. bool oled_scroll_left(void) {
  749. if (!oled_initialized) {
  750. return oled_scrolling;
  751. }
  752. // Dont enable scrolling if we need to update the display
  753. // This prevents scrolling of bad data from starting the scroll too early after init
  754. if (!oled_dirty && !oled_scrolling) {
  755. uint8_t display_scroll_left[] = {I2C_CMD, SCROLL_LEFT, 0x00, oled_scroll_start, oled_scroll_speed, oled_scroll_end, 0x00, 0xFF, ACTIVATE_SCROLL};
  756. if (!oled_send_cmd(display_scroll_left, ARRAY_SIZE(display_scroll_left))) {
  757. print("oled_scroll_left cmd failed\n");
  758. return oled_scrolling;
  759. }
  760. oled_scrolling = true;
  761. }
  762. return oled_scrolling;
  763. }
  764. bool oled_scroll_off(void) {
  765. if (!oled_initialized) {
  766. return !oled_scrolling;
  767. }
  768. if (oled_scrolling) {
  769. static const uint8_t PROGMEM display_scroll_off[] = {I2C_CMD, DEACTIVATE_SCROLL};
  770. if (!oled_send_cmd_P(display_scroll_off, ARRAY_SIZE(display_scroll_off))) {
  771. print("oled_scroll_off cmd failed\n");
  772. return oled_scrolling;
  773. }
  774. oled_scrolling = false;
  775. oled_dirty = OLED_ALL_BLOCKS_MASK;
  776. }
  777. return !oled_scrolling;
  778. }
  779. bool is_oled_scrolling(void) {
  780. return oled_scrolling;
  781. }
  782. bool oled_invert(bool invert) {
  783. if (!oled_initialized) {
  784. return oled_inverted;
  785. }
  786. if (invert && !oled_inverted) {
  787. static const uint8_t PROGMEM display_inverted[] = {I2C_CMD, INVERT_DISPLAY};
  788. if (!oled_send_cmd_P(display_inverted, ARRAY_SIZE(display_inverted))) {
  789. print("oled_invert cmd failed\n");
  790. return oled_inverted;
  791. }
  792. oled_inverted = true;
  793. } else if (!invert && oled_inverted) {
  794. static const uint8_t PROGMEM display_normal[] = {I2C_CMD, NORMAL_DISPLAY};
  795. if (!oled_send_cmd_P(display_normal, ARRAY_SIZE(display_normal))) {
  796. print("oled_invert cmd failed\n");
  797. return oled_inverted;
  798. }
  799. oled_inverted = false;
  800. }
  801. return oled_inverted;
  802. }
  803. uint8_t oled_max_chars(void) {
  804. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  805. return OLED_DISPLAY_WIDTH / OLED_FONT_WIDTH;
  806. }
  807. return OLED_DISPLAY_HEIGHT / OLED_FONT_WIDTH;
  808. }
  809. uint8_t oled_max_lines(void) {
  810. if (!HAS_FLAGS(oled_rotation, OLED_ROTATION_90)) {
  811. return OLED_DISPLAY_HEIGHT / OLED_FONT_HEIGHT;
  812. }
  813. return OLED_DISPLAY_WIDTH / OLED_FONT_HEIGHT;
  814. }
  815. void oled_task(void) {
  816. if (!oled_initialized) {
  817. return;
  818. }
  819. #if OLED_UPDATE_INTERVAL > 0
  820. if (timer_elapsed(oled_update_timeout) >= OLED_UPDATE_INTERVAL) {
  821. oled_update_timeout = timer_read();
  822. oled_set_cursor(0, 0);
  823. oled_task_kb();
  824. }
  825. #else
  826. oled_set_cursor(0, 0);
  827. oled_task_kb();
  828. #endif
  829. #if OLED_SCROLL_TIMEOUT > 0
  830. if (oled_dirty && oled_scrolling) {
  831. oled_scroll_timeout = timer_read32() + OLED_SCROLL_TIMEOUT;
  832. oled_scroll_off();
  833. }
  834. #endif
  835. // Smart render system, no need to check for dirty
  836. oled_render();
  837. // Display timeout check
  838. #if OLED_TIMEOUT > 0
  839. if (oled_active && timer_expired32(timer_read32(), oled_timeout)) {
  840. oled_off();
  841. }
  842. #endif
  843. #if OLED_SCROLL_TIMEOUT > 0
  844. if (!oled_scrolling && timer_expired32(timer_read32(), oled_scroll_timeout)) {
  845. # ifdef OLED_SCROLL_TIMEOUT_RIGHT
  846. oled_scroll_right();
  847. # else
  848. oled_scroll_left();
  849. # endif
  850. }
  851. #endif
  852. }
  853. __attribute__((weak)) bool oled_task_kb(void) {
  854. return oled_task_user();
  855. }
  856. __attribute__((weak)) bool oled_task_user(void) {
  857. return true;
  858. }