oled_driver.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. // an enumeration of the chips this driver supports
  18. #define OLED_IC_SSD1306 0
  19. #define OLED_IC_SH1106 1
  20. #if defined(OLED_DISPLAY_CUSTOM)
  21. // Expected user to implement the necessary defines
  22. #elif defined(OLED_DISPLAY_128X64)
  23. // Double height 128x64
  24. # ifndef OLED_DISPLAY_WIDTH
  25. # define OLED_DISPLAY_WIDTH 128
  26. # endif
  27. # ifndef OLED_DISPLAY_HEIGHT
  28. # define OLED_DISPLAY_HEIGHT 64
  29. # endif
  30. # ifndef OLED_MATRIX_SIZE
  31. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 1024 (compile time mathed)
  32. # endif
  33. # ifndef OLED_BLOCK_TYPE
  34. # define OLED_BLOCK_TYPE uint16_t
  35. # endif
  36. # ifndef OLED_BLOCK_COUNT
  37. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 32 (compile time mathed)
  38. # endif
  39. # ifndef OLED_BLOCK_SIZE
  40. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  41. # endif
  42. # ifndef OLED_COM_PINS
  43. # define OLED_COM_PINS COM_PINS_ALT
  44. # endif
  45. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  46. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  47. # ifndef OLED_SOURCE_MAP
  48. # define OLED_SOURCE_MAP \
  49. { 0, 8, 16, 24, 32, 40, 48, 56 }
  50. # endif
  51. # ifndef OLED_TARGET_MAP
  52. # define OLED_TARGET_MAP \
  53. { 56, 48, 40, 32, 24, 16, 8, 0 }
  54. # endif
  55. // If OLED_BLOCK_TYPE is uint32_t, these tables would look like:
  56. // #define OLED_SOURCE_MAP { 32, 40, 48, 56 }
  57. // #define OLED_TARGET_MAP { 24, 16, 8, 0 }
  58. // If OLED_BLOCK_TYPE is uint16_t, these tables would look like:
  59. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  60. // #define OLED_TARGET_MAP { 56, 48, 40, 32, 24, 16, 8, 0 }
  61. // If OLED_BLOCK_TYPE is uint8_t, these tables would look like:
  62. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120 }
  63. // #define OLED_TARGET_MAP { 56, 120, 48, 112, 40, 104, 32, 96, 24, 88, 16, 80, 8, 72, 0, 64 }
  64. #else // defined(OLED_DISPLAY_128X64)
  65. // Default 128x32
  66. # ifndef OLED_DISPLAY_WIDTH
  67. # define OLED_DISPLAY_WIDTH 128
  68. # endif
  69. # ifndef OLED_DISPLAY_HEIGHT
  70. # define OLED_DISPLAY_HEIGHT 32
  71. # endif
  72. # ifndef OLED_MATRIX_SIZE
  73. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 512 (compile time mathed)
  74. # endif
  75. # ifndef OLED_BLOCK_TYPE
  76. # define OLED_BLOCK_TYPE uint16_t // Type to use for segmenting the oled display for smart rendering, use unsigned types only
  77. # endif
  78. # ifndef OLED_BLOCK_COUNT
  79. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 16 (compile time mathed)
  80. # endif
  81. # ifndef OLED_BLOCK_SIZE
  82. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  83. # endif
  84. # ifndef OLED_COM_PINS
  85. # define OLED_COM_PINS COM_PINS_SEQ
  86. # endif
  87. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  88. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  89. # ifndef OLED_SOURCE_MAP
  90. # define OLED_SOURCE_MAP \
  91. { 0, 8, 16, 24 }
  92. # endif
  93. # ifndef OLED_TARGET_MAP
  94. # define OLED_TARGET_MAP \
  95. { 24, 16, 8, 0 }
  96. # endif
  97. // If OLED_BLOCK_TYPE is uint8_t, these tables would look like:
  98. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  99. // #define OLED_TARGET_MAP { 48, 32, 16, 0, 56, 40, 24, 8 }
  100. #endif // defined(OLED_DISPLAY_CUSTOM)
  101. #if !defined(OLED_IC)
  102. # define OLED_IC OLED_IC_SSD1306
  103. #endif
  104. // the column address corresponding to the first column in the display hardware
  105. #if !defined(OLED_COLUMN_OFFSET)
  106. # define OLED_COLUMN_OFFSET 0
  107. #endif
  108. // Address to use for the i2c oled communication
  109. #if !defined(OLED_DISPLAY_ADDRESS)
  110. # define OLED_DISPLAY_ADDRESS 0x3C
  111. #endif
  112. // Custom font file to use
  113. #if !defined(OLED_FONT_H)
  114. # define OLED_FONT_H "glcdfont.c"
  115. #endif
  116. // unsigned char value of the first character in the font file
  117. #if !defined(OLED_FONT_START)
  118. # define OLED_FONT_START 0
  119. #endif
  120. // unsigned char value of the last character in the font file
  121. #if !defined(OLED_FONT_END)
  122. # define OLED_FONT_END 223
  123. #endif
  124. // Font render width
  125. #if !defined(OLED_FONT_WIDTH)
  126. # define OLED_FONT_WIDTH 6
  127. #endif
  128. // Font render height
  129. #if !defined(OLED_FONT_HEIGHT)
  130. # define OLED_FONT_HEIGHT 8
  131. #endif
  132. // Default brightness level
  133. #if !defined(OLED_BRIGHTNESS)
  134. # define OLED_BRIGHTNESS 255
  135. #endif
  136. #if !defined(OLED_TIMEOUT)
  137. # if defined(OLED_DISABLE_TIMEOUT)
  138. # define OLED_TIMEOUT 0
  139. # else
  140. # define OLED_TIMEOUT 60000
  141. # endif
  142. #endif
  143. #if !defined(OLED_FADE_OUT_INTERVAL)
  144. # define OLED_FADE_OUT_INTERVAL 0x00
  145. #endif
  146. #if OLED_FADE_OUT_INTERVAL > 0x0F || OLED_FADE_OUT_INTERVAL < 0x00
  147. # error OLED_FADE_OUT_INTERVAL must be between 0x00 and 0x0F
  148. #endif
  149. #if !defined(OLED_I2C_TIMEOUT)
  150. # define OLED_I2C_TIMEOUT 100
  151. #endif
  152. #if !defined(OLED_UPDATE_INTERVAL) && defined(SPLIT_KEYBOARD)
  153. # define OLED_UPDATE_INTERVAL 50
  154. #endif
  155. #if !defined(OLED_UPDATE_PROCESS_LIMIT)
  156. # define OLED_UPDATE_PROCESS_LIMIT 1
  157. #endif
  158. typedef struct __attribute__((__packed__)) {
  159. uint8_t *current_element;
  160. uint16_t remaining_element_count;
  161. } oled_buffer_reader_t;
  162. // OLED Rotation enum values are flags
  163. typedef enum {
  164. OLED_ROTATION_0 = 0,
  165. OLED_ROTATION_90 = 1,
  166. OLED_ROTATION_180 = 2,
  167. OLED_ROTATION_270 = 3, // OLED_ROTATION_90 | OLED_ROTATION_180
  168. } oled_rotation_t;
  169. // Initialize the oled display, rotating the rendered output based on the define passed in.
  170. // Returns true if the OLED was initialized successfully
  171. bool oled_init(oled_rotation_t rotation);
  172. // Called at the start of oled_init, weak function overridable by the user
  173. // rotation - the value passed into oled_init
  174. // Return new oled_rotation_t if you want to override default rotation
  175. oled_rotation_t oled_init_kb(oled_rotation_t rotation);
  176. oled_rotation_t oled_init_user(oled_rotation_t rotation);
  177. // Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
  178. void oled_clear(void);
  179. // Renders the dirty chunks of the buffer to oled display
  180. void oled_render(void);
  181. // Moves cursor to character position indicated by column and line, wraps if out of bounds
  182. // Max column denoted by 'oled_max_chars()' and max lines by 'oled_max_lines()' functions
  183. void oled_set_cursor(uint8_t col, uint8_t line);
  184. // Advances the cursor to the next page, writing ' ' if true
  185. // Wraps to the begining when out of bounds
  186. void oled_advance_page(bool clearPageRemainder);
  187. // Moves the cursor forward 1 character length
  188. // Advance page if there is not enough room for the next character
  189. // Wraps to the begining when out of bounds
  190. void oled_advance_char(void);
  191. // Writes a single character to the buffer at current cursor position
  192. // Advances the cursor while writing, inverts the pixels if true
  193. // Main handler that writes character data to the display buffer
  194. void oled_write_char(const char data, bool invert);
  195. // Writes a string to the buffer at current cursor position
  196. // Advances the cursor while writing, inverts the pixels if true
  197. void oled_write(const char *data, bool invert);
  198. // Writes a string to the buffer at current cursor position
  199. // Advances the cursor while writing, inverts the pixels if true
  200. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  201. void oled_write_ln(const char *data, bool invert);
  202. // Pans the buffer to the right (or left by passing true) by moving contents of the buffer
  203. // Useful for moving the screen in preparation for new drawing
  204. void oled_pan(bool left);
  205. // Returns a pointer to the requested start index in the buffer plus remaining
  206. // buffer length as struct
  207. oled_buffer_reader_t oled_read_raw(uint16_t start_index);
  208. // Writes a string to the buffer at current cursor position
  209. void oled_write_raw(const char *data, uint16_t size);
  210. // Writes a single byte into the buffer at the specified index
  211. void oled_write_raw_byte(const char data, uint16_t index);
  212. // Sets a specific pixel on or off
  213. // Coordinates start at top-left and go right and down for positive x and y
  214. void oled_write_pixel(uint8_t x, uint8_t y, bool on);
  215. #if defined(__AVR__)
  216. // Writes a PROGMEM string to the buffer at current cursor position
  217. // Advances the cursor while writing, inverts the pixels if true
  218. // Remapped to call 'void oled_write(const char *data, bool invert);' on ARM
  219. void oled_write_P(const char *data, bool invert);
  220. // Writes a PROGMEM string to the buffer at current cursor position
  221. // Advances the cursor while writing, inverts the pixels if true
  222. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  223. // Remapped to call 'void oled_write_ln(const char *data, bool invert);' on ARM
  224. void oled_write_ln_P(const char *data, bool invert);
  225. // Writes a PROGMEM string to the buffer at current cursor position
  226. void oled_write_raw_P(const char *data, uint16_t size);
  227. #else
  228. # define oled_write_P(data, invert) oled_write(data, invert)
  229. # define oled_write_ln_P(data, invert) oled_write_ln(data, invert)
  230. # define oled_write_raw_P(data, size) oled_write_raw(data, size)
  231. #endif // defined(__AVR__)
  232. // Can be used to manually turn on the screen if it is off
  233. // Returns true if the screen was on or turns on
  234. bool oled_on(void);
  235. // Can be used to manually turn off the screen if it is on
  236. // Returns true if the screen was off or turns off
  237. bool oled_off(void);
  238. // Returns true if the oled is currently on, false if it is
  239. // not
  240. bool is_oled_on(void);
  241. // Sets the brightness of the display
  242. uint8_t oled_set_brightness(uint8_t level);
  243. // Gets the current brightness of the display
  244. uint8_t oled_get_brightness(void);
  245. // Basically it's oled_render, but with timeout management and oled_task_user calling!
  246. void oled_task(void);
  247. // Called at the start of oled_task, weak function overridable by the user
  248. bool oled_task_kb(void);
  249. bool oled_task_user(void);
  250. // Set the specific 8 lines rows of the screen to scroll.
  251. // 0 is the default for start, and 7 for end, which is the entire
  252. // height of the screen. For 128x32 screens, rows 4-7 are not used.
  253. void oled_scroll_set_area(uint8_t start_line, uint8_t end_line);
  254. // Sets scroll speed, 0-7, fastest to slowest. Default is three.
  255. // Does not take effect until scrolling is either started or restarted
  256. // the ssd1306 supports 8 speeds with the delay
  257. // listed below betwen each frame of the scrolling effect
  258. // 0=2, 1=3, 2=4, 3=5, 4=25, 5=64, 6=128, 7=256
  259. void oled_scroll_set_speed(uint8_t speed);
  260. // Scrolls the entire display right
  261. // Returns true if the screen was scrolling or starts scrolling
  262. // NOTE: display contents cannot be changed while scrolling
  263. bool oled_scroll_right(void);
  264. // Scrolls the entire display left
  265. // Returns true if the screen was scrolling or starts scrolling
  266. // NOTE: display contents cannot be changed while scrolling
  267. bool oled_scroll_left(void);
  268. // Turns off display scrolling
  269. // Returns true if the screen was not scrolling or stops scrolling
  270. bool oled_scroll_off(void);
  271. // Returns true if the oled is currently scrolling, false if it is
  272. // not
  273. bool is_oled_scrolling(void);
  274. // Inverts the display
  275. // Returns true if the screen was or is inverted
  276. bool oled_invert(bool invert);
  277. // Returns the maximum number of characters that will fit on a line
  278. uint8_t oled_max_chars(void);
  279. // Returns the maximum number of lines that will fit on the oled
  280. uint8_t oled_max_lines(void);