143 lines
4.4 KiB
C
143 lines
4.4 KiB
C
#pragma once
|
|
|
|
#include <esp_err.h>
|
|
#include <stdint.h>
|
|
#include <soc/gpio_num.h>
|
|
#include <driver/spi_master.h>
|
|
#include <driver/i2c_master.h>
|
|
|
|
// Display dimensions
|
|
#define ST7567_WIDTH 128
|
|
#define ST7567_HEIGHT 64
|
|
#define ST7567_PAGES (ST7567_HEIGHT / 8)
|
|
|
|
#define ST7567_DEFAULT_CONTRAST 0x35
|
|
|
|
// ST7567 command set
|
|
#define ST7567_CMD_DISPLAY_OFF 0xAE
|
|
#define ST7567_CMD_DISPLAY_ON 0xAF
|
|
#define ST7567_CMD_START_LINE 0x40 // OR with 0-63
|
|
#define ST7567_CMD_PAGE_ADDR 0xB0 // OR with 0-8
|
|
#define ST7567_CMD_COL_ADDR_HIGH 0x10 // OR with high nibble
|
|
#define ST7567_CMD_COL_ADDR_LOW 0x00 // OR with low nibble
|
|
#define ST7567_CMD_ADC_NORMAL 0xA0 // column addr 0 = SEG0
|
|
#define ST7567_CMD_ADC_REVERSE 0xA1 // column addr 131 = SEG0
|
|
#define ST7567_CMD_ALL_PIXEL_NORMAL 0xA4
|
|
#define ST7567_CMD_ALL_PIXEL_ON 0xA5
|
|
#define ST7567_CMD_DISPLAY_NORMAL 0xA6
|
|
#define ST7567_CMD_DISPLAY_INVERSE 0xA7
|
|
#define ST7567_CMD_BIAS_1_9 0xA2
|
|
#define ST7567_CMD_BIAS_1_7 0xA3
|
|
#define ST7567_CMD_RESET 0xE2
|
|
#define ST7567_CMD_COM_NORMAL 0xC0
|
|
#define ST7567_CMD_COM_REVERSE 0xC8
|
|
#define ST7567_CMD_POWER_CTRL 0x28 // OR with 0-7 (booster/regulator/follower bits)
|
|
#define ST7567_CMD_RESISTOR_RATIO 0x20 // OR with 0-7
|
|
#define ST7567_CMD_CONTRAST_MODE 0x81 // followed by a second byte, 0-63
|
|
#define ST7567_CMD_STATIC_INDICATOR_OFF 0xAC
|
|
#define ST7567_CMD_NOP 0xE3
|
|
|
|
#define ST7567_I2C_ADDR_DEFAULT 0x3f
|
|
#define ST7567_I2C_CTRL_CMD 0x00
|
|
#define ST7567_I2C_CTRL_DATA 0x40
|
|
#define ST7567_I2C_TIMEOUT_MS 100
|
|
#define ST7567_DEFAULT_I2C_CLOCK_HZ 400000
|
|
|
|
#define ST7567_SPI_TIMEOUT_MS 100
|
|
#define ST7567_DEFAULT_SPI_CLOCK_HZ 8000000
|
|
#define ST7567_DC_CMD 0
|
|
#define ST7567_DC_DATA 1
|
|
|
|
typedef enum {
|
|
ST7567_BUS_TYPE_I2C,
|
|
ST7567_BUS_TYPE_SPI,
|
|
} st7567_bus_type_t;
|
|
|
|
typedef struct {
|
|
void *bus;
|
|
st7567_bus_type_t bus_type;
|
|
gpio_num_t dc_gpio;
|
|
uint8_t framebuffer[ST7567_WIDTH * ST7567_PAGES];
|
|
} st7567_device_t;
|
|
|
|
typedef struct {
|
|
i2c_master_bus_handle_t i2c_bus; // already-initialized I2C bus
|
|
uint16_t i2c_addr; // 7-bit device address, 0 = use ST7567_I2C_ADDR_DEFAULT
|
|
uint32_t i2c_clock_speed_hz; // 0 = use 400kHz default
|
|
uint8_t contrast; // 0-63, 0 = use a sane default (0x20)
|
|
} st7567_config_i2c_t;
|
|
|
|
typedef struct {
|
|
spi_host_device_t spi_host; // already-initialized SPI bus (spi_bus_initialize called by caller)
|
|
gpio_num_t cs_gpio; // chip select
|
|
gpio_num_t dc_gpio; // data/command select (required, no in-band control byte over SPI)
|
|
gpio_num_t rst_gpio; // hardware reset, GPIO_NUM_NC if not wired
|
|
uint32_t spi_clock_speed_hz; // 0 = use 8MHz default
|
|
uint8_t contrast; // 0-63, 0 = use a sane default (0x20)
|
|
} st7567_config_spi_t;
|
|
|
|
/**
|
|
* @brief Initialize ST7567 display on an I2C bus
|
|
*
|
|
* @param config Configuration structure
|
|
* @param device Pointer to store device state (framebuffer etc, caller-owned)
|
|
* @return esp_err_t ESP_OK on success
|
|
*/
|
|
esp_err_t st7567_init_i2c(const st7567_config_i2c_t *config, st7567_device_t *device);
|
|
|
|
/**
|
|
* @brief Initialize ST7567 display on an SPI bus
|
|
*
|
|
* @param config Configuration structure
|
|
* @param device Pointer to store device state (framebuffer etc, caller-owned)
|
|
* @return esp_err_t ESP_OK on success
|
|
*/
|
|
esp_err_t st7567_init_spi(const st7567_config_spi_t *config, st7567_device_t *device);
|
|
|
|
/**
|
|
* @brief Clear the display (just wipe framebuffer, don't send any commands)
|
|
*
|
|
* @param p Device handle
|
|
*/
|
|
esp_err_t st7567_clear_buffer(void *p);
|
|
|
|
/**
|
|
* @brief Clear the display (both GDRAM and local framebuffer)
|
|
*
|
|
* @param dev Device handle
|
|
* @return esp_err_t ESP_OK on success
|
|
*/
|
|
esp_err_t st7567_clear(void *dev);
|
|
|
|
/**
|
|
* @brief Set contrast (electronic volume)
|
|
*
|
|
* @param dev Device handle
|
|
* @param contrast 0-63
|
|
* @return esp_err_t ESP_OK on success
|
|
*/
|
|
esp_err_t st7567_set_contrast(const st7567_device_t *dev, uint8_t contrast);
|
|
|
|
/**
|
|
* @brief Push the local framebuffer contents to the display
|
|
*
|
|
* @param p Device handle
|
|
* @return esp_err_t ESP_OK on success
|
|
*/
|
|
esp_err_t st7567_update_screen(const void *p);
|
|
|
|
/**
|
|
* @brief Turn a pixel ON at given coordinates
|
|
* @param dev Device handle
|
|
* @param x x coordinate
|
|
* @param y y coordinate
|
|
*/
|
|
esp_err_t st7567_draw_pixel(void *dev, uint8_t x, uint8_t y);
|
|
|
|
/**
|
|
* @brief Turn a pixel OFF at given coordinates
|
|
* @param dev Device handle
|
|
* @param x x coordinate
|
|
* @param y y coordinate
|
|
*/
|
|
esp_err_t st7567_clear_pixel(void *dev, uint8_t x, uint8_t y); |