oled_driver.c 31 KB

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