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
@@ -3,5 +3,5 @@ file(GLOB_RECURSE SOURCE_FILES Source/*.c*)
idf_component_register(
SRCS ${SOURCE_FILES}
INCLUDE_DIRS "Source"
REQUIRES Tactility esp_lvgl_port esp_io_expander esp_io_expander_tca95xx_16bit BQ24295 esp_lcd_touch esp_lcd_touch_xpt2046
REQUIRES Tactility esp_lvgl_port esp_io_expander esp_io_expander_tca95xx_16bit BQ24295 XPT2046
)
+3 -3
View File
@@ -1,8 +1,8 @@
#include "Tactility/lvgl/LvglSync.h"
#include "UnPhoneFeatures.h"
#include "hal/UnPhoneDisplayConstants.h"
#include "Xpt2046Power.h"
#include "hal/UnPhoneDisplay.h"
#include "hal/UnPhonePower.h"
#include "hal/UnPhoneDisplayConstants.h"
#include "hal/UnPhoneSdCard.h"
#include <Tactility/hal/Configuration.h>
@@ -14,7 +14,7 @@ extern const tt::hal::Configuration unPhone = {
.initBoot = unPhoneInitPower,
.createDisplay = createDisplay,
.sdcard = createUnPhoneSdCard(),
.power = unPhoneGetPower,
.power = getOrCreatePower,
.i2c = {
tt::hal::i2c::Configuration {
.name = "Internal",
+6 -5
View File
@@ -1,12 +1,13 @@
#include "UnPhoneDisplay.h"
#include "UnPhoneDisplayConstants.h"
#include "UnPhoneTouch.h"
#include "UnPhoneFeatures.h"
#include <Tactility/Log.h>
#include "UnPhoneFeatures.h"
#include "esp_err.h"
#include "hx8357/disp_spi.h"
#include "hx8357/hx8357.h"
#include <esp_err.h>
#include <hx8357/disp_spi.h>
#include <hx8357/hx8357.h>
#define TAG "unphone_display"
#define BUFFER_SIZE (UNPHONE_LCD_HORIZONTAL_RESOLUTION * UNPHONE_LCD_DRAW_BUFFER_HEIGHT * LV_COLOR_DEPTH / 8)
@@ -64,7 +65,7 @@ bool UnPhoneDisplay::stop() {
}
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable UnPhoneDisplay::createTouch() {
return std::make_shared<UnPhoneTouch>();
return ::createTouch();
}
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay() {
@@ -1,94 +0,0 @@
#include "UnPhonePower.h"
#include "UnPhoneTouch.h"
#include <Tactility/Log.h>
#define TAG "unphone_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 UnPhonePower::supportsMetric(MetricType type) const {
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
return true;
default:
return false;
}
}
bool UnPhonePower::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 UnPhonePower::readBatteryVoltageOnce(uint32_t& output) const {
auto* touch = UnPhoneTouch::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 UnPhonePower::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> unPhoneGetPower() {
if (power == nullptr) {
power = std::make_shared<UnPhonePower>();
}
return power;
}
-27
View File
@@ -1,27 +0,0 @@
#pragma once
#include "Tactility/hal/power/PowerDevice.h"
#include <memory>
using tt::hal::power::PowerDevice;
class UnPhonePower : public PowerDevice {
public:
UnPhonePower() = default;
~UnPhonePower() = 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> unPhoneGetPower();
+9 -94
View File
@@ -1,100 +1,15 @@
#include "UnPhoneTouch.h"
#include "UnPhoneDisplayConstants.h"
#include <Tactility/Log.h>
#include "esp_err.h"
#include "esp_lcd_touch_xpt2046.h"
#include "esp_lvgl_port.h"
#include <Tactility/lvgl/LvglSync.h>
#define TAG "unphone_touch"
std::shared_ptr<Xpt2046Touch> createTouch() {
auto configuration = std::make_unique<Xpt2046Touch::Configuration>(
UNPHONE_LCD_SPI_HOST,
GPIO_NUM_38,
320,
480
);
#define UNPHONE_TOUCH_X_MAX 320
#define UNPHONE_TOUCH_Y_MAX 480
UnPhoneTouch* UnPhoneTouch::instance = nullptr;
bool UnPhoneTouch::start(lv_display_t* display) {
const esp_lcd_panel_io_spi_config_t io_config = ESP_LCD_TOUCH_IO_SPI_XPT2046_CONFIG(GPIO_NUM_38);
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 = UNPHONE_TOUCH_X_MAX,
.y_max = UNPHONE_TOUCH_Y_MAX,
.rst_gpio_num = GPIO_NUM_NC,
.int_gpio_num = GPIO_NUM_NC,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
.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 UnPhoneTouch::stop() {
instance = nullptr;
cleanup();
return true;
}
void UnPhoneTouch::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 UnPhoneTouch::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;
return std::make_shared<Xpt2046Touch>(std::move(configuration));
}
+4 -28
View File
@@ -1,32 +1,8 @@
#pragma once
#include "Tactility/hal/touch/TouchDevice.h"
#include <Tactility/TactilityCore.h>
#include <esp_lcd_panel_io_interface.h>
#include <esp_lcd_touch.h>
#include <memory>
#include <Xpt2046Touch.h>
class UnPhoneTouch : public tt::hal::touch::TouchDevice {
extern std::shared_ptr<Xpt2046Touch> touchInstance;
private:
static UnPhoneTouch* instance;
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:
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 UnPhoneTouch* getInstance() { return instance; }
};
std::shared_ptr<Xpt2046Touch> createTouch();