refactor(display): extract tile layout logic to library helper class
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
#include "DisplayDriverTFT.h"
|
||||
#include <KlubhausCore.h>
|
||||
|
||||
extern DisplayManager display;
|
||||
|
||||
void DisplayDriverTFT::begin() {
|
||||
// Backlight
|
||||
@@ -134,25 +137,19 @@ void DisplayDriverTFT::drawDashboard(const ScreenState& st) {
|
||||
_tft.setCursor(DISPLAY_WIDTH - 50, 5);
|
||||
_tft.printf("WiFi:%s", st.wifiSsid.length() > 0 ? "ON" : "OFF");
|
||||
|
||||
// Render tiles via DisplayManager (abstracted to library)
|
||||
// For now, draw directly using our tile implementation
|
||||
constexpr int cols = 2;
|
||||
constexpr int rows = 2;
|
||||
constexpr int tileW = DISPLAY_WIDTH / cols;
|
||||
constexpr int tileH = (DISPLAY_HEIGHT - 30) / rows;
|
||||
constexpr int margin = 8;
|
||||
// Get tile layouts from library helper
|
||||
int tileCount = display.calculateDashboardLayouts(30, 8);
|
||||
const TileLayout* layouts = display.getTileLayouts();
|
||||
|
||||
const char* tileLabels[] = { "Alert", "Silent", "Status", "Reboot" };
|
||||
const uint16_t tileColors[] = { 0x0280, 0x0400, 0x0440, 0x0100 };
|
||||
|
||||
for(int i = 0; i < 4; i++) {
|
||||
int col = i % cols;
|
||||
int row = i / cols;
|
||||
|
||||
int x = col * tileW + margin;
|
||||
int y = 30 + row * tileH + margin;
|
||||
int w = tileW - 2 * margin;
|
||||
int h = tileH - 2 * margin;
|
||||
for(int i = 0; i < tileCount && i < 4; i++) {
|
||||
const TileLayout& lay = layouts[i];
|
||||
int x = lay.x;
|
||||
int y = lay.y;
|
||||
int w = lay.w;
|
||||
int h = lay.h;
|
||||
|
||||
// Tile background
|
||||
_tft.fillRoundRect(x, y, w, h, 8, tileColors[i]);
|
||||
@@ -180,7 +177,6 @@ TouchEvent DisplayDriverTFT::readTouch() {
|
||||
evt.pressed = true;
|
||||
evt.x = tx;
|
||||
evt.y = ty;
|
||||
Serial.printf("[TOUCH] x=%d, y=%d\n", tx, ty);
|
||||
}
|
||||
return evt;
|
||||
}
|
||||
@@ -196,22 +192,6 @@ void DisplayDriverTFT::transformTouch(int* x, int* y) {
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
void DisplayDriverTFT::drawTileAt(int x, int y, int w, int h, const char* label, uint16_t bgColor) {
|
||||
// Tile background
|
||||
_tft.fillRoundRect(x, y, w, h, 8, bgColor);
|
||||
|
||||
// Tile border
|
||||
_tft.drawRoundRect(x, y, w, h, 8, TFT_WHITE);
|
||||
|
||||
// Tile label
|
||||
_tft.setTextColor(TFT_WHITE);
|
||||
_tft.setTextSize(2);
|
||||
int textLen = strlen(label);
|
||||
int textW = textLen * 12; // approx width
|
||||
_tft.setCursor(x + w/2 - textW/2, y + h/2 - 10);
|
||||
_tft.print(label);
|
||||
}
|
||||
|
||||
HoldState DisplayDriverTFT::updateHold(unsigned long holdMs) {
|
||||
HoldState h;
|
||||
TouchEvent t = readTouch();
|
||||
|
||||
@@ -14,12 +14,11 @@ public:
|
||||
uint16_t getRawTouchZ();
|
||||
HoldState updateHold(unsigned long holdMs) override;
|
||||
void updateHint(int x, int y, bool active) override;
|
||||
int width() override { return DISPLAY_WIDTH; }
|
||||
int height() override { return DISPLAY_HEIGHT; }
|
||||
int width() override { return _tft.width(); }
|
||||
int height() override { return _tft.height(); }
|
||||
|
||||
// Dashboard tiles - library handles grid math, we just draw
|
||||
// Dashboard - uses transform for touch coordinate correction
|
||||
void transformTouch(int* x, int* y) override;
|
||||
void drawTileAt(int x, int y, int w, int h, const char* label, uint16_t bgColor) override;
|
||||
|
||||
private:
|
||||
void drawBoot(const ScreenState& st);
|
||||
|
||||
@@ -24,11 +24,6 @@ void loop() {
|
||||
// Read touch
|
||||
TouchEvent evt = display.readTouch();
|
||||
|
||||
// Touch debug (useful for new boards)
|
||||
if(evt.pressed) {
|
||||
Serial.printf("[TOUCH] pressed: x=%d, y=%d\n", evt.x, evt.y);
|
||||
}
|
||||
|
||||
// State machine tick
|
||||
logic.update();
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
#include "board_config.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <KlubhausCore.h>
|
||||
|
||||
// ── Globals ──
|
||||
static LGFX* _gfx = nullptr;
|
||||
extern DisplayManager display;
|
||||
|
||||
// ── Forward declarations ──
|
||||
static void initDisplay();
|
||||
@@ -52,9 +54,9 @@ void DisplayDriverGFX::setBacklight(bool on) {
|
||||
}
|
||||
}
|
||||
|
||||
int DisplayDriverGFX::width() { return DISP_W; }
|
||||
int DisplayDriverGFX::width() { return _gfx ? _gfx->width() : DISP_W; }
|
||||
|
||||
int DisplayDriverGFX::height() { return DISP_H; }
|
||||
int DisplayDriverGFX::height() { return _gfx ? _gfx->height() : DISP_H; }
|
||||
|
||||
// ── Touch handling ──
|
||||
|
||||
@@ -96,22 +98,6 @@ int DisplayDriverGFX::dashboardTouch(int x, int y) {
|
||||
return row * cols + col;
|
||||
}
|
||||
|
||||
void DisplayDriverGFX::drawTileAt(int x, int y, int w, int h, const char* label, uint16_t bgColor) {
|
||||
// Tile background (use fillRect - LovyanGFX may not have fillRoundRect)
|
||||
_gfx->fillRect(x, y, w, h, bgColor);
|
||||
|
||||
// Tile border
|
||||
_gfx->drawRect(x, y, w, h, 0xFFFF);
|
||||
|
||||
// Tile label
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(2);
|
||||
int textLen = strlen(label);
|
||||
int textW = textLen * 12;
|
||||
_gfx->setCursor(x + w/2 - textW/2, y + h/2 - 10);
|
||||
_gfx->print(label);
|
||||
}
|
||||
|
||||
HoldState DisplayDriverGFX::updateHold(unsigned long holdMs) {
|
||||
HoldState state;
|
||||
|
||||
@@ -256,24 +242,18 @@ void DisplayDriverGFX::drawDashboard(const ScreenState& state) {
|
||||
_gfx->setCursor(DISP_W - 100, 10);
|
||||
_gfx->printf("WiFi:%s", state.wifiSsid.length() > 0 ? "ON" : "OFF");
|
||||
|
||||
// Tiles: 2 rows × 4 columns
|
||||
constexpr int cols = 4;
|
||||
constexpr int rows = 2;
|
||||
constexpr int tileW = DISP_W / cols;
|
||||
constexpr int tileH = (DISP_H - 30) / rows;
|
||||
constexpr int margin = 8;
|
||||
// Get tile layouts from library helper
|
||||
int tileCount = display.calculateDashboardLayouts(30, 8);
|
||||
const TileLayout* layouts = display.getTileLayouts();
|
||||
|
||||
// Draw placeholder tiles (8 total for 2x4 grid)
|
||||
const char* tileLabels[] = { "1", "2", "3", "4", "5", "6", "7", "8" };
|
||||
|
||||
for(int i = 0; i < 8; i++) {
|
||||
int col = i % cols;
|
||||
int row = i / cols;
|
||||
|
||||
int x = col * tileW + margin;
|
||||
int y = 30 + row * tileH + margin;
|
||||
int w = tileW - 2 * margin;
|
||||
int h = tileH - 2 * margin;
|
||||
for(int i = 0; i < tileCount && i < 8; i++) {
|
||||
const TileLayout& lay = layouts[i];
|
||||
int x = lay.x;
|
||||
int y = lay.y;
|
||||
int w = lay.w;
|
||||
int h = lay.h;
|
||||
|
||||
// Tile background
|
||||
_gfx->fillRoundRect(x, y, w, h, 8, 0x0220);
|
||||
@@ -281,10 +261,10 @@ void DisplayDriverGFX::drawDashboard(const ScreenState& state) {
|
||||
// Tile border
|
||||
_gfx->drawRoundRect(x, y, w, h, 8, 0xFFFF);
|
||||
|
||||
// Tile number
|
||||
// Tile label
|
||||
_gfx->setTextColor(0xFFFF);
|
||||
_gfx->setTextSize(2);
|
||||
_gfx->setCursor(x + w / 2 - 10, y + h / 2 - 10);
|
||||
_gfx->setCursor(x + w/2 - 10, y + h/2 - 10);
|
||||
_gfx->print(tileLabels[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,6 @@ public:
|
||||
int width() override;
|
||||
int height() override;
|
||||
|
||||
// Dashboard tiles - library handles grid math, we just draw
|
||||
void drawTileAt(int x, int y, int w, int h, const char* label, uint16_t bgColor) override;
|
||||
|
||||
// ── Internal ──
|
||||
static DisplayDriverGFX& instance();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user