st7565.h (7859B)
1 /* 2 Copyright 2021 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 2 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #pragma once 19 20 #include <stdint.h> 21 #include <stdbool.h> 22 23 #include "spi_master.h" 24 25 #ifndef ST7565_DISPLAY_WIDTH 26 # define ST7565_DISPLAY_WIDTH 128 27 #endif 28 #ifndef ST7565_DISPLAY_HEIGHT 29 # define ST7565_DISPLAY_HEIGHT 32 30 #endif 31 #ifndef ST7565_MATRIX_SIZE 32 # define ST7565_MATRIX_SIZE (ST7565_DISPLAY_HEIGHT / 8 * ST7565_DISPLAY_WIDTH) // 1024 (compile time mathed) 33 #endif 34 #ifndef ST7565_BLOCK_TYPE 35 # define ST7565_BLOCK_TYPE uint16_t 36 #endif 37 #ifndef ST7565_BLOCK_COUNT 38 # define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8) // 32 (compile time mathed) 39 #endif 40 #ifndef ST7565_BLOCK_SIZE 41 # define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT) // 32 (compile time mathed) 42 #endif 43 44 // the column address corresponding to the first column in the display hardware 45 #if !defined(ST7565_COLUMN_OFFSET) 46 # define ST7565_COLUMN_OFFSET 0 47 #endif 48 49 // spi clock divisor 50 #if !defined(ST7565_SPI_CLK_DIVISOR) 51 # define ST7565_SPI_CLK_DIVISOR 4 52 #endif 53 54 // Custom font file to use 55 #if !defined(ST7565_FONT_H) 56 # define ST7565_FONT_H "glcdfont.c" 57 #endif 58 // unsigned char value of the first character in the font file 59 #if !defined(ST7565_FONT_START) 60 # define ST7565_FONT_START 0 61 #endif 62 // unsigned char value of the last character in the font file 63 #if !defined(ST7565_FONT_END) 64 # define ST7565_FONT_END 223 65 #endif 66 // Font render width 67 #if !defined(ST7565_FONT_WIDTH) 68 # define ST7565_FONT_WIDTH 6 69 #endif 70 // Font render height 71 #if !defined(ST7565_FONT_HEIGHT) 72 # define ST7565_FONT_HEIGHT 8 73 #endif 74 // Default contrast level 75 #if !defined(ST7565_CONTRAST) 76 # define ST7565_CONTRAST 32 77 #endif 78 79 #if !defined(ST7565_TIMEOUT) 80 # if defined(ST7565_DISABLE_TIMEOUT) 81 # define ST7565_TIMEOUT 0 82 # else 83 # define ST7565_TIMEOUT 60000 84 # endif 85 #endif 86 87 #if !defined(ST7565_UPDATE_INTERVAL) && defined(SPLIT_KEYBOARD) 88 # define ST7565_UPDATE_INTERVAL 50 89 #endif 90 91 typedef struct __attribute__((__packed__)) { 92 uint8_t *current_element; 93 uint16_t remaining_element_count; 94 } display_buffer_reader_t; 95 96 // Rotation enum values are flags 97 typedef enum { DISPLAY_ROTATION_0, DISPLAY_ROTATION_180 } display_rotation_t; 98 99 // Initialize the display, rotating the rendered output based on the define passed in. 100 // Returns true if the display was initialized successfully 101 bool st7565_init(display_rotation_t rotation); 102 103 // Called at the start of st7565_init, weak function overridable by the user 104 // rotation - the value passed into st7565_init 105 // Return new display_rotation_t if you want to override default rotation 106 display_rotation_t st7565_init_user(display_rotation_t rotation); 107 108 // Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering 109 void st7565_clear(void); 110 111 // Renders the dirty chunks of the buffer to display 112 void st7565_render(void); 113 114 // Moves cursor to character position indicated by column and line, wraps if out of bounds 115 // Max column denoted by 'st7565_max_chars()' and max lines by 'st7565_max_lines()' functions 116 void st7565_set_cursor(uint8_t col, uint8_t line); 117 118 // Advances the cursor to the next page, writing ' ' if true 119 // Wraps to the beginning when out of bounds 120 void st7565_advance_page(bool clearPageRemainder); 121 122 // Moves the cursor forward 1 character length 123 // Advance page if there is not enough room for the next character 124 // Wraps to the beginning when out of bounds 125 void st7565_advance_char(void); 126 127 // Writes a single character to the buffer at current cursor position 128 // Advances the cursor while writing, inverts the pixels if true 129 // Main handler that writes character data to the display buffer 130 void st7565_write_char(const char data, bool invert); 131 132 // Writes a string to the buffer at current cursor position 133 // Advances the cursor while writing, inverts the pixels if true 134 void st7565_write(const char *data, bool invert); 135 136 // Writes a string to the buffer at current cursor position 137 // Advances the cursor while writing, inverts the pixels if true 138 // Advances the cursor to the next page, wiring ' ' to the remainder of the current page 139 void st7565_write_ln(const char *data, bool invert); 140 141 // Pans the buffer to the right (or left by passing true) by moving contents of the buffer 142 // Useful for moving the screen in preparation for new drawing 143 void st7565_pan(bool left); 144 145 // Returns a pointer to the requested start index in the buffer plus remaining 146 // buffer length as struct 147 display_buffer_reader_t st7565_read_raw(uint16_t start_index); 148 149 // Writes a string to the buffer at current cursor position 150 void st7565_write_raw(const char *data, uint16_t size); 151 152 // Writes a single byte into the buffer at the specified index 153 void st7565_write_raw_byte(const char data, uint16_t index); 154 155 // Sets a specific pixel on or off 156 // Coordinates start at top-left and go right and down for positive x and y 157 void st7565_write_pixel(uint8_t x, uint8_t y, bool on); 158 159 #if defined(__AVR__) 160 // Writes a PROGMEM string to the buffer at current cursor position 161 // Advances the cursor while writing, inverts the pixels if true 162 // Remapped to call 'void st7565_write(const char *data, bool invert);' on ARM 163 void st7565_write_P(const char *data, bool invert); 164 165 // Writes a PROGMEM string to the buffer at current cursor position 166 // Advances the cursor while writing, inverts the pixels if true 167 // Advances the cursor to the next page, wiring ' ' to the remainder of the current page 168 // Remapped to call 'void st7565_write_ln(const char *data, bool invert);' on ARM 169 void st7565_write_ln_P(const char *data, bool invert); 170 171 // Writes a PROGMEM string to the buffer at current cursor position 172 void st7565_write_raw_P(const char *data, uint16_t size); 173 #else 174 # define st7565_write_P(data, invert) st7565_write(data, invert) 175 # define st7565_write_ln_P(data, invert) st7565_write_ln(data, invert) 176 # define st7565_write_raw_P(data, size) st7565_write_raw(data, size) 177 #endif // defined(__AVR__) 178 179 // Can be used to manually turn on the screen if it is off 180 // Returns true if the screen was on or turns on 181 bool st7565_on(void); 182 183 // Called when st7565_on() turns on the screen, weak function overridable by the user 184 // Not called if the screen is already on 185 void st7565_on_user(void); 186 187 // Can be used to manually turn off the screen if it is on 188 // Returns true if the screen was off or turns off 189 bool st7565_off(void); 190 191 // Called when st7565_off() turns off the screen, weak function overridable by the user 192 // Not called if the screen is already off 193 void st7565_off_user(void); 194 195 // Returns true if the screen is currently on, false if it is 196 // not 197 bool st7565_is_on(void); 198 199 // Basically it's st7565_render, but with timeout management and st7565_task_user calling! 200 void st7565_task(void); 201 202 // Called at the start of st7565_task, weak function overridable by the user 203 void st7565_task_user(void); 204 205 // Inverts the display 206 // Returns true if the screen was or is inverted 207 bool st7565_invert(bool invert); 208 209 // Returns the maximum number of characters that will fit on a line 210 uint8_t st7565_max_chars(void); 211 212 // Returns the maximum number of lines that will fit on the display 213 uint8_t st7565_max_lines(void); 214 215 void st7565_reset(void); 216 217 spi_status_t st7565_send_cmd(uint8_t cmd); 218 219 spi_status_t st7565_send_data(uint8_t *data, uint16_t length);