Refactored

This commit is contained in:
Ken Van Hoeylandt 2025-08-17 13:29:10 +02:00
parent 0e238e7252
commit 629ec002f5
9 changed files with 202 additions and 112 deletions

View File

@ -1,6 +1,6 @@
#pragma once
#include <tt_hal_display.h>
#include <tt_hal_touch.h>
#include "drivers/DisplayDriver.h"
#include "drivers/TouchDriver.h"
void runApplication(DisplayDriverHandle display, TouchDriverHandle touch);
void runApplication(DisplayDriver* display, TouchDriver* touch);

View File

@ -1,21 +0,0 @@
#pragma once
#include <tt_hal_display.h>
#include <tt_hal_touch.h>
class Drivers {
DeviceId displayId = 0;
DeviceId touchId = 0;
public:
DisplayDriverHandle display = nullptr;
TouchDriverHandle touch = nullptr;
bool validateSupport();
bool start();
void stop();
};

View File

@ -0,0 +1,49 @@
#pragma once
#include <tt_hal_display.h>
class PixelBuffer {
uint16_t width;
uint16_t height;
ColorFormat colorFormat;
void* data;
public:
PixelBuffer(uint16_t width, uint16_t height, ColorFormat colorFormat);
~PixelBuffer();
uint16_t getWidth() const {
return width;
}
uint16_t getHeight() const {
return height;
}
ColorFormat getColorFormat() const {
return colorFormat;
}
void* getData() const {
return data;
}
uint32_t getDataSize() const {
return width * height * getPixelSize();
}
void* getDataAtRow(uint16_t row) const {
auto address = reinterpret_cast<uint32_t>(data) + (row * getRowSize());
return reinterpret_cast<void*>(address);
}
uint16_t getRowSize() const {
return width * getPixelSize();
}
uint8_t getPixelSize() const;
void clear() const;
};

View File

@ -0,0 +1,40 @@
#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);
}
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);
}
};

View File

@ -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);
}
};

View File

@ -1,7 +1,6 @@
#include "Application.h"
#include "PixelBuffer.h"
#include <cstdlib>
#include <cstring>
#include <tt_kernel.h>
const uint8_t logo[] = {
@ -31,46 +30,31 @@ const uint8_t logo[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x06, 0xff, 0x05, 0xff, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
static void waitForTouch(TouchDriverHandle touch) {
static void waitForTouch(TouchDriver* touch) {
// Wait for touch
uint16_t x, y, strength;
uint8_t pointCount = 0;
do {
tt_hal_touch_driver_get_touched_points(touch, &x, &y, &strength, &pointCount, 1);
touch->getTouchedPoints(&x, &y, &strength, &pointCount, 1);
tt_kernel_delay_millis(20);
} while (pointCount == 0);
}
uint32_t getPixelSize(ColorFormat colorFormat) {
switch (colorFormat) {
case COLOR_FORMAT_MONOCHROME:
return 1;
case COLOR_FORMAT_BGR565:
return 2;
case COLOR_FORMAT_RGB565:
return 2;
case COLOR_FORMAT_RGB888:
return 3;
default:
return 0;
}
}
void runApplication(DisplayDriver* display, TouchDriver* touch) {
void runApplication(DisplayDriverHandle display, TouchDriverHandle touch) {
PixelBuffer buffer(display->getWidth(), display->getHeight(), display->getColorFormat());
buffer.clear();
auto display_width = tt_hal_display_driver_get_pixel_width(display);
auto display_height = tt_hal_display_driver_get_pixel_height(display);
auto color_format = tt_hal_display_driver_get_colorformat(display);
auto buffer_height = display_height / 10;
auto buffer_size = display_width * buffer_height * getPixelSize(color_format);
void* buffer = malloc(buffer_size);
memset(buffer, 0, buffer_size);
for (int i = 0; i < 10; i++) {
tt_hal_display_driver_draw_bitmap(display, 0, i * buffer_height, display_width, (i + 1) * buffer_height, buffer);
// SPI displays don't allow us to render everything at once, so we send 1/10th of the display at a time
auto chunks = 10;
auto chunk_row_pixel_height = buffer.getHeight() / chunks;
for (int i = 0; i < chunks; i++) {
auto row = buffer.getHeight() * i / 10;
auto chunk_data = buffer.getDataAtRow(row);
display->drawBitmap(0, row, buffer.getWidth(), row + chunk_row_pixel_height, chunk_data);
}
tt_hal_display_driver_draw_bitmap(display, 0, 0, 24, 24, logo);
display->drawBitmap(0, 0, 24, 24, logo);
waitForTouch(touch);
}

View File

@ -1,44 +0,0 @@
#include "Drivers.h"
#include <esp_log.h>
constexpr auto TAG = "Drivers";
bool Drivers::validateSupport() {
uint16_t display_count = 0;
if (!tt_hal_device_find(DEVICE_TYPE_DISPLAY, &displayId, &display_count, 1)) {
ESP_LOGI(TAG, "No display device found");
return false;
}
if (!tt_hal_display_driver_supported(displayId)) {
ESP_LOGI(TAG, "Display doesn't support driver interface");
return false;
}
uint16_t touch_count = 0;
if (!tt_hal_device_find(DEVICE_TYPE_TOUCH, &touchId, &touch_count, 1)) {
ESP_LOGI(TAG, "No touch device found");
return false;
}
if (!tt_hal_touch_driver_supported(touchId)) {
ESP_LOGI(TAG, "Touch doesn't support driver interface");
return false;
}
return true;
}
bool Drivers::start() {
display = tt_hal_display_driver_alloc(displayId);
touch = tt_hal_touch_driver_alloc(touchId);
return true;
}
void Drivers::stop() {
tt_hal_display_driver_free(display);
tt_hal_touch_driver_free(touch);
display = nullptr;
touch = nullptr;
}

View File

@ -1,36 +1,53 @@
#include "Application.h"
#include "Drivers.h"
#include "drivers/DisplayDriver.h"
#include "drivers/TouchDriver.h"
#include <esp_log.h>
#include <tt_app.h>
#include <tt_lvgl.h>
static void onCreate(AppHandle appHandle, void* data) {
Drivers drivers;
constexpr auto TAG = "Main";
// Find the hardware devices and verify support for the driver interface that we need
// Not all graphics and touch drivers support accessing them directly
if (!drivers.validateSupport()) {
tt_app_stop();
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();
// Start using the drivers
if (drivers.start()) {
// Run the main logic
runApplication(drivers.display, drivers.touch);
// Stop the drivers
drivers.stop();
}
ESP_LOGI(TAG, "Creating display driver");
auto display = new DisplayDriver(display_id);
ESP_LOGI(TAG, "Creating display 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, "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();
}
}

View File

@ -0,0 +1,37 @@
#include "PixelBuffer.h"
#include <cstdlib>
#include <cstring>
PixelBuffer::PixelBuffer(uint16_t width, uint16_t height, ColorFormat colorFormat) :
width(width),
height(height),
colorFormat(colorFormat) {
data = malloc(width * height * getPixelSize());
assert(data != nullptr);
}
PixelBuffer::~PixelBuffer() {
free(data);
}
void PixelBuffer::clear() const {
memset(data, 0, getDataSize());
}
uint8_t PixelBuffer::getPixelSize() const {
switch (colorFormat) {
case COLOR_FORMAT_MONOCHROME:
return 1;
case COLOR_FORMAT_BGR565:
return 2;
case COLOR_FORMAT_RGB565:
return 2;
case COLOR_FORMAT_RGB888:
return 3;
default:
return 0;
}
}