Hal refactored (#99)

This commit is contained in:
Ken Van Hoeylandt
2024-12-02 00:32:39 +01:00
committed by GitHub
parent 0188ce721c
commit 33bb742dfb
103 changed files with 1222 additions and 1228 deletions
@@ -0,0 +1,89 @@
#include "M5stackDisplay.h"
#include "M5stackTouch.h"
#include "Log.h"
#include <TactilityCore.h>
#define TAG "m5shared_display"
#include "M5Unified.hpp"
#include "esp_err.h"
#include "esp_lvgl_port.h"
static void flush_callback(lv_display_t* display, const lv_area_t* area, uint8_t* px_map) {
M5GFX& gfx = *(M5GFX*)lv_display_get_driver_data(display);
int32_t width = (area->x2 - area->x1 + 1);
int32_t height = (area->y2 - area->y1 + 1);
gfx.startWrite();
gfx.setAddrWindow(area->x1, area->y1, width, height);
gfx.writePixels((lgfx::rgb565_t*)px_map, width * height);
gfx.endWrite();
lv_display_flush_ready(display);
}
_Nullable lv_disp_t* createDisplay(bool usePsram) {
M5GFX& gfx = M5.Display;
static lv_display_t* display = lv_display_create(gfx.width(), gfx.height());
LV_ASSERT_MALLOC(display)
if (display == nullptr) {
return nullptr;
}
lv_display_set_driver_data(display, &gfx);
lv_display_set_flush_cb(display, flush_callback);
const size_t bytes_per_pixel = 2; // assume 16-bit color
const size_t buffer_size = gfx.width() * gfx.height() * bytes_per_pixel / 8;
if (usePsram) {
static auto* buffer1 = (uint8_t*)heap_caps_malloc(buffer_size, MALLOC_CAP_SPIRAM);
LV_ASSERT_MALLOC(buffer1)
static auto* buffer2 = (uint8_t*)heap_caps_malloc(buffer_size, MALLOC_CAP_SPIRAM);
LV_ASSERT_MALLOC(buffer2)
lv_display_set_buffers(
display,
(void*)buffer1,
(void*)buffer2,
buffer_size,
LV_DISPLAY_RENDER_MODE_PARTIAL
);
} else {
static auto* buffer = (uint8_t*)malloc(buffer_size);
LV_ASSERT_MALLOC(buffer)
lv_display_set_buffers(
display,
(void*)buffer,
nullptr,
buffer_size,
LV_DISPLAY_RENDER_MODE_PARTIAL
);
}
return display;
}
bool M5stackDisplay::start() {
TT_LOG_I(TAG, "Starting");
displayHandle = createDisplay(true);
TT_LOG_I(TAG, "Finished");
return displayHandle != nullptr;
}
bool M5stackDisplay::stop() {
tt_assert(displayHandle != nullptr);
lv_display_delete(displayHandle);
displayHandle = nullptr;
return true;
}
tt::hal::Touch* _Nullable M5stackDisplay::createTouch() {
return static_cast<tt::hal::Touch*>(new M5stackTouch());
}
tt::hal::Display* createDisplay() {
return static_cast<tt::hal::Display*>(new M5stackDisplay());
}
@@ -0,0 +1,33 @@
#pragma once
#include "lvgl.h"
#include "hal/Display.h"
extern lv_disp_t* displayHandle;
class M5stackDisplay : public tt::hal::Display {
private:
lv_display_t* displayHandle = nullptr;
public:
bool start() override;
bool stop() override;
void setPowerOn(bool turnOn) override {}
bool isPoweredOn() const override { return true; };
bool supportsPowerControl() const override { return false; }
tt::hal::Touch* _Nullable createTouch() override;
void setBacklightDuty(uint8_t backlightDuty) override {}
uint8_t getBacklightDuty() const override { return 255; }
bool supportsBacklightDuty() const override { return false; }
lv_display_t* _Nullable getLvglDisplay() const override { return displayHandle; }
};
tt::hal::Display* createDisplay();
@@ -0,0 +1,48 @@
#include "M5stackTouch.h"
#include "M5Unified.hpp"
#include "esp_err.h"
#include "Log.h"
#define TAG "m5stack_touch"
static void touchReadCallback(TT_UNUSED lv_indev_t* indev, lv_indev_data_t* data) {
lgfx::touch_point_t point; // Making it static makes it unreliable
bool touched = M5.Lcd.getTouch(&point) > 0;
if (!touched) {
data->state = LV_INDEV_STATE_REL;
} else {
data->state = LV_INDEV_STATE_PR;
data->point.x = point.x;
data->point.y = point.y;
}
}
_Nullable lv_indev_t* createTouch() {
static lv_indev_t* indev = lv_indev_create();
LV_ASSERT_MALLOC(indev)
if (indev == nullptr) {
return nullptr;
}
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, touchReadCallback);
return indev;
}
bool M5stackTouch::start(lv_display_t* display) {
TT_LOG_I(TAG, "Adding touch to LVGL");
deviceHandle = createTouch();
if (deviceHandle == nullptr) {
TT_LOG_E(TAG, "Adding touch failed");
return false;
}
return true;
}
bool M5stackTouch::stop() {
tt_assert(deviceHandle != nullptr);
lv_indev_delete(deviceHandle);
deviceHandle = nullptr;
return true;
}
@@ -0,0 +1,13 @@
#pragma once
#include "hal/Touch.h"
#include "TactilityCore.h"
class M5stackTouch : public tt::hal::Touch {
private:
lv_indev_t* _Nullable deviceHandle = nullptr;
public:
bool start(lv_display_t* display) override;
bool stop() override;
lv_indev_t* _Nullable getLvglIndev() override { return deviceHandle; }
};