Files
esp-st7567/st7567/st7567-common.h
2026-08-20 21:37:57 +02:00

108 lines
3.1 KiB
C

#pragma once
#include <esp_err.h>
#include <stdint.h>
#include <soc/gpio_num.h>
// 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
// Display dimensions
#define ST7567_WIDTH 128
#define ST7567_HEIGHT 64
#define ST7567_PAGES (ST7567_HEIGHT / 8)
typedef struct {
void* bus;
gpio_num_t dc_gpio;
uint8_t framebuffer[ST7567_WIDTH * ST7567_PAGES];
} st7567_device_t;
/**
* @brief Send a single command byte to the ST7567
*
* @param dev Device handle
* @param cmd Command byte
* @return esp_err_t ESP_OK on success
*/
esp_err_t st7567_send_cmd(const st7567_device_t *dev, uint8_t cmd);
/**
* @brief Send a data buffer to the ST7567
*
* @param dev Device handle
* @param data Pointer to data buffer
* @param len Length of data buffer
* @return esp_err_t ESP_OK on success
*/
esp_err_t st7567_send_data(const st7567_device_t *dev, const uint8_t *data, size_t len);
/**
* @brief Clear the display (just wipe framebuffer, don't send any commands)
*
* @param dev Device handle
*/
void st7567_clear_buffer(st7567_device_t *dev);
/**
* @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(st7567_device_t *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 dev Device handle
* @return esp_err_t ESP_OK on success
*/
esp_err_t st7567_update_screen(const st7567_device_t *dev);
/**
* @brief Turn a pixel ON at given coordinates
* @param dev Device handle
* @param x x coordinate
* @param y y coordinate
*/
void 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
*/
void st7567_clear_pixel(void *dev, uint8_t x, uint8_t y);