From 01ce7813680334f2aa8d0af4c2f072cbfeaadafd Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Sat, 16 Aug 2025 22:33:34 +0200 Subject: [PATCH] TactilityC updates for hal and kernel --- Tactility/Include/Tactility/hal/Device.h | 2 + Tactility/Source/hal/Device.cpp | 9 +++ TactilityC/Include/tt_hal_device.h | 33 +++++++++++ TactilityC/Include/tt_hal_display.h | 34 +++++++++++ TactilityC/Include/tt_kernel.h | 31 ++++++++++ TactilityC/Include/tt_lvgl.h | 15 +++++ TactilityC/Source/tt_hal_device.cpp | 48 +++++++++++++++ TactilityC/Source/tt_hal_display.cpp | 74 ++++++++++++++++++++++++ TactilityC/Source/tt_init.cpp | 28 +++++++++ TactilityC/Source/tt_kernel.cpp | 42 ++++++++++++++ TactilityC/Source/tt_lvgl.cpp | 17 ++++++ 11 files changed, 333 insertions(+) create mode 100644 TactilityC/Include/tt_hal_device.h create mode 100644 TactilityC/Include/tt_hal_display.h create mode 100644 TactilityC/Include/tt_kernel.h create mode 100644 TactilityC/Include/tt_lvgl.h create mode 100644 TactilityC/Source/tt_hal_device.cpp create mode 100644 TactilityC/Source/tt_hal_display.cpp create mode 100644 TactilityC/Source/tt_kernel.cpp create mode 100644 TactilityC/Source/tt_lvgl.cpp diff --git a/Tactility/Include/Tactility/hal/Device.h b/Tactility/Include/Tactility/hal/Device.h index 864550ca..abccbb09 100644 --- a/Tactility/Include/Tactility/hal/Device.h +++ b/Tactility/Include/Tactility/hal/Device.h @@ -95,6 +95,8 @@ std::vector> findDevices(Device::Type type) { } } +void findDevices(Device::Type type, std::function&)> onDeviceFound); + /** Find the first device of the specified type and cast it to the specified class */ template std::shared_ptr findFirstDevice(Device::Type type) { diff --git a/Tactility/Source/hal/Device.cpp b/Tactility/Source/hal/Device.cpp index 21d8a548..c9dc4002 100644 --- a/Tactility/Source/hal/Device.cpp +++ b/Tactility/Source/hal/Device.cpp @@ -88,6 +88,15 @@ std::vector> findDevices(Device::Type type) { }); } +void findDevices(Device::Type type, std::function&)> onDeviceFound) { + auto devices_view = findDevices(type); + for (auto& device : devices_view) { + if (!onDeviceFound(device)) { + break; + } + } +} + std::vector> getDevices() { return devices; } diff --git a/TactilityC/Include/tt_hal_device.h b/TactilityC/Include/tt_hal_device.h new file mode 100644 index 00000000..e08c167c --- /dev/null +++ b/TactilityC/Include/tt_hal_device.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum DeviceType { + DEVICE_TYPE_I2C, + DEVICE_TYPE_DISPLAY, + DEVICE_TYPE_TOUCH, + DEVICE_TYPE_SDCARD, + DEVICE_TYPE_KEYBOARD, + DEVICE_TYPE_POWER, + DEVICE_TYPE_GPS +}; + +typedef uint32_t DeviceId; + +/** + * Find one or more devices of a certain type. + * @param[in] type the type to look for + * @param[inout] deviceIds the output ids, which should fit at least maxCount amount of devices + * @param[out] count the resulting number of device ids that were returned + * @param[in] maxCount the maximum number of items that the "deviceIds" output can contain (minimum value is 1) + * @return true if one or more devices were found + */ +bool tt_hal_device_find(DeviceType type, DeviceId* deviceIds, uint16_t* count, uint16_t maxCount); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Include/tt_hal_display.h b/TactilityC/Include/tt_hal_display.h new file mode 100644 index 00000000..ef4725ed --- /dev/null +++ b/TactilityC/Include/tt_hal_display.h @@ -0,0 +1,34 @@ +#pragma once + +#include "tt_hal_device.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* DisplayDriverHandle; + +enum ColorFormat { + COLOR_FORMAT_MONOCHROME, // 1 bpp + COLOR_FORMAT_BGR565, + COLOR_FORMAT_RGB565, + COLOR_FORMAT_RGB888 +}; + +bool tt_hal_display_driver_supported(DeviceId id); + +DisplayDriverHandle tt_hal_display_driver_alloc(DeviceId id); + +void tt_hal_display_driver_free(DisplayDriverHandle handle); + +ColorFormat tt_hal_display_driver_get_colorformat(DisplayDriverHandle handle); + +uint16_t tt_hal_display_driver_get_pixel_width(DisplayDriverHandle handle); + +uint16_t tt_hal_display_driver_get_pixel_height(DisplayDriverHandle handle); + +void tt_hal_display_driver_draw_bitmap(DisplayDriverHandle handle, int xStart, int yStart, int xEnd, int yEnd, const void* pixelData); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Include/tt_kernel.h b/TactilityC/Include/tt_kernel.h new file mode 100644 index 00000000..1c7b6f75 --- /dev/null +++ b/TactilityC/Include/tt_kernel.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned long TickType; + +void tt_kernel_delay_millis(uint32_t milliseconds); + +void tt_kernel_delay_micros(uint32_t microSeconds); + +void tt_kernel_delay_ticks(TickType ticks); + +TickType tt_kernel_get_ticks(); + +TickType tt_kernel_millis_to_ticks(uint32_t milliSeconds); + +bool tt_kernel_delay_until_tick(TickType tick); + +uint32_t tt_kernel_get_tick_frequency(); + +uint32_t tt_kernel_get_millis(); + +unsigned long tt_kernel_get_micros(); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Include/tt_lvgl.h b/TactilityC/Include/tt_lvgl.h new file mode 100644 index 00000000..e5f35722 --- /dev/null +++ b/TactilityC/Include/tt_lvgl.h @@ -0,0 +1,15 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +bool tt_lvgl_is_started(); + +void tt_lvgl_start(); + +void tt_lvgl_stop(); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Source/tt_hal_device.cpp b/TactilityC/Source/tt_hal_device.cpp new file mode 100644 index 00000000..1ba090dc --- /dev/null +++ b/TactilityC/Source/tt_hal_device.cpp @@ -0,0 +1,48 @@ +#include "tt_hal_device.h" + +#include "Tactility/Check.h" + +#include + +static tt::hal::Device::Type toTactilityDeviceType(DeviceType type) { + switch (type) { + case DEVICE_TYPE_I2C: + return tt::hal::Device::Type::I2c; + case DEVICE_TYPE_DISPLAY: + return tt::hal::Device::Type::Display; + case DEVICE_TYPE_TOUCH: + return tt::hal::Device::Type::Touch; + case DEVICE_TYPE_SDCARD: + return tt::hal::Device::Type::SdCard; + case DEVICE_TYPE_KEYBOARD: + return tt::hal::Device::Type::Keyboard; + case DEVICE_TYPE_POWER: + return tt::hal::Device::Type::Power; + case DEVICE_TYPE_GPS: + return tt::hal::Device::Type::Gps; + default: + tt_crash("Device::Type not supported"); + } +} + +extern "C" { + +bool tt_hal_device_find(DeviceType type, DeviceId* deviceIds, uint16_t* count, uint16_t maxCount) { + assert(maxCount > 0); + + int16_t currentIndex = -1; + uint16_t maxIndex = maxCount - 1; + + findDevices(toTactilityDeviceType(type), [&](const std::shared_ptr& device) { + currentIndex++; + deviceIds[currentIndex] = device->getId(); + // Continue if there is storage capacity left + return currentIndex < maxIndex; + }); + + *count = currentIndex + 1; + + return currentIndex >= 0; +} + +} diff --git a/TactilityC/Source/tt_hal_display.cpp b/TactilityC/Source/tt_hal_display.cpp new file mode 100644 index 00000000..2be3ee66 --- /dev/null +++ b/TactilityC/Source/tt_hal_display.cpp @@ -0,0 +1,74 @@ +#include "tt_hal_display.h" + +#include "Tactility/Check.h" +#include "Tactility/hal/Device.h" +#include "Tactility/hal/display/DisplayDevice.h" +#include "Tactility/hal/display/DisplayDriver.h" + +static ColorFormat toColorFormat(tt::hal::display::ColorFormat format) { + switch (format) { + case tt::hal::display::ColorFormat::Monochrome: + return COLOR_FORMAT_MONOCHROME; + case tt::hal::display::ColorFormat::BGR565: + return COLOR_FORMAT_BGR565; + case tt::hal::display::ColorFormat::RGB565: + return COLOR_FORMAT_RGB565; + case tt::hal::display::ColorFormat::RGB888: + return COLOR_FORMAT_RGB888; + default: + tt_crash("ColorFormat not supported"); + } +} + +struct DriverWrapper { + std::shared_ptr driver; + DriverWrapper(std::shared_ptr driver) : driver(driver) {} +}; + +static std::shared_ptr findValidDisplayDevice(tt::hal::Device::Id id) { + auto device = tt::hal::findDevice(id); + if (device == nullptr || device->getType() != tt::hal::Device::Type::Display) { + return nullptr; + } + return std::reinterpret_pointer_cast(device); +} + +extern "C" { + +bool tt_hal_display_driver_supported(DeviceId id) { + auto display = findValidDisplayDevice(id); + return display != nullptr && display->supportsDisplayDriver(); +} + +DisplayDriverHandle tt_hal_display_driver_alloc(DeviceId id) { + auto display = findValidDisplayDevice(id); + assert(display->supportsDisplayDriver()); + return new DriverWrapper(display->getDisplayDriver()); +} + +void tt_hal_display_driver_free(DisplayDriverHandle handle) { + DriverWrapper* wrapper = static_cast(handle); + delete wrapper; +} + +ColorFormat tt_hal_display_driver_get_colorformat(DisplayDriverHandle handle) { + DriverWrapper* wrapper = static_cast(handle); + return toColorFormat(wrapper->driver->getColorFormat()); +} + +uint16_t tt_hal_display_driver_get_pixel_width(DisplayDriverHandle handle) { + DriverWrapper* wrapper = static_cast(handle); + return wrapper->driver->getPixelWidth(); +} + +uint16_t tt_hal_display_driver_get_pixel_height(DisplayDriverHandle handle) { + DriverWrapper* wrapper = static_cast(handle); + return wrapper->driver->getPixelHeight(); +} + +void tt_hal_display_driver_draw_bitmap(DisplayDriverHandle handle, int xStart, int yStart, int xEnd, int yEnd, const void* pixelData) { + DriverWrapper* wrapper = static_cast(handle); + wrapper->driver->drawBitmap(xStart, yStart, xEnd, yEnd, pixelData); +} + +} \ No newline at end of file diff --git a/TactilityC/Source/tt_init.cpp b/TactilityC/Source/tt_init.cpp index 55139462..0bf683f0 100644 --- a/TactilityC/Source/tt_init.cpp +++ b/TactilityC/Source/tt_init.cpp @@ -6,7 +6,11 @@ #include "tt_app_selectiondialog.h" #include "tt_bundle.h" #include "tt_gps.h" +#include "tt_hal_device.h" +#include "tt_hal_display.h" #include "tt_hal_i2c.h" +#include "tt_kernel.h" +#include "tt_lvgl.h" #include "tt_lvgl_keyboard.h" #include "tt_lvgl_spinner.h" #include "tt_lvgl_toolbar.h" @@ -103,6 +107,9 @@ const esp_elfsym elf_symbols[] { ESP_ELFSYM_EXPORT(esp_log_write), ESP_ELFSYM_EXPORT(esp_log_timestamp), // Tactility + ESP_ELFSYM_EXPORT(tt_app_start), + ESP_ELFSYM_EXPORT(tt_app_start_with_bundle), + ESP_ELFSYM_EXPORT(tt_app_stop), ESP_ELFSYM_EXPORT(tt_app_register), ESP_ELFSYM_EXPORT(tt_app_get_parameters), ESP_ELFSYM_EXPORT(tt_app_set_result), @@ -121,6 +128,14 @@ const esp_elfsym elf_symbols[] { ESP_ELFSYM_EXPORT(tt_bundle_put_string), ESP_ELFSYM_EXPORT(tt_gps_has_coordinates), ESP_ELFSYM_EXPORT(tt_gps_get_coordinates), + ESP_ELFSYM_EXPORT(tt_hal_device_find), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_alloc), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_draw_bitmap), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_free), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_get_colorformat), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_get_pixel_height), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_get_pixel_width), + ESP_ELFSYM_EXPORT(tt_hal_display_driver_supported), ESP_ELFSYM_EXPORT(tt_hal_i2c_start), ESP_ELFSYM_EXPORT(tt_hal_i2c_stop), ESP_ELFSYM_EXPORT(tt_hal_i2c_is_started), @@ -132,6 +147,19 @@ const esp_elfsym elf_symbols[] { ESP_ELFSYM_EXPORT(tt_hal_i2c_master_has_device_at_address), ESP_ELFSYM_EXPORT(tt_hal_i2c_lock), ESP_ELFSYM_EXPORT(tt_hal_i2c_unlock), + ESP_ELFSYM_EXPORT(tt_kernel_delay_millis), + ESP_ELFSYM_EXPORT(tt_kernel_delay_micros), + ESP_ELFSYM_EXPORT(tt_kernel_delay_ticks), + ESP_ELFSYM_EXPORT(tt_kernel_get_ticks), + ESP_ELFSYM_EXPORT(tt_kernel_millis_to_ticks), + ESP_ELFSYM_EXPORT(tt_kernel_delay_until_tick), + ESP_ELFSYM_EXPORT(tt_kernel_get_tick_frequency), + ESP_ELFSYM_EXPORT(tt_kernel_get_millis), + ESP_ELFSYM_EXPORT(tt_kernel_get_micros), + ESP_ELFSYM_EXPORT(tt_lvgl_is_started), + ESP_ELFSYM_EXPORT(tt_lvgl_start), + ESP_ELFSYM_EXPORT(tt_lvgl_stop), + ESP_ELFSYM_EXPORT(tt_lvgl_software_keyboard_show), ESP_ELFSYM_EXPORT(tt_lvgl_software_keyboard_show), ESP_ELFSYM_EXPORT(tt_lvgl_software_keyboard_hide), ESP_ELFSYM_EXPORT(tt_lvgl_software_keyboard_is_enabled), diff --git a/TactilityC/Source/tt_kernel.cpp b/TactilityC/Source/tt_kernel.cpp new file mode 100644 index 00000000..46c1b632 --- /dev/null +++ b/TactilityC/Source/tt_kernel.cpp @@ -0,0 +1,42 @@ +#include "tt_kernel.h" +#include + +extern "C" { + +void tt_kernel_delay_millis(uint32_t milliseconds) { + tt::kernel::delayMillis(milliseconds); +} + +void tt_kernel_delay_micros(uint32_t microSeconds) { + tt::kernel::delayMicros(microSeconds); +} + +void tt_kernel_delay_ticks(TickType ticks) { + tt::kernel::delayTicks((TickType_t)ticks); +} + +TickType tt_kernel_get_ticks() { + return tt::kernel::getTicks(); +} + +TickType tt_kernel_millis_to_ticks(uint32_t milliSeconds) { + return tt::kernel::millisToTicks(milliSeconds); +} + +bool tt_kernel_delay_until_tick(TickType tick) { + return tt::kernel::delayUntilTick(tick); +} + +uint32_t tt_kernel_get_tick_frequency() { + return tt::kernel::getTickFrequency(); +} + +uint32_t tt_kernel_get_millis() { + return tt::kernel::getMillis(); +} + +unsigned long tt_kernel_get_micros() { + return tt::kernel::getMicros(); +} + +} \ No newline at end of file diff --git a/TactilityC/Source/tt_lvgl.cpp b/TactilityC/Source/tt_lvgl.cpp new file mode 100644 index 00000000..f7bf8a6c --- /dev/null +++ b/TactilityC/Source/tt_lvgl.cpp @@ -0,0 +1,17 @@ +#include + +extern "C" { + +bool tt_lvgl_is_started() { + return tt::lvgl::isStarted(); +} + +void tt_lvgl_start() { + tt::lvgl::start(); +} + +void tt_lvgl_stop() { + tt::lvgl::stop(); +} + +}