refactor(Style): Add font abstraction and CSS-like styling constants
This commit is contained in:
@@ -1,5 +1,85 @@
|
||||
#include "DisplayDriverTFT.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <KlubhausCore.h>
|
||||
#include <TFT_eSPI.h>
|
||||
|
||||
extern DisplayManager display;
|
||||
|
||||
// ── Fonts ───────────────────────────────────────────────────
|
||||
// TFT_eSPI built-in fonts for 320x240 display (further scaled)
|
||||
// Using FreeFonts for better readability
|
||||
|
||||
void DisplayDriverTFT::setTitleFont() { _tft.setFreeFont(&FreeSansBold12pt7b); }
|
||||
|
||||
void DisplayDriverTFT::setBodyFont() { _tft.setFreeFont(&FreeSans9pt7b); }
|
||||
|
||||
void DisplayDriverTFT::setLabelFont() { _tft.setFreeFont(&FreeSans9pt7b); }
|
||||
|
||||
void DisplayDriverTFT::setDefaultFont() { _tft.setTextFont(2); }
|
||||
|
||||
// ── Test harness ───────────────────────────────────────────────
|
||||
|
||||
// Test harness: parse serial commands to inject synthetic touches
|
||||
// Commands:
|
||||
// TEST:touch x y press - simulate press at (x, y)
|
||||
// TEST:touch x y release - simulate release at (x, y)
|
||||
// TEST:touch clear - clear test mode
|
||||
bool DisplayDriverTFT::parseTestTouch(int* outX, int* outY, bool* outPressed) {
|
||||
if(!Serial.available())
|
||||
return false;
|
||||
|
||||
if(Serial.peek() != 'T') {
|
||||
return false;
|
||||
}
|
||||
|
||||
String cmd = Serial.readStringUntil('\n');
|
||||
cmd.trim();
|
||||
|
||||
if(!cmd.startsWith("TEST:touch"))
|
||||
return false;
|
||||
|
||||
int firstSpace = cmd.indexOf(' ');
|
||||
if(firstSpace < 0)
|
||||
return false;
|
||||
|
||||
String args = cmd.substring(firstSpace + 1);
|
||||
args.trim();
|
||||
|
||||
if(args.equals("clear")) {
|
||||
_testMode = false;
|
||||
Serial.println("[TEST] Test mode cleared");
|
||||
return false;
|
||||
}
|
||||
|
||||
int secondSpace = args.indexOf(' ');
|
||||
if(secondSpace < 0)
|
||||
return false;
|
||||
|
||||
String xStr = args.substring(0, secondSpace);
|
||||
String yState = args.substring(secondSpace + 1);
|
||||
yState.trim();
|
||||
|
||||
int x = xStr.toInt();
|
||||
int y = yState.substring(0, yState.indexOf(' ')).toInt();
|
||||
String state = yState.substring(yState.indexOf(' ') + 1);
|
||||
state.trim();
|
||||
|
||||
bool pressed = state.equals("press");
|
||||
|
||||
Serial.printf("[TEST] Injecting touch: (%d,%d) %s\n", x, y, pressed ? "press" : "release");
|
||||
|
||||
if(outX)
|
||||
*outX = x;
|
||||
if(outY)
|
||||
*outY = y;
|
||||
if(outPressed)
|
||||
*outPressed = pressed;
|
||||
|
||||
_testMode = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void DisplayDriverTFT::begin() {
|
||||
// Backlight
|
||||
pinMode(PIN_LCD_BL, OUTPUT);
|
||||
@@ -145,6 +225,34 @@ void DisplayDriverTFT::drawDashboard(const ScreenState& st) {
|
||||
|
||||
TouchEvent DisplayDriverTFT::readTouch() {
|
||||
TouchEvent evt;
|
||||
|
||||
// Check for test injection via serial
|
||||
int testX, testY;
|
||||
bool testPressed;
|
||||
if(parseTestTouch(&testX, &testY, &testPressed)) {
|
||||
if(testPressed && !_touchWasPressed) {
|
||||
evt.pressed = true;
|
||||
_touchDownX = testX;
|
||||
_touchDownY = testY;
|
||||
evt.downX = _touchDownX;
|
||||
evt.downY = _touchDownY;
|
||||
} else if(!testPressed && _touchWasPressed) {
|
||||
evt.released = true;
|
||||
evt.downX = _touchDownX;
|
||||
evt.downY = _touchDownY;
|
||||
}
|
||||
|
||||
if(testPressed) {
|
||||
evt.x = testX;
|
||||
evt.y = testY;
|
||||
evt.downX = _touchDownX;
|
||||
evt.downY = _touchDownY;
|
||||
}
|
||||
|
||||
_touchWasPressed = testPressed;
|
||||
return evt;
|
||||
}
|
||||
|
||||
uint16_t tx, ty;
|
||||
uint8_t touched = _tft.getTouch(&tx, &ty, 100);
|
||||
|
||||
|
||||
@@ -11,12 +11,18 @@ public:
|
||||
void setBacklight(bool on) override;
|
||||
void render(const ScreenState& state) override;
|
||||
TouchEvent readTouch() override;
|
||||
int dashboardTouch(int x, int y) override;
|
||||
int dashboardTouch(int x, int y);
|
||||
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; }
|
||||
|
||||
// Fonts
|
||||
void setTitleFont() override;
|
||||
void setBodyFont() override;
|
||||
void setLabelFont() override;
|
||||
void setDefaultFont() override;
|
||||
|
||||
private:
|
||||
void drawBoot(const ScreenState& st);
|
||||
void drawAlert(const ScreenState& st);
|
||||
@@ -34,4 +40,8 @@ private:
|
||||
bool _touchWasPressed = false;
|
||||
int _touchDownX = -1;
|
||||
int _touchDownY = -1;
|
||||
|
||||
// Test mode for touch injection
|
||||
bool _testMode = false;
|
||||
bool parseTestTouch(int* outX, int* outY, bool* outPressed);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FQBN="esp32:esp32:esp32:FlashSize=4M,PartitionScheme=default"
|
||||
PORT="/dev/ttyUSB0"
|
||||
LIBS="--libraries ./vendor/esp32-32e/TFT_eSPI"
|
||||
OPTS="-DDEBUG_MODE -DBOARD_HAS_PSRAM"
|
||||
OPTS="-DDEBUG_MODE -DBOARD_HAS_PSRAM -DLOCAL_SECRETS -DLOAD_GFXFF=1"
|
||||
|
||||
@@ -20,3 +20,22 @@
|
||||
// If using capacitive touch (e.g. FT6236), configure I2C pins here:
|
||||
// #define TOUCH_SDA 21
|
||||
// #define TOUCH_SCL 22
|
||||
|
||||
// ── Style Constants (CSS-like) ────────────────────────────────────────
|
||||
// Spacing - scaled for 320x240
|
||||
#define STYLE_SPACING_X 4
|
||||
#define STYLE_SPACING_Y 4
|
||||
#define STYLE_HEADER_HEIGHT 20
|
||||
#define STYLE_TILE_GAP 4
|
||||
#define STYLE_TILE_PADDING 6
|
||||
#define STYLE_TILE_RADIUS 4
|
||||
|
||||
// Colors
|
||||
#define STYLE_COLOR_BG TFT_BLACK
|
||||
#define STYLE_COLOR_HEADER 0x1A1A
|
||||
#define STYLE_COLOR_FG TFT_WHITE
|
||||
#define STYLE_COLOR_ALERT TFT_RED
|
||||
#define STYLE_COLOR_TILE_1 0x0280
|
||||
#define STYLE_COLOR_TILE_2 0x0400
|
||||
#define STYLE_COLOR_TILE_3 0x0440
|
||||
#define STYLE_COLOR_TILE_4 0x0100
|
||||
|
||||
@@ -4,7 +4,14 @@
|
||||
|
||||
#include "DisplayDriverTFT.h"
|
||||
#include "board_config.h"
|
||||
|
||||
// Include local secrets.h if it exists (board-specific credentials),
|
||||
// otherwise KlubhausCore/src/secrets.h provides defaults.
|
||||
#ifdef LOCAL_SECRETS
|
||||
#include "secrets.h"
|
||||
#else
|
||||
#include <secrets.h>
|
||||
#endif
|
||||
|
||||
#include <KlubhausCore.h>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user