oled_driver.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #define OLED_IC_SH1107 2
  21. #if defined(OLED_DISPLAY_CUSTOM)
  22. // Expected user to implement the necessary defines
  23. #elif defined(OLED_DISPLAY_128X64)
  24. // Double height 128x64
  25. # ifndef OLED_DISPLAY_WIDTH
  26. # define OLED_DISPLAY_WIDTH 128
  27. # endif
  28. # ifndef OLED_DISPLAY_HEIGHT
  29. # define OLED_DISPLAY_HEIGHT 64
  30. # endif
  31. # ifndef OLED_MATRIX_SIZE
  32. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 1024 (compile time mathed)
  33. # endif
  34. # ifndef OLED_BLOCK_TYPE
  35. # define OLED_BLOCK_TYPE uint16_t
  36. # endif
  37. # ifndef OLED_BLOCK_COUNT
  38. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 32 (compile time mathed)
  39. # endif
  40. # ifndef OLED_BLOCK_SIZE
  41. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  42. # endif
  43. # ifndef OLED_COM_PINS
  44. # define OLED_COM_PINS COM_PINS_ALT
  45. # endif
  46. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  47. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  48. # ifndef OLED_SOURCE_MAP
  49. # define OLED_SOURCE_MAP {0, 8, 16, 24, 32, 40, 48, 56}
  50. # endif
  51. # ifndef OLED_TARGET_MAP
  52. # define OLED_TARGET_MAP {56, 48, 40, 32, 24, 16, 8, 0}
  53. # endif
  54. // If OLED_BLOCK_TYPE is uint32_t, these tables would look like:
  55. // #define OLED_SOURCE_MAP { 32, 40, 48, 56 }
  56. // #define OLED_TARGET_MAP { 24, 16, 8, 0 }
  57. // If OLED_BLOCK_TYPE is uint16_t, these tables would look like:
  58. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  59. // #define OLED_TARGET_MAP { 56, 48, 40, 32, 24, 16, 8, 0 }
  60. // If OLED_BLOCK_TYPE is uint8_t, these tables would look like:
  61. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120 }
  62. // #define OLED_TARGET_MAP { 56, 120, 48, 112, 40, 104, 32, 96, 24, 88, 16, 80, 8, 72, 0, 64 }
  63. #elif defined(OLED_DISPLAY_64X32)
  64. # ifndef OLED_DISPLAY_WIDTH
  65. # define OLED_DISPLAY_WIDTH 64
  66. # endif
  67. # ifndef OLED_DISPLAY_HEIGHT
  68. # define OLED_DISPLAY_HEIGHT 32
  69. # endif
  70. # ifndef OLED_COLUMN_OFFSET
  71. # define OLED_COLUMN_OFFSET 32
  72. # endif
  73. # ifndef OLED_MATRIX_SIZE
  74. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH)
  75. # endif
  76. # ifndef OLED_BLOCK_TYPE
  77. # define OLED_BLOCK_TYPE uint8_t
  78. # endif
  79. # ifndef OLED_BLOCK_COUNT
  80. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 8 (compile time mathed)
  81. # endif
  82. # ifndef OLED_BLOCK_SIZE
  83. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  84. # endif
  85. # ifndef OLED_COM_PINS
  86. # define OLED_COM_PINS COM_PINS_ALT
  87. # endif
  88. # ifndef OLED_SOURCE_MAP
  89. # define OLED_SOURCE_MAP {0, 8, 16, 24}
  90. # endif
  91. # ifndef OLED_TARGET_MAP
  92. # define OLED_TARGET_MAP {24, 16, 8, 0}
  93. # endif
  94. #elif defined(OLED_DISPLAY_64X48)
  95. # ifndef OLED_DISPLAY_WIDTH
  96. # define OLED_DISPLAY_WIDTH 64
  97. # endif
  98. # ifndef OLED_DISPLAY_HEIGHT
  99. # define OLED_DISPLAY_HEIGHT 48
  100. # endif
  101. # ifndef OLED_COLUMN_OFFSET
  102. # define OLED_COLUMN_OFFSET 32
  103. # endif
  104. # ifndef OLED_MATRIX_SIZE
  105. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH)
  106. # endif
  107. # ifndef OLED_BLOCK_TYPE
  108. # define OLED_BLOCK_TYPE uint32_t
  109. # endif
  110. # ifndef OLED_BLOCK_COUNT
  111. # define OLED_BLOCK_COUNT 24
  112. # endif
  113. # ifndef OLED_BLOCK_SIZE
  114. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
  115. # endif
  116. # ifndef OLED_COM_PINS
  117. # define OLED_COM_PINS COM_PINS_ALT
  118. # endif
  119. # ifndef OLED_SOURCE_MAP
  120. # define OLED_SOURCE_MAP {0, 8}
  121. # endif
  122. # ifndef OLED_TARGET_MAP
  123. # define OLED_TARGET_MAP {8, 0}
  124. # endif
  125. #elif defined(OLED_DISPLAY_64X128)
  126. # ifndef OLED_DISPLAY_WIDTH
  127. # define OLED_DISPLAY_WIDTH 64
  128. # endif
  129. # ifndef OLED_DISPLAY_HEIGHT
  130. # define OLED_DISPLAY_HEIGHT 128
  131. # endif
  132. # ifndef OLED_IC
  133. # define OLED_IC OLED_IC_SH1107
  134. # endif
  135. # ifndef OLED_COM_PIN_OFFSET
  136. # define OLED_COM_PIN_OFFSET 32
  137. # endif
  138. # ifndef OLED_MATRIX_SIZE
  139. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH)
  140. # endif
  141. # ifndef OLED_BLOCK_TYPE
  142. # define OLED_BLOCK_TYPE uint16_t
  143. # endif
  144. # ifndef OLED_BLOCK_COUNT
  145. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8)
  146. # endif
  147. # ifndef OLED_BLOCK_SIZE
  148. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT)
  149. # endif
  150. # ifndef OLED_COM_PINS
  151. # define OLED_COM_PINS COM_PINS_ALT
  152. # endif
  153. # ifndef OLED_SOURCE_MAP
  154. # define OLED_SOURCE_MAP {0, 8, 16, 24, 32, 40, 48, 56}
  155. # endif
  156. # ifndef OLED_TARGET_MAP
  157. # define OLED_TARGET_MAP {56, 48, 40, 32, 24, 16, 8, 0}
  158. # endif
  159. #elif defined(OLED_DISPLAY_128X128)
  160. // Quad height 128x128
  161. # ifndef OLED_DISPLAY_WIDTH
  162. # define OLED_DISPLAY_WIDTH 128
  163. # endif
  164. # ifndef OLED_DISPLAY_HEIGHT
  165. # define OLED_DISPLAY_HEIGHT 128
  166. # endif
  167. # ifndef OLED_IC
  168. # define OLED_IC OLED_IC_SH1107
  169. # endif
  170. # ifndef OLED_MATRIX_SIZE
  171. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 2048 (compile time mathed)
  172. # endif
  173. # ifndef OLED_BLOCK_TYPE
  174. # define OLED_BLOCK_TYPE uint32_t
  175. # endif
  176. # ifndef OLED_BLOCK_COUNT
  177. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 32 (compile time mathed)
  178. # endif
  179. # ifndef OLED_BLOCK_SIZE
  180. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 64 (compile time mathed)
  181. # endif
  182. # ifndef OLED_COM_PINS
  183. # define OLED_COM_PINS COM_PINS_ALT
  184. # endif
  185. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  186. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  187. # ifndef OLED_SOURCE_MAP
  188. # define OLED_SOURCE_MAP {0, 8, 16, 24, 32, 40, 48, 56}
  189. # endif
  190. # ifndef OLED_TARGET_MAP
  191. # define OLED_TARGET_MAP {56, 48, 40, 32, 24, 16, 8, 0}
  192. # endif
  193. #else // defined(OLED_DISPLAY_128X64)
  194. // Default 128x32
  195. # ifndef OLED_DISPLAY_WIDTH
  196. # define OLED_DISPLAY_WIDTH 128
  197. # endif
  198. # ifndef OLED_DISPLAY_HEIGHT
  199. # define OLED_DISPLAY_HEIGHT 32
  200. # endif
  201. # ifndef OLED_MATRIX_SIZE
  202. # define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 512 (compile time mathed)
  203. # endif
  204. # ifndef OLED_BLOCK_TYPE
  205. # define OLED_BLOCK_TYPE uint16_t // Type to use for segmenting the oled display for smart rendering, use unsigned types only
  206. # endif
  207. # ifndef OLED_BLOCK_COUNT
  208. # define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 16 (compile time mathed)
  209. # endif
  210. # ifndef OLED_BLOCK_SIZE
  211. # define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  212. # endif
  213. # ifndef OLED_COM_PINS
  214. # define OLED_COM_PINS COM_PINS_SEQ
  215. # endif
  216. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  217. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  218. # ifndef OLED_SOURCE_MAP
  219. # define OLED_SOURCE_MAP {0, 8, 16, 24}
  220. # endif
  221. # ifndef OLED_TARGET_MAP
  222. # define OLED_TARGET_MAP {24, 16, 8, 0}
  223. # endif
  224. // If OLED_BLOCK_TYPE is uint8_t, these tables would look like:
  225. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  226. // #define OLED_TARGET_MAP { 48, 32, 16, 0, 56, 40, 24, 8 }
  227. #endif // defined(OLED_DISPLAY_CUSTOM)
  228. #if !defined(OLED_IC)
  229. # define OLED_IC OLED_IC_SSD1306
  230. #endif
  231. // the column address corresponding to the first column in the display hardware
  232. #if !defined(OLED_COLUMN_OFFSET)
  233. # define OLED_COLUMN_OFFSET 0
  234. #endif
  235. // Address to use for the i2c oled communication
  236. #if !defined(OLED_DISPLAY_ADDRESS)
  237. # define OLED_DISPLAY_ADDRESS 0x3C
  238. #endif
  239. // Custom font file to use
  240. #if !defined(OLED_FONT_H)
  241. # define OLED_FONT_H "glcdfont.c"
  242. #endif
  243. // unsigned char value of the first character in the font file
  244. #if !defined(OLED_FONT_START)
  245. # define OLED_FONT_START 0
  246. #endif
  247. // unsigned char value of the last character in the font file
  248. #if !defined(OLED_FONT_END)
  249. # define OLED_FONT_END 223
  250. #endif
  251. // Font render width
  252. #if !defined(OLED_FONT_WIDTH)
  253. # define OLED_FONT_WIDTH 6
  254. #endif
  255. // Font render height
  256. #if !defined(OLED_FONT_HEIGHT)
  257. # define OLED_FONT_HEIGHT 8
  258. #endif
  259. // Default brightness level
  260. #if !defined(OLED_BRIGHTNESS)
  261. # define OLED_BRIGHTNESS 255
  262. #endif
  263. #if !defined(OLED_TIMEOUT)
  264. # if defined(OLED_DISABLE_TIMEOUT)
  265. # define OLED_TIMEOUT 0
  266. # else
  267. # define OLED_TIMEOUT 60000
  268. # endif
  269. #endif
  270. #if !defined(OLED_FADE_OUT_INTERVAL)
  271. # define OLED_FADE_OUT_INTERVAL 0x00
  272. #endif
  273. #if OLED_FADE_OUT_INTERVAL > 0x0F || OLED_FADE_OUT_INTERVAL < 0x00
  274. # error OLED_FADE_OUT_INTERVAL must be between 0x00 and 0x0F
  275. #endif
  276. #if !defined(OLED_I2C_TIMEOUT)
  277. # define OLED_I2C_TIMEOUT 100
  278. #endif
  279. #if !defined(OLED_UPDATE_INTERVAL) && defined(SPLIT_KEYBOARD)
  280. # define OLED_UPDATE_INTERVAL 50
  281. #endif
  282. #if !defined(OLED_UPDATE_PROCESS_LIMIT)
  283. # define OLED_UPDATE_PROCESS_LIMIT 1
  284. #endif
  285. typedef struct __attribute__((__packed__)) {
  286. uint8_t *current_element;
  287. uint16_t remaining_element_count;
  288. } oled_buffer_reader_t;
  289. // OLED Rotation enum values are flags
  290. typedef enum {
  291. OLED_ROTATION_0 = 0,
  292. OLED_ROTATION_90 = 1,
  293. OLED_ROTATION_180 = 2,
  294. OLED_ROTATION_270 = 3, // OLED_ROTATION_90 | OLED_ROTATION_180
  295. } oled_rotation_t;
  296. // Initialize the oled display, rotating the rendered output based on the define passed in.
  297. // Returns true if the OLED was initialized successfully
  298. bool oled_init(oled_rotation_t rotation);
  299. // Send commands and data to screen
  300. bool oled_send_cmd(const uint8_t *data, uint16_t size);
  301. bool oled_send_cmd_P(const uint8_t *data, uint16_t size);
  302. bool oled_send_data(const uint8_t *data, uint16_t size);
  303. void oled_driver_init(void);
  304. // Called at the start of oled_init, weak function overridable by the user
  305. // rotation - the value passed into oled_init
  306. // Return new oled_rotation_t if you want to override default rotation
  307. oled_rotation_t oled_init_kb(oled_rotation_t rotation);
  308. oled_rotation_t oled_init_user(oled_rotation_t rotation);
  309. // Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
  310. void oled_clear(void);
  311. // Alias to oled_render_dirty to avoid a change in api.
  312. #define oled_render() oled_render_dirty(false)
  313. // Renders all dirty blocks to the display at one time or a subset depending on the value of
  314. // all.
  315. void oled_render_dirty(bool all);
  316. // Moves cursor to character position indicated by column and line, wraps if out of bounds
  317. // Max column denoted by 'oled_max_chars()' and max lines by 'oled_max_lines()' functions
  318. void oled_set_cursor(uint8_t col, uint8_t line);
  319. // Advances the cursor to the next page, writing ' ' if true
  320. // Wraps to the beginning when out of bounds
  321. void oled_advance_page(bool clearPageRemainder);
  322. // Moves the cursor forward 1 character length
  323. // Advance page if there is not enough room for the next character
  324. // Wraps to the beginning when out of bounds
  325. void oled_advance_char(void);
  326. // Writes a single character to the buffer at current cursor position
  327. // Advances the cursor while writing, inverts the pixels if true
  328. // Main handler that writes character data to the display buffer
  329. void oled_write_char(const char data, bool invert);
  330. // Writes a string to the buffer at current cursor position
  331. // Advances the cursor while writing, inverts the pixels if true
  332. void oled_write(const char *data, bool invert);
  333. // Writes a string to the buffer at current cursor position
  334. // Advances the cursor while writing, inverts the pixels if true
  335. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  336. void oled_write_ln(const char *data, bool invert);
  337. // Pans the buffer to the right (or left by passing true) by moving contents of the buffer
  338. // Useful for moving the screen in preparation for new drawing
  339. void oled_pan(bool left);
  340. // Returns a pointer to the requested start index in the buffer plus remaining
  341. // buffer length as struct
  342. oled_buffer_reader_t oled_read_raw(uint16_t start_index);
  343. // Writes a string to the buffer at current cursor position
  344. void oled_write_raw(const char *data, uint16_t size);
  345. // Writes a single byte into the buffer at the specified index
  346. void oled_write_raw_byte(const char data, uint16_t index);
  347. // Sets a specific pixel on or off
  348. // Coordinates start at top-left and go right and down for positive x and y
  349. void oled_write_pixel(uint8_t x, uint8_t y, bool on);
  350. #if defined(__AVR__)
  351. // Writes a PROGMEM string to the buffer at current cursor position
  352. // Advances the cursor while writing, inverts the pixels if true
  353. // Remapped to call 'void oled_write(const char *data, bool invert);' on ARM
  354. void oled_write_P(const char *data, bool invert);
  355. // Writes a PROGMEM string to the buffer at current cursor position
  356. // Advances the cursor while writing, inverts the pixels if true
  357. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  358. // Remapped to call 'void oled_write_ln(const char *data, bool invert);' on ARM
  359. void oled_write_ln_P(const char *data, bool invert);
  360. // Writes a PROGMEM string to the buffer at current cursor position
  361. void oled_write_raw_P(const char *data, uint16_t size);
  362. #else
  363. # define oled_write_P(data, invert) oled_write(data, invert)
  364. # define oled_write_ln_P(data, invert) oled_write_ln(data, invert)
  365. # define oled_write_raw_P(data, size) oled_write_raw(data, size)
  366. #endif // defined(__AVR__)
  367. // Can be used to manually turn on the screen if it is off
  368. // Returns true if the screen was on or turns on
  369. bool oled_on(void);
  370. // Can be used to manually turn off the screen if it is on
  371. // Returns true if the screen was off or turns off
  372. bool oled_off(void);
  373. // Returns true if the oled is currently on, false if it is
  374. // not
  375. bool is_oled_on(void);
  376. // Sets the brightness level of the display
  377. uint8_t oled_set_brightness(uint8_t level);
  378. // Gets the current brightness level of the display
  379. uint8_t oled_get_brightness(void);
  380. // Basically it's oled_render, but with timeout management and oled_task_user calling!
  381. void oled_task(void);
  382. // Called at the start of oled_task, weak function overridable by the user
  383. bool oled_task_kb(void);
  384. bool oled_task_user(void);
  385. // Set the specific 8 lines rows of the screen to scroll.
  386. // 0 is the default for start, and 7 for end, which is the entire
  387. // height of the screen. For 128x32 screens, rows 4-7 are not used.
  388. void oled_scroll_set_area(uint8_t start_line, uint8_t end_line);
  389. // Sets scroll speed, 0-7, fastest to slowest. Default is three.
  390. // Does not take effect until scrolling is either started or restarted
  391. // the ssd1306 supports 8 speeds with the delay
  392. // listed below between each frame of the scrolling effect
  393. // 0=2, 1=3, 2=4, 3=5, 4=25, 5=64, 6=128, 7=256
  394. void oled_scroll_set_speed(uint8_t speed);
  395. // Begin scrolling the entire display right
  396. // Returns true if the screen was scrolling or starts scrolling
  397. // NOTE: display contents cannot be changed while scrolling
  398. bool oled_scroll_right(void);
  399. // Begin scrolling the entire display left
  400. // Returns true if the screen was scrolling or starts scrolling
  401. // NOTE: display contents cannot be changed while scrolling
  402. bool oled_scroll_left(void);
  403. // Turns off display scrolling
  404. // Returns true if the screen was not scrolling or stops scrolling
  405. bool oled_scroll_off(void);
  406. // Returns true if the oled is currently scrolling, false if it is
  407. // not
  408. bool is_oled_scrolling(void);
  409. // Inverts the display
  410. // Returns true if the screen was or is inverted
  411. bool oled_invert(bool invert);
  412. // Returns the maximum number of characters that will fit on a line
  413. uint8_t oled_max_chars(void);
  414. // Returns the maximum number of lines that will fit on the oled
  415. uint8_t oled_max_lines(void);