C++ conversion (#80)

Converted project to C++
This commit is contained in:
Ken Van Hoeylandt
2024-11-22 20:26:08 +01:00
committed by GitHub
parent 6d80144e12
commit 85e26636a3
488 changed files with 6017 additions and 39466 deletions
+5
View File
@@ -0,0 +1,5 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS "."
REQUIRES Tactility esp_lvgl_port esp_lcd_touch_cst816s esp_lcd_ili9341 driver vfs fatfs
)
+96
View File
@@ -0,0 +1,96 @@
#include "config.h"
#include "TactilityCore.h"
#include "display_i.h"
#include <driver/spi_common.h>
#define TAG "twodotfour_bootstrap"
static bool init_i2c() {
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_33,
.scl_io_num = GPIO_NUM_32,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master = {
.clk_speed = 400000
}
};
if (i2c_param_config(TWODOTFOUR_TOUCH_I2C_PORT, &i2c_conf) != ESP_OK) {
TT_LOG_E(TAG, "i2c config failed");
return false;
}
if (i2c_driver_install(TWODOTFOUR_TOUCH_I2C_PORT, i2c_conf.mode, 0, 0, 0) != ESP_OK) {
TT_LOG_E(TAG, "i2c driver install failed");
return false;
}
return true;
}
static bool init_spi2() {
const spi_bus_config_t bus_config = {
.mosi_io_num = TWODOTFOUR_SPI2_PIN_MOSI,
.miso_io_num = GPIO_NUM_NC,
.sclk_io_num = TWODOTFOUR_SPI2_PIN_SCLK,
.quadwp_io_num = GPIO_NUM_NC,
.quadhd_io_num = GPIO_NUM_NC,
.max_transfer_sz = TWODOTFOUR_SPI2_TRANSACTION_LIMIT
};
if (spi_bus_initialize(SPI2_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
TT_LOG_E(TAG, "SPI bus init failed");
return false;
}
return true;
}
static bool init_spi3() {
const spi_bus_config_t bus_config = {
.mosi_io_num = TWODOTFOUR_SPI3_PIN_MOSI,
.miso_io_num = TWODOTFOUR_SPI3_PIN_MISO,
.sclk_io_num = TWODOTFOUR_SPI3_PIN_SCLK,
.quadwp_io_num = GPIO_NUM_NC,
.quadhd_io_num = GPIO_NUM_NC,
.max_transfer_sz = TWODOTFOUR_SPI3_TRANSACTION_LIMIT
};
if (spi_bus_initialize(SPI3_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
TT_LOG_E(TAG, "SPI bus init failed");
return false;
}
return true;
}
bool twodotfour_bootstrap() {
TT_LOG_I(TAG, "Init I2C");
if (!init_i2c()) {
TT_LOG_E(TAG, "Init I2C failed");
return false;
}
TT_LOG_I(TAG, "Init SPI2");
if (!init_spi2()) {
TT_LOG_E(TAG, "Init SPI2 failed");
return false;
}
TT_LOG_I(TAG, "Init SPI3");
if (!init_spi3()) {
TT_LOG_E(TAG, "Init SPI3 failed");
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;
}
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "driver/spi_common.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
// SPI 2 - display
#define TWODOTFOUR_SPI2_PIN_SCLK GPIO_NUM_14
#define TWODOTFOUR_SPI2_PIN_MOSI GPIO_NUM_13
#define TWODOTFOUR_SPI2_TRANSACTION_LIMIT TWODOTFOUR_LCD_DRAW_BUFFER_SIZE
// SPI 3 - sdcard
#define TWODOTFOUR_SPI3_PIN_SCLK GPIO_NUM_18
#define TWODOTFOUR_SPI3_PIN_MOSI GPIO_NUM_23
#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
#define TWODOTFOUR_SDCARD_SPI_FREQUENCY 800000U
#define TWODOTFOUR_SDCARD_FORMAT_ON_MOUNT_FAILED false
#define TWODOTFOUR_SDCARD_MAX_OPEN_FILES 4
#define TWODOTFOUR_SDCARD_ALLOC_UNIT_SIZE (16 * 1024)
#define TWODOTFOUR_SDCARD_STATUS_CHECK_ENABLED false
+126
View File
@@ -0,0 +1,126 @@
#include "config.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"
// 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)
bool twodotfour_backlight_init() {
ledc_timer_config_t ledc_timer = {
.speed_mode = TWODOTFOUR_LCD_BACKLIGHT_LEDC_MODE,
.duty_resolution = TWODOTFOUR_LCD_BACKLIGHT_LEDC_DUTY_RES,
.timer_num = TWODOTFOUR_LCD_BACKLIGHT_LEDC_TIMER,
.freq_hz = TWODOTFOUR_LCD_BACKLIGHT_LEDC_FREQUENCY,
.clk_cfg = LEDC_AUTO_CLK
};
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
TT_LOG_E(TAG, "Backlight led timer config failed");
return false;
}
return true;
}
void twodotfour_backlight_set(uint8_t duty) {
ledc_channel_config_t ledc_channel = {
.gpio_num = TWODOTFOUR_LCD_PIN_BACKLIGHT,
.speed_mode = TWODOTFOUR_LCD_BACKLIGHT_LEDC_MODE,
.channel = TWODOTFOUR_LCD_BACKLIGHT_LEDC_CHANNEL,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = TWODOTFOUR_LCD_BACKLIGHT_LEDC_TIMER,
.duty = duty,
.hpoint = 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:
if (ledc_channel_config(&ledc_channel) != ESP_OK) {
TT_LOG_E(TAG, "Failed to configure display backlight");
}
}
lv_disp_t* twodotfour_display_init() {
TT_LOG_I(TAG, "Display init");
const esp_lcd_panel_io_spi_config_t panel_io_config = ILI9341_PANEL_IO_SPI_CONFIG(
TWODOTFOUR_LCD_PIN_CS,
TWODOTFOUR_LCD_PIN_DC,
nullptr,
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) {
TT_LOG_E(TAG, "Failed to create panel");
return nullptr;
}
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = GPIO_NUM_NC,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
.bits_per_pixel = TWODOTFOUR_LCD_BITS_PER_PIXEL,
};
esp_lcd_panel_handle_t panel_handle;
if (esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to create ili9341");
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;
}
if (esp_lcd_panel_mirror(panel_handle, true, false) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set panel to mirror");
return nullptr;
}
if (esp_lcd_panel_disp_on_off(panel_handle, true) != ESP_OK) {
TT_LOG_E(TAG, "Failed to turn display on");
return nullptr;
}
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.buffer_size = TWODOTFOUR_LCD_DRAW_BUFFER_SIZE,
.double_buffer = false,
.hres = TWODOTFOUR_LCD_HORIZONTAL_RESOLUTION,
.vres = TWODOTFOUR_LCD_VERTICAL_RESOLUTION,
.monochrome = false,
.rotation = {
.swap_xy = false,
.mirror_x = true,
.mirror_y = false,
},
.flags = {
.buff_dma = true,
.buff_spiram = false,
.sw_rotate = false,
.swap_bytes = true
}
};
return lvgl_port_add_disp(&disp_cfg);
}
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include <cstdint>
bool twodotfour_backlight_init();
void twodotfour_backlight_set(uint8_t duty);
+56
View File
@@ -0,0 +1,56 @@
#include "esp_lvgl_port.h"
#include "Log.h"
#include "Ui/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::ThreadPriorityHigh,
.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::sync_set(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}
+79
View File
@@ -0,0 +1,79 @@
#include "Hal/Sdcard.h"
#include "Check.h"
#include "Log.h"
#include "config.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#define TAG "twodotfour_sdcard"
typedef struct {
const char* mount_point;
sdmmc_card_t* card;
} MountData;
static void* sdcard_mount(const char* mount_point) {
TT_LOG_I(TAG, "Mounting %s", mount_point);
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = TWODOTFOUR_SDCARD_FORMAT_ON_MOUNT_FAILED,
.max_files = TWODOTFOUR_SDCARD_MAX_OPEN_FILES,
.allocation_unit_size = TWODOTFOUR_SDCARD_ALLOC_UNIT_SIZE,
.disk_status_check_enable = TWODOTFOUR_SDCARD_STATUS_CHECK_ENABLED
};
sdmmc_card_t* card;
// Init without card detect (CD) and write protect (WD)
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = TWODOTFOUR_SDCARD_PIN_CS;
slot_config.host_id = TWODOTFOUR_SDCARD_SPI_HOST;
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = TWODOTFOUR_SDCARD_SPI_FREQUENCY;
esp_err_t ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
TT_LOG_E(TAG, "Mounting failed. Ensure the card is formatted with FAT.");
} else {
TT_LOG_E(TAG, "Mounting failed (%s)", esp_err_to_name(ret));
}
return nullptr;
}
auto* data = static_cast<MountData*>(malloc(sizeof(MountData)));
*data = (MountData) {
.mount_point = mount_point,
.card = card
};
sdmmc_card_print_info(stdout, data->card);
return data;
}
static void sdcard_unmount(void* context) {
auto* data = static_cast<MountData*>(context);
TT_LOG_I(TAG, "Unmounting %s", data->mount_point);
tt_assert(data != nullptr);
if (esp_vfs_fat_sdcard_unmount(data->mount_point, data->card) != ESP_OK) {
TT_LOG_E(TAG, "Unmount failed for %s", data->mount_point);
}
free(data);
}
static bool sdcard_is_mounted(void* context) {
auto* data = static_cast<MountData*>(context);
return (data != nullptr) && (sdmmc_get_status(data->card) == ESP_OK);
}
extern const tt::hal::sdcard::SdCard twodotfour_sdcard = {
.mount = &sdcard_mount,
.unmount = &sdcard_unmount,
.is_mounted = &sdcard_is_mounted,
.mount_behaviour = tt::hal::sdcard::MountBehaviourAnytime
};
+50
View File
@@ -0,0 +1,50 @@
#include "config.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_lcd_touch_cst816s.h"
#include "esp_lcd_touch.h"
#include "Log.h"
#define TAG "twodotfour_cst816"
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");
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);
/*
if (esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TWODOTFOUR_TOUCH_I2C_PORT, &touch_io_config, io_handle) != ESP_OK) {
TT_LOG_E(TAG, "Touch I2C IO init failed");
return false;
}
*/
esp_lcd_touch_config_t config = {
.x_max = 240,
.y_max = 320,
.rst_gpio_num = GPIO_NUM_25,
.int_gpio_num = GPIO_NUM_21,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
.process_coordinates = nullptr,
.interrupt_callback = nullptr,
.user_data = nullptr
};
if (esp_lcd_touch_new_i2c_cst816s(*io_handle, &config, touch_handle) != ESP_OK) {
TT_LOG_E(TAG, "Driver init failed");
return false;
}
return true;
}
+17
View File
@@ -0,0 +1,17 @@
#include "yellow_board.h"
#include "display_i.h"
bool twodotfour_lvgl_init();
bool twodotfour_bootstrap();
extern const tt::hal::sdcard::SdCard twodotfour_sdcard;
const tt::hal::Configuration yellow_board_24inch_cap = {
.bootstrap = &twodotfour_bootstrap,
.init_graphics = &twodotfour_lvgl_init,
.display = {
.set_backlight_duty = &twodotfour_backlight_set
},
.sdcard = &twodotfour_sdcard,
.power = nullptr
};
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include "Hal/Configuration.h"
// Capacitive touch version of the 2.4" yellow board
extern const tt::hal::Configuration yellow_board_24inch_cap;