logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

oled_driver.c (31613B)


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