oled_driver.h 8.0 KB

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