Tab5 features, StackChan, fixes, drivers.... (#526)

This commit is contained in:
Shadowtrance
2026-05-29 07:31:25 +10:00
committed by GitHub
parent 5c78d55b04
commit a59fbf4ed5
140 changed files with 2500 additions and 835 deletions
@@ -0,0 +1,58 @@
#include "Display.h"
#include <Axp2101.h>
#include <Ft6x36Touch.h>
#include <Ili934xDisplay.h>
#include <Tactility/Logger.h>
#include <Tactility/hal/i2c/I2c.h>
static const auto LOGGER = tt::Logger("StackChanDisplay");
static void setBacklightDuty(uint8_t backlightDuty) {
const uint8_t voltage = 20 + ((8 * backlightDuty) / 255); // [0b00000, 0b11100]
if (!tt::hal::i2c::masterWriteRegister(I2C_NUM_0, AXP2101_ADDRESS, 0x99, &voltage, 1, 1000)) {
LOGGER.error("Failed to set display backlight voltage");
}
}
static std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
auto configuration = std::make_unique<Ft6x36Touch::Configuration>(
I2C_NUM_0,
319,//LCD_HORIZONTAL_RESOLUTION,
239,//LCD_VERTICAL_RESOLUTION,
false,
false,
false
);
return std::make_shared<Ft6x36Touch>(std::move(configuration));
}
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay() {
Ili934xDisplay::Configuration panel_configuration = {
.horizontalResolution = LCD_HORIZONTAL_RESOLUTION,
.verticalResolution = LCD_VERTICAL_RESOLUTION,
.gapX = 0,
.gapY = 0,
.swapXY = false,
.mirrorX = false,
.mirrorY = false,
.invertColor = true,
.swapBytes = true,
.bufferSize = LCD_BUFFER_SIZE,
.touch = createTouch(),
.backlightDutyFunction = ::setBacklightDuty,
.resetPin = GPIO_NUM_NC,
.rgbElementOrder = LCD_RGB_ELEMENT_ORDER_BGR
};
auto spi_configuration = std::make_shared<Ili934xDisplay::SpiConfiguration>(Ili934xDisplay::SpiConfiguration {
.spiHostDevice = LCD_SPI_HOST,
.csPin = LCD_PIN_CS,
.dcPin = LCD_PIN_DC,
.pixelClockFrequency = 40'000'000,
.transactionQueueDepth = 10
});
return std::make_shared<Ili934xDisplay>(panel_configuration, spi_configuration, true);
}
@@ -0,0 +1,17 @@
#pragma once
#include <Tactility/hal/display/DisplayDevice.h>
#include <driver/gpio.h>
#include <driver/spi_common.h>
// Display
constexpr auto LCD_SPI_HOST = SPI2_HOST;
constexpr auto LCD_PIN_CS = GPIO_NUM_3;
constexpr auto LCD_PIN_DC = GPIO_NUM_35;
constexpr auto LCD_HORIZONTAL_RESOLUTION = 320;
constexpr auto LCD_VERTICAL_RESOLUTION = 240;
constexpr auto LCD_BUFFER_HEIGHT = LCD_VERTICAL_RESOLUTION / 10;
constexpr auto LCD_BUFFER_SIZE = LCD_HORIZONTAL_RESOLUTION * LCD_BUFFER_HEIGHT;
constexpr auto LCD_SPI_TRANSFER_SIZE_LIMIT = LCD_BUFFER_SIZE * LV_COLOR_DEPTH / 8;
std::shared_ptr<tt::hal::display::DisplayDevice> createDisplay();
@@ -0,0 +1,86 @@
#include "Power.h"
#include <Tactility/hal/power/PowerDevice.h>
#include <drivers/ina226.h>
#include <tactility/device.h>
#include <tactility/log.h>
using namespace tt::hal::power;
static constexpr auto* TAG = "StackChanPower";
// 1S Li-ion cell (550 mAh): 3.2V (empty) 4.2V (full)
static constexpr float MIN_BATTERY_VOLTAGE_MV = 3200.0f;
static constexpr float MAX_BATTERY_VOLTAGE_MV = 4200.0f;
class StackChanPower final : public PowerDevice {
public:
explicit StackChanPower(::Device* ina226Device) : ina226(ina226Device) {}
std::string getName() const override { return "M5Stack StackChan Power"; }
std::string getDescription() const override { return "Battery monitoring via INA226 over I2C"; }
bool supportsMetric(MetricType type) const override {
switch (type) {
using enum MetricType;
case BatteryVoltage:
case ChargeLevel:
case Current:
return ina226 != nullptr;
default:
return false;
}
}
bool getMetric(MetricType type, MetricData& data) override {
switch (type) {
using enum MetricType;
case BatteryVoltage: {
if (ina226 == nullptr) return false;
float volts = 0.0f;
if (ina226_read_bus_voltage(ina226, &volts) != ERROR_NONE) return false;
data.valueAsUint32 = static_cast<uint32_t>(volts * 1000.0f);
return true;
}
case ChargeLevel: {
if (ina226 == nullptr) return false;
float volts = 0.0f;
if (ina226_read_bus_voltage(ina226, &volts) != ERROR_NONE) return false;
float voltage_mv = volts * 1000.0f;
if (voltage_mv >= MAX_BATTERY_VOLTAGE_MV) {
data.valueAsUint8 = 100;
} else if (voltage_mv <= MIN_BATTERY_VOLTAGE_MV) {
data.valueAsUint8 = 0;
} else {
float factor = (voltage_mv - MIN_BATTERY_VOLTAGE_MV) / (MAX_BATTERY_VOLTAGE_MV - MIN_BATTERY_VOLTAGE_MV);
data.valueAsUint8 = static_cast<uint8_t>(factor * 100.0f);
}
return true;
}
case Current: {
if (ina226 == nullptr) return false;
float amps = 0.0f;
if (ina226_read_shunt_current(ina226, &amps) != ERROR_NONE) return false;
data.valueAsInt32 = static_cast<int32_t>(amps * 1000.0f);
return true;
}
default:
return false;
}
}
private:
::Device* ina226;
};
std::shared_ptr<PowerDevice> createPower() {
auto* ina226 = device_find_by_name("ina226");
if (ina226 == nullptr) {
LOG_E(TAG, "ina226 device not found");
}
return std::make_shared<StackChanPower>(ina226);
}
@@ -0,0 +1,6 @@
#pragma once
#include <memory>
#include <Tactility/hal/power/PowerDevice.h>
std::shared_ptr<tt::hal::power::PowerDevice> createPower();
@@ -0,0 +1,30 @@
#include "SdCard.h"
#include <tactility/device.h>
#include <Tactility/lvgl/LvglSync.h>
#include <Tactility/hal/sdcard/SpiSdCardDevice.h>
constexpr auto STACKCHAN_SDCARD_PIN_CS = GPIO_NUM_4;
constexpr auto STACKCHAN_LCD_PIN_CS = GPIO_NUM_3;
using tt::hal::sdcard::SpiSdCardDevice;
std::shared_ptr<SdCardDevice> createSdCard() {
auto configuration = std::make_unique<SpiSdCardDevice::Config>(
STACKCHAN_SDCARD_PIN_CS,
GPIO_NUM_NC,
GPIO_NUM_NC,
GPIO_NUM_NC,
SdCardDevice::MountBehaviour::AtBoot,
tt::lvgl::getSyncLock(),
std::vector { STACKCHAN_LCD_PIN_CS }
);
auto* spi_controller = device_find_by_name("spi0");
check(spi_controller, "spi0 not found");
return std::make_shared<SpiSdCardDevice>(
std::move(configuration),
spi_controller
);
}
@@ -0,0 +1,7 @@
#pragma once
#include "Tactility/hal/sdcard/SdCardDevice.h"
using tt::hal::sdcard::SdCardDevice;
std::shared_ptr<SdCardDevice> createSdCard();