New board: Elecrow CrowPanel Basic 2.8" (#225)

- Implemented Elecrow CrowPanel Basic 2.8"
- Change default "invert" setting for ILI934x driver from `true` to `false`
- Created `Xpt2046` driver subproject
- Refactored unPhone to use new `Xpt2046` driver subproject
This commit is contained in:
Ken Van Hoeylandt
2025-02-19 22:41:39 +01:00
committed by GitHub
parent 0563e42dc9
commit 6e8fbae62b
31 changed files with 566 additions and 150 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ public:
unsigned int verticalResolution;
bool mirrorX = false;
bool mirrorY = false;
bool invertColor = true;
bool invertColor = false;
uint32_t bufferSize = 0; // Size in pixel count. 0 means default, which is 1/10 of the screen size
std::shared_ptr<tt::hal::touch::TouchDevice> touch;
std::function<void(uint8_t)> _Nullable backlightDutyFunction = nullptr;
+5
View File
@@ -0,0 +1,5 @@
idf_component_register(
SRC_DIRS "Source"
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lcd_touch esp_lcd_touch_xpt2046
)
+3
View File
@@ -0,0 +1,3 @@
# XPT2046
A basic XPT2046 touch driver.
+96
View File
@@ -0,0 +1,96 @@
#include "Xpt2046Power.h"
#include "Xpt2046Touch.h"
#include <Tactility/Log.h>
#define TAG "xpt2046_power"
#define BATTERY_VOLTAGE_MIN 3.2f
#define BATTERY_VOLTAGE_MAX 4.2f
static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
float volts = std::min((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN);
float voltage_factor = std::min(1.0f, voltage_percentage);
auto charge_level = (uint8_t) (voltage_factor * 100.f);
TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level);
return charge_level;
}
bool Xpt2046Power::supportsMetric(MetricType type) const {
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
return true;
default:
return false;
}
}
bool Xpt2046Power::getMetric(MetricType type, MetricData& data) {
switch (type) {
using enum MetricType;
case BatteryVoltage:
return readBatteryVoltageSampled(data.valueAsUint32);
case ChargeLevel: {
uint32_t milli_volt;
if (readBatteryVoltageSampled(milli_volt)) {
data.valueAsUint8 = estimateChargeLevelFromVoltage(milli_volt);
return true;
} else {
return false;
}
}
default:
return false;
}
}
bool Xpt2046Power::readBatteryVoltageOnce(uint32_t& output) const {
// Make a safe copy
auto touch = Xpt2046Touch::getInstance();
if (touch != nullptr) {
float vbat;
if (touch->getVBat(vbat)) {
// Convert to mV
output = (uint32_t)(vbat * 1000.f);
return true;
}
}
return false;
}
#define MAX_VOLTAGE_SAMPLES 15
bool Xpt2046Power::readBatteryVoltageSampled(uint32_t& output) const {
size_t samples_read = 0;
uint32_t sample_accumulator = 0;
uint32_t sample_read_buffer;
for (size_t i = 0; i < MAX_VOLTAGE_SAMPLES; ++i) {
if (readBatteryVoltageOnce(sample_read_buffer)) {
sample_accumulator += sample_read_buffer;
samples_read++;
}
}
if (samples_read > 0) {
output = sample_accumulator / samples_read;
return true;
} else {
return false;
}
}
static std::shared_ptr<PowerDevice> power;
std::shared_ptr<PowerDevice> getOrCreatePower() {
if (power == nullptr) {
power = std::make_shared<Xpt2046Power>();
}
return power;
}
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include <Tactility/hal/power/PowerDevice.h>
#include <memory>
using tt::hal::power::PowerDevice;
/**
* Power management based on the voltage measurement at the LCD panel.
* This estimates the battery power left.
*/
class Xpt2046Power : public PowerDevice {
public:
Xpt2046Power() = default;
~Xpt2046Power() = default;
std::string getName() const final { return "XPT2046 Power Measurement"; }
std::string getDescription() const final { return "Power interface via XPT2046 voltage measurement"; }
bool supportsMetric(MetricType type) const override;
bool getMetric(MetricType type, MetricData& data) override;
private:
bool readBatteryVoltageOnce(uint32_t& output) const;
bool readBatteryVoltageSampled(uint32_t& output) const;
};
std::shared_ptr<PowerDevice> getOrCreatePower();
+98
View File
@@ -0,0 +1,98 @@
#include "Xpt2046Touch.h"
#include <Tactility/Log.h>
#include <Tactility/lvgl/LvglSync.h>
#include <esp_err.h>
#include <esp_lcd_touch_xpt2046.h>
#include <esp_lvgl_port.h>
#define TAG "xpt2046_touch"
Xpt2046Touch* Xpt2046Touch::instance = nullptr;
bool Xpt2046Touch::start(lv_display_t* display) {
const esp_lcd_panel_io_spi_config_t io_config = ESP_LCD_TOUCH_IO_SPI_XPT2046_CONFIG(configuration->spiPinCs);
if (esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &ioHandle) != ESP_OK) {
TT_LOG_E(TAG, "Touch IO SPI creation failed");
return false;
}
esp_lcd_touch_config_t config = {
.x_max = configuration->xMax,
.y_max = configuration->yMax,
.rst_gpio_num = GPIO_NUM_NC,
.int_gpio_num = GPIO_NUM_NC,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = configuration->swapXy,
.mirror_x = configuration->mirrorX,
.mirror_y = configuration->mirrorY,
},
.process_coordinates = nullptr,
.interrupt_callback = nullptr,
.user_data = nullptr,
.driver_data = nullptr
};
if (esp_lcd_touch_new_spi_xpt2046(ioHandle, &config, &touchHandle) != ESP_OK) {
TT_LOG_E(TAG, "XPT2046 driver init failed");
cleanup();
return false;
}
const lvgl_port_touch_cfg_t touch_cfg = {
.disp = display,
.handle = touchHandle,
};
TT_LOG_I(TAG, "Adding touch to LVGL");
deviceHandle = lvgl_port_add_touch(&touch_cfg);
if (deviceHandle == nullptr) {
TT_LOG_E(TAG, "Adding touch failed");
cleanup();
return false;
}
instance = this;
return true;
}
bool Xpt2046Touch::stop() {
instance = nullptr;
cleanup();
return true;
}
void Xpt2046Touch::cleanup() {
if (deviceHandle != nullptr) {
lv_indev_delete(deviceHandle);
deviceHandle = nullptr;
}
if (touchHandle != nullptr) {
esp_lcd_touch_del(touchHandle);
touchHandle = nullptr;
}
if (ioHandle != nullptr) {
esp_lcd_panel_io_del(ioHandle);
ioHandle = nullptr;
}
}
bool Xpt2046Touch::getVBat(float& outputVbat) {
if (touchHandle != nullptr) {
// Shares the SPI bus with the display, so we have to sync/lock as this method might be called from anywhere
if (tt::lvgl::lock(50 / portTICK_PERIOD_MS)) {
esp_lcd_touch_xpt2046_read_battery_level(touchHandle, &outputVbat);
tt::lvgl::unlock();
return true;
}
}
return false;
}
+71
View File
@@ -0,0 +1,71 @@
#pragma once
#include "Tactility/hal/touch/TouchDevice.h"
#include <Tactility/TactilityCore.h>
#include <esp_lcd_panel_io_interface.h>
#include <esp_lcd_touch.h>
class Xpt2046Touch : public tt::hal::touch::TouchDevice {
public:
class Configuration {
public:
Configuration(
esp_lcd_spi_bus_handle_t spiDevice,
gpio_num_t spiPinCs,
uint16_t xMax,
uint16_t yMax,
bool swapXy = false,
bool mirrorX = false,
bool mirrorY = false
) : spiDevice(spiDevice),
spiPinCs(spiPinCs),
xMax(xMax),
yMax(yMax),
swapXy(swapXy),
mirrorX(mirrorX),
mirrorY(mirrorY)
{}
esp_lcd_spi_bus_handle_t spiDevice;
gpio_num_t spiPinCs;
uint16_t xMax;
uint16_t yMax;
bool swapXy;
bool mirrorX;
bool mirrorY;
};
private:
static Xpt2046Touch* instance;
std::unique_ptr<Configuration> configuration;
esp_lcd_panel_io_handle_t _Nullable ioHandle = nullptr;
esp_lcd_touch_handle_t _Nullable touchHandle = nullptr;
lv_indev_t* _Nullable deviceHandle = nullptr;
void cleanup();
public:
explicit Xpt2046Touch(std::unique_ptr<Configuration> inConfiguration) : configuration(std::move(inConfiguration)) {
assert(configuration != nullptr);
}
std::string getName() const final { return "XPT2046"; }
std::string getDescription() const final { return "I2C touch driver"; }
bool start(lv_display_t* display) override;
bool stop() override;
lv_indev_t* _Nullable getLvglIndev() override { return deviceHandle; }
bool getVBat(float& outputVbat);
/** Used for accessing getVBat() in Power driver */
static Xpt2046Touch* getInstance() { return instance; }
};