oled_driver.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #if defined(OLED_DISPLAY_CUSTOM)
  18. // Expected user to implement the necessary defines
  19. #elif defined(OLED_DISPLAY_128X64)
  20. // Double height 128x64
  21. #ifndef OLED_DISPLAY_WIDTH
  22. #define OLED_DISPLAY_WIDTH 128
  23. #endif
  24. #ifndef OLED_DISPLAY_HEIGHT
  25. #define OLED_DISPLAY_HEIGHT 64
  26. #endif
  27. #ifndef OLED_MATRIX_SIZE
  28. #define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 1024 (compile time mathed)
  29. #endif
  30. #ifndef OLED_BLOCK_TYPE
  31. #define OLED_BLOCK_TYPE uint16_t
  32. #endif
  33. #ifndef OLED_BLOCK_COUNT
  34. #define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 32 (compile time mathed)
  35. #endif
  36. #ifndef OLED_BLOCK_SIZE
  37. #define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  38. #endif
  39. #ifndef OLED_COM_PINS
  40. #define OLED_COM_PINS COM_PINS_ALT
  41. #endif
  42. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  43. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  44. #ifndef OLED_SOURCE_MAP
  45. #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  46. #endif
  47. #ifndef OLED_TARGET_MAP
  48. #define OLED_TARGET_MAP { 56, 48, 40, 32, 24, 16, 8, 0 }
  49. #endif
  50. // If OLED_BLOCK_TYPE is uint32_t, these tables would look like:
  51. // #define OLED_SOURCE_MAP { 32, 40, 48, 56 }
  52. // #define OLED_TARGET_MAP { 24, 16, 8, 0 }
  53. // If OLED_BLOCK_TYPE is uint16_t, these tables would look like:
  54. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  55. // #define OLED_TARGET_MAP { 56, 48, 40, 32, 24, 16, 8, 0 }
  56. // If OLED_BLOCK_TYPE is uint8_t, these tables would look like:
  57. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120 }
  58. // #define OLED_TARGET_MAP { 56, 120, 48, 112, 40, 104, 32, 96, 24, 88, 16, 80, 8, 72, 0, 64 }
  59. #else // defined(OLED_DISPLAY_128X64)
  60. // Default 128x32
  61. #ifndef OLED_DISPLAY_WIDTH
  62. #define OLED_DISPLAY_WIDTH 128
  63. #endif
  64. #ifndef OLED_DISPLAY_HEIGHT
  65. #define OLED_DISPLAY_HEIGHT 32
  66. #endif
  67. #ifndef OLED_MATRIX_SIZE
  68. #define OLED_MATRIX_SIZE (OLED_DISPLAY_HEIGHT / 8 * OLED_DISPLAY_WIDTH) // 512 (compile time mathed)
  69. #endif
  70. #ifndef OLED_BLOCK_TYPE
  71. #define OLED_BLOCK_TYPE uint16_t // Type to use for segmenting the oled display for smart rendering, use unsigned types only
  72. #endif
  73. #ifndef OLED_BLOCK_COUNT
  74. #define OLED_BLOCK_COUNT (sizeof(OLED_BLOCK_TYPE) * 8) // 16 (compile time mathed)
  75. #endif
  76. #ifndef OLED_BLOCK_SIZE
  77. #define OLED_BLOCK_SIZE (OLED_MATRIX_SIZE / OLED_BLOCK_COUNT) // 32 (compile time mathed)
  78. #endif
  79. #ifndef OLED_COM_PINS
  80. #define OLED_COM_PINS COM_PINS_SEQ
  81. #endif
  82. // For 90 degree rotation, we map our internal matrix to oled matrix using fixed arrays
  83. // The OLED writes to it's memory horizontally, starting top left, but our memory starts bottom left in this mode
  84. #ifndef OLED_SOURCE_MAP
  85. #define OLED_SOURCE_MAP { 0, 8, 16, 24 }
  86. #endif
  87. #ifndef OLED_TARGET_MAP
  88. #define OLED_TARGET_MAP { 24, 16, 8, 0 }
  89. #endif
  90. // If OLED_BLOCK_TYPE is uint8_t, these tables would look like:
  91. // #define OLED_SOURCE_MAP { 0, 8, 16, 24, 32, 40, 48, 56 }
  92. // #define OLED_TARGET_MAP { 48, 32, 16, 0, 56, 40, 24, 8 }
  93. #endif // defined(OLED_DISPLAY_CUSTOM)
  94. // Address to use for tthe i2d oled communication
  95. #if !defined(OLED_DISPLAY_ADDRESS)
  96. #define OLED_DISPLAY_ADDRESS 0x3C
  97. #endif
  98. // Custom font file to use
  99. #if !defined(OLED_FONT_H)
  100. #define OLED_FONT_H "glcdfont.c"
  101. #endif
  102. // unsigned char value of the first character in the font file
  103. #if !defined(OLED_FONT_START)
  104. #define OLED_FONT_START 0
  105. #endif
  106. // unsigned char value of the last character in the font file
  107. #if !defined(OLED_FONT_END)
  108. #define OLED_FONT_END 224
  109. #endif
  110. // Font render width
  111. #if !defined(OLED_FONT_WIDTH)
  112. #define OLED_FONT_WIDTH 6
  113. #endif
  114. // Font render height
  115. #if !defined(OLED_FONT_HEIGHT)
  116. #define OLED_FONT_HEIGHT 8
  117. #endif
  118. // OLED Rotation enum values are flags
  119. typedef enum {
  120. OLED_ROTATION_0 = 0,
  121. OLED_ROTATION_90 = 1,
  122. OLED_ROTATION_180 = 2,
  123. OLED_ROTATION_270 = 3, // OLED_ROTATION_90 | OLED_ROTATION_180
  124. } oled_rotation_t;
  125. // Initialize the oled display, rotating the rendered output based on the define passed in.
  126. // Returns true if the OLED was initialized successfully
  127. bool oled_init(oled_rotation_t rotation);
  128. // Called at the start of oled_init, weak function overridable by the user
  129. // rotation - the value passed into oled_init
  130. // Return new oled_rotation_t if you want to override default rotation
  131. oled_rotation_t oled_init_user(oled_rotation_t rotation);
  132. // Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
  133. void oled_clear(void);
  134. // Renders the dirty chunks of the buffer to oled display
  135. void oled_render(void);
  136. // Moves cursor to character position indicated by column and line, wraps if out of bounds
  137. // Max column denoted by 'oled_max_chars()' and max lines by 'oled_max_lines()' functions
  138. void oled_set_cursor(uint8_t col, uint8_t line);
  139. // Advances the cursor to the next page, writing ' ' if true
  140. // Wraps to the begining when out of bounds
  141. void oled_advance_page(bool clearPageRemainder);
  142. // Moves the cursor forward 1 character length
  143. // Advance page if there is not enough room for the next character
  144. // Wraps to the begining when out of bounds
  145. void oled_advance_char(void);
  146. // Writes a single character to the buffer at current cursor position
  147. // Advances the cursor while writing, inverts the pixels if true
  148. // Main handler that writes character data to the display buffer
  149. void oled_write_char(const char data, bool invert);
  150. // Writes a string to the buffer at current cursor position
  151. // Advances the cursor while writing, inverts the pixels if true
  152. void oled_write(const char *data, bool invert);
  153. // Writes a string to the buffer at current cursor position
  154. // Advances the cursor while writing, inverts the pixels if true
  155. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  156. void oled_write_ln(const char *data, bool invert);
  157. #if defined(__AVR__)
  158. // Writes a PROGMEM string to the buffer at current cursor position
  159. // Advances the cursor while writing, inverts the pixels if true
  160. // Remapped to call 'void oled_write(const char *data, bool invert);' on ARM
  161. void oled_write_P(const char *data, bool invert);
  162. // Writes a PROGMEM string to the buffer at current cursor position
  163. // Advances the cursor while writing, inverts the pixels if true
  164. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  165. // Remapped to call 'void oled_write_ln(const char *data, bool invert);' on ARM
  166. void oled_write_ln_P(const char *data, bool invert);
  167. #else
  168. // Writes a string to the buffer at current cursor position
  169. // Advances the cursor while writing, inverts the pixels if true
  170. #define oled_write_P(data, invert) oled_write(data, invert)
  171. // Writes a string to the buffer at current cursor position
  172. // Advances the cursor while writing, inverts the pixels if true
  173. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  174. #define oled_write_ln_P(data, invert) oled_write(data, invert)
  175. #endif // defined(__AVR__)
  176. // Can be used to manually turn on the screen if it is off
  177. // Returns true if the screen was on or turns on
  178. bool oled_on(void);
  179. // Can be used to manually turn off the screen if it is on
  180. // Returns true if the screen was off or turns off
  181. bool oled_off(void);
  182. // Basically it's oled_render, but with timeout management and oled_task_user calling!
  183. void oled_task(void);
  184. // Called at the start of oled_task, weak function overridable by the user
  185. void oled_task_user(void);
  186. // Scrolls the entire display right
  187. // Returns true if the screen was scrolling or starts scrolling
  188. // NOTE: display contents cannot be changed while scrolling
  189. bool oled_scroll_right(void);
  190. // Scrolls the entire display left
  191. // Returns true if the screen was scrolling or starts scrolling
  192. // NOTE: display contents cannot be changed while scrolling
  193. bool oled_scroll_left(void);
  194. // Turns off display scrolling
  195. // Returns true if the screen was not scrolling or stops scrolling
  196. bool oled_scroll_off(void);
  197. // Returns the maximum number of characters that will fit on a line
  198. uint8_t oled_max_chars(void);
  199. // Returns the maximum number of lines that will fit on the oled
  200. uint8_t oled_max_lines(void);