You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
20 KiB

  1. # OLED Driver
  2. ## Supported Hardware
  3. OLED modules using SSD1306 or SH1106 driver ICs, communicating over I2C.
  4. Tested combinations:
  5. |IC |Size |Platform|Notes |
  6. |---------|------|--------|------------------------|
  7. |SSD1306 |128x32|AVR |Primary support |
  8. |SSD1306 |128x64|AVR |Verified working |
  9. |SSD1306 |128x32|Arm | |
  10. |SH1106 |128x64|AVR |No rotation or scrolling|
  11. Hardware configurations using Arm-based microcontrollers or different sizes of OLED modules may be compatible, but are untested.
  12. ## Usage
  13. To enable the OLED feature, there are two steps. First, when compiling your keyboard, you'll need to add the following to your `rules.mk`:
  14. ```make
  15. OLED_ENABLE = yes
  16. ```
  17. ## OLED type
  18. |OLED Driver |Supported Device |
  19. |-------------------|---------------------------|
  20. |SSD1306 (default) |For both SSD1306 and SH1106|
  21. e.g.
  22. ```make
  23. OLED_DRIVER = SSD1306
  24. ```
  25. Then in your `keymap.c` file, implement the OLED task call. This example assumes your keymap has three layers named `_QWERTY`, `_FN` and `_ADJ`:
  26. ```c
  27. #ifdef OLED_ENABLE
  28. bool oled_task_user(void) {
  29. // Host Keyboard Layer Status
  30. oled_write_P(PSTR("Layer: "), false);
  31. switch (get_highest_layer(layer_state)) {
  32. case _QWERTY:
  33. oled_write_P(PSTR("Default\n"), false);
  34. break;
  35. case _FN:
  36. oled_write_P(PSTR("FN\n"), false);
  37. break;
  38. case _ADJ:
  39. oled_write_P(PSTR("ADJ\n"), false);
  40. break;
  41. default:
  42. // Or use the write_ln shortcut over adding '\n' to the end of your string
  43. oled_write_ln_P(PSTR("Undefined"), false);
  44. }
  45. // Host Keyboard LED Status
  46. led_t led_state = host_keyboard_led_state();
  47. oled_write_P(led_state.num_lock ? PSTR("NUM ") : PSTR(" "), false);
  48. oled_write_P(led_state.caps_lock ? PSTR("CAP ") : PSTR(" "), false);
  49. oled_write_P(led_state.scroll_lock ? PSTR("SCR ") : PSTR(" "), false);
  50. return false;
  51. }
  52. #endif
  53. ```
  54. ## Logo Example
  55. In the default font, certain ranges of characters are reserved for a QMK logo. To render this logo to the OLED screen, use the following code example:
  56. ```c
  57. static void render_logo(void) {
  58. static const char PROGMEM qmk_logo[] = {
  59. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94,
  60. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4,
  61. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00
  62. };
  63. oled_write_P(qmk_logo, false);
  64. }
  65. bool oled_task_user(void) {
  66. render_logo();
  67. return false;
  68. }
  69. ```
  70. ?> The default font file is located at `drivers/oled/glcdfont.c` and its location can be overwritten with the `OLED_FONT_H` configuration option. Font file content can be edited with external tools such as [Helix Font Editor](https://helixfonteditor.netlify.app/) and [Logo Editor](https://joric.github.io/qle/).
  71. ## Buffer Read Example
  72. For some purposes, you may need to read the current state of the OLED display
  73. buffer. The `oled_read_raw` function can be used to safely read bytes from the
  74. buffer.
  75. In this example, calling `fade_display` in the `oled_task_user` function will
  76. slowly fade away whatever is on the screen by turning random pixels black over
  77. time.
  78. ```c
  79. //Setup some mask which can be or'd with bytes to turn off pixels
  80. const uint8_t single_bit_masks[8] = {127, 191, 223, 239, 247, 251, 253, 254};
  81. static void fade_display(void) {
  82. //Define the reader structure
  83. oled_buffer_reader_t reader;
  84. uint8_t buff_char;
  85. if (random() % 30 == 0) {
  86. srand(timer_read());
  87. // Fetch a pointer for the buffer byte at index 0. The return structure
  88. // will have the pointer and the number of bytes remaining from this
  89. // index position if we want to perform a sequential read by
  90. // incrementing the buffer pointer
  91. reader = oled_read_raw(0);
  92. //Loop over the remaining buffer and erase pixels as we go
  93. for (uint16_t i = 0; i < reader.remaining_element_count; i++) {
  94. //Get the actual byte in the buffer by dereferencing the pointer
  95. buff_char = *reader.current_element;
  96. if (buff_char != 0) {
  97. oled_write_raw_byte(buff_char & single_bit_masks[rand() % 8], i);
  98. }
  99. //increment the pointer to fetch a new byte during the next loop
  100. reader.current_element++;
  101. }
  102. }
  103. }
  104. ```
  105. ## Other Examples
  106. In split keyboards, it is very common to have two OLED displays that each render different content and are oriented or flipped differently. You can do this by switching which content to render by using the return value from `is_keyboard_master()` or `is_keyboard_left()` found in `split_util.h`, e.g:
  107. ```c
  108. #ifdef OLED_ENABLE
  109. oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  110. if (!is_keyboard_master()) {
  111. return OLED_ROTATION_180; // flips the display 180 degrees if offhand
  112. }
  113. return rotation;
  114. }
  115. bool oled_task_user(void) {
  116. if (is_keyboard_master()) {
  117. render_status(); // Renders the current keyboard state (layer, lock, caps, scroll, etc)
  118. } else {
  119. render_logo(); // Renders a static logo
  120. oled_scroll_left(); // Turns on scrolling
  121. }
  122. return false;
  123. }
  124. #endif
  125. ```
  126. ## Basic Configuration
  127. These configuration options should be placed in `config.h`. Example:
  128. ```c
  129. #define OLED_BRIGHTNESS 128
  130. ```
  131. |Define |Default |Description |
  132. |---------------------------|-----------------|--------------------------------------------------------------------------------------------------------------------------|
  133. |`OLED_DISPLAY_ADDRESS` |`0x3C` |The i2c address of the OLED Display |
  134. |`OLED_FONT_H` |`"glcdfont.c"` |The font code file to use for custom fonts |
  135. |`OLED_FONT_START` |`0` |The starting character index for custom fonts |
  136. |`OLED_FONT_END` |`223` |The ending character index for custom fonts |
  137. |`OLED_FONT_WIDTH` |`6` |The font width |
  138. |`OLED_FONT_HEIGHT` |`8` |The font height (untested) |
  139. |`OLED_TIMEOUT` |`60000` |Turns off the OLED screen after 60000ms of screen update inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
  140. |`OLED_FADE_OUT` |*Not defined* |Enables fade out animation. Use together with `OLED_TIMEOUT`. |
  141. |`OLED_FADE_OUT_INTERVAL` |`0` |The speed of fade out animation, from 0 to 15. Larger values are slower. |
  142. |`OLED_SCROLL_TIMEOUT` |`0` |Scrolls the OLED screen after 0ms of OLED inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
  143. |`OLED_SCROLL_TIMEOUT_RIGHT`|*Not defined* |Scroll timeout direction is right when defined, left when undefined. |
  144. |`OLED_IC` |`OLED_IC_SSD1306`|Set to `OLED_IC_SH1106` if you're using the SH1106 OLED controller. |
  145. |`OLED_COLUMN_OFFSET` |`0` |(SH1106 only.) Shift output to the right this many pixels.<br />Useful for 128x64 displays centered on a 132x64 SH1106 IC.|
  146. |`OLED_BRIGHTNESS` |`255` |The default brightness level of the OLED, from 0 to 255. |
  147. |`OLED_UPDATE_INTERVAL` |`0` |Set the time interval for updating the OLED display in ms. This will improve the matrix scan rate. |
  148. ## 128x64 & Custom sized OLED Displays
  149. The default display size for this feature is 128x32 and all necessary defines are precalculated with that in mind. We have added a define, `OLED_DISPLAY_128X64`, to switch all the values to be used in a 128x64 display, as well as added a custom define, `OLED_DISPLAY_CUSTOM`, that allows you to provide the necessary values to the driver.
  150. |Define |Default |Description |
  151. |---------------------|---------------|----------------------------------------------------------------------------------------------------------------------------------------|
  152. |`OLED_DISPLAY_128X64`|*Not defined* |Changes the display defines for use with 128x64 displays. |
  153. |`OLED_DISPLAY_CUSTOM`|*Not defined* |Changes the display defines for use with custom displays.<br>Requires user to implement the below defines. |
  154. |`OLED_DISPLAY_WIDTH` |`128` |The width of the OLED display. |
  155. |`OLED_DISPLAY_HEIGHT`|`32` |The height of the OLED display. |
  156. |`OLED_MATRIX_SIZE` |`512` |The local buffer size to allocate.<br>`(OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH)`. |
  157. |`OLED_BLOCK_TYPE` |`uint16_t` |The unsigned integer type to use for dirty rendering. |
  158. |`OLED_BLOCK_COUNT` |`16` |The number of blocks the display is divided into for dirty rendering.<br>`(sizeof(OLED_BLOCK_TYPE) * 8)`. |
  159. |`OLED_BLOCK_SIZE` |`32` |The size of each block for dirty rendering<br>`(OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)`. |
  160. |`OLED_COM_PINS` |`COM_PINS_SEQ` |How the SSD1306 chip maps it's memory to display.<br>Options are `COM_PINS_SEQ`, `COM_PINS_ALT`, `COM_PINS_SEQ_LR`, & `COM_PINS_ALT_LR`.|
  161. |`OLED_SOURCE_MAP` |`{ 0, ... N }` |Precalculated source array to use for mapping source buffer to target OLED memory in 90 degree rendering. |
  162. |`OLED_TARGET_MAP` |`{ 24, ... N }`|Precalculated target array to use for mapping source buffer to target OLED memory in 90 degree rendering. |
  163. ### 90 Degree Rotation - Technical Mumbo Jumbo
  164. !> Rotation is unsupported on the SH1106.
  165. ```c
  166. // OLED Rotation enum values are flags
  167. typedef enum {
  168. OLED_ROTATION_0 = 0,
  169. OLED_ROTATION_90 = 1,
  170. OLED_ROTATION_180 = 2,
  171. OLED_ROTATION_270 = 3, // OLED_ROTATION_90 | OLED_ROTATION_180
  172. } oled_rotation_t;
  173. ```
  174. OLED displays driven by SSD1306 drivers only natively support in hardware 0 degree and 180 degree rendering. This feature is done in software and not free. Using this feature will increase the time to calculate what data to send over i2c to the OLED. If you are strapped for cycles, this can cause keycodes to not register. In testing however, the rendering time on an ATmega32U4 board only went from 2ms to 5ms and keycodes not registering was only noticed once we hit 15ms.
  175. 90 degree rotation is achieved by using bitwise operations to rotate each 8 block of memory and uses two precalculated arrays to remap buffer memory to OLED memory. The memory map defines are precalculated for remap performance and are calculated based on the display height, width, and block size. For example, in the 128x32 implementation with a `uint8_t` block type, we have a 64 byte block size. This gives us eight 8 byte blocks that need to be rotated and rendered. The OLED renders horizontally two 8 byte blocks before moving down a page, e.g:
  176. | | | | | | |
  177. |---|---|---|---|---|---|
  178. | 0 | 1 | | | | |
  179. | 2 | 3 | | | | |
  180. | 4 | 5 | | | | |
  181. | 6 | 7 | | | | |
  182. However the local buffer is stored as if it was Height x Width display instead of Width x Height, e.g:
  183. | | | | | | |
  184. |---|---|---|---|---|---|
  185. | 3 | 7 | | | | |
  186. | 2 | 6 | | | | |
  187. | 1 | 5 | | | | |
  188. | 0 | 4 | | | | |
  189. So those precalculated arrays just index the memory offsets in the order in which each one iterates its data.
  190. ## OLED API
  191. ```c
  192. // OLED rotation enum values are flags
  193. typedef enum {
  194. OLED_ROTATION_0 = 0,
  195. OLED_ROTATION_90 = 1,
  196. OLED_ROTATION_180 = 2,
  197. OLED_ROTATION_270 = 3, // OLED_ROTATION_90 | OLED_ROTATION_180
  198. } oled_rotation_t;
  199. // Initialize the OLED display, rotating the rendered output based on the define passed in.
  200. // Returns true if the OLED was initialized successfully
  201. bool oled_init(oled_rotation_t rotation);
  202. // Called at the start of oled_init, weak function overridable by the user
  203. // rotation - the value passed into oled_init
  204. // Return new oled_rotation_t if you want to override default rotation
  205. oled_rotation_t oled_init_kb(oled_rotation_t rotation);
  206. oled_rotation_t oled_init_user(oled_rotation_t rotation);
  207. // Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
  208. void oled_clear(void);
  209. // Renders the dirty chunks of the buffer to OLED display
  210. void oled_render(void);
  211. // Moves cursor to character position indicated by column and line, wraps if out of bounds
  212. // Max column denoted by 'oled_max_chars()' and max lines by 'oled_max_lines()' functions
  213. void oled_set_cursor(uint8_t col, uint8_t line);
  214. // Advances the cursor to the next page, writing ' ' if true
  215. // Wraps to the beginning when out of bounds
  216. void oled_advance_page(bool clearPageRemainder);
  217. // Moves the cursor forward 1 character length
  218. // Advance page if there is not enough room for the next character
  219. // Wraps to the beginning when out of bounds
  220. void oled_advance_char(void);
  221. // Writes a single character to the buffer at current cursor position
  222. // Advances the cursor while writing, inverts the pixels if true
  223. // Main handler that writes character data to the display buffer
  224. void oled_write_char(const char data, bool invert);
  225. // Writes a string to the buffer at current cursor position
  226. // Advances the cursor while writing, inverts the pixels if true
  227. void oled_write(const char *data, bool invert);
  228. // Writes a string to the buffer at current cursor position
  229. // Advances the cursor while writing, inverts the pixels if true
  230. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  231. void oled_write_ln(const char *data, bool invert);
  232. // Pans the buffer to the right (or left by passing true) by moving contents of the buffer
  233. // Useful for moving the screen in preparation for new drawing
  234. // oled_scroll_left or oled_scroll_right should be preferred for all cases of moving a static
  235. // image such as a logo or to avoid burn-in as it's much, much less cpu intensive
  236. void oled_pan(bool left);
  237. // Returns a pointer to the requested start index in the buffer plus remaining
  238. // buffer length as struct
  239. oled_buffer_reader_t oled_read_raw(uint16_t start_index);
  240. // Writes a string to the buffer at current cursor position
  241. void oled_write_raw(const char *data, uint16_t size);
  242. // Writes a single byte into the buffer at the specified index
  243. void oled_write_raw_byte(const char data, uint16_t index);
  244. // Sets a specific pixel on or off
  245. // Coordinates start at top-left and go right and down for positive x and y
  246. void oled_write_pixel(uint8_t x, uint8_t y, bool on);
  247. // Writes a PROGMEM string to the buffer at current cursor position
  248. // Advances the cursor while writing, inverts the pixels if true
  249. // Remapped to call 'void oled_write(const char *data, bool invert);' on ARM
  250. void oled_write_P(const char *data, bool invert);
  251. // Writes a PROGMEM string to the buffer at current cursor position
  252. // Advances the cursor while writing, inverts the pixels if true
  253. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  254. // Remapped to call 'void oled_write_ln(const char *data, bool invert);' on ARM
  255. void oled_write_ln_P(const char *data, bool invert);
  256. // Writes a PROGMEM string to the buffer at current cursor position
  257. void oled_write_raw_P(const char *data, uint16_t size);
  258. // Can be used to manually turn on the screen if it is off
  259. // Returns true if the screen was on or turns on
  260. bool oled_on(void);
  261. // Can be used to manually turn off the screen if it is on
  262. // Returns true if the screen was off or turns off
  263. bool oled_off(void);
  264. // Returns true if the oled is currently on, false if it is
  265. // not
  266. bool is_oled_on(void);
  267. // Sets the brightness level of the display
  268. uint8_t oled_set_brightness(uint8_t level);
  269. // Gets the current brightness level of the display
  270. uint8_t oled_get_brightness(void);
  271. // Basically it's oled_render, but with timeout management and oled_task_user calling!
  272. void oled_task(void);
  273. // Called at the start of oled_task, weak function overridable by the user
  274. bool oled_task_kb(void);
  275. bool oled_task_user(void);
  276. // Set the specific 8 lines rows of the screen to scroll.
  277. // 0 is the default for start, and 7 for end, which is the entire
  278. // height of the screen. For 128x32 screens, rows 4-7 are not used.
  279. void oled_scroll_set_area(uint8_t start_line, uint8_t end_line);
  280. // Sets scroll speed, 0-7, fastest to slowest. Default is three.
  281. // Does not take effect until scrolling is either started or restarted
  282. // the ssd1306 supports 8 speeds with the delay
  283. // listed below betwen each frame of the scrolling effect
  284. // 0=2, 1=3, 2=4, 3=5, 4=25, 5=64, 6=128, 7=256
  285. void oled_scroll_set_speed(uint8_t speed);
  286. // Begin scrolling the entire display right
  287. // Returns true if the screen was scrolling or starts scrolling
  288. // NOTE: display contents cannot be changed while scrolling
  289. bool oled_scroll_right(void);
  290. // Begin scrolling the entire display left
  291. // Returns true if the screen was scrolling or starts scrolling
  292. // NOTE: display contents cannot be changed while scrolling
  293. bool oled_scroll_left(void);
  294. // Turns off display scrolling
  295. // Returns true if the screen was not scrolling or stops scrolling
  296. bool oled_scroll_off(void);
  297. // Returns true if the oled is currently scrolling, false if it is
  298. // not
  299. bool is_oled_scrolling(void);
  300. // Inverts the display
  301. // Returns true if the screen was or is inverted
  302. bool oled_invert(bool invert);
  303. // Returns the maximum number of characters that will fit on a line
  304. uint8_t oled_max_chars(void);
  305. // Returns the maximum number of lines that will fit on the OLED
  306. uint8_t oled_max_lines(void);
  307. ```
  308. !> Scrolling and rotation are unsupported on the SH1106.
  309. ## SSD1306.h Driver Conversion Guide
  310. |Old API |Recommended New API |
  311. |-------------------------|---------------------------------|
  312. |`struct CharacterMatrix` |*removed - delete all references*|
  313. |`iota_gfx_init` |`oled_init` |
  314. |`iota_gfx_on` |`oled_on` |
  315. |`iota_gfx_off` |`oled_off` |
  316. |`iota_gfx_flush` |`oled_render` |
  317. |`iota_gfx_write_char` |`oled_write_char` |
  318. |`iota_gfx_write` |`oled_write` |
  319. |`iota_gfx_write_P` |`oled_write_P` |
  320. |`iota_gfx_clear_screen` |`oled_clear` |
  321. |`matrix_clear` |*removed - delete all references*|
  322. |`matrix_write_char_inner`|`oled_write_char` |
  323. |`matrix_write_char` |`oled_write_char` |
  324. |`matrix_write` |`oled_write` |
  325. |`matrix_write_ln` |`oled_write_ln` |
  326. |`matrix_write_P` |`oled_write_P` |
  327. |`matrix_write_ln_P` |`oled_write_ln_P` |
  328. |`matrix_render` |`oled_render` |
  329. |`iota_gfx_task` |`oled_task` |
  330. |`iota_gfx_task_user` |`oled_task_user` |