Initial content commit

This commit is contained in:
Ken Van Hoeylandt
2025-09-23 21:33:04 +02:00
parent 5a89c6c7fb
commit 755c6407d3
38 changed files with 4289 additions and 0 deletions
@@ -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 = TT_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);
}
};