|
- static const char *TAG = "display";
- #include <esp_log.h>
-
-
-
- #include "display.hpp"
-
-
- Display::Display(size_t w, size_t h) :
- width(w), height(h), pitch(w), buffer(NULL),
- bus(NULL), iface_drv(NULL), driver{.deinit = NULL}
- {
- esp_err_t ret;
-
- spi_config_t bus_cfg = {
- .miso_io_num = GPIO_NUM_4,
- .mosi_io_num = GPIO_NUM_35,
- .sclk_io_num = GPIO_NUM_36,
- .max_transfer_sz = (int)(height * width * sizeof(*buffer) + 8),
- };
-
- bus = spi_bus_create(SPI2_HOST, &bus_cfg);
- if (NULL == bus) {
- ESP_LOGE(TAG, "Failed to acquire bus");
- return;
- }
-
- scr_interface_spi_config_t spi_dev_cfg = {
- .spi_bus = bus,
- .pin_num_cs = 34,
- .pin_num_dc = 37,
- .clk_freq = 40 * 1000 * 1000,
- .swap_data = 1,
- };
-
- ret = scr_interface_create(SCREEN_IFACE_SPI, &spi_dev_cfg, &iface_drv);
- if (ESP_OK != ret) {
- ESP_LOGE(TAG, "Failed to create interface");
- return;
- }
-
- ret = scr_find_driver(SCREEN_CONTROLLER_ST7789, &driver);
- if (ESP_OK != ret) {
- ESP_LOGE(TAG, "Failed to find driver");
- return;
- }
-
- scr_controller_config_t lcd_cfg = {
- .interface_drv = iface_drv,
- .pin_num_rst = 38,
- .pin_num_bckl = 33,
- .rst_active_level = 0,
- .bckl_active_level = 1,
- .width = (uint16_t)width,
- .height = (uint16_t)height,
- .offset_hor = 52,
- .offset_ver = 40,
- .rotate = SCR_DIR_LRTB,
- };
-
- ret = driver.init(&lcd_cfg);
- if (ESP_OK != ret) {
- ESP_LOGE(TAG, "Failed to initialize driver");
- return;
- }
-
- buffer = new uint16_t[height * pitch]();
- refresh();
- }
-
- Display::~Display() {
- if (buffer) delete[] buffer;
- if (driver.deinit) driver.deinit();
- if (iface_drv) scr_interface_delete(iface_drv);
- if (bus) spi_bus_delete(&bus);
- }
-
- void Display::refresh() {
- driver.draw_bitmap(0, 0, width, height, buffer);
- }
|