189 lines
4.9 KiB
C++
189 lines
4.9 KiB
C++
/**
|
|
* @file TouchscreenUI.h
|
|
* @brief Touchscreen user interface for locomotive test bench
|
|
*
|
|
* Provides a graphical interface on the ILI9341 TFT display with touch controls for:
|
|
* - Power ON/OFF button
|
|
* - DCC/Analog mode switching
|
|
* - Speed slider (0-100%)
|
|
* - 2-rail/3-rail configuration selector
|
|
* - Direction control
|
|
* - Status display
|
|
*
|
|
* @author Locomotive Test Bench Project
|
|
* @date 2025
|
|
*/
|
|
|
|
#ifndef TOUCHSCREEN_UI_H
|
|
#define TOUCHSCREEN_UI_H
|
|
|
|
#include <Arduino.h>
|
|
#include <TFT_eSPI.h>
|
|
#include <XPT2046_Touchscreen.h>
|
|
#include "Config.h"
|
|
#include "MotorController.h"
|
|
#include "DCCGenerator.h"
|
|
#include "RelayController.h"
|
|
|
|
// Touch calibration values for ESP32-2432S028R
|
|
#define TS_MIN_X 200
|
|
#define TS_MAX_X 3700
|
|
#define TS_MIN_Y 200
|
|
#define TS_MAX_Y 3750
|
|
|
|
// UI Colors
|
|
#define COLOR_BG 0x0000 // Black
|
|
#define COLOR_PANEL 0x2945 // Dark gray
|
|
#define COLOR_TEXT 0xFFFF // White
|
|
#define COLOR_POWER_ON 0x07E0 // Green
|
|
#define COLOR_POWER_OFF 0xF800 // Red
|
|
#define COLOR_DCC 0x07FF // Cyan
|
|
#define COLOR_ANALOG 0xFFE0 // Yellow
|
|
#define COLOR_SLIDER 0x435C // Gray
|
|
#define COLOR_SLIDER_ACTIVE 0x07E0 // Green
|
|
#define COLOR_BUTTON 0x4A49 // Button gray
|
|
#define COLOR_BUTTON_ACTIVE 0x2124 // Darker gray
|
|
#define COLOR_FUNCTION_OFF 0x31A6 // Dark blue-gray
|
|
#define COLOR_FUNCTION_ON 0xFD20 // Orange
|
|
|
|
/**
|
|
* @struct Button
|
|
* @brief Simple button structure for touch areas
|
|
*/
|
|
struct Button {
|
|
int16_t x, y, w, h;
|
|
String label;
|
|
uint16_t color;
|
|
bool visible;
|
|
};
|
|
|
|
/**
|
|
* @class TouchscreenUI
|
|
* @brief Manages touchscreen display and user interactions
|
|
*
|
|
* Provides complete UI for controlling the locomotive test bench,
|
|
* handling touch events, updating displays, and coordinating with
|
|
* motor controller, DCC generator, and relay controller.
|
|
*/
|
|
class TouchscreenUI {
|
|
public:
|
|
/**
|
|
* @brief Constructor
|
|
* @param cfg Pointer to configuration object
|
|
* @param motor Pointer to motor controller
|
|
* @param dcc Pointer to DCC generator
|
|
* @param relay Pointer to relay controller
|
|
*/
|
|
TouchscreenUI(Config* cfg, MotorController* motor, DCCGenerator* dcc, RelayController* relay);
|
|
|
|
/**
|
|
* @brief Initialize touchscreen and display
|
|
*
|
|
* Sets up TFT display, touch controller, and draws initial UI.
|
|
*/
|
|
void begin();
|
|
|
|
/**
|
|
* @brief Update UI and handle touch events
|
|
*
|
|
* Must be called regularly from main loop.
|
|
* Handles touch detection, UI updates, and state changes.
|
|
*/
|
|
void update();
|
|
|
|
/**
|
|
* @brief Force full screen redraw
|
|
*/
|
|
void redraw();
|
|
|
|
/**
|
|
* @brief Get power state
|
|
* @return true if power is ON
|
|
*/
|
|
bool isPowerOn() { return powerOn; }
|
|
|
|
private:
|
|
TFT_eSPI tft;
|
|
XPT2046_Touchscreen touch;
|
|
|
|
Config* config;
|
|
MotorController* motorController;
|
|
DCCGenerator* dccGenerator;
|
|
RelayController* relayController;
|
|
|
|
bool powerOn;
|
|
uint8_t lastSpeed;
|
|
bool lastDirection;
|
|
bool lastIsDCC;
|
|
bool lastIs3Rail;
|
|
uint32_t lastDccFunctions;
|
|
|
|
// Programming screen state
|
|
bool programmingMode;
|
|
uint16_t cvNumber;
|
|
uint8_t cvValue;
|
|
uint16_t newAddress;
|
|
uint8_t keypadMode; // 0=address, 1=CV number, 2=CV value
|
|
|
|
// UI element positions
|
|
Button btnPower;
|
|
Button btnMode;
|
|
Button btnRails;
|
|
Button btnDirection;
|
|
Button btnDccAddress;
|
|
|
|
// DCC function buttons (F0-F12)
|
|
#define NUM_FUNCTIONS 13
|
|
Button btnFunctions[NUM_FUNCTIONS];
|
|
|
|
// Programming mode buttons
|
|
Button btnProgramming;
|
|
Button btnProgBack;
|
|
Button btnFactoryReset;
|
|
Button btnSetAddress;
|
|
Button btnReadCV;
|
|
Button btnWriteCV;
|
|
|
|
// Numeric keypad (0-9, backspace, enter)
|
|
#define NUM_KEYPAD_BUTTONS 12
|
|
Button btnKeypad[NUM_KEYPAD_BUTTONS];
|
|
|
|
// Slider position and state
|
|
int16_t sliderX, sliderY, sliderW, sliderH;
|
|
int16_t sliderKnobX;
|
|
bool sliderPressed;
|
|
|
|
// Private methods
|
|
void drawUI();
|
|
void drawPowerButton();
|
|
void drawModeButton();
|
|
void drawRailsButton();
|
|
void drawDirectionButton();
|
|
void drawSpeedSlider();
|
|
void drawStatusBar();
|
|
void drawDccFunctions();
|
|
void drawDccAddressButton();
|
|
void drawProgrammingScreen();
|
|
void drawNumericKeypad();
|
|
void drawProgrammingStatus();
|
|
|
|
void handleTouch(int16_t x, int16_t y);
|
|
void updatePowerState(bool state);
|
|
void updateMode(bool isDCC);
|
|
void updateRailMode(bool is3Rail);
|
|
void updateDirection();
|
|
void updateSpeed(uint8_t newSpeed);
|
|
void toggleDccFunction(uint8_t function);
|
|
void enterProgrammingMode();
|
|
void exitProgrammingMode();
|
|
void handleKeypadPress(uint8_t key);
|
|
void performFactoryReset();
|
|
void performSetAddress();
|
|
void performReadCV();
|
|
void performWriteCV();
|
|
|
|
int16_t mapTouch(int16_t value, int16_t inMin, int16_t inMax, int16_t outMin, int16_t outMax);
|
|
};
|
|
|
|
#endif // TOUCHSCREEN_UI_H
|