-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocoDriverPage.cpp
More file actions
173 lines (140 loc) · 5.8 KB
/
Copy pathLocoDriverPage.cpp
File metadata and controls
173 lines (140 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "LocoDriverPage.h"
#include "ThreadSafeTFT.h"
#include "PageManager.h"
#include "ExtendedKeys.h"
#include "LocoCommandManagerFactory.h"
// Updated constructor to use LocoCommandManagerFactory
LocoDriverPage::LocoDriverPage() {
// Get the command manager through the factory with default config file path
locoManager = LocoCommandManagerFactory::getInstance().getLocoCommandManager();
// Initialize with default values
currentSpeed = 0;
currentBrake = 0;
}
void LocoDriverPage::draw() {
ThreadSafeTFT::withLock([this](TFT_eSPI& tft) {
// Clear screen
tft.fillScreen(TFT_BLACK);
// Draw title
tft.setTextColor(TFT_WHITE);
tft.drawCentreString("Train Controls", 160, 20, 4);
// Draw the gauges
drawSpeedGauge(tft);
drawBrakeGauge(tft);
// Draw labels for the gauges
tft.setTextColor(TFT_WHITE);
tft.drawCentreString("Speed", speedGaugeX, speedGaugeY + gaugeRadius + 10, 2);
tft.drawCentreString("Brake", brakeGaugeX, brakeGaugeY + gaugeRadius + 10, 2);
// Draw current values
tft.setTextColor(TFT_YELLOW);
tft.drawCentreString(String(currentSpeed) + " km/h", speedGaugeX, speedGaugeY + gaugeRadius + 30, 2);
tft.drawCentreString(String(currentBrake) + " psi", brakeGaugeX, brakeGaugeY + gaugeRadius + 30, 2);
// Draw key instructions at bottom of screen
tft.setTextColor(TFT_CYAN);
tft.drawString("UP/DOWN: Speed", 10, 220, 2);
tft.drawString("B1/B2: Brake", 220, 220, 2);
});
}
void LocoDriverPage::drawSpeedGauge(TFT_eSPI& tft) {
// Draw gauge background
tft.fillCircle(speedGaugeX, speedGaugeY, gaugeRadius, TFT_DARKGREY);
tft.fillCircle(speedGaugeX, speedGaugeY, gaugeRadius - 5, TFT_BLACK);
// Draw gauge labels
drawGaugeLabels(tft, speedGaugeX, speedGaugeY, 100, gaugeRadius);
// Draw the needle
drawNeedle(tft, speedGaugeX, speedGaugeY, currentSpeed, 100, gaugeRadius - 10, TFT_RED);
}
void LocoDriverPage::drawBrakeGauge(TFT_eSPI& tft) {
// Draw gauge background
tft.fillCircle(brakeGaugeX, brakeGaugeY, gaugeRadius, TFT_DARKGREY);
tft.fillCircle(brakeGaugeX, brakeGaugeY, gaugeRadius - 5, TFT_BLACK);
// Draw gauge labels
drawGaugeLabels(tft, brakeGaugeX, brakeGaugeY, 100, gaugeRadius);
// Draw the needle
drawNeedle(tft, brakeGaugeX, brakeGaugeY, currentBrake, 100, gaugeRadius - 10, TFT_GREEN);
}
void LocoDriverPage::drawNeedle(TFT_eSPI& tft, int centerX, int centerY, int value, int maxValue, int radius, uint16_t color) {
// Calculate angle based on value (0-270 degrees, where 0 is at 9 o'clock position)
float angle = map(value, 0, maxValue, 0, 270) - 135;
angle = angle * PI / 180.0; // Convert to radians
// Calculate needle endpoint
int endX = centerX + radius * cos(angle);
int endY = centerY + radius * sin(angle);
// Draw the needle
tft.drawLine(centerX, centerY, endX, endY, color);
tft.drawLine(centerX-1, centerY, endX, endY, color); // Make needle thicker
tft.drawLine(centerX+1, centerY, endX, endY, color);
// Draw center point
tft.fillCircle(centerX, centerY, 5, color);
}
void LocoDriverPage::drawGaugeLabels(TFT_eSPI& tft, int centerX, int centerY, int maxValue, int radius) {
tft.setTextColor(TFT_WHITE);
// Draw major tick marks and labels at 0, 25, 50, 75, 100%
for (int i = 0; i <= 4; i++) {
int value = i * (maxValue / 4);
float angle = map(value, 0, maxValue, 0, 270) - 135;
angle = angle * PI / 180.0; // Convert to radians
// Calculate tick mark positions
int innerX = centerX + (radius - 15) * cos(angle);
int innerY = centerY + (radius - 15) * sin(angle);
int outerX = centerX + radius * cos(angle);
int outerY = centerY + radius * sin(angle);
// Draw tick mark
tft.drawLine(innerX, innerY, outerX, outerY, TFT_WHITE);
// Draw label
int labelX = centerX + (radius - 25) * cos(angle);
int labelY = centerY + (radius - 25) * sin(angle);
tft.drawString(String(value), labelX, labelY, 1);
}
}
void LocoDriverPage::handleInput(IKeyboard* keyboard) {
uint16_t keys = keyboard->getPressedKeys();
bool needsRedraw = false;
// Check for brake control keys
if (keys & ExtendedKeys::KEY_TIGHT_BRAKE) {
// Increase brake pressure (max 100)
currentBrake = min(currentBrake + 5, 100);
locoManager->setBrake(currentBrake); // Changed from . to ->
needsRedraw = true;
}
if (keys & ExtendedKeys::KEY_RELEASE_BRAKE) {
// Decrease brake pressure (min 0)
currentBrake = max(currentBrake - 5, 0);
locoManager->setBrake(currentBrake); // Changed from . to ->
needsRedraw = true;
}
// Normal navigation keys can be used for speed control
if (keys & KEY_UP) {
// Increase speed (max 100)
currentSpeed = min(currentSpeed + 5, 100);
locoManager->setSpeed(currentSpeed); // Changed from . to ->
needsRedraw = true;
}
if (keys & KEY_DOWN) {
// Decrease speed (min 0)
currentSpeed = max(currentSpeed - 5, 0);
locoManager->setSpeed(currentSpeed); // Changed from . to ->
needsRedraw = true;
}
// Go back to main menu with OK button
if (keys & KEY_OK) {
PageManager::popPage();
return;
}
// Only redraw if values changed
if (needsRedraw) {
draw();
}
}
void LocoDriverPage::updateSpeed(int speed) {
if (currentSpeed != speed) {
currentSpeed = speed;
draw();
}
}
void LocoDriverPage::updateBrake(int brake) {
if (currentBrake != brake) {
currentBrake = brake;
draw();
}
}