#ifndef ST7920_H #define ST7920_H #include "esp_err.h" #include "driver/spi_master.h" // Display dimensions #define ST7920_WIDTH 128 #define ST7920_HEIGHT 64 // SPI commands for ST7920 #define ST7920_CMD_BASIC_FUNC 0x30 // Basic instruction set #define ST7920_CMD_EXTENDED_FUNC 0x34 // Extended instruction set #define ST7920_CMD_DISPLAY_ON 0x0C // Display ON #define ST7920_CMD_DISPLAY_OFF 0x08 // Display OFF #define ST7920_CMD_CLEAR 0x01 // Clear display #define ST7920_CMD_ENTRY_MODE 0x06 // Entry mode #define ST7920_CMD_HOME 0x02 // Return home #define ST7920_CMD_GRAPHIC_ON 0x36 // Extended + Graphic mode ON #define ST7920_CMD_SET_GRAPHIC_ADDR 0x80 // Set GDRAM address // Configuration structure typedef struct { spi_host_device_t spi_host; int pin_cs; int pin_bl; int spi_clock_speed_hz; } st7920_config_t; // Handle for ST7920 device typedef struct { spi_device_handle_t spi; int pin_cs; int pin_bl; uint8_t framebuffer[ST7920_WIDTH * ST7920_HEIGHT / 8]; } st7920_device_t; /** * @brief Initialize ST7920 display * * @param config Configuration structure * @param out_dev Pointer to store device handle * @return esp_err_t ESP_OK on success */ esp_err_t st7920_init(const st7920_config_t *config, st7920_device_t *device); /** * @brief Send command to ST7920 * * @param dev Device handle * @param cmd Command byte * @return esp_err_t ESP_OK on success */ esp_err_t st7920_send_cmd(const st7920_device_t *dev, uint8_t cmd); /** * @brief Send data to ST7920 * * @param dev Device handle * @param data Data byte * @return esp_err_t ESP_OK on success */ esp_err_t st7920_send_data(const st7920_device_t *dev, uint8_t data); /** * @brief Clear the display * * @param dev Device handle * @return esp_err_t ESP_OK on success */ esp_err_t st7920_clear(void *p); /** * @brief Set backlight on or off * * @param dev Device handle * @param backlight Backlight enabled */ esp_err_t st7920_set_backlight(const st7920_device_t *dev, bool backlight); /** * @brief Update the entire display with framebuffer contents * * @param dev Device handle * @return esp_err_t ESP_OK on success */ esp_err_t st7920_update_screen(const void *dev); /** * @brief Turn a pixel ON at given coordinates * @param dev Device handle * @param x x coordinate * @param y y coordinate */ esp_err_t st7920_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 st7920_clear_pixel(void *dev, uint8_t x, uint8_t y); #endif // ST7920_H