Merge develop into main (#303)

- `DisplayDevice` improvements related `DisplayDriver`
- Replaced incorrect usage of `spiBusHandle` with `spiHostDevice` in all SPI display devices
- Disabled `DisplayDriver` support for RGB displays for now
- Updated `GraphicsDemo` project for the above changes
- TactilityC improvements:
  - created `tt_hal_device_find()`
  - created `tt_hal_display_*`
  - created `tt_hal_touch_*`
  - created `tt_lvgl_*`
  - export `tt_app_*` calls
This commit is contained in:
Ken Van Hoeylandt
2025-08-17 22:48:51 +02:00
committed by GitHub
parent d875ade8cb
commit fbaff8cbac
43 changed files with 1554 additions and 70 deletions
@@ -0,0 +1,7 @@
file(GLOB_RECURSE SOURCE_FILES Source/*.c*)
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Include"
REQUIRES TactilitySDK
)
@@ -0,0 +1,6 @@
#pragma once
#include "drivers/DisplayDriver.h"
#include "drivers/TouchDriver.h"
void runApplication(DisplayDriver* display, TouchDriver* touch);
@@ -0,0 +1,125 @@
#pragma once
#include <esp_log.h>
#include "drivers/Colors.h"
#include <cstring>
#include <tt_hal_display.h>
class PixelBuffer {
uint16_t pixelWidth;
uint16_t pixelHeight;
ColorFormat colorFormat;
uint8_t* data;
public:
PixelBuffer(uint16_t pixelWidth, uint16_t pixelHeight, ColorFormat colorFormat) :
pixelWidth(pixelWidth),
pixelHeight(pixelHeight),
colorFormat(colorFormat)
{
data = static_cast<uint8_t*>(malloc(pixelWidth * pixelHeight * getPixelSize()));
assert(data != nullptr);
}
~PixelBuffer() {
free(data);
}
uint16_t getPixelWidth() const {
return pixelWidth;
}
uint16_t getPixelHeight() const {
return pixelHeight;
}
ColorFormat getColorFormat() const {
return colorFormat;
}
void* getData() const {
return data;
}
uint32_t getDataSize() const {
return pixelWidth * pixelHeight * getPixelSize();
}
void* getDataAtRow(uint16_t row) const {
auto address = reinterpret_cast<uint32_t>(data) + (row * getRowDataSize());
return reinterpret_cast<void*>(address);
}
uint16_t getRowDataSize() const {
return pixelWidth * getPixelSize();
}
uint8_t getPixelSize() const {
switch (colorFormat) {
case COLOR_FORMAT_MONOCHROME:
return 1;
case COLOR_FORMAT_BGR565:
case COLOR_FORMAT_BGR565_SWAPPED:
case COLOR_FORMAT_RGB565:
case COLOR_FORMAT_RGB565_SWAPPED:
return 2;
case COLOR_FORMAT_RGB888:
return 3;
default:
// TODO: Crash with error
return 0;
}
}
uint8_t* getPixelAddress(uint16_t x, uint16_t y) const {
uint32_t offset = ((y * getPixelWidth()) + x) * getPixelSize();
uint32_t address = reinterpret_cast<uint32_t>(data) + offset;
return reinterpret_cast<uint8_t*>(address);
}
void setPixel(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b) const {
auto address = getPixelAddress(x, y);
switch (colorFormat) {
case COLOR_FORMAT_MONOCHROME:
*address = (uint8_t)((uint16_t)r + (uint16_t)g + (uint16_t)b / 3);
break;
case COLOR_FORMAT_BGR565:
Colors::rgb888ToBgr565(r, g, b, reinterpret_cast<uint16_t*>(address));
break;
case COLOR_FORMAT_BGR565_SWAPPED: {
// TODO: Make proper conversion function
Colors::rgb888ToBgr565(r, g, b, reinterpret_cast<uint16_t*>(address));
uint8_t temp = *address;
*address = *(address + 1);
*(address + 1) = temp;
break;
}
case COLOR_FORMAT_RGB565: {
Colors::rgb888ToRgb565(r, g, b, reinterpret_cast<uint16_t*>(address));
break;
}
case COLOR_FORMAT_RGB565_SWAPPED: {
// TODO: Make proper conversion function
Colors::rgb888ToRgb565(r, g, b, reinterpret_cast<uint16_t*>(address));
uint8_t temp = *address;
*address = *(address + 1);
*(address + 1) = temp;
break;
}
case COLOR_FORMAT_RGB888: {
uint8_t pixel[3] = { r, g, b };
memcpy(address, pixel, 3);
break;
}
default:
// NO-OP
break;
}
}
void clear(int value = 0) const {
memset(data, value, getDataSize());
}
};
@@ -0,0 +1,35 @@
#pragma once
class Colors {
public:
static void rgb888ToRgb565(uint8_t red, uint8_t green, uint8_t blue, uint16_t* rgb565) {
uint16_t _rgb565 = (red >> 3);
_rgb565 = (_rgb565 << 6) | (green >> 2);
_rgb565 = (_rgb565 << 5) | (blue >> 3);
*rgb565 = _rgb565;
}
static void rgb888ToBgr565(uint8_t red, uint8_t green, uint8_t blue, uint16_t* bgr565) {
uint16_t _bgr565 = (blue >> 3);
_bgr565 = (_bgr565 << 6) | (green >> 2);
_bgr565 = (_bgr565 << 5) | (red >> 3);
*bgr565 = _bgr565;
}
static void rgb565ToRgb888(uint16_t rgb565, uint32_t* rgb888) {
uint32_t _rgb565 = rgb565;
uint8_t b = (_rgb565 >> 8) & 0xF8;
uint8_t g = (_rgb565 >> 3) & 0xFC;
uint8_t r = (_rgb565 << 3) & 0xF8;
uint8_t* r8p = reinterpret_cast<uint8_t*>(rgb888);
uint8_t* g8p = r8p + 1;
uint8_t* b8p = r8p + 2;
*r8p = r | ((r >> 3) & 0x7);
*g8p = g | ((g >> 2) & 0x3);
*b8p = b | ((b >> 3) & 0x7);
}
};
@@ -0,0 +1,48 @@
#pragma once
#include <cassert>
#include <tt_hal_display.h>
/**
* Wrapper for tt_hal_display_driver_*
*/
class DisplayDriver {
DisplayDriverHandle handle = nullptr;
public:
explicit DisplayDriver(DeviceId id) {
assert(tt_hal_display_driver_supported(id));
handle = tt_hal_display_driver_alloc(id);
assert(handle != nullptr);
}
~DisplayDriver() {
tt_hal_display_driver_free(handle);
}
bool lock(TickType timeout = MAX_TICKS) const {
return tt_hal_display_driver_lock(handle, timeout);
}
void unlock() const {
tt_hal_display_driver_unlock(handle);
}
uint16_t getWidth() const {
return tt_hal_display_driver_get_pixel_width(handle);
}
uint16_t getHeight() const {
return tt_hal_display_driver_get_pixel_height(handle);
}
ColorFormat getColorFormat() const {
return tt_hal_display_driver_get_colorformat(handle);
}
void drawBitmap(int xStart, int yStart, int xEnd, int yEnd, const void* pixelData) const {
tt_hal_display_driver_draw_bitmap(handle, xStart, yStart, xEnd, yEnd, pixelData);
}
};
@@ -0,0 +1,28 @@
#pragma once
#include <cassert>
#include <tt_hal_touch.h>
/**
* Wrapper for tt_hal_touch_driver_*
*/
class TouchDriver {
TouchDriverHandle handle = nullptr;
public:
explicit TouchDriver(DeviceId id) {
assert(tt_hal_touch_driver_supported(id));
handle = tt_hal_touch_driver_alloc(id);
assert(handle != nullptr);
}
~TouchDriver() {
tt_hal_touch_driver_free(handle);
}
bool getTouchedPoints(uint16_t* x, uint16_t* y, uint16_t* strength, uint8_t* count, uint8_t maxCount) const {
return tt_hal_touch_driver_get_touched_points(handle, x, y, strength, count, maxCount);
}
};
@@ -0,0 +1,72 @@
#include "Application.h"
#include "PixelBuffer.h"
#include "esp_log.h"
#include <tt_kernel.h>
constexpr auto TAG = "Application";
static bool isTouched(TouchDriver* touch) {
uint16_t x, y, strength;
uint8_t pointCount = 0;
return touch->getTouchedPoints(&x, &y, &strength, &pointCount, 1);
}
void createRgbRow(PixelBuffer& buffer) {
uint8_t offset = buffer.getPixelWidth() / 3;
for (int i = 0; i < buffer.getPixelWidth(); ++i) {
if (i < offset) {
buffer.setPixel(i, 0, 255, 0, 0);
} else if (i < offset * 2) {
buffer.setPixel(i, 0, 0, 255, 0);
} else {
buffer.setPixel(i, 0, 0, 0, 255);
}
}
}
void createRgbFadingRow(PixelBuffer& buffer) {
uint8_t stroke = buffer.getPixelWidth() / 3;
for (int i = 0; i < buffer.getPixelWidth(); ++i) {
if (i < stroke) {
auto color = i * 255 / stroke;
buffer.setPixel(i, 0, color, 0, 0);
} else if (i < stroke * 2) {
auto color = (i - stroke) * 255 / stroke;
buffer.setPixel(i, 0, 0, color, 0);
} else {
auto color = (i - (2*stroke)) * 255 / stroke;
buffer.setPixel(i, 0, 0, 0, color);
}
}
}
void runApplication(DisplayDriver* display, TouchDriver* touch) {
// Single row buffers
PixelBuffer line_clear_buffer(display->getWidth(), 1, display->getColorFormat());
line_clear_buffer.clear();
PixelBuffer line_buffer(display->getWidth(), 1, display->getColorFormat());
line_buffer.clear();
do {
// Draw row by row
// This is placed in a loop to test the SPI locking mechanismss
for (int i = 0; i < display->getHeight(); i++) {
if (i == 0) {
createRgbRow(line_buffer);
} else if (i == display->getHeight() / 2) {
createRgbFadingRow(line_buffer);
}
display->lock();
display->drawBitmap(0, i, display->getWidth(), i + 1, line_buffer.getData());
display->unlock();
}
// Give other tasks space to breathe
// SPI displays would otherwise time out SPI SD card access
tt_kernel_delay_ticks(1);
} while (!isTouched(touch));
}
@@ -0,0 +1,71 @@
#include "Application.h"
#include "drivers/DisplayDriver.h"
#include "drivers/TouchDriver.h"
#include <esp_log.h>
#include <tt_app.h>
#include <tt_lvgl.h>
constexpr auto TAG = "Main";
static void onCreate(AppHandle appHandle, void* data) {
uint16_t display_count = 0;
DeviceId display_id;
if (!tt_hal_device_find(DEVICE_TYPE_DISPLAY, &display_id, &display_count, 1)) {
ESP_LOGI(TAG, "No display device found");
return;
}
uint16_t touch_count = 0;
DeviceId touch_id;
if (!tt_hal_device_find(DEVICE_TYPE_TOUCH, &touch_id, &touch_count, 1)) {
ESP_LOGI(TAG, "No touch device found");
return;
}
// Stop LVGL first (because it's currently using the drivers we want to use)
tt_lvgl_stop();
ESP_LOGI(TAG, "Creating display driver");
auto display = new DisplayDriver(display_id);
ESP_LOGI(TAG, "Creating touch driver");
auto touch = new TouchDriver(touch_id);
// Run the main logic
ESP_LOGI(TAG, "Running application");
runApplication(display, touch);
ESP_LOGI(TAG, "Cleanup display driver");
delete display;
ESP_LOGI(TAG, "Cleanup touch driver");
delete touch;
ESP_LOGI(TAG, "Stopping application");
tt_app_stop();
}
static void onDestroy(AppHandle appHandle, void* data) {
// Restart LVGL to resume rendering of regular apps
if (!tt_lvgl_is_started()) {
ESP_LOGI(TAG, "Restarting LVGL");
tt_lvgl_start();
}
}
ExternalAppManifest manifest = {
.name = "Hello World",
.onCreate = onCreate,
.onDestroy = onDestroy
};
extern "C" {
int main(int argc, char* argv[]) {
tt_app_register(&manifest);
return 0;
}
}