qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

oled_driver.h (9793B)


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