Hal refactored (#99)

This commit is contained in:
Ken Van Hoeylandt
2024-12-02 00:32:39 +01:00
committed by GitHub
parent 0188ce721c
commit 33bb742dfb
103 changed files with 1222 additions and 1228 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS "."
SRC_DIRS "Source" "Source/hal"
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lvgl_port esp_lcd esp_lcd_touch_gt911 driver vfs fatfs
)
@@ -1,11 +1,25 @@
#include "TactilityCore.h"
#include "config.h"
#include "display.h"
#include "keyboard.h"
#include "hal/TdeckDisplayConstants.h"
#include <driver/spi_common.h>
#include <soc/gpio_num.h>
#include <driver/ledc.h>
#define TAG "tdeck"
// SPI
#define TDECK_SPI_HOST SPI2_HOST
#define TDECK_SPI_PIN_SCLK GPIO_NUM_40
#define TDECK_SPI_PIN_MOSI GPIO_NUM_41
#define TDECK_SPI_PIN_MISO GPIO_NUM_38
#define TDECK_SPI_TRANSFER_SIZE_LIMIT (TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_SPI_TRANSFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8))
#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)
static bool init_spi() {
spi_bus_config_t bus_config = {
.mosi_io_num = TDECK_SPI_PIN_MOSI,
@@ -21,20 +35,11 @@ static bool init_spi() {
bool tdeck_init_hardware() {
TT_LOG_I(TAG, "Init SPI");
if (!init_spi()) {
TT_LOG_E(TAG, "Init SPI failed");
return false;
}
// Don't turn the backlight on yet - Tactility init will take care of it
TT_LOG_I(TAG, "Init backlight");
if (!tdeck_backlight_init()) {
TT_LOG_E(TAG, "Init backlight failed");
return false;
}
// We wait for the keyboard peripheral to be booted up
keyboard_wait_for_response();
return true;
}
@@ -1,5 +1,7 @@
#include "lilygo_tdeck.h"
#include "display.h"
#include "hal/Configuration.h"
#include "hal/TdeckDisplay.h"
#include "hal/TdeckKeyboard.h"
#include "hal/sdcard/Sdcard.h"
bool tdeck_init_power();
bool tdeck_init_hardware();
@@ -8,12 +10,11 @@ bool tdeck_init_lvgl();
extern const tt::hal::sdcard::SdCard tdeck_sdcard;
extern const tt::hal::Configuration lilygo_tdeck = {
.initPower = tdeck_init_power,
.initBoot = tdeck_init_power,
.initHardware = tdeck_init_hardware,
.initLvgl = &tdeck_init_lvgl,
.display = {
.setBacklightDuty = &tdeck_backlight_set
},
.initLvgl = tdeck_init_lvgl,
.createDisplay = createDisplay,
.createKeyboard = createKeyboard,
.sdcard = &tdeck_sdcard,
.power = nullptr,
.i2c = {
@@ -1,5 +1,5 @@
#pragma once
#include "hal/Configuration.h"
#include <hal/Configuration.h>
extern const tt::hal::Configuration lilygo_tdeck;
+33
View File
@@ -0,0 +1,33 @@
#include "Log.h"
#include "Thread.h"
#include "lvgl/LvglSync.h"
#include "esp_lvgl_port.h"
#include "hal/TdeckDisplay.h"
#define TAG "tdeck_lvgl"
// LVGL
// The minimum task stack seems to be about 3500, but that crashes the wifi app in some scenarios
// At 4000, it crashes when the fps renderer is available
#define TDECK_LVGL_TASK_STACK_DEPTH 8192
bool tdeck_init_lvgl() {
static lv_disp_t* display = nullptr;
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = tt::THREAD_PRIORITY_RENDER,
.task_stack = TDECK_LVGL_TASK_STACK_DEPTH,
.task_affinity = -1, // core pinning
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
TT_LOG_D(TAG, "LVGL port init");
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
TT_LOG_E(TAG, "LVGL port init failed");
return false;
}
tt::lvgl::syncSet(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}
@@ -1,8 +1,11 @@
#include "config.h"
#include <driver/gpio.h>
#include "TactilityCore.h"
#define TAG "tdeck"
// Power on
#define TDECK_POWERON_GPIO GPIO_NUM_10
static bool tdeck_power_on() {
gpio_config_t device_power_signal_config = {
.pin_bit_mask = BIT64(TDECK_POWERON_GPIO),
@@ -30,17 +33,5 @@ bool tdeck_init_power() {
return false;
}
/**
* Without this delay, the touch driver randomly fails when the device is USB-powered:
* > lcd_panel.io.i2c: panel_io_i2c_rx_buffer(135): i2c transaction failed
* > GT911: touch_gt911_read_cfg(352): GT911 read error!
* This might not be a problem with a lipo, but I haven't been able to test that.
* I tried to solve it just like I did with the keyboard:
* By reading from I2C until it succeeds and to then init the driver.
* It doesn't work, because it never recovers from the error.
*/
TT_LOG_I(TAG, "Waiting after power-on");
tt::delay_ms(TDECK_POWERON_DELAY);
return true;
}
@@ -1,7 +1,6 @@
#include "hal/sdcard/Sdcard.h"
#include "Check.h"
#include "Log.h"
#include "config.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
@@ -9,6 +8,18 @@
#define TAG "tdeck_sdcard"
#define TDECK_SDCARD_SPI_HOST SPI2_HOST
#define TDECK_SDCARD_PIN_CS GPIO_NUM_39
#define TDECK_SDCARD_SPI_FREQUENCY 800000U
#define TDECK_SDCARD_FORMAT_ON_MOUNT_FAILED false
#define TDECK_SDCARD_MAX_OPEN_FILES 4
#define TDECK_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define TDECK_SDCARD_STATUS_CHECK_ENABLED false
// Other
#define TDECK_LCD_PIN_CS GPIO_NUM_12
#define TDECK_RADIO_PIN_CS GPIO_NUM_9
typedef struct {
const char* mount_point;
sdmmc_card_t* card;
@@ -85,7 +96,7 @@ static void* _Nullable sdcard_mount(const char* mount_point) {
} else {
TT_LOG_E(TAG, "Mounting failed (%s)", esp_err_to_name(ret));
}
return NULL;
return nullptr;
}
auto* data = static_cast<MountData*>(malloc(sizeof(MountData)));
@@ -100,7 +111,7 @@ static void* _Nullable sdcard_mount(const char* mount_point) {
static void* sdcard_init_and_mount(const char* mount_point) {
if (!sdcard_init()) {
TT_LOG_E(TAG, "Failed to set SPI CS pins high. This is a pre-requisite for mounting.");
return NULL;
return nullptr;
}
auto* data = static_cast<MountData*>(sdcard_mount(mount_point));
if (data == nullptr) {
@@ -125,6 +136,7 @@ static void sdcard_unmount(void* context) {
free(data);
}
// TODO: Refactor to "bool getStatus(Status* status)" method so that it can fail when the lvgl lock fails
static bool sdcard_is_mounted(void* context) {
auto* data = static_cast<MountData*>(context);
/**
@@ -132,13 +144,18 @@ static bool sdcard_is_mounted(void* context) {
* Writing and reading to the bus from 2 devices at the same time causes crashes.
* This work-around ensures that this check is only happening when LVGL isn't rendering.
*/
if (tt::lvgl::lock(100)) {
bool result = (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
tt::lvgl::unlock();
return result;
} else {
return false;
bool locked = tt::lvgl::lock(100); // TODO: Refactor to a more reliable locking mechanism
if (!locked) {
TT_LOG_W(TAG, "Failed to get LVGL lock");
}
bool result = (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
if (locked) {
tt::lvgl::unlock();
}
return result;
}
extern const tt::hal::sdcard::SdCard tdeck_sdcard = {
@@ -1,15 +1,23 @@
#include "config.h"
#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"
#include "Log.h"
#define TAG "tdeck_display"
bool tdeck_backlight_init() {
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,
@@ -27,7 +35,7 @@ bool tdeck_backlight_init() {
return true;
}
void tdeck_backlight_set(uint8_t duty) {
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,
@@ -43,12 +51,12 @@ void tdeck_backlight_set(uint8_t duty) {
// 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:
if (ledc_channel_config(&ledc_channel) != ESP_OK) {
TT_LOG_E(TAG, "Failed to configure display backlight");
}
return ledc_channel_config(&ledc_channel) == ESP_OK;
}
lv_display_t* tdeck_display_init() {
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,
@@ -71,10 +79,9 @@ lv_display_t* tdeck_display_init() {
}
};
esp_lcd_panel_io_handle_t io_handle;
if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TDECK_LCD_SPI_HOST, &panel_io_config, &io_handle) != ESP_OK) {
TT_LOG_E(TAG, "failed to create panel IO");
return nullptr;
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 = {
@@ -88,45 +95,44 @@ lv_display_t* tdeck_display_init() {
.vendor_config = nullptr
};
esp_lcd_panel_handle_t panel_handle;
if (esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "failed to create panel");
return 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(panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "failed to reset panel");
return nullptr;
if (esp_lcd_panel_reset(panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to reset panel");
return false;
}
if (esp_lcd_panel_init(panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "failed to init panel");
return nullptr;
if (esp_lcd_panel_init(panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to init panel");
return false;
}
if (esp_lcd_panel_invert_color(panel_handle, true) != ESP_OK) {
TT_LOG_E(TAG, "failed to init panel");
return nullptr;
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(panel_handle, true) != ESP_OK) {
TT_LOG_E(TAG, "failed to init panel");
return nullptr;
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(panel_handle, true, false) != ESP_OK) {
TT_LOG_E(TAG, "failed to init panel");
return nullptr;
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(panel_handle, true) != ESP_OK) {
TT_LOG_E(TAG, "failed to turn display on");
return nullptr;
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 = io_handle,
.panel_handle = panel_handle,
.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,
@@ -146,5 +152,55 @@ lv_display_t* tdeck_display_init() {
},
};
return lvgl_port_add_disp(&disp_cfg);
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)
@@ -1,34 +1,16 @@
#include "keyboard.h"
#include "config.h"
#include "lvgl.h"
#include "TactilityCore.h"
#include "lvgl/LvglKeypad.h"
#include "TdeckKeyboard.h"
#include "hal/i2c/I2c.h"
#include <driver/i2c.h>
#define TAG "tdeck_keyboard"
typedef struct {
lv_indev_t* device;
} KeyboardData;
#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);
}
void keyboard_wait_for_response() {
TT_LOG_I(TAG, "Waiting for keyboard response...");
bool awake = false;
uint8_t read_buffer = 0x00;
do {
awake = keyboard_i2c_read(&read_buffer);
if (!awake) {
tt::delay_ms(100);
}
} while (!awake);
TT_LOG_I(TAG, "Keyboard responded");
}
/**
* The callback simulates press and release events, because the T-Deck
* keyboard only publishes press events on I2C.
@@ -61,21 +43,25 @@ static void keyboard_read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t*
last_buffer = read_buffer;
}
Keyboard keyboard_alloc(_Nullable lv_disp_t* display) {
auto* data = static_cast<KeyboardData*>(malloc(sizeof(KeyboardData)));
data->device = lv_indev_create();
lv_indev_set_type(data->device, LV_INDEV_TYPE_KEYPAD);
lv_indev_set_read_cb(data->device, &keyboard_read_callback);
lv_indev_set_display(data->device, display);
tt::lvgl::keypad_set_indev(data->device);
return data;
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;
}
void keyboard_free(Keyboard keyboard) {
auto* data = static_cast<KeyboardData*>(keyboard);
lv_indev_delete(data->device);
free(data);
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; }
};
-64
View File
@@ -1,64 +0,0 @@
#pragma once
#include "driver/i2c.h"
#include "driver/gpio.h"
// Main bus, used by GT911 touch hardware and keyboard
#define TDECK_I2C_BUS_HANDLE I2C_NUM_0
// SPI
#define TDECK_SPI_HOST SPI2_HOST
#define TDECK_SPI_PIN_SCLK GPIO_NUM_40
#define TDECK_SPI_PIN_MOSI GPIO_NUM_41
#define TDECK_SPI_PIN_MISO GPIO_NUM_38
#define TDECK_SPI_TRANSFER_SIZE_LIMIT (TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_SPI_TRANSFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8))
// Power on
#define TDECK_POWERON_GPIO GPIO_NUM_10
#define TDECK_POWERON_DELAY 2000 // milliseconds - see bootstrap.c for explanation
// Display
#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)
// LVGL
// The minimum task stack seems to be about 3500, but that crashes the wifi app in some scenarios
// At 4000, it crashes when the fps renderer is available
#define TDECK_LVGL_TASK_STACK_DEPTH 8192
// Dipslay 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)
// Touch (GT911)
#define TDECK_TOUCH_I2C_BUS_HANDLE TDECK_I2C_BUS_HANDLE
#define TDECK_TOUCH_X_MAX 240
#define TDECK_TOUCH_Y_MAX 320
#define TDECK_TOUCH_PIN_INT GPIO_NUM_16
// SD Card
#define TDECK_SDCARD_SPI_HOST SPI2_HOST
#define TDECK_SDCARD_PIN_CS GPIO_NUM_39
#define TDECK_SDCARD_SPI_FREQUENCY 800000U
#define TDECK_SDCARD_FORMAT_ON_MOUNT_FAILED false
#define TDECK_SDCARD_MAX_OPEN_FILES 4
#define TDECK_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define TDECK_SDCARD_STATUS_CHECK_ENABLED false
// Keyboard
#define TDECK_KEYBOARD_I2C_BUS_HANDLE TDECK_I2C_BUS_HANDLE
#define TDECK_KEYBOARD_SLAVE_ADDRESS 0x55
// Lora (optional)
#define TDECK_RADIO_PIN_CS GPIO_NUM_9
-9
View File
@@ -1,9 +0,0 @@
#pragma once
#include "lvgl.h"
lv_display_t* tdeck_display_init();
bool tdeck_backlight_init();
void tdeck_backlight_set(uint8_t duty);
-10
View File
@@ -1,10 +0,0 @@
#pragma once
#include "lvgl.h"
void keyboard_wait_for_response();
typedef void* Keyboard;
Keyboard keyboard_alloc(_Nullable lv_disp_t* display);
void keyboard_free(Keyboard keyboard);
-67
View File
@@ -1,67 +0,0 @@
#include "Log.h"
#include "Thread.h"
#include "lvgl/LvglSync.h"
#include "config.h"
#include "display.h"
#include "esp_lvgl_port.h"
#include "keyboard.h"
#define TAG "tdeck_lvgl"
bool tdeck_init_touch(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle);
bool tdeck_init_lvgl() {
static lv_disp_t* display = nullptr;
static esp_lcd_panel_io_handle_t touch_io_handle;
static esp_lcd_touch_handle_t touch_handle;
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = tt::THREAD_PRIORITY_RENDER,
.task_stack = TDECK_LVGL_TASK_STACK_DEPTH,
.task_affinity = -1, // core pinning
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
TT_LOG_D(TAG, "LVGL port init");
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
TT_LOG_E(TAG, "LVGL port init failed");
return false;
}
// Add display
TT_LOG_D(TAG, "Creating display");
display = tdeck_display_init();
if (display == nullptr) {
TT_LOG_E(TAG, "Creating display failed");
return false;
}
// Add touch
TT_LOG_D(TAG, "Creating touch");
if (!tdeck_init_touch(&touch_io_handle, &touch_handle)) {
TT_LOG_E(TAG, "Creating touch failed");
return false;
}
const lvgl_port_touch_cfg_t touch_cfg = {
.disp = display,
.handle = touch_handle,
};
TT_LOG_D(TAG, "Adding touch");
lv_indev_t _Nullable* touch_indev = lvgl_port_add_touch(&touch_cfg);
if (touch_indev == nullptr) {
TT_LOG_E(TAG, "Adding touch failed");
return false;
}
// Set syncing functions
tt::lvgl::syncSet(&lvgl_port_lock, &lvgl_port_unlock);
keyboard_alloc(display);
return true;
}
-58
View File
@@ -1,58 +0,0 @@
#include "config.h"
#include "esp_err.h"
#include "esp_lcd_panel_io_interface.h"
#include "esp_lcd_touch_gt911.h"
#include "Log.h"
#include "Kernel.h"
#define TAG "tdeck_touch"
bool tdeck_init_touch(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle) {
const esp_lcd_panel_io_i2c_config_t touch_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,
&touch_io_config,
io_handle
);
/*
if (
esp_lcd_new_panel_io_i2c(
(esp_lcd_i2c_bus_handle_t)TDECK_TOUCH_I2C_BUS_HANDLE,
&touch_io_config,
io_handle
) != 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
};
if (esp_lcd_touch_new_i2c_gt911(*io_handle, &config, touch_handle) != ESP_OK) {
TT_LOG_E(TAG, "gt911 driver creation failed");
return false;
}
return true;
}
+2 -1
View File
@@ -2,8 +2,9 @@
#include "M5stackShared.h"
extern const tt::hal::Configuration m5stack_core2 = {
.initPower = &m5stack_bootstrap,
.initBoot = &m5stack_bootstrap,
.initLvgl = &m5stack_lvgl_init,
.createDisplay = createDisplay,
.sdcard = &m5stack_sdcard,
.power = &m5stack_power,
.i2c = {
@@ -1,11 +1,10 @@
#include "M5stackCoreS3.h"
#include "M5stackShared.h"
extern const tt::hal::sdcard::SdCard m5stack_cores3_sdcard;
const tt::hal::Configuration m5stack_cores3 = {
.initPower = &m5stack_bootstrap,
.initBoot = &m5stack_bootstrap,
.initLvgl = &m5stack_lvgl_init,
.createDisplay = createDisplay,
.sdcard = &m5stack_sdcard,
.power = &m5stack_power,
.i2c = {
+1 -1
View File
@@ -1,5 +1,5 @@
idf_component_register(
SRC_DIRS "Source"
SRC_DIRS "Source" "Source/hal"
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lvgl_port M5Unified vfs fatfs
)
+2 -17
View File
@@ -5,8 +5,8 @@
#define TAG "cores3_lvgl"
_Nullable lv_disp_t* m5stack_lvgl_display(bool usePsram);
_Nullable lv_indev_t* m5stack_lvgl_touch();
_Nullable lv_disp_t* createDisplay(bool usePsram);
_Nullable lv_indev_t* createTouch();
bool m5stack_lvgl_init() {
static lv_display_t* display = nullptr;
@@ -24,21 +24,6 @@ bool m5stack_lvgl_init() {
return false;
}
// Add display
display = m5stack_lvgl_display(true);
if (display == nullptr) {
TT_LOG_E(TAG, "failed to add display");
return false;
}
// Add touch
lv_indev_t* touch_indev = m5stack_lvgl_touch();
if (touch_indev == nullptr) {
TT_LOG_E(TAG, "failed to add touch");
return false;
}
lv_indev_set_display(touch_indev, display);
// Set syncing functions
tt::lvgl::syncSet(&lvgl_port_lock, &lvgl_port_unlock);
-29
View File
@@ -1,29 +0,0 @@
#include "M5Unified.hpp"
#include "lvgl.h"
#include "TactilityCore.h"
#define TAG "cores3_touch"
static void read_callback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
lgfx::touch_point_t point; // Making it static makes it unreliable
bool touched = M5.Lcd.getTouch(&point) > 0;
if (!touched) {
data->state = LV_INDEV_STATE_REL;
} else {
data->state = LV_INDEV_STATE_PR;
data->point.x = point.x;
data->point.y = point.y;
}
}
_Nullable lv_indev_t* m5stack_lvgl_touch() {
static lv_indev_t* indev = lv_indev_create();
LV_ASSERT_MALLOC(indev)
if (indev == nullptr) {
return nullptr;
}
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, read_callback);
return indev;
}
@@ -1,6 +1,8 @@
#pragma once
#include "hal/Power.h"
#include "hal/M5stackTouch.h"
#include "hal/M5stackDisplay.h"
#include "hal/sdcard/Sdcard.h"
extern bool m5stack_bootstrap();
@@ -1,4 +1,11 @@
#include "TactilityCore.h"
#include "M5stackDisplay.h"
#include "M5stackTouch.h"
#include "Log.h"
#include <TactilityCore.h>
#define TAG "m5shared_display"
#include "M5Unified.hpp"
#include "esp_err.h"
@@ -18,8 +25,7 @@ static void flush_callback(lv_display_t* display, const lv_area_t* area, uint8_t
lv_display_flush_ready(display);
}
_Nullable lv_disp_t* m5stack_lvgl_display(bool usePsram) {
_Nullable lv_disp_t* createDisplay(bool usePsram) {
M5GFX& gfx = M5.Display;
static lv_display_t* display = lv_display_create(gfx.width(), gfx.height());
@@ -59,3 +65,25 @@ _Nullable lv_disp_t* m5stack_lvgl_display(bool usePsram) {
return display;
}
bool M5stackDisplay::start() {
TT_LOG_I(TAG, "Starting");
displayHandle = createDisplay(true);
TT_LOG_I(TAG, "Finished");
return displayHandle != nullptr;
}
bool M5stackDisplay::stop() {
tt_assert(displayHandle != nullptr);
lv_display_delete(displayHandle);
displayHandle = nullptr;
return true;
}
tt::hal::Touch* _Nullable M5stackDisplay::createTouch() {
return static_cast<tt::hal::Touch*>(new M5stackTouch());
}
tt::hal::Display* createDisplay() {
return static_cast<tt::hal::Display*>(new M5stackDisplay());
}
@@ -0,0 +1,33 @@
#pragma once
#include "lvgl.h"
#include "hal/Display.h"
extern lv_disp_t* displayHandle;
class M5stackDisplay : public tt::hal::Display {
private:
lv_display_t* displayHandle = nullptr;
public:
bool start() override;
bool stop() override;
void setPowerOn(bool turnOn) override {}
bool isPoweredOn() const override { return true; };
bool supportsPowerControl() const override { return false; }
tt::hal::Touch* _Nullable createTouch() override;
void setBacklightDuty(uint8_t backlightDuty) override {}
uint8_t getBacklightDuty() const override { return 255; }
bool supportsBacklightDuty() const override { return false; }
lv_display_t* _Nullable getLvglDisplay() const override { return displayHandle; }
};
tt::hal::Display* createDisplay();
@@ -0,0 +1,48 @@
#include "M5stackTouch.h"
#include "M5Unified.hpp"
#include "esp_err.h"
#include "Log.h"
#define TAG "m5stack_touch"
static void touchReadCallback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
lgfx::touch_point_t point; // Making it static makes it unreliable
bool touched = M5.Lcd.getTouch(&point) > 0;
if (!touched) {
data->state = LV_INDEV_STATE_REL;
} else {
data->state = LV_INDEV_STATE_PR;
data->point.x = point.x;
data->point.y = point.y;
}
}
_Nullable lv_indev_t* createTouch() {
static lv_indev_t* indev = lv_indev_create();
LV_ASSERT_MALLOC(indev)
if (indev == nullptr) {
return nullptr;
}
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, touchReadCallback);
return indev;
}
bool M5stackTouch::start(lv_display_t* display) {
TT_LOG_I(TAG, "Adding touch to LVGL");
deviceHandle = createTouch();
if (deviceHandle == nullptr) {
TT_LOG_E(TAG, "Adding touch failed");
return false;
}
return true;
}
bool M5stackTouch::stop() {
tt_assert(deviceHandle != nullptr);
lv_indev_delete(deviceHandle);
deviceHandle = nullptr;
return true;
}
@@ -0,0 +1,13 @@
#pragma once
#include "hal/Touch.h"
#include "TactilityCore.h"
class M5stackTouch : public tt::hal::Touch {
private:
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; }
};
@@ -1,8 +1,7 @@
#include "lvgl_task.h"
#include "LvglTask.h"
#include "lvgl.h"
#include "Log.h"
#include "lvgl_hal.h"
#include "TactilityCore.h"
#include "Thread.h"
#include "lvgl/LvglSync.h"
@@ -66,18 +65,23 @@ void lvgl_task_start() {
lvgl_task,
"lvgl",
8192,
NULL,
nullptr,
tt::Thread::PriorityHigh, // Should be higher than main app task
NULL
nullptr
);
tt_assert(task_result == pdTRUE);
}
lv_disp_t* displayHandle = nullptr;
static void lvgl_task(TT_UNUSED void* arg) {
TT_LOG_I(TAG, "lvgl task started");
lv_disp_t* display = lvgl_hal_init();
/** Ideally. the display handle would be created during Simulator.start(),
* but somehow that doesn't work. Waiting here from a ThreadFlag when that happens
* also doesn't work. It seems that it must be called from this task. */
displayHandle = lv_sdl_window_create(320, 240);
uint32_t task_delay_ms = task_max_sleep_ms;
@@ -96,8 +100,7 @@ static void lvgl_task(TT_UNUSED void* arg) {
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
lv_disp_remove(display);
vTaskDelete(NULL);
lv_disp_remove(displayHandle);
displayHandle = nullptr;
}
@@ -1,21 +1,31 @@
#include "Main.h"
#include "TactilityCore.h"
#include "Thread.h"
#include "FreeRTOS.h"
#include "task.h"
#include "Simulator.h"
#define TAG "freertos"
static void main_task(TT_UNUSED void* parameter) {
namespace simulator {
MainFunction mainFunction = nullptr;
void setMain(MainFunction newMainFunction) {
mainFunction = newMainFunction;
}
static void freertosMainTask(TT_UNUSED void* parameter) {
TT_LOG_I(TAG, "starting app_main()");
executeMainFunction();
assert(simulator::mainFunction);
mainFunction();
TT_LOG_I(TAG, "returned from app_main()");
vTaskDelete(nullptr);
}
int main_stub() {
void freertosMain() {
BaseType_t task_result = xTaskCreate(
main_task,
freertosMainTask,
"main",
8192,
nullptr,
@@ -29,6 +39,8 @@ int main_stub() {
vTaskStartScheduler();
}
} // namespace
/**
* Assert implementation as defined in the FreeRTOSConfig.h
* It allows you to set breakpoints and debug asserts.
@@ -47,3 +59,4 @@ void vAssertCalled(unsigned long line, const char* const file) {
}
taskEXIT_CRITICAL();
}
+3
View File
@@ -0,0 +1,3 @@
#pragma once
typedef void (*MainFunction)();
-12
View File
@@ -1,12 +0,0 @@
#include "Simulator.h"
MainFunction mainFunction = nullptr;
void setMainForSim(MainFunction newMainFunction) {
mainFunction = newMainFunction;
}
void executeMainFunction() {
assert(mainFunction);
mainFunction();
}
+17 -5
View File
@@ -1,10 +1,22 @@
#pragma once
#include "hal/Configuration.h"
#include "Main.h"
extern const tt::hal::Configuration sim_hardware;
namespace simulator {
/** Set the function pointer of the real app_main() */
void setMain(MainFunction mainFunction);
/** The actual main task */
void freertosMain();
}
typedef void (*MainFunction)();
void setMainForSim(MainFunction mainFunction);
void executeMainFunction();
int main_stub();
extern "C" {
void app_main(); // ESP-IDF's main function, implemented in the application
}
int main() {
// Actual main function that passes on app_main() (to be executed in a FreeRTOS task) and bootstraps FreeRTOS
simulator::setMain(app_main);
simulator::freertosMain();
return 0;
}
@@ -1,18 +1,20 @@
#include "hal/Configuration.h"
#include "lvgl_task.h"
#include "LvglTask.h"
#include "src/lv_init.h"
#include "SdlDisplay.h"
#include "SdlKeyboard.h"
#define TAG "hardware"
extern const tt::hal::Power power;
static bool lvgl_init() {
static bool initBoot() {
lv_init();
lvgl_task_start();
return true;
}
TT_UNUSED static void lvgl_deinit() {
TT_UNUSED static void deinitPower() {
lvgl_task_interrupt();
while (lvgl_task_is_running()) {
tt::delay_ms(10);
@@ -23,11 +25,10 @@ TT_UNUSED static void lvgl_deinit() {
#endif
}
extern const tt::hal::Configuration sim_hardware = {
.initLvgl = &lvgl_init,
.display = {
.setBacklightDuty = nullptr,
},
extern const tt::hal::Configuration hardware = {
.initBoot = initBoot,
.createDisplay = createDisplay,
.createKeyboard = createKeyboard,
.sdcard = nullptr,
.power = &power,
.i2c = {
+31
View File
@@ -0,0 +1,31 @@
#include "hal/Power.h"
static bool is_charging_enabled = false;
static bool isCharging() {
return is_charging_enabled;
}
static bool isChargingEnabled() {
return is_charging_enabled;
}
static void setChargingEnabled(bool enabled) {
is_charging_enabled = enabled;
}
static uint8_t getChargeLevel() {
return 204;
}
static int32_t getCurrent() {
return is_charging_enabled ? 100 : -50;
}
extern const tt::hal::Power power = {
.isCharging = isCharging,
.isChargingEnabled = isChargingEnabled,
.setChargingEnabled = setChargingEnabled,
.getChargeLevel = getChargeLevel,
.getCurrent = getCurrent
};
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "SdlTouch.h"
#include "hal/Display.h"
extern lv_disp_t* displayHandle;
class SdlDisplay : public tt::hal::Display {
public:
bool start() override {
return displayHandle != nullptr;
}
bool stop() override { tt_crash("Not supported"); }
void setPowerOn(bool turnOn) override {}
bool isPoweredOn() const override { return displayHandle != nullptr; }
bool supportsPowerControl() const override { return false; }
tt::hal::Touch* _Nullable createTouch() override { return dynamic_cast<tt::hal::Touch*>(new SdlTouch()); }
void setBacklightDuty(uint8_t backlightDuty) override {}
uint8_t getBacklightDuty() const override { return 255; }
bool supportsBacklightDuty() const override { return false; }
lv_display_t* _Nullable getLvglDisplay() const override { return displayHandle; }
};
tt::hal::Display* createDisplay() {
return static_cast<tt::hal::Display*>(new SdlDisplay());
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "hal/Keyboard.h"
#include <TactilityCore.h>
class SdlKeyboard : public tt::hal::Keyboard {
private:
lv_indev_t* _Nullable handle = nullptr;
public:
bool start(lv_display_t* display) override {
handle = lv_sdl_keyboard_create();
return handle != nullptr;
}
bool stop() override { tt_crash("Not supported"); }
bool isAttached() const override { return true; }
lv_indev_t* _Nullable getLvglIndev() override { return handle; }
};
tt::hal::Keyboard* createKeyboard() {
return static_cast<tt::hal::Keyboard*>(new SdlKeyboard());
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "hal/Touch.h"
#include <TactilityCore.h>
class SdlTouch : public tt::hal::Touch {
private:
lv_indev_t* _Nullable handle = nullptr;
public:
bool start(lv_display_t* display) override {
handle = lv_sdl_mouse_create();
return handle != nullptr;
}
bool stop() override { tt_crash("Not supported"); }
lv_indev_t* _Nullable getLvglIndev() override { return handle; }
};
-15
View File
@@ -1,15 +0,0 @@
#include "lvgl.h"
#include "lvgl/LvglKeypad.h"
lv_display_t* lvgl_hal_init() {
static lv_display_t* display = NULL;
static lv_indev_t* mouse = NULL;
static lv_indev_t* keyboard = NULL;
display = lv_sdl_window_create(320, 240);
mouse = lv_sdl_mouse_create();
keyboard = lv_sdl_keyboard_create();
tt::lvgl::keypad_set_indev(keyboard);
return display;
}
-5
View File
@@ -1,5 +0,0 @@
#pragma once
#include <cstdint>
lv_display_t* lvgl_hal_init();
-31
View File
@@ -1,31 +0,0 @@
#include "hal/Power.h"
static bool is_charging_enabled = false;
static bool power_is_charging() {
return is_charging_enabled;
}
static bool power_is_charging_enabled() {
return is_charging_enabled;
}
static void power_set_charging_enabled(bool enabled) {
is_charging_enabled = enabled;
}
static uint8_t power_get_charge_level() {
return 204;
}
static int32_t power_get_current() {
return is_charging_enabled ? 100 : -50;
}
extern const tt::hal::Power power = {
.isCharging = &power_is_charging,
.isChargingEnabled = &power_is_charging_enabled,
.setChargingEnabled = &power_set_charging_enabled,
.getChargeLevel = &power_get_charge_level,
.getCurrent = &power_get_current
};
-5
View File
@@ -1,5 +0,0 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS "."
REQUIRES Tactility lvgl esp_lcd esp_lcd_touch_gt911
)
-39
View File
@@ -1,39 +0,0 @@
#include "config.h"
#include "TactilityCore.h"
#include <driver/i2c.h>
#define TAG "waveshare_bootstrap"
#define WAVESHARE_I2C_MASTER_TX_BUF_DISABLE 0
#define WAVESHARE_I2C_MASTER_RX_BUF_DISABLE 0
static esp_err_t i2c_init() {
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_8,
.scl_io_num = GPIO_NUM_9,
.sda_pullup_en = false,
.scl_pullup_en = false,
.master = {
.clk_speed = 400000
}
};
i2c_param_config(WAVESHARE_TOUCH_I2C_PORT, &i2c_conf);
return i2c_driver_install(
WAVESHARE_TOUCH_I2C_PORT,
i2c_conf.mode,
WAVESHARE_I2C_MASTER_RX_BUF_DISABLE,
WAVESHARE_I2C_MASTER_TX_BUF_DISABLE,
0
) == ESP_OK;
}
bool ws3t_bootstrap() {
if (!i2c_init()) {
TT_LOG_E(TAG, "I2C init failed");
return false;
}
return true;
}
-3
View File
@@ -1,3 +0,0 @@
#pragma once
bool ws3t_bootstrap();
-38
View File
@@ -1,38 +0,0 @@
/**
* The WaveShare S3 Touch uses a panel with the ST7262 display driver.
*/
#pragma once
#define WAVESHARE_LCD_HOR_RES 800
#define WAVESHARE_LCD_VER_RES 480
#define WAVESHARE_LCD_PIXEL_CLOCK_HZ (12 * 1000 * 1000) // NOTE: original was 14MHz, but we had to slow it down with PSRAM frame buffer
#define WAVESHARE_LCD_PIN_NUM_HSYNC 46
#define WAVESHARE_LCD_PIN_NUM_VSYNC 3
#define WAVESHARE_LCD_PIN_NUM_DE 5
#define WAVESHARE_LCD_PIN_NUM_PCLK 7
#define WAVESHARE_LCD_PIN_NUM_DATA0 14 // B3
#define WAVESHARE_LCD_PIN_NUM_DATA1 38 // B4
#define WAVESHARE_LCD_PIN_NUM_DATA2 18 // B5
#define WAVESHARE_LCD_PIN_NUM_DATA3 17 // B6
#define WAVESHARE_LCD_PIN_NUM_DATA4 10 // B7
#define WAVESHARE_LCD_PIN_NUM_DATA5 39 // G2
#define WAVESHARE_LCD_PIN_NUM_DATA6 0 // G3
#define WAVESHARE_LCD_PIN_NUM_DATA7 45 // G4
#define WAVESHARE_LCD_PIN_NUM_DATA8 48 // G5
#define WAVESHARE_LCD_PIN_NUM_DATA9 47 // G6
#define WAVESHARE_LCD_PIN_NUM_DATA10 21 // G7
#define WAVESHARE_LCD_PIN_NUM_DATA11 1 // R3
#define WAVESHARE_LCD_PIN_NUM_DATA12 2 // R4
#define WAVESHARE_LCD_PIN_NUM_DATA13 42 // R5
#define WAVESHARE_LCD_PIN_NUM_DATA14 41 // R6
#define WAVESHARE_LCD_PIN_NUM_DATA15 40 // R7
#define WAVESHARE_LCD_PIN_NUM_DISP_EN (-1)
#define WAVESHARE_LCD_BUFFER_HEIGHT (WAVESHARE_LCD_VER_RES / 3) // How many rows of pixels to buffer - 1/3rd is about 1MB
#define WAVESHARE_LCD_USE_DOUBLE_FB true // Performance boost at the cost of about extra PSRAM(SPIRAM)
#define WAVESHARE_LVGL_TICK_PERIOD_MS 2 // TODO: Setting it to 5 causes a crash - why?
#define LVGL_MAX_SLEEP 500
#define WAVESHARE_TOUCH_I2C_PORT I2C_NUM_0
-206
View File
@@ -1,206 +0,0 @@
#include "config.h"
#include "lvgl.h"
#include "TactilityCore.h"
#include "Thread.h"
#include "esp_err.h"
#include "esp_lcd_panel_ops.h"
#include <esp_lcd_panel_rgb.h>
#include <esp_timer.h>
#include <sys/cdefs.h>
#include "RtosCompatSemaphore.h"
#define TAG "waveshare_s3_touch_display"
SemaphoreHandle_t sem_vsync_end = nullptr;
SemaphoreHandle_t sem_gui_ready = nullptr;
SemaphoreHandle_t lvgl_mux = nullptr;
#if WAVESHARE_LCD_USE_DOUBLE_FB
#define WAVESHARE_LCD_NUM_FB 2
#else
#define WAVESHARE_LCD_NUM_FB 1
#endif
static bool lvgl_is_running = false;
bool ws3t_display_lock(uint32_t timeout_ms) {
assert(lvgl_mux && "lvgl_port_init must be called first");
const TickType_t timeout_ticks = (timeout_ms == 0) ? tt::TtWaitForever : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTakeRecursive(lvgl_mux, timeout_ticks) == pdTRUE;
}
void ws3t_display_unlock(void) {
assert(lvgl_mux && "lvgl_port_init must be called first");
xSemaphoreGiveRecursive(lvgl_mux);
}
// Display_task should have lower priority than lvgl_tick_task below
static int32_t display_task(TT_UNUSED void* parameter) {
uint32_t task_delay_ms = LVGL_MAX_SLEEP;
ESP_LOGI(TAG, "Starting LVGL task");
lvgl_is_running = true;
while (lvgl_is_running) {
if (ws3t_display_lock(0)) {
task_delay_ms = lv_timer_handler();
ws3t_display_unlock();
}
if ((task_delay_ms > LVGL_MAX_SLEEP) || (1 == task_delay_ms)) {
task_delay_ms = LVGL_MAX_SLEEP;
} else if (task_delay_ms < 1) {
task_delay_ms = 1;
}
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
vTaskDelete(nullptr);
return 0;
}
static bool on_vsync_event(
TT_UNUSED esp_lcd_panel_handle_t panel,
TT_UNUSED const esp_lcd_rgb_panel_event_data_t* event_data,
TT_UNUSED void* user_data
) {
BaseType_t high_task_awoken = pdFALSE;
if (xSemaphoreTakeFromISR(sem_gui_ready, &high_task_awoken) == pdTRUE) {
xSemaphoreGiveFromISR(sem_vsync_end, &high_task_awoken);
}
return high_task_awoken == pdTRUE;
}
static void lvgl_tick_task(TT_UNUSED void* arg) {
// Tell how much time has passed
lv_tick_inc(WAVESHARE_LVGL_TICK_PERIOD_MS);
}
static void display_flush_callback(lv_display_t* disp, const lv_area_t* area, uint8_t* px_map) {
auto panel_handle = static_cast<esp_lcd_panel_handle_t>(lv_display_get_user_data(disp));
int offsetx1 = area->x1;
int offsetx2 = area->x2;
int offsety1 = area->y1;
int offsety2 = area->y2;
xSemaphoreGive(sem_gui_ready);
xSemaphoreTake(sem_vsync_end, portMAX_DELAY);
// pass the draw buffer to the driver
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, px_map);
lv_disp_flush_ready(disp);
}
lv_disp_t* ws3t_display_create() {
TT_LOG_I(TAG, "display init");
sem_vsync_end = xSemaphoreCreateBinary();
tt_assert(sem_vsync_end);
sem_gui_ready = xSemaphoreCreateBinary();
tt_assert(sem_gui_ready);
lvgl_mux = xSemaphoreCreateRecursiveMutex();
tt_assert(lvgl_mux);
tt::Thread* thread = new tt::Thread("display_task", 8192, &display_task, nullptr);
thread->setPriority(tt::Thread::PriorityHigh); // TODO: try out THREAD_PRIORITY_RENDER
thread->start();
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_rgb_panel_config_t panel_config = {
.clk_src = LCD_CLK_SRC_DEFAULT,
.timings = {
.pclk_hz = WAVESHARE_LCD_PIXEL_CLOCK_HZ,
.h_res = WAVESHARE_LCD_HOR_RES,
.v_res = WAVESHARE_LCD_VER_RES,
.hsync_pulse_width = 10,
// The following parameters should refer to LCD spec
.hsync_back_porch = 10,
.hsync_front_porch = 20,
.vsync_pulse_width = 10,
.vsync_back_porch = 10,
.vsync_front_porch = 10,
},
.data_width = 16, // RGB565 in parallel mode, thus 16bit in width
.bits_per_pixel = 16,
.num_fbs = WAVESHARE_LCD_NUM_FB,
.bounce_buffer_size_px = 0,
.sram_trans_align = 0,
.psram_trans_align = 64,
.hsync_gpio_num = WAVESHARE_LCD_PIN_NUM_HSYNC,
.vsync_gpio_num = WAVESHARE_LCD_PIN_NUM_VSYNC,
.de_gpio_num = WAVESHARE_LCD_PIN_NUM_DE,
.pclk_gpio_num = WAVESHARE_LCD_PIN_NUM_PCLK,
.disp_gpio_num = WAVESHARE_LCD_PIN_NUM_DISP_EN,
.data_gpio_nums = {WAVESHARE_LCD_PIN_NUM_DATA0, WAVESHARE_LCD_PIN_NUM_DATA1, WAVESHARE_LCD_PIN_NUM_DATA2, WAVESHARE_LCD_PIN_NUM_DATA3, WAVESHARE_LCD_PIN_NUM_DATA4, WAVESHARE_LCD_PIN_NUM_DATA5, WAVESHARE_LCD_PIN_NUM_DATA6, WAVESHARE_LCD_PIN_NUM_DATA7, WAVESHARE_LCD_PIN_NUM_DATA8, WAVESHARE_LCD_PIN_NUM_DATA9, WAVESHARE_LCD_PIN_NUM_DATA10, WAVESHARE_LCD_PIN_NUM_DATA11, WAVESHARE_LCD_PIN_NUM_DATA12, WAVESHARE_LCD_PIN_NUM_DATA13, WAVESHARE_LCD_PIN_NUM_DATA14, WAVESHARE_LCD_PIN_NUM_DATA15},
.flags = {.disp_active_low = false, .refresh_on_demand = false, .fb_in_psram = true, .double_fb = WAVESHARE_LCD_USE_DOUBLE_FB, .no_fb = false, .bb_invalidate_cache = false}
};
if (esp_lcd_new_rgb_panel(&panel_config, &panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to create RGB panel");
return nullptr;
}
esp_lcd_rgb_panel_event_callbacks_t cbs = {
.on_vsync = on_vsync_event,
.on_bounce_empty = nullptr,
.on_bounce_frame_finish = nullptr
};
if (esp_lcd_rgb_panel_register_event_callbacks(panel_handle, &cbs, nullptr) != ESP_OK) {
TT_LOG_E(TAG, "Failed to register callbacks");
return nullptr;
}
if (esp_lcd_panel_reset(panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to reset panel");
return nullptr;
}
if (esp_lcd_panel_init(panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to init panel");
return nullptr;
}
lv_init();
#if WAVESHARE_LCD_USE_DOUBLE_FB
// initialize LVGL draw buffers
lv_draw_buf_t* buffer1 = lv_draw_buf_create(WAVESHARE_LCD_HOR_RES, WAVESHARE_LCD_BUFFER_HEIGHT, LV_COLOR_FORMAT_RGB565, 0);
lv_draw_buf_t* buffer2 = lv_draw_buf_create(WAVESHARE_LCD_HOR_RES, WAVESHARE_LCD_BUFFER_HEIGHT, LV_COLOR_FORMAT_RGB565, 0);
tt_assert(buffer1);
tt_assert(buffer2);
#else
lv_draw_buf_t* buffer1 = lv_draw_buf_create(WAVESHARE_LCD_HOR_RES, WAVESHARE_LCD_VER_RES, LV_COLOR_FORMAT_RGB565, 0);
lv_draw_buf_t* buffer2 = NULL;
tt_assert(buffer1);
#endif // WAVESHARE_USE_DOUBLE_FB
lv_display_t* display = lv_display_create(WAVESHARE_LCD_HOR_RES, WAVESHARE_LCD_VER_RES);
lv_display_set_draw_buffers(display, buffer1, buffer2);
lv_display_set_flush_cb(display, &display_flush_callback);
lv_display_set_user_data(display, panel_handle);
lv_display_set_antialiasing(display, false);
lv_display_set_render_mode(display, LV_DISPLAY_RENDER_MODE_PARTIAL);
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &lvgl_tick_task,
.name = "lvgl_tick"
};
esp_timer_handle_t lvgl_tick_timer = nullptr;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, WAVESHARE_LVGL_TICK_PERIOD_MS * 1000));
return display;
}
void ws3t_display_destroy() {
// TODO: de-init display, its buffer and touch, stop display tasks, stop timer
// TODO: see esp_lvlg_port.c for more info
if (lvgl_mux) {
vSemaphoreDelete(lvgl_mux);
lvgl_mux = nullptr;
}
#if LV_ENABLE_GC || !LV_MEM_CUSTOM
lv_deinit();
#endif
}
-10
View File
@@ -1,10 +0,0 @@
#pragma once
#include "lvgl.h"
#include <cstdint>
bool ws3t_display_lock(uint32_t timeout_ms);
void ws3t_display_unlock(void);
lv_display_t* ws3t_display_create();
void ws3t_display_destroy();
-18
View File
@@ -1,18 +0,0 @@
#include "lvgl_i.h"
#include "display_i.h"
#include "touch_i.h"
#include "lvgl/LvglSync.h"
bool ws3t_init_lvgl() {
tt::lvgl::syncSet(&ws3t_display_lock, &ws3t_display_unlock);
lv_display_t* display = ws3t_display_create();
if (display == nullptr) {
return false;
}
ws3t_touch_init(display);
return true;
}
-3
View File
@@ -1,3 +0,0 @@
#pragma once
bool ws3t_init_lvgl();
-72
View File
@@ -1,72 +0,0 @@
#include "config.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_lcd_touch_gt911.h"
#include "esp_log.h"
#include "lvgl.h"
#define TAG "waveshare_s3_touch_i2c"
static esp_lcd_touch_handle_t touch_init_internal() {
static esp_lcd_panel_io_handle_t tp_io_handle = nullptr;
static esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
ESP_LOGI(TAG, "Initialize touch IO");
// TODO: Revert on new ESP-IDF version
static_assert(ESP_IDF_VERSION == ESP_IDF_VERSION_VAL(5, 3, 1));
// ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)WAVESHARE_TOUCH_I2C_PORT, &tp_io_config, &tp_io_handle));
esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)WAVESHARE_TOUCH_I2C_PORT, &tp_io_config, &tp_io_handle);
esp_lcd_touch_config_t tp_cfg = {
.x_max = WAVESHARE_LCD_VER_RES,
.y_max = WAVESHARE_LCD_HOR_RES,
.rst_gpio_num = GPIO_NUM_NC,
.int_gpio_num = GPIO_NUM_NC,
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
};
/* Initialize touch */
ESP_LOGI(TAG, "Initialize touch controller GT911");
esp_lcd_touch_handle_t tp = nullptr;
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
return tp;
}
static void touch_callback(lv_indev_t* indev, lv_indev_data_t* data) {
uint16_t touchpad_x[1] = {0};
uint16_t touchpad_y[1] = {0};
uint8_t touchpad_cnt = 0;
/* Read touch controller data */
auto touch_handle = static_cast<esp_lcd_touch_handle_t>(lv_indev_get_user_data(indev));
esp_lcd_touch_read_data(touch_handle);
/* Get coordinates */
bool touchpad_pressed = esp_lcd_touch_get_coordinates(touch_handle, touchpad_x, touchpad_y, nullptr, &touchpad_cnt, 1);
if (touchpad_pressed && touchpad_cnt > 0) {
data->point.x = touchpad_x[0];
data->point.y = touchpad_y[0];
data->state = LV_INDEV_STATE_PR;
} else {
data->state = LV_INDEV_STATE_REL;
}
}
void ws3t_touch_init(lv_display_t* display) {
esp_lcd_touch_handle_t touch_handle = touch_init_internal();
ESP_LOGI(TAG, "Register display indev to LVGL");
static lv_indev_t* device = nullptr;
device = lv_indev_create();
lv_indev_set_type(device, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(device, &touch_callback);
lv_indev_set_display(device, display);
lv_indev_set_user_data(device, touch_handle);
}
-5
View File
@@ -1,5 +0,0 @@
#pragma once
#include "lvgl.h"
void ws3t_touch_init(lv_display_t* display);
@@ -1,51 +0,0 @@
#include "waveshare_s3_touch.h"
#include "lvgl_i.h"
bool ws3t_bootstrap();
extern const tt::hal::Configuration waveshare_s3_touch = {
.initPower = &ws3t_bootstrap,
.initLvgl = &ws3t_init_lvgl,
.display = { .setBacklightDuty = nullptr },
.sdcard = nullptr,
.power = nullptr,
.i2c = {
tt::hal::i2c::Configuration {
.name = "First",
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitDisabled,
.canReinit = true,
.hasMutableConfiguration = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_NC,
.scl_io_num = GPIO_NUM_NC,
.sda_pullup_en = true,
.scl_pullup_en = true,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
},
tt::hal::i2c::Configuration {
.name = "Second",
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitDisabled,
.canReinit = true,
.hasMutableConfiguration = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_NC,
.scl_io_num = GPIO_NUM_NC,
.sda_pullup_en = true,
.scl_pullup_en = true,
.master = {
.clk_speed = 400000
},
.clk_flags = 0
}
}
}
};
@@ -1,6 +0,0 @@
#pragma once
#include "hal/Configuration.h"
// Waveshare S3 Touch LCD 4.3
extern const tt::hal::Configuration waveshare_s3_touch;
+2 -2
View File
@@ -1,5 +1,5 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS "."
SRC_DIRS "Source" "Source/hal"
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lvgl_port esp_lcd_touch_cst816s esp_lcd_ili9341 driver vfs fatfs
)
@@ -1,6 +1,6 @@
#include "config.h"
#include "Config.h"
#include "TactilityCore.h"
#include "display_i.h"
#include "hal/YellowTouchConstants.h"
#include <driver/spi_common.h>
#define TAG "twodotfour_bootstrap"
@@ -66,7 +66,7 @@ static bool init_spi3() {
return true;
}
bool twodotfour_bootstrap() {
bool twodotfour_boot() {
TT_LOG_I(TAG, "Init I2C");
if (!init_i2c()) {
TT_LOG_E(TAG, "Init I2C failed");
@@ -85,12 +85,5 @@ bool twodotfour_bootstrap() {
return false;
}
// Don't turn the backlight on yet - Tactility init will take care of it
TT_LOG_I(TAG, "Init backlight");
if (!twodotfour_backlight_init()) {
TT_LOG_E(TAG, "Init backlight failed");
return false;
}
return true;
}
@@ -3,6 +3,7 @@
#include "driver/spi_common.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "hal/YellowDisplayConstants.h"
// SPI 2 - display
#define TWODOTFOUR_SPI2_PIN_SCLK GPIO_NUM_14
@@ -15,20 +16,6 @@
#define TWODOTFOUR_SPI3_PIN_MISO GPIO_NUM_19
#define TWODOTFOUR_SPI3_TRANSACTION_LIMIT 8192 // TODO: Determine proper limit
// Display
#define TWODOTFOUR_LCD_SPI_HOST SPI2_HOST
#define TWODOTFOUR_LCD_HORIZONTAL_RESOLUTION 240
#define TWODOTFOUR_LCD_VERTICAL_RESOLUTION 320
#define TWODOTFOUR_LCD_BITS_PER_PIXEL 16
#define TWODOTFOUR_LCD_DRAW_BUFFER_HEIGHT (TWODOTFOUR_LCD_VERTICAL_RESOLUTION / 10)
#define TWODOTFOUR_LCD_DRAW_BUFFER_SIZE (TWODOTFOUR_LCD_HORIZONTAL_RESOLUTION * TWODOTFOUR_LCD_DRAW_BUFFER_HEIGHT * (TWODOTFOUR_LCD_BITS_PER_PIXEL / 8))
#define TWODOTFOUR_LCD_PIN_CS GPIO_NUM_15
#define TWODOTFOUR_LCD_PIN_DC GPIO_NUM_2
#define TWODOTFOUR_LCD_PIN_BACKLIGHT GPIO_NUM_27
// Touch
#define TWODOTFOUR_TOUCH_I2C_PORT I2C_NUM_0
// SD Card
#define TWODOTFOUR_SDCARD_SPI_HOST SPI3_HOST
#define TWODOTFOUR_SDCARD_PIN_CS GPIO_NUM_5
+26
View File
@@ -0,0 +1,26 @@
#include "esp_lvgl_port.h"
#include "Log.h"
#include "lvgl/LvglSync.h"
#include "Thread.h"
#define TAG "twodotfour_lvgl"
bool twodotfour_lvgl_init() {
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = tt::Thread::PriorityHigh,
.task_stack = 8096,
.task_affinity = -1, // core pinning
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
TT_LOG_E(TAG, "lvgl port init failed");
return false;
}
// Set syncing functions
tt::lvgl::syncSet(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}
@@ -1,7 +1,7 @@
#include "hal/sdcard/Sdcard.h"
#include "Check.h"
#include "Log.h"
#include "config.h"
#include "Config.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
@@ -1,17 +1,15 @@
#include "yellow_board.h"
#include "display_i.h"
#include "YellowBoard.h"
#include "hal/YellowDisplay.h"
bool twodotfour_lvgl_init();
bool twodotfour_bootstrap();
bool twodotfour_boot();
extern const tt::hal::sdcard::SdCard twodotfour_sdcard;
const tt::hal::Configuration yellow_board_24inch_cap = {
.initPower = &twodotfour_bootstrap,
.initBoot = &twodotfour_boot,
.initLvgl = &twodotfour_lvgl_init,
.display = {
.setBacklightDuty = &twodotfour_backlight_set
},
.createDisplay = createDisplay,
.sdcard = &twodotfour_sdcard,
.power = nullptr,
.i2c = {
@@ -1,24 +1,22 @@
#include "config.h"
#include "TactilityCore.h"
#include "YellowDisplay.h"
#include "YellowDisplayConstants.h"
#include "YellowTouch.h"
#include "Log.h"
#include <TactilityCore.h>
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "esp_lcd_ili9341.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lvgl_port.h"
#define TAG "twodotfour_ili9341"
#define TAG "yellow_display"
// Dipslay backlight (PWM)
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_TIMER LEDC_TIMER_0
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_DUTY_RES LEDC_TIMER_8_BIT
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_FREQUENCY (1000)
static bool isBacklightInitialized = false;
bool twodotfour_backlight_init() {
static bool initBacklight() {
ledc_timer_config_t ledc_timer = {
.speed_mode = TWODOTFOUR_LCD_BACKLIGHT_LEDC_MODE,
.duty_resolution = TWODOTFOUR_LCD_BACKLIGHT_LEDC_DUTY_RES,
@@ -35,7 +33,7 @@ bool twodotfour_backlight_init() {
return true;
}
void twodotfour_backlight_set(uint8_t duty) {
static bool setBacklight(uint8_t duty) {
ledc_channel_config_t ledc_channel = {
.gpio_num = TWODOTFOUR_LCD_PIN_BACKLIGHT,
.speed_mode = TWODOTFOUR_LCD_BACKLIGHT_LEDC_MODE,
@@ -43,18 +41,22 @@ void twodotfour_backlight_set(uint8_t duty) {
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = TWODOTFOUR_LCD_BACKLIGHT_LEDC_TIMER,
.duty = duty,
.hpoint = 0
.hpoint = 0,
.flags = {
.output_invert = false
}
};
// 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:
if (ledc_channel_config(&ledc_channel) != ESP_OK) {
TT_LOG_E(TAG, "Failed to configure display backlight");
TT_LOG_E(TAG, "Backlight init failed");
return false;
}
return true;
}
lv_disp_t* twodotfour_display_init() {
TT_LOG_I(TAG, "Display init");
bool YellowDisplay::start() {
TT_LOG_I(TAG, "Starting");
const esp_lcd_panel_io_spi_config_t panel_io_config = ILI9341_PANEL_IO_SPI_CONFIG(
TWODOTFOUR_LCD_PIN_CS,
@@ -63,47 +65,50 @@ lv_disp_t* twodotfour_display_init() {
nullptr
);
esp_lcd_panel_io_handle_t io_handle;
if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TWODOTFOUR_LCD_SPI_HOST, &panel_io_config, &io_handle) != ESP_OK) {
if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)TWODOTFOUR_LCD_SPI_HOST, &panel_io_config, &ioHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to create panel");
return nullptr;
return false;
}
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = GPIO_NUM_NC,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
.bits_per_pixel = TWODOTFOUR_LCD_BITS_PER_PIXEL,
.flags = {
.reset_active_high = false
},
.vendor_config = nullptr
};
esp_lcd_panel_handle_t panel_handle;
if (esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle) != ESP_OK) {
if (esp_lcd_new_panel_ili9341(ioHandle, &panel_config, &panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to create ili9341");
return nullptr;
return false;
}
if (esp_lcd_panel_reset(panel_handle) != ESP_OK) {
if (esp_lcd_panel_reset(panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to reset panel");
return nullptr;
return false;
}
if (esp_lcd_panel_init(panel_handle) != ESP_OK) {
if (esp_lcd_panel_init(panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to init panel");
return nullptr;
return false;
}
if (esp_lcd_panel_mirror(panel_handle, true, false) != ESP_OK) {
if (esp_lcd_panel_mirror(panelHandle, true, false) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set panel to mirror");
return nullptr;
return false;
}
if (esp_lcd_panel_disp_on_off(panel_handle, true) != ESP_OK) {
if (esp_lcd_panel_disp_on_off(panelHandle, true) != ESP_OK) {
TT_LOG_E(TAG, "Failed to turn display on");
return nullptr;
return false;
}
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.io_handle = ioHandle,
.panel_handle = panelHandle,
.buffer_size = TWODOTFOUR_LCD_DRAW_BUFFER_SIZE,
.double_buffer = false,
.hres = TWODOTFOUR_LCD_HORIZONTAL_RESOLUTION,
@@ -122,5 +127,45 @@ lv_disp_t* twodotfour_display_init() {
}
};
return lvgl_port_add_disp(&disp_cfg);
displayHandle = lvgl_port_add_disp(&disp_cfg);
TT_LOG_I(TAG, "Finished");
return displayHandle != nullptr;
}
bool YellowDisplay::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 YellowDisplay::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::Touch* _Nullable YellowDisplay::createTouch() {
return static_cast<tt::hal::Touch*>(new YellowTouch());
}
tt::hal::Display* createDisplay() {
return static_cast<tt::hal::Display*>(new YellowDisplay());
}
@@ -0,0 +1,37 @@
#pragma once
#include "lvgl.h"
#include "hal/Display.h"
#include "esp_lcd_panel_io.h"
extern lv_disp_t* displayHandle;
class YellowDisplay : 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;
public:
bool start() override;
bool stop() override;
void setPowerOn(bool turnOn) override {}
bool isPoweredOn() const override { return true; };
bool supportsPowerControl() const override { return false; }
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; }
};
tt::hal::Display* createDisplay();
@@ -0,0 +1,21 @@
#pragma once
// Dipslay backlight (PWM)
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_TIMER LEDC_TIMER_0
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_DUTY_RES LEDC_TIMER_8_BIT
#define TWODOTFOUR_LCD_BACKLIGHT_LEDC_FREQUENCY (1000)
#define TWODOTFOUR_LCD_PIN_BACKLIGHT GPIO_NUM_27
// Display
#define TWODOTFOUR_LCD_SPI_HOST SPI2_HOST
#define TWODOTFOUR_LCD_HORIZONTAL_RESOLUTION 240
#define TWODOTFOUR_LCD_VERTICAL_RESOLUTION 320
#define TWODOTFOUR_LCD_BITS_PER_PIXEL 16
#define TWODOTFOUR_LCD_DRAW_BUFFER_HEIGHT (TWODOTFOUR_LCD_VERTICAL_RESOLUTION / 10)
#define TWODOTFOUR_LCD_DRAW_BUFFER_SIZE (TWODOTFOUR_LCD_HORIZONTAL_RESOLUTION * TWODOTFOUR_LCD_DRAW_BUFFER_HEIGHT * (TWODOTFOUR_LCD_BITS_PER_PIXEL / 8))
#define TWODOTFOUR_LCD_PIN_CS GPIO_NUM_15
#define TWODOTFOUR_LCD_PIN_DC GPIO_NUM_2
@@ -1,22 +1,26 @@
#include "config.h"
#include "YellowTouch.h"
#include "YellowTouchConstants.h"
#include "Log.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_lcd_touch_cst816s.h"
#include "esp_lcd_touch.h"
#include "Log.h"
#include "esp_lvgl_port.h"
#define TAG "twodotfour_cst816"
#define TAG "m5stack_touch"
bool twodotfour_touch_init(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle) {
TT_LOG_I(TAG, "Touch init");
bool YellowTouch::start(lv_display_t* display) {
TT_LOG_I(TAG, "Starting");
esp_lcd_panel_io_handle_t ioHandle;
esp_lcd_touch_handle_t touchHandle;
const esp_lcd_panel_io_i2c_config_t touch_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG();
// TODO: Check when ESP-IDF publishes fix (5.3.2 or 5.4.x)
static_assert(ESP_IDF_VERSION == ESP_IDF_VERSION_VAL(5, 3, 1));
esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TWODOTFOUR_TOUCH_I2C_PORT, &touch_io_config, io_handle);
esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TWODOTFOUR_TOUCH_I2C_PORT, &touch_io_config, &ioHandle);
/*
if (esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TWODOTFOUR_TOUCH_I2C_PORT, &touch_io_config, io_handle) != ESP_OK) {
if (esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TWODOTFOUR_TOUCH_I2C_PORT, &touch_io_config, &ioHandle) != ESP_OK) {
TT_LOG_E(TAG, "Touch I2C IO init failed");
return false;
}
@@ -41,10 +45,29 @@ bool twodotfour_touch_init(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_h
.user_data = nullptr
};
if (esp_lcd_touch_new_i2c_cst816s(*io_handle, &config, touch_handle) != ESP_OK) {
if (esp_lcd_touch_new_i2c_cst816s(ioHandle, &config, &touchHandle) != ESP_OK) {
TT_LOG_E(TAG, "Driver init failed");
return false;
}
const lvgl_port_touch_cfg_t touch_cfg = {
.disp = display,
.handle = touchHandle,
};
deviceHandle = lvgl_port_add_touch(&touch_cfg);
if (deviceHandle == nullptr) {
TT_LOG_E(TAG, "Adding touch failed");
return false;
}
TT_LOG_I(TAG, "Finished");
return true;
}
bool YellowTouch::stop() {
tt_assert(deviceHandle != nullptr);
lv_indev_delete(deviceHandle);
deviceHandle = nullptr;
return true;
}
@@ -0,0 +1,13 @@
#pragma once
#include "hal/Touch.h"
#include "TactilityCore.h"
class YellowTouch : public tt::hal::Touch {
private:
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; }
};
@@ -0,0 +1,5 @@
#pragma once
// Touch
#define TWODOTFOUR_TOUCH_I2C_PORT I2C_NUM_0
-6
View File
@@ -1,6 +0,0 @@
#pragma once
#include <cstdint>
bool twodotfour_backlight_init();
void twodotfour_backlight_set(uint8_t duty);
-56
View File
@@ -1,56 +0,0 @@
#include "esp_lvgl_port.h"
#include "Log.h"
#include "lvgl/LvglSync.h"
#include "Thread.h"
#define TAG "twodotfour_lvgl"
lv_display_t* twodotfour_display_init();
bool twodotfour_touch_init(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle);
bool twodotfour_lvgl_init() {
static lv_display_t* display = nullptr;
static esp_lcd_panel_io_handle_t touch_io_handle;
static esp_lcd_touch_handle_t touch_handle;
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = tt::Thread::PriorityHigh,
.task_stack = 8096,
.task_affinity = -1, // core pinning
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
TT_LOG_E(TAG, "lvgl port init failed");
return false;
}
// Add display
display = twodotfour_display_init();
if (display == nullptr) {
TT_LOG_E(TAG, "failed to add display");
return false;
}
// Add touch
if (!twodotfour_touch_init(&touch_io_handle, &touch_handle)) {
return false;
}
const lvgl_port_touch_cfg_t touch_cfg = {
.disp = display,
.handle = touch_handle,
};
auto* touch_indev= lvgl_port_add_touch(&touch_cfg);
if (touch_indev == nullptr) {
TT_LOG_E(TAG, "failed to add touch to lvgl");
return false;
}
// Set syncing functions
tt::lvgl::syncSet(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}