You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.7 KiB

  1. /*
  2. Copyright 2021
  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. #include "spi_master.h"
  18. #ifndef ST7565_DISPLAY_WIDTH
  19. # define ST7565_DISPLAY_WIDTH 128
  20. #endif
  21. #ifndef ST7565_DISPLAY_HEIGHT
  22. # define ST7565_DISPLAY_HEIGHT 32
  23. #endif
  24. #ifndef ST7565_MATRIX_SIZE
  25. # define ST7565_MATRIX_SIZE (ST7565_DISPLAY_HEIGHT / 8 * ST7565_DISPLAY_WIDTH) // 1024 (compile time mathed)
  26. #endif
  27. #ifndef ST7565_BLOCK_TYPE
  28. # define ST7565_BLOCK_TYPE uint16_t
  29. #endif
  30. #ifndef ST7565_BLOCK_COUNT
  31. # define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8) // 32 (compile time mathed)
  32. #endif
  33. #ifndef ST7565_BLOCK_SIZE
  34. # define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT) // 32 (compile time mathed)
  35. #endif
  36. // the column address corresponding to the first column in the display hardware
  37. #if !defined(ST7565_COLUMN_OFFSET)
  38. # define ST7565_COLUMN_OFFSET 0
  39. #endif
  40. // spi clock divisor
  41. #if !defined(ST7565_SPI_CLK_DIVISOR)
  42. # define ST7565_SPI_CLK_DIVISOR 4
  43. #endif
  44. // Custom font file to use
  45. #if !defined(ST7565_FONT_H)
  46. # define ST7565_FONT_H "glcdfont.c"
  47. #endif
  48. // unsigned char value of the first character in the font file
  49. #if !defined(ST7565_FONT_START)
  50. # define ST7565_FONT_START 0
  51. #endif
  52. // unsigned char value of the last character in the font file
  53. #if !defined(ST7565_FONT_END)
  54. # define ST7565_FONT_END 223
  55. #endif
  56. // Font render width
  57. #if !defined(ST7565_FONT_WIDTH)
  58. # define ST7565_FONT_WIDTH 6
  59. #endif
  60. // Font render height
  61. #if !defined(ST7565_FONT_HEIGHT)
  62. # define ST7565_FONT_HEIGHT 8
  63. #endif
  64. // Default contrast level
  65. #if !defined(ST7565_CONTRAST)
  66. # define ST7565_CONTRAST 32
  67. #endif
  68. #if !defined(ST7565_TIMEOUT)
  69. # if defined(ST7565_DISABLE_TIMEOUT)
  70. # define ST7565_TIMEOUT 0
  71. # else
  72. # define ST7565_TIMEOUT 60000
  73. # endif
  74. #endif
  75. #if !defined(ST7565_UPDATE_INTERVAL) && defined(SPLIT_KEYBOARD)
  76. # define ST7565_UPDATE_INTERVAL 50
  77. #endif
  78. typedef struct __attribute__((__packed__)) {
  79. uint8_t *current_element;
  80. uint16_t remaining_element_count;
  81. } display_buffer_reader_t;
  82. // Rotation enum values are flags
  83. typedef enum { DISPLAY_ROTATION_0, DISPLAY_ROTATION_180 } display_rotation_t;
  84. // Initialize the display, rotating the rendered output based on the define passed in.
  85. // Returns true if the display was initialized successfully
  86. bool st7565_init(display_rotation_t rotation);
  87. // Called at the start of st7565_init, weak function overridable by the user
  88. // rotation - the value passed into st7565_init
  89. // Return new display_rotation_t if you want to override default rotation
  90. display_rotation_t st7565_init_user(display_rotation_t rotation);
  91. // Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
  92. void st7565_clear(void);
  93. // Renders the dirty chunks of the buffer to display
  94. void st7565_render(void);
  95. // Moves cursor to character position indicated by column and line, wraps if out of bounds
  96. // Max column denoted by 'st7565_max_chars()' and max lines by 'st7565_max_lines()' functions
  97. void st7565_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 st7565_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 st7565_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 st7565_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 st7565_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 st7565_write_ln(const char *data, bool invert);
  116. // Pans the buffer to the right (or left by passing true) by moving contents of the buffer
  117. // Useful for moving the screen in preparation for new drawing
  118. void st7565_pan(bool left);
  119. // Returns a pointer to the requested start index in the buffer plus remaining
  120. // buffer length as struct
  121. display_buffer_reader_t st7565_read_raw(uint16_t start_index);
  122. // Writes a string to the buffer at current cursor position
  123. void st7565_write_raw(const char *data, uint16_t size);
  124. // Writes a single byte into the buffer at the specified index
  125. void st7565_write_raw_byte(const char data, uint16_t index);
  126. // Sets a specific pixel on or off
  127. // Coordinates start at top-left and go right and down for positive x and y
  128. void st7565_write_pixel(uint8_t x, uint8_t y, bool on);
  129. #if defined(__AVR__)
  130. // Writes a PROGMEM string to the buffer at current cursor position
  131. // Advances the cursor while writing, inverts the pixels if true
  132. // Remapped to call 'void st7565_write(const char *data, bool invert);' on ARM
  133. void st7565_write_P(const char *data, bool invert);
  134. // Writes a PROGMEM string to the buffer at current cursor position
  135. // Advances the cursor while writing, inverts the pixels if true
  136. // Advances the cursor to the next page, wiring ' ' to the remainder of the current page
  137. // Remapped to call 'void st7565_write_ln(const char *data, bool invert);' on ARM
  138. void st7565_write_ln_P(const char *data, bool invert);
  139. // Writes a PROGMEM string to the buffer at current cursor position
  140. void st7565_write_raw_P(const char *data, uint16_t size);
  141. #else
  142. # define st7565_write_P(data, invert) st7565_write(data, invert)
  143. # define st7565_write_ln_P(data, invert) st7565_write_ln(data, invert)
  144. # define st7565_write_raw_P(data, size) st7565_write_raw(data, size)
  145. #endif // defined(__AVR__)
  146. // Can be used to manually turn on the screen if it is off
  147. // Returns true if the screen was on or turns on
  148. bool st7565_on(void);
  149. // Called when st7565_on() turns on the screen, weak function overridable by the user
  150. // Not called if the screen is already on
  151. void st7565_on_user(void);
  152. // Can be used to manually turn off the screen if it is on
  153. // Returns true if the screen was off or turns off
  154. bool st7565_off(void);
  155. // Called when st7565_off() turns off the screen, weak function overridable by the user
  156. // Not called if the screen is already off
  157. void st7565_off_user(void);
  158. // Returns true if the screen is currently on, false if it is
  159. // not
  160. bool st7565_is_on(void);
  161. // Basically it's st7565_render, but with timeout management and st7565_task_user calling!
  162. void st7565_task(void);
  163. // Called at the start of st7565_task, weak function overridable by the user
  164. void st7565_task_user(void);
  165. // Inverts the display
  166. // Returns true if the screen was or is inverted
  167. bool st7565_invert(bool invert);
  168. // Returns the maximum number of characters that will fit on a line
  169. uint8_t st7565_max_chars(void);
  170. // Returns the maximum number of lines that will fit on the display
  171. uint8_t st7565_max_lines(void);
  172. void st7565_reset(void);
  173. spi_status_t st7565_send_cmd(uint8_t cmd);
  174. spi_status_t st7565_send_data(uint8_t *data, uint16_t length);