Hal refactored (#99)
This commit is contained in:
committed by
GitHub
parent
0188ce721c
commit
33bb742dfb
@@ -0,0 +1,206 @@
|
||||
#include "TdeckDisplay.h"
|
||||
#include "TdeckDisplayConstants.h"
|
||||
#include "TdeckTouch.h"
|
||||
#include "Log.h"
|
||||
|
||||
#include <TactilityCore.h>
|
||||
|
||||
#include "driver/ledc.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_lcd_panel_ops.h"
|
||||
#include "esp_lcd_panel_vendor.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
|
||||
#define TAG "tdeck_display"
|
||||
|
||||
static bool isBacklightInitialized = false;
|
||||
|
||||
static bool initBacklight() {
|
||||
TT_LOG_I(TAG, "Init backlight");
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.speed_mode = TDECK_LCD_BACKLIGHT_LEDC_MODE,
|
||||
.duty_resolution = TDECK_LCD_BACKLIGHT_LEDC_DUTY_RES,
|
||||
.timer_num = TDECK_LCD_BACKLIGHT_LEDC_TIMER,
|
||||
.freq_hz = TDECK_LCD_BACKLIGHT_LEDC_FREQUENCY,
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
.deconfigure = false
|
||||
};
|
||||
|
||||
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Backlight led timer config failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool setBacklight(uint8_t duty) {
|
||||
ledc_channel_config_t ledc_channel = {
|
||||
.gpio_num = TDECK_LCD_PIN_BACKLIGHT,
|
||||
.speed_mode = TDECK_LCD_BACKLIGHT_LEDC_MODE,
|
||||
.channel = TDECK_LCD_BACKLIGHT_LEDC_CHANNEL,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.timer_sel = TDECK_LCD_BACKLIGHT_LEDC_TIMER,
|
||||
.duty = duty,
|
||||
.hpoint = 0,
|
||||
.flags = {
|
||||
.output_invert = 0
|
||||
}
|
||||
};
|
||||
|
||||
// Setting the config in the timer init and then calling ledc_set_duty() doesn't work when
|
||||
// the app is running. For an unknown reason we have to call this config method every time:
|
||||
return ledc_channel_config(&ledc_channel) == ESP_OK;
|
||||
}
|
||||
|
||||
bool TdeckDisplay::start() {
|
||||
TT_LOG_I(TAG, "Starting");
|
||||
|
||||
const esp_lcd_panel_io_spi_config_t panel_io_config = {
|
||||
.cs_gpio_num = TDECK_LCD_PIN_CS,
|
||||
.dc_gpio_num = TDECK_LCD_PIN_DC,
|
||||
.spi_mode = 0,
|
||||
.pclk_hz = TDECK_LCD_SPI_FREQUENCY,
|
||||
.trans_queue_depth = 10,
|
||||
.on_color_trans_done = nullptr,
|
||||
.user_ctx = nullptr,
|
||||
.lcd_cmd_bits = 8,
|
||||
.lcd_param_bits = 8,
|
||||
.flags = {
|
||||
.dc_high_on_cmd = 0,
|
||||
.dc_low_on_data = 0,
|
||||
.dc_low_on_param = 0,
|
||||
.octal_mode = 0,
|
||||
.quad_mode = 0,
|
||||
.sio_mode = 1,
|
||||
.lsb_first = 0,
|
||||
.cs_high_active = 0,
|
||||
}
|
||||
};
|
||||
|
||||
if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TDECK_LCD_SPI_HOST, &panel_io_config, &ioHandle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to create panel IO");
|
||||
return false;
|
||||
}
|
||||
|
||||
const esp_lcd_panel_dev_config_t panel_config = {
|
||||
.reset_gpio_num = GPIO_NUM_NC,
|
||||
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
|
||||
.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
|
||||
.bits_per_pixel = TDECK_LCD_BITS_PER_PIXEL,
|
||||
.flags = {
|
||||
.reset_active_high = 0
|
||||
},
|
||||
.vendor_config = nullptr
|
||||
};
|
||||
|
||||
if (esp_lcd_new_panel_st7789(ioHandle, &panel_config, &panelHandle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to create panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_reset(panelHandle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to reset panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_init(panelHandle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to init panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_invert_color(panelHandle, true) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to init panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_swap_xy(panelHandle, true) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to init panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_mirror(panelHandle, true, false) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to init panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_disp_on_off(panelHandle, true) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to turn display on");
|
||||
return false;
|
||||
}
|
||||
|
||||
const lvgl_port_display_cfg_t disp_cfg = {
|
||||
.io_handle = ioHandle,
|
||||
.panel_handle = panelHandle,
|
||||
.buffer_size = TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_DRAW_BUFFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8),
|
||||
.double_buffer = true, // Disable to free up SPIRAM
|
||||
.trans_size = 0,
|
||||
.hres = TDECK_LCD_HORIZONTAL_RESOLUTION,
|
||||
.vres = TDECK_LCD_VERTICAL_RESOLUTION,
|
||||
.monochrome = false,
|
||||
.rotation = {
|
||||
.swap_xy = true,
|
||||
.mirror_x = true,
|
||||
.mirror_y = false,
|
||||
},
|
||||
.flags = {
|
||||
.buff_dma = false,
|
||||
.buff_spiram = true,
|
||||
.sw_rotate = false,
|
||||
.swap_bytes = true
|
||||
},
|
||||
};
|
||||
|
||||
displayHandle = lvgl_port_add_disp(&disp_cfg);
|
||||
TT_LOG_I(TAG, "Finished");
|
||||
return displayHandle != nullptr;
|
||||
}
|
||||
|
||||
bool TdeckDisplay::stop() {
|
||||
tt_assert(displayHandle != nullptr);
|
||||
|
||||
lvgl_port_remove_disp(displayHandle);
|
||||
|
||||
if (esp_lcd_panel_del(panelHandle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_io_del(ioHandle) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
displayHandle = nullptr;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void TdeckDisplay::setPowerOn(bool turnOn) {
|
||||
if (esp_lcd_panel_disp_on_off(panelHandle, turnOn) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to turn display on/off");
|
||||
} else {
|
||||
poweredOn = turnOn;
|
||||
}
|
||||
}
|
||||
|
||||
tt::hal::Touch* _Nullable TdeckDisplay::createTouch() {
|
||||
return static_cast<tt::hal::Touch*>(new TdeckTouch());
|
||||
}
|
||||
|
||||
void TdeckDisplay::setBacklightDuty(uint8_t backlightDuty) {
|
||||
if (!isBacklightInitialized) {
|
||||
tt_check(initBacklight());
|
||||
isBacklightInitialized = true;
|
||||
}
|
||||
|
||||
if (setBacklight(backlightDuty)) {
|
||||
TT_LOG_I(TAG, "Backlight set: %d", backlightDuty);
|
||||
lastBacklightDuty = backlightDuty;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to configure display backlight");
|
||||
}
|
||||
}
|
||||
|
||||
tt::hal::Display* createDisplay() {
|
||||
return static_cast<tt::hal::Display*>(new TdeckDisplay());
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <esp_lcd_types.h>
|
||||
#include "lvgl.h"
|
||||
#include "hal/Display.h"
|
||||
|
||||
extern lv_disp_t* displayHandle;
|
||||
|
||||
class TdeckDisplay : public tt::hal::Display {
|
||||
|
||||
private:
|
||||
|
||||
esp_lcd_panel_io_handle_t ioHandle = nullptr;
|
||||
esp_lcd_panel_handle_t panelHandle = nullptr;
|
||||
lv_display_t* displayHandle = nullptr;
|
||||
uint8_t lastBacklightDuty = 255;
|
||||
bool poweredOn = false;
|
||||
|
||||
public:
|
||||
|
||||
bool start() override;
|
||||
|
||||
bool stop() override;
|
||||
|
||||
void setPowerOn(bool turnOn) override;
|
||||
bool isPoweredOn() const override { return poweredOn; };
|
||||
bool supportsPowerControl() const override { return true; }
|
||||
|
||||
tt::hal::Touch* _Nullable createTouch() override;
|
||||
|
||||
void setBacklightDuty(uint8_t backlightDuty) override;
|
||||
uint8_t getBacklightDuty() const override { return lastBacklightDuty; }
|
||||
bool supportsBacklightDuty() const override { return true; }
|
||||
|
||||
lv_display_t* _Nullable getLvglDisplay() const override { return displayHandle; }
|
||||
|
||||
private:
|
||||
|
||||
static bool startBacklight();
|
||||
};
|
||||
|
||||
tt::hal::Display* createDisplay();
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#define TDECK_LCD_SPI_HOST SPI2_HOST
|
||||
#define TDECK_LCD_PIN_CS GPIO_NUM_12
|
||||
#define TDECK_LCD_PIN_DC GPIO_NUM_11 // RS
|
||||
#define TDECK_LCD_PIN_BACKLIGHT GPIO_NUM_42
|
||||
#define TDECK_LCD_SPI_FREQUENCY 40000000
|
||||
#define TDECK_LCD_HORIZONTAL_RESOLUTION 320
|
||||
#define TDECK_LCD_VERTICAL_RESOLUTION 240
|
||||
#define TDECK_LCD_BITS_PER_PIXEL 16
|
||||
#define TDECK_LCD_DRAW_BUFFER_HEIGHT (TDECK_LCD_VERTICAL_RESOLUTION / 10)
|
||||
#define TDECK_LCD_SPI_TRANSFER_HEIGHT (TDECK_LCD_VERTICAL_RESOLUTION / 10)
|
||||
|
||||
// Backlight (PWM)
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_TIMER LEDC_TIMER_0
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_DUTY_RES LEDC_TIMER_8_BIT
|
||||
#define TDECK_LCD_BACKLIGHT_LEDC_FREQUENCY (4000)
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "TdeckKeyboard.h"
|
||||
#include "hal/i2c/I2c.h"
|
||||
#include <driver/i2c.h>
|
||||
|
||||
#define TAG "tdeck_keyboard"
|
||||
|
||||
#define TDECK_KEYBOARD_I2C_BUS_HANDLE I2C_NUM_0
|
||||
#define TDECK_KEYBOARD_SLAVE_ADDRESS 0x55
|
||||
|
||||
static inline bool keyboard_i2c_read(uint8_t* output) {
|
||||
return tt::hal::i2c::masterRead(TDECK_KEYBOARD_I2C_BUS_HANDLE, TDECK_KEYBOARD_SLAVE_ADDRESS, output, 1, 100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback simulates press and release events, because the T-Deck
|
||||
* keyboard only publishes press events on I2C.
|
||||
* LVGL currently works without those extra release events, but they
|
||||
* are implemented for correctness and future compatibility.
|
||||
*
|
||||
* @param indev_drv
|
||||
* @param data
|
||||
*/
|
||||
static void keyboard_read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
|
||||
static uint8_t last_buffer = 0x00;
|
||||
uint8_t read_buffer = 0x00;
|
||||
|
||||
// Defaults
|
||||
data->key = 0;
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
|
||||
if (keyboard_i2c_read(&read_buffer)) {
|
||||
if (read_buffer == 0 && read_buffer != last_buffer) {
|
||||
TT_LOG_I(TAG, "Released %d", last_buffer);
|
||||
data->key = last_buffer;
|
||||
data->state = LV_INDEV_STATE_RELEASED;
|
||||
} else if (read_buffer != 0) {
|
||||
TT_LOG_I(TAG, "Pressed %d", read_buffer);
|
||||
data->key = read_buffer;
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
}
|
||||
}
|
||||
|
||||
last_buffer = read_buffer;
|
||||
}
|
||||
|
||||
bool TdeckKeyboard::start(lv_display_t* display) {
|
||||
deviceHandle = lv_indev_create();
|
||||
lv_indev_set_type(deviceHandle, LV_INDEV_TYPE_KEYPAD);
|
||||
lv_indev_set_read_cb(deviceHandle, &keyboard_read_callback);
|
||||
lv_indev_set_display(deviceHandle, display);
|
||||
lv_indev_set_user_data(deviceHandle, this);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TdeckKeyboard::stop() {
|
||||
lv_indev_delete(deviceHandle);
|
||||
deviceHandle = nullptr;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TdeckKeyboard::isAttached() const {
|
||||
return tt::hal::i2c::masterCheckAddressForDevice(TDECK_KEYBOARD_I2C_BUS_HANDLE, TDECK_KEYBOARD_SLAVE_ADDRESS, 100);
|
||||
}
|
||||
|
||||
tt::hal::Keyboard* createKeyboard() {
|
||||
return dynamic_cast<tt::hal::Keyboard*>(new TdeckKeyboard());
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "hal/Keyboard.h"
|
||||
#include <TactilityCore.h>
|
||||
#include "esp_lcd_panel_io_interface.h"
|
||||
#include "esp_lcd_touch.h"
|
||||
|
||||
class TdeckKeyboard : public tt::hal::Keyboard {
|
||||
private:
|
||||
lv_indev_t* _Nullable deviceHandle = nullptr;
|
||||
public:
|
||||
bool start(lv_display_t* display) override;
|
||||
bool stop() override;
|
||||
bool isAttached() const override;
|
||||
lv_indev_t* _Nullable getLvglIndev() override { return deviceHandle; }
|
||||
};
|
||||
|
||||
tt::hal::Keyboard* createKeyboard();
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "TdeckTouch.h"
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_lcd_touch_gt911.h"
|
||||
#include "Log.h"
|
||||
#include "Kernel.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
|
||||
#define TAG "tdeck_touch"
|
||||
|
||||
// Touch (GT911)
|
||||
#define TDECK_TOUCH_I2C_BUS_HANDLE I2C_NUM_0
|
||||
#define TDECK_TOUCH_X_MAX 240
|
||||
#define TDECK_TOUCH_Y_MAX 320
|
||||
#define TDECK_TOUCH_PIN_INT GPIO_NUM_16
|
||||
|
||||
bool TdeckTouch::start(lv_display_t* display) {
|
||||
const esp_lcd_panel_io_i2c_config_t io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
|
||||
|
||||
// TODO: Revert on new ESP-IDF version
|
||||
static_assert(ESP_IDF_VERSION == ESP_IDF_VERSION_VAL(5, 3, 1));
|
||||
esp_lcd_new_panel_io_i2c(
|
||||
(esp_lcd_i2c_bus_handle_t)TDECK_TOUCH_I2C_BUS_HANDLE,
|
||||
&io_config,
|
||||
&ioHandle
|
||||
);
|
||||
/*
|
||||
if (
|
||||
esp_lcd_new_panel_io_i2c(
|
||||
(esp_lcd_i2c_bus_handle_t)TDECK_TOUCH_I2C_BUS_HANDLE,
|
||||
&touch_io_config,
|
||||
&ioHandle
|
||||
) != ESP_OK
|
||||
) {
|
||||
TT_LOG_E(TAG, "touch io i2c creation failed");
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
esp_lcd_touch_config_t config = {
|
||||
.x_max = TDECK_TOUCH_X_MAX,
|
||||
.y_max = TDECK_TOUCH_Y_MAX,
|
||||
.rst_gpio_num = GPIO_NUM_NC,
|
||||
.int_gpio_num = TDECK_TOUCH_PIN_INT,
|
||||
.levels = {
|
||||
.reset = 0,
|
||||
.interrupt = 0,
|
||||
},
|
||||
.flags = {
|
||||
.swap_xy = 1,
|
||||
.mirror_x = 1,
|
||||
.mirror_y = 0,
|
||||
},
|
||||
.process_coordinates = nullptr,
|
||||
.interrupt_callback = nullptr,
|
||||
.user_data = nullptr,
|
||||
.driver_data = nullptr
|
||||
};
|
||||
|
||||
if (esp_lcd_touch_new_i2c_gt911(ioHandle, &config, &touchHandle) != ESP_OK) {
|
||||
TT_LOG_E(TAG, "GT199 driver init failed");
|
||||
// TODO: De-init IO
|
||||
return false;
|
||||
}
|
||||
|
||||
const lvgl_port_touch_cfg_t touch_cfg = {
|
||||
.disp = display,
|
||||
.handle = touchHandle,
|
||||
};
|
||||
|
||||
TT_LOG_I(TAG, "Adding touch to LVGL");
|
||||
deviceHandle = lvgl_port_add_touch(&touch_cfg);
|
||||
if (deviceHandle == nullptr) {
|
||||
TT_LOG_E(TAG, "Adding touch failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TdeckTouch::stop() {
|
||||
if (esp_lcd_touch_del(touchHandle) == ESP_OK) {
|
||||
touchHandle = nullptr;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Deleting driver failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (esp_lcd_panel_io_del(ioHandle) == ESP_OK) {
|
||||
ioHandle = nullptr;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Deleting IO handle failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "hal/Touch.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "esp_lcd_panel_io_interface.h"
|
||||
#include "esp_lcd_touch.h"
|
||||
|
||||
class TdeckTouch : public tt::hal::Touch {
|
||||
private:
|
||||
esp_lcd_panel_io_handle_t _Nullable ioHandle = nullptr;
|
||||
esp_lcd_touch_handle_t _Nullable touchHandle = nullptr;
|
||||
lv_indev_t* _Nullable deviceHandle = nullptr;
|
||||
public:
|
||||
bool start(lv_display_t* display) override;
|
||||
bool stop() override;
|
||||
lv_indev_t* _Nullable getLvglIndev() override { return deviceHandle; }
|
||||
};
|
||||
Reference in New Issue
Block a user