Add ST7565 LCD driver (#13089)
Co-authored-by: Joakim Tufvegren <jocke@barbanet.com>
This commit is contained in:
		
							parent
							
								
									cfc7ee61c5
								
							
						
					
					
						commit
						b2fdd48744
					
				
					 7 changed files with 995 additions and 0 deletions
				
			
		
							
								
								
									
										479
									
								
								drivers/lcd/st7565.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										479
									
								
								drivers/lcd/st7565.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,479 @@
 | 
			
		|||
/*
 | 
			
		||||
Copyright 2021
 | 
			
		||||
 | 
			
		||||
This program is free software: you can redistribute it and/or modify
 | 
			
		||||
it under the terms of the GNU General Public License as published by
 | 
			
		||||
the Free Software Foundation, either version 2 of the License, or
 | 
			
		||||
(at your option) any later version.
 | 
			
		||||
 | 
			
		||||
This program is distributed in the hope that it will be useful,
 | 
			
		||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
You should have received a copy of the GNU General Public License
 | 
			
		||||
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include "st7565.h"
 | 
			
		||||
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "keyboard.h"
 | 
			
		||||
#include "progmem.h"
 | 
			
		||||
#include "timer.h"
 | 
			
		||||
#include "wait.h"
 | 
			
		||||
 | 
			
		||||
#include ST7565_FONT_H
 | 
			
		||||
 | 
			
		||||
// Fundamental Commands
 | 
			
		||||
#define CONTRAST 0x81
 | 
			
		||||
#define DISPLAY_ALL_ON 0xA5
 | 
			
		||||
#define DISPLAY_ALL_ON_RESUME 0xA4
 | 
			
		||||
#define NORMAL_DISPLAY 0xA6
 | 
			
		||||
#define DISPLAY_ON 0xAF
 | 
			
		||||
#define DISPLAY_OFF 0xAE
 | 
			
		||||
#define NOP 0xE3
 | 
			
		||||
 | 
			
		||||
// Addressing Setting Commands
 | 
			
		||||
#define PAM_SETCOLUMN_LSB 0x00
 | 
			
		||||
#define PAM_SETCOLUMN_MSB 0x10
 | 
			
		||||
#define PAM_PAGE_ADDR 0xB0  // 0xb0 -- 0xb7
 | 
			
		||||
 | 
			
		||||
// Hardware Configuration Commands
 | 
			
		||||
#define DISPLAY_START_LINE 0x40
 | 
			
		||||
#define SEGMENT_REMAP 0xA0
 | 
			
		||||
#define SEGMENT_REMAP_INV 0xA1
 | 
			
		||||
#define COM_SCAN_INC 0xC0
 | 
			
		||||
#define COM_SCAN_DEC 0xC8
 | 
			
		||||
#define LCD_BIAS_7 0xA3
 | 
			
		||||
#define LCD_BIAS_9 0xA2
 | 
			
		||||
#define RESISTOR_RATIO 0x20
 | 
			
		||||
#define POWER_CONTROL 0x28
 | 
			
		||||
 | 
			
		||||
// Misc defines
 | 
			
		||||
#ifndef ST7565_BLOCK_COUNT
 | 
			
		||||
#    define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef ST7565_BLOCK_SIZE
 | 
			
		||||
#    define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define ST7565_ALL_BLOCKS_MASK (((((ST7565_BLOCK_TYPE)1 << (ST7565_BLOCK_COUNT - 1)) - 1) << 1) | 1)
 | 
			
		||||
 | 
			
		||||
#define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
 | 
			
		||||
 | 
			
		||||
// Display buffer's is the same as the display memory layout
 | 
			
		||||
// this is so we don't end up with rounding errors with
 | 
			
		||||
// parts of the display unusable or don't get cleared correctly
 | 
			
		||||
// and also allows for drawing & inverting
 | 
			
		||||
uint8_t            st7565_buffer[ST7565_MATRIX_SIZE];
 | 
			
		||||
uint8_t *          st7565_cursor;
 | 
			
		||||
ST7565_BLOCK_TYPE  st7565_dirty       = 0;
 | 
			
		||||
bool               st7565_initialized = false;
 | 
			
		||||
bool               st7565_active      = false;
 | 
			
		||||
display_rotation_t st7565_rotation    = DISPLAY_ROTATION_0;
 | 
			
		||||
#if ST7565_TIMEOUT > 0
 | 
			
		||||
uint32_t st7565_timeout;
 | 
			
		||||
#endif
 | 
			
		||||
#if ST7565_UPDATE_INTERVAL > 0
 | 
			
		||||
uint16_t st7565_update_timeout;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// Flips the rendering bits for a character at the current cursor position
 | 
			
		||||
static void InvertCharacter(uint8_t *cursor) {
 | 
			
		||||
    const uint8_t *end = cursor + ST7565_FONT_WIDTH;
 | 
			
		||||
    while (cursor < end) {
 | 
			
		||||
        *cursor = ~(*cursor);
 | 
			
		||||
        cursor++;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool st7565_init(display_rotation_t rotation) {
 | 
			
		||||
    setPinOutput(ST7565_A0_PIN);
 | 
			
		||||
    writePinHigh(ST7565_A0_PIN);
 | 
			
		||||
    setPinOutput(ST7565_RST_PIN);
 | 
			
		||||
    writePinHigh(ST7565_RST_PIN);
 | 
			
		||||
 | 
			
		||||
    st7565_rotation = st7565_init_user(rotation);
 | 
			
		||||
 | 
			
		||||
    spi_init();
 | 
			
		||||
    spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
 | 
			
		||||
 | 
			
		||||
    st7565_reset();
 | 
			
		||||
 | 
			
		||||
    st7565_send_cmd(LCD_BIAS_7);
 | 
			
		||||
    if (!HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
 | 
			
		||||
        st7565_send_cmd(SEGMENT_REMAP);
 | 
			
		||||
        st7565_send_cmd(COM_SCAN_DEC);
 | 
			
		||||
    } else {
 | 
			
		||||
        st7565_send_cmd(SEGMENT_REMAP_INV);
 | 
			
		||||
        st7565_send_cmd(COM_SCAN_INC);
 | 
			
		||||
    }
 | 
			
		||||
    st7565_send_cmd(DISPLAY_START_LINE | 0x00);
 | 
			
		||||
    st7565_send_cmd(CONTRAST);
 | 
			
		||||
    st7565_send_cmd(ST7565_CONTRAST);
 | 
			
		||||
    st7565_send_cmd(RESISTOR_RATIO | 0x01);
 | 
			
		||||
    st7565_send_cmd(POWER_CONTROL | 0x04);
 | 
			
		||||
    wait_ms(50);
 | 
			
		||||
    st7565_send_cmd(POWER_CONTROL | 0x06);
 | 
			
		||||
    wait_ms(50);
 | 
			
		||||
    st7565_send_cmd(POWER_CONTROL | 0x07);
 | 
			
		||||
    wait_ms(10);
 | 
			
		||||
    st7565_send_cmd(DISPLAY_ON);
 | 
			
		||||
    st7565_send_cmd(DISPLAY_ALL_ON_RESUME);
 | 
			
		||||
    st7565_send_cmd(NORMAL_DISPLAY);
 | 
			
		||||
 | 
			
		||||
    spi_stop();
 | 
			
		||||
 | 
			
		||||
#if ST7565_TIMEOUT > 0
 | 
			
		||||
    st7565_timeout = timer_read32() + ST7565_TIMEOUT;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    st7565_clear();
 | 
			
		||||
    st7565_initialized = true;
 | 
			
		||||
    st7565_active      = true;
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__attribute__((weak)) display_rotation_t st7565_init_user(display_rotation_t rotation) { return rotation; }
 | 
			
		||||
 | 
			
		||||
void st7565_clear(void) {
 | 
			
		||||
    memset(st7565_buffer, 0, sizeof(st7565_buffer));
 | 
			
		||||
    st7565_cursor = &st7565_buffer[0];
 | 
			
		||||
    st7565_dirty  = ST7565_ALL_BLOCKS_MASK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t crot(uint8_t a, int8_t n) {
 | 
			
		||||
    const uint8_t mask = 0x7;
 | 
			
		||||
    n &= mask;
 | 
			
		||||
    return a << n | a >> (-n & mask);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_render(void) {
 | 
			
		||||
    if (!st7565_initialized) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Do we have work to do?
 | 
			
		||||
    st7565_dirty &= ST7565_ALL_BLOCKS_MASK;
 | 
			
		||||
    if (!st7565_dirty) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Find first dirty block
 | 
			
		||||
    uint8_t update_start = 0;
 | 
			
		||||
    while (!(st7565_dirty & ((ST7565_BLOCK_TYPE)1 << update_start))) {
 | 
			
		||||
        ++update_start;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Calculate commands to set memory addressing bounds.
 | 
			
		||||
    uint8_t start_page   = ST7565_BLOCK_SIZE * update_start / ST7565_DISPLAY_WIDTH;
 | 
			
		||||
    uint8_t start_column = ST7565_BLOCK_SIZE * update_start % ST7565_DISPLAY_WIDTH;
 | 
			
		||||
    // IC has 132 segment drivers, for panels with less width we need to offset the starting column
 | 
			
		||||
    if (HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
 | 
			
		||||
        start_column += (132 - ST7565_DISPLAY_WIDTH);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
 | 
			
		||||
 | 
			
		||||
    st7565_send_cmd(PAM_PAGE_ADDR | start_page);
 | 
			
		||||
    st7565_send_cmd(PAM_SETCOLUMN_LSB | ((ST7565_COLUMN_OFFSET + start_column) & 0x0f));
 | 
			
		||||
    st7565_send_cmd(PAM_SETCOLUMN_MSB | ((ST7565_COLUMN_OFFSET + start_column) >> 4 & 0x0f));
 | 
			
		||||
 | 
			
		||||
    st7565_send_data(&st7565_buffer[ST7565_BLOCK_SIZE * update_start], ST7565_BLOCK_SIZE);
 | 
			
		||||
 | 
			
		||||
    // Turn on display if it is off
 | 
			
		||||
    st7565_on();
 | 
			
		||||
 | 
			
		||||
    // Clear dirty flag
 | 
			
		||||
    st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_set_cursor(uint8_t col, uint8_t line) {
 | 
			
		||||
    uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
 | 
			
		||||
 | 
			
		||||
    // Out of bounds?
 | 
			
		||||
    if (index >= ST7565_MATRIX_SIZE) {
 | 
			
		||||
        index = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    st7565_cursor = &st7565_buffer[index];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_advance_page(bool clearPageRemainder) {
 | 
			
		||||
    uint16_t index     = st7565_cursor - &st7565_buffer[0];
 | 
			
		||||
    uint8_t  remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
 | 
			
		||||
 | 
			
		||||
    if (clearPageRemainder) {
 | 
			
		||||
        // Remaining Char count
 | 
			
		||||
        remaining = remaining / ST7565_FONT_WIDTH;
 | 
			
		||||
 | 
			
		||||
        // Write empty character until next line
 | 
			
		||||
        while (remaining--) st7565_write_char(' ', false);
 | 
			
		||||
    } else {
 | 
			
		||||
        // Next page index out of bounds?
 | 
			
		||||
        if (index + remaining >= ST7565_MATRIX_SIZE) {
 | 
			
		||||
            index     = 0;
 | 
			
		||||
            remaining = 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        st7565_cursor = &st7565_buffer[index + remaining];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_advance_char(void) {
 | 
			
		||||
    uint16_t nextIndex      = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
 | 
			
		||||
    uint8_t  remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
 | 
			
		||||
 | 
			
		||||
    // Do we have enough space on the current line for the next character
 | 
			
		||||
    if (remainingSpace < ST7565_FONT_WIDTH) {
 | 
			
		||||
        nextIndex += remainingSpace;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Did we go out of bounds
 | 
			
		||||
    if (nextIndex >= ST7565_MATRIX_SIZE) {
 | 
			
		||||
        nextIndex = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Update cursor position
 | 
			
		||||
    st7565_cursor = &st7565_buffer[nextIndex];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Main handler that writes character data to the display buffer
 | 
			
		||||
void st7565_write_char(const char data, bool invert) {
 | 
			
		||||
    // Advance to the next line if newline
 | 
			
		||||
    if (data == '\n') {
 | 
			
		||||
        // Old source wrote ' ' until end of line...
 | 
			
		||||
        st7565_advance_page(true);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (data == '\r') {
 | 
			
		||||
        st7565_advance_page(false);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // copy the current render buffer to check for dirty after
 | 
			
		||||
    static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
 | 
			
		||||
    memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
 | 
			
		||||
 | 
			
		||||
    _Static_assert(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
 | 
			
		||||
 | 
			
		||||
    // set the reder buffer data
 | 
			
		||||
    uint8_t cast_data = (uint8_t)data;  // font based on unsigned type for index
 | 
			
		||||
    if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
 | 
			
		||||
        memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
 | 
			
		||||
    } else {
 | 
			
		||||
        const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
 | 
			
		||||
        memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Invert if needed
 | 
			
		||||
    if (invert) {
 | 
			
		||||
        InvertCharacter(st7565_cursor);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Dirty check
 | 
			
		||||
    if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
 | 
			
		||||
        uint16_t index = st7565_cursor - &st7565_buffer[0];
 | 
			
		||||
        st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
 | 
			
		||||
        // Edgecase check if the written data spans the 2 chunks
 | 
			
		||||
        st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Finally move to the next char
 | 
			
		||||
    st7565_advance_char();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write(const char *data, bool invert) {
 | 
			
		||||
    const char *end = data + strlen(data);
 | 
			
		||||
    while (data < end) {
 | 
			
		||||
        st7565_write_char(*data, invert);
 | 
			
		||||
        data++;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write_ln(const char *data, bool invert) {
 | 
			
		||||
    st7565_write(data, invert);
 | 
			
		||||
    st7565_advance_page(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_pan(bool left) {
 | 
			
		||||
    uint16_t i = 0;
 | 
			
		||||
    for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
 | 
			
		||||
        if (left) {
 | 
			
		||||
            for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
 | 
			
		||||
                i                = y * ST7565_DISPLAY_WIDTH + x;
 | 
			
		||||
                st7565_buffer[i] = st7565_buffer[i + 1];
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
 | 
			
		||||
                i                = y * ST7565_DISPLAY_WIDTH + x;
 | 
			
		||||
                st7565_buffer[i] = st7565_buffer[i - 1];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    st7565_dirty = ST7565_ALL_BLOCKS_MASK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
 | 
			
		||||
    if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
 | 
			
		||||
    display_buffer_reader_t ret_reader;
 | 
			
		||||
    ret_reader.current_element         = &st7565_buffer[start_index];
 | 
			
		||||
    ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
 | 
			
		||||
    return ret_reader;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write_raw_byte(const char data, uint16_t index) {
 | 
			
		||||
    if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
 | 
			
		||||
    if (st7565_buffer[index] == data) return;
 | 
			
		||||
    st7565_buffer[index] = data;
 | 
			
		||||
    st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write_raw(const char *data, uint16_t size) {
 | 
			
		||||
    uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
 | 
			
		||||
    if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
 | 
			
		||||
    for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
 | 
			
		||||
        if (st7565_buffer[i] == data[i]) continue;
 | 
			
		||||
        st7565_buffer[i] = data[i];
 | 
			
		||||
        st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
 | 
			
		||||
    if (x >= ST7565_DISPLAY_WIDTH) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
 | 
			
		||||
    if (index >= ST7565_MATRIX_SIZE) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    uint8_t data = st7565_buffer[index];
 | 
			
		||||
    if (on) {
 | 
			
		||||
        data |= (1 << (y % 8));
 | 
			
		||||
    } else {
 | 
			
		||||
        data &= ~(1 << (y % 8));
 | 
			
		||||
    }
 | 
			
		||||
    if (st7565_buffer[index] != data) {
 | 
			
		||||
        st7565_buffer[index] = data;
 | 
			
		||||
        st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined(__AVR__)
 | 
			
		||||
void st7565_write_P(const char *data, bool invert) {
 | 
			
		||||
    uint8_t c = pgm_read_byte(data);
 | 
			
		||||
    while (c != 0) {
 | 
			
		||||
        st7565_write_char(c, invert);
 | 
			
		||||
        c = pgm_read_byte(++data);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write_ln_P(const char *data, bool invert) {
 | 
			
		||||
    st7565_write_P(data, invert);
 | 
			
		||||
    st7565_advance_page(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void st7565_write_raw_P(const char *data, uint16_t size) {
 | 
			
		||||
    uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
 | 
			
		||||
    if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
 | 
			
		||||
    for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
 | 
			
		||||
        uint8_t c = pgm_read_byte(data++);
 | 
			
		||||
        if (st7565_buffer[i] == c) continue;
 | 
			
		||||
        st7565_buffer[i] = c;
 | 
			
		||||
        st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#endif  // defined(__AVR__)
 | 
			
		||||
 | 
			
		||||
bool st7565_on(void) {
 | 
			
		||||
    if (!st7565_initialized) {
 | 
			
		||||
        return st7565_active;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#if ST7565_TIMEOUT > 0
 | 
			
		||||
    st7565_timeout = timer_read32() + ST7565_TIMEOUT;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (!st7565_active) {
 | 
			
		||||
        spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
 | 
			
		||||
        st7565_send_cmd(DISPLAY_ON);
 | 
			
		||||
        spi_stop();
 | 
			
		||||
        st7565_active = true;
 | 
			
		||||
        st7565_on_user();
 | 
			
		||||
    }
 | 
			
		||||
    return st7565_active;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__attribute__((weak)) void st7565_on_user(void) {}
 | 
			
		||||
 | 
			
		||||
bool st7565_off(void) {
 | 
			
		||||
    if (!st7565_initialized) {
 | 
			
		||||
        return !st7565_active;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (st7565_active) {
 | 
			
		||||
        spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
 | 
			
		||||
        st7565_send_cmd(DISPLAY_OFF);
 | 
			
		||||
        spi_stop();
 | 
			
		||||
        st7565_active = false;
 | 
			
		||||
        st7565_off_user();
 | 
			
		||||
    }
 | 
			
		||||
    return !st7565_active;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__attribute__((weak)) void st7565_off_user(void) {}
 | 
			
		||||
 | 
			
		||||
bool st7565_is_on(void) { return st7565_active; }
 | 
			
		||||
 | 
			
		||||
uint8_t st7565_max_chars(void) { return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH; }
 | 
			
		||||
 | 
			
		||||
uint8_t st7565_max_lines(void) { return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT; }
 | 
			
		||||
 | 
			
		||||
void st7565_task(void) {
 | 
			
		||||
    if (!st7565_initialized) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#if ST7565_UPDATE_INTERVAL > 0
 | 
			
		||||
    if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
 | 
			
		||||
        st7565_update_timeout = timer_read();
 | 
			
		||||
        st7565_set_cursor(0, 0);
 | 
			
		||||
        st7565_task_user();
 | 
			
		||||
    }
 | 
			
		||||
#else
 | 
			
		||||
    st7565_set_cursor(0, 0);
 | 
			
		||||
    st7565_task_user();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    // Smart render system, no need to check for dirty
 | 
			
		||||
    st7565_render();
 | 
			
		||||
 | 
			
		||||
    // Display timeout check
 | 
			
		||||
#if ST7565_TIMEOUT > 0
 | 
			
		||||
    if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
 | 
			
		||||
        st7565_off();
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
__attribute__((weak)) void st7565_task_user(void) {}
 | 
			
		||||
 | 
			
		||||
void st7565_reset(void) {
 | 
			
		||||
    writePinLow(ST7565_RST_PIN);
 | 
			
		||||
    wait_ms(20);
 | 
			
		||||
    writePinHigh(ST7565_RST_PIN);
 | 
			
		||||
    wait_ms(20);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
spi_status_t st7565_send_cmd(uint8_t cmd) {
 | 
			
		||||
    writePinLow(ST7565_A0_PIN);
 | 
			
		||||
    return spi_write(cmd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
 | 
			
		||||
    writePinHigh(ST7565_A0_PIN);
 | 
			
		||||
    return spi_transmit(data, length);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										215
									
								
								drivers/lcd/st7565.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										215
									
								
								drivers/lcd/st7565.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,215 @@
 | 
			
		|||
/*
 | 
			
		||||
Copyright 2021
 | 
			
		||||
 | 
			
		||||
This program is free software: you can redistribute it and/or modify
 | 
			
		||||
it under the terms of the GNU General Public License as published by
 | 
			
		||||
the Free Software Foundation, either version 2 of the License, or
 | 
			
		||||
(at your option) any later version.
 | 
			
		||||
 | 
			
		||||
This program is distributed in the hope that it will be useful,
 | 
			
		||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
You should have received a copy of the GNU General Public License
 | 
			
		||||
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
#include "spi_master.h"
 | 
			
		||||
 | 
			
		||||
#ifndef ST7565_DISPLAY_WIDTH
 | 
			
		||||
#    define ST7565_DISPLAY_WIDTH 128
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef ST7565_DISPLAY_HEIGHT
 | 
			
		||||
#    define ST7565_DISPLAY_HEIGHT 32
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef ST7565_MATRIX_SIZE
 | 
			
		||||
#    define ST7565_MATRIX_SIZE (ST7565_DISPLAY_HEIGHT / 8 * ST7565_DISPLAY_WIDTH)  // 1024 (compile time mathed)
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef ST7565_BLOCK_TYPE
 | 
			
		||||
#    define ST7565_BLOCK_TYPE uint16_t
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef ST7565_BLOCK_COUNT
 | 
			
		||||
#    define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)  // 32 (compile time mathed)
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef ST7565_BLOCK_SIZE
 | 
			
		||||
#    define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)  // 32 (compile time mathed)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// the column address corresponding to the first column in the display hardware
 | 
			
		||||
#if !defined(ST7565_COLUMN_OFFSET)
 | 
			
		||||
#    define ST7565_COLUMN_OFFSET 0
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// spi clock divisor
 | 
			
		||||
#if !defined(ST7565_SPI_CLK_DIVISOR)
 | 
			
		||||
#    define ST7565_SPI_CLK_DIVISOR 4
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// Custom font file to use
 | 
			
		||||
#if !defined(ST7565_FONT_H)
 | 
			
		||||
#    define ST7565_FONT_H "glcdfont.c"
 | 
			
		||||
#endif
 | 
			
		||||
// unsigned char value of the first character in the font file
 | 
			
		||||
#if !defined(ST7565_FONT_START)
 | 
			
		||||
#    define ST7565_FONT_START 0
 | 
			
		||||
#endif
 | 
			
		||||
// unsigned char value of the last character in the font file
 | 
			
		||||
#if !defined(ST7565_FONT_END)
 | 
			
		||||
#    define ST7565_FONT_END 223
 | 
			
		||||
#endif
 | 
			
		||||
// Font render width
 | 
			
		||||
#if !defined(ST7565_FONT_WIDTH)
 | 
			
		||||
#    define ST7565_FONT_WIDTH 6
 | 
			
		||||
#endif
 | 
			
		||||
// Font render height
 | 
			
		||||
#if !defined(ST7565_FONT_HEIGHT)
 | 
			
		||||
#    define ST7565_FONT_HEIGHT 8
 | 
			
		||||
#endif
 | 
			
		||||
// Default contrast level
 | 
			
		||||
#if !defined(ST7565_CONTRAST)
 | 
			
		||||
#    define ST7565_CONTRAST 32
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if !defined(ST7565_TIMEOUT)
 | 
			
		||||
#    if defined(ST7565_DISABLE_TIMEOUT)
 | 
			
		||||
#        define ST7565_TIMEOUT 0
 | 
			
		||||
#    else
 | 
			
		||||
#        define ST7565_TIMEOUT 60000
 | 
			
		||||
#    endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if !defined(ST7565_UPDATE_INTERVAL) && defined(SPLIT_KEYBOARD)
 | 
			
		||||
#    define ST7565_UPDATE_INTERVAL 50
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
typedef struct __attribute__((__packed__)) {
 | 
			
		||||
    uint8_t *current_element;
 | 
			
		||||
    uint16_t remaining_element_count;
 | 
			
		||||
} display_buffer_reader_t;
 | 
			
		||||
 | 
			
		||||
// Rotation enum values are flags
 | 
			
		||||
typedef enum { DISPLAY_ROTATION_0, DISPLAY_ROTATION_180 } display_rotation_t;
 | 
			
		||||
 | 
			
		||||
// Initialize the display, rotating the rendered output based on the define passed in.
 | 
			
		||||
// Returns true if the display was initialized successfully
 | 
			
		||||
bool st7565_init(display_rotation_t rotation);
 | 
			
		||||
 | 
			
		||||
// Called at the start of st7565_init, weak function overridable by the user
 | 
			
		||||
// rotation - the value passed into st7565_init
 | 
			
		||||
// Return new display_rotation_t if you want to override default rotation
 | 
			
		||||
display_rotation_t st7565_init_user(display_rotation_t rotation);
 | 
			
		||||
 | 
			
		||||
// Clears the display buffer, resets cursor position to 0, and sets the buffer to dirty for rendering
 | 
			
		||||
void st7565_clear(void);
 | 
			
		||||
 | 
			
		||||
// Renders the dirty chunks of the buffer to display
 | 
			
		||||
void st7565_render(void);
 | 
			
		||||
 | 
			
		||||
// Moves cursor to character position indicated by column and line, wraps if out of bounds
 | 
			
		||||
// Max column denoted by 'st7565_max_chars()' and max lines by 'st7565_max_lines()' functions
 | 
			
		||||
void st7565_set_cursor(uint8_t col, uint8_t line);
 | 
			
		||||
 | 
			
		||||
// Advances the cursor to the next page, writing ' ' if true
 | 
			
		||||
// Wraps to the begining when out of bounds
 | 
			
		||||
void st7565_advance_page(bool clearPageRemainder);
 | 
			
		||||
 | 
			
		||||
// Moves the cursor forward 1 character length
 | 
			
		||||
// Advance page if there is not enough room for the next character
 | 
			
		||||
// Wraps to the begining when out of bounds
 | 
			
		||||
void st7565_advance_char(void);
 | 
			
		||||
 | 
			
		||||
// Writes a single character to the buffer at current cursor position
 | 
			
		||||
// Advances the cursor while writing, inverts the pixels if true
 | 
			
		||||
// Main handler that writes character data to the display buffer
 | 
			
		||||
void st7565_write_char(const char data, bool invert);
 | 
			
		||||
 | 
			
		||||
// Writes a string to the buffer at current cursor position
 | 
			
		||||
// Advances the cursor while writing, inverts the pixels if true
 | 
			
		||||
void st7565_write(const char *data, bool invert);
 | 
			
		||||
 | 
			
		||||
// Writes a string to the buffer at current cursor position
 | 
			
		||||
// Advances the cursor while writing, inverts the pixels if true
 | 
			
		||||
// Advances the cursor to the next page, wiring ' ' to the remainder of the current page
 | 
			
		||||
void st7565_write_ln(const char *data, bool invert);
 | 
			
		||||
 | 
			
		||||
// Pans the buffer to the right (or left by passing true) by moving contents of the buffer
 | 
			
		||||
// Useful for moving the screen in preparation for new drawing
 | 
			
		||||
void st7565_pan(bool left);
 | 
			
		||||
 | 
			
		||||
// Returns a pointer to the requested start index in the buffer plus remaining
 | 
			
		||||
// buffer length as struct
 | 
			
		||||
display_buffer_reader_t st7565_read_raw(uint16_t start_index);
 | 
			
		||||
 | 
			
		||||
// Writes a string to the buffer at current cursor position
 | 
			
		||||
void st7565_write_raw(const char *data, uint16_t size);
 | 
			
		||||
 | 
			
		||||
// Writes a single byte into the buffer at the specified index
 | 
			
		||||
void st7565_write_raw_byte(const char data, uint16_t index);
 | 
			
		||||
 | 
			
		||||
// Sets a specific pixel on or off
 | 
			
		||||
// Coordinates start at top-left and go right and down for positive x and y
 | 
			
		||||
void st7565_write_pixel(uint8_t x, uint8_t y, bool on);
 | 
			
		||||
 | 
			
		||||
#if defined(__AVR__)
 | 
			
		||||
// Writes a PROGMEM string to the buffer at current cursor position
 | 
			
		||||
// Advances the cursor while writing, inverts the pixels if true
 | 
			
		||||
// Remapped to call 'void st7565_write(const char *data, bool invert);' on ARM
 | 
			
		||||
void st7565_write_P(const char *data, bool invert);
 | 
			
		||||
 | 
			
		||||
// Writes a PROGMEM string to the buffer at current cursor position
 | 
			
		||||
// Advances the cursor while writing, inverts the pixels if true
 | 
			
		||||
// Advances the cursor to the next page, wiring ' ' to the remainder of the current page
 | 
			
		||||
// Remapped to call 'void st7565_write_ln(const char *data, bool invert);' on ARM
 | 
			
		||||
void st7565_write_ln_P(const char *data, bool invert);
 | 
			
		||||
 | 
			
		||||
// Writes a PROGMEM string to the buffer at current cursor position
 | 
			
		||||
void st7565_write_raw_P(const char *data, uint16_t size);
 | 
			
		||||
#else
 | 
			
		||||
#    define st7565_write_P(data, invert) st7565_write(data, invert)
 | 
			
		||||
#    define st7565_write_ln_P(data, invert) st7565_write_ln(data, invert)
 | 
			
		||||
#    define st7565_write_raw_P(data, size) st7565_write_raw(data, size)
 | 
			
		||||
#endif  // defined(__AVR__)
 | 
			
		||||
 | 
			
		||||
// Can be used to manually turn on the screen if it is off
 | 
			
		||||
// Returns true if the screen was on or turns on
 | 
			
		||||
bool st7565_on(void);
 | 
			
		||||
 | 
			
		||||
// Called when st7565_on() turns on the screen, weak function overridable by the user
 | 
			
		||||
// Not called if the screen is already on
 | 
			
		||||
void st7565_on_user(void);
 | 
			
		||||
 | 
			
		||||
// Can be used to manually turn off the screen if it is on
 | 
			
		||||
// Returns true if the screen was off or turns off
 | 
			
		||||
bool st7565_off(void);
 | 
			
		||||
 | 
			
		||||
// Called when st7565_off() turns off the screen, weak function overridable by the user
 | 
			
		||||
// Not called if the screen is already off
 | 
			
		||||
void st7565_off_user(void);
 | 
			
		||||
 | 
			
		||||
// Returns true if the screen is currently on, false if it is
 | 
			
		||||
// not
 | 
			
		||||
bool st7565_is_on(void);
 | 
			
		||||
 | 
			
		||||
// Basically it's st7565_render, but with timeout management and st7565_task_user calling!
 | 
			
		||||
void st7565_task(void);
 | 
			
		||||
 | 
			
		||||
// Called at the start of st7565_task, weak function overridable by the user
 | 
			
		||||
void st7565_task_user(void);
 | 
			
		||||
 | 
			
		||||
// Returns the maximum number of characters that will fit on a line
 | 
			
		||||
uint8_t st7565_max_chars(void);
 | 
			
		||||
 | 
			
		||||
// Returns the maximum number of lines that will fit on the display
 | 
			
		||||
uint8_t st7565_max_lines(void);
 | 
			
		||||
 | 
			
		||||
void st7565_reset(void);
 | 
			
		||||
 | 
			
		||||
spi_status_t st7565_send_cmd(uint8_t cmd);
 | 
			
		||||
 | 
			
		||||
spi_status_t st7565_send_data(uint8_t *data, uint16_t length);
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue