logo

qmk_firmware

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

oled_driver.h (17052B)


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