-
Notifications
You must be signed in to change notification settings - Fork 0
/
RallyComputerDisplat.ino
291 lines (233 loc) · 9.09 KB
/
RallyComputerDisplat.ino
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GPS.h> // Include your GPS library here
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
// GSM/GPS variables (assuming GPS is used for time)
Adafruit_GPS gps = Adafruit_GPS(&Serial1); // Initialize GPS on Serial1
bool blinkState = false;
unsigned long lastBlinkTime = 0;
const unsigned long BLINK_INTERVAL = 500;
unsigned long lastPageSwitchTime = 0;
const unsigned long PAGE_SWITCH_DELAY = 2000;
int currentPage = 0;
unsigned long stopwatchStart = 0;
unsigned long stopwatchElapsed = 0;
bool stopwatchRunning = false;
int scrollOffset = 0; // Variable for scrolling message
void setup() {
SerialUSB.begin(115200);
gps.begin(9600); // Start GPS at 9600 baud
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC)) {
SerialUSB.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.display();
}
void loop() {
unsigned long currentTime = millis();
// Update GPS to fetch real-time clock (RTC)
if (gps.newNMEAreceived()) {
gps.parse(gps.lastNMEA()); // Parse new NMEA data
}
// Handle page switching
if (currentPage == 0 && currentTime - lastPageSwitchTime >= PAGE_SWITCH_DELAY) {
currentPage = 1; // Switch to the main UI after 2 seconds
}
// Blinking logic for battery low warning
if (currentTime - lastBlinkTime >= BLINK_INTERVAL) {
blinkState = !blinkState; // Toggle blink state
lastBlinkTime = currentTime;
}
// Render appropriate page
display.clearDisplay();
switch (currentPage) {
case 0:
drawStartupPage();
break;
case 1:
drawMainUI(gpsTime(), "C1", 100, "--.--", "--.--", "Hazard 153m ahead! Car3 pressed SOS. Stay safe!");
break;
case 2:
drawStopwatchAndElapsedUI(gpsTime(), "C1", 20, "00:00:00", "00:00:00", "Broadcasting message");
break;
case 3:
drawStartEndTimePage(gpsTime(), "C1", 20, "00:00:00", "00:00:00");
break;
case 4:
drawOverallResultsPage(gpsTime(), "C1", 20, "--.--", "00:00:00", "--");
break;
}
display.display();
delay(10);
}
// Function to fetch time from GPS (RTC)
String gpsTime() {
if (gps.hour < 10) {
return String("0") + gps.hour + ":" + (gps.minute < 10 ? "0" + String(gps.minute) : String(gps.minute));
}
return String(gps.hour) + ":" + (gps.minute < 10 ? "0" + String(gps.minute) : String(gps.minute));
}
// Draw the startup page
void drawStartupPage() {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(SCREEN_WIDTH / 6, SCREEN_HEIGHT / 2 - 10);
display.println("BLUEBAND SPORTS");
}
// Draw the main UI page
/* void drawMainUI(String time, String carID, int batteryPercentage, String speed, String distance, String message) {
// Top section
display.setTextSize(1);
display.setCursor(0, 0);
display.print(time);
// Car ID with circle (toggle between filled and hollow)
display.setCursor(SCREEN_WIDTH / 2 - 10, 0);
display.print(carID);
drawCircleWithToggle(SCREEN_WIDTH / 2 + 18, 4, blinkState);
drawBatteryStatus(batteryPercentage);
// Divider line
drawLine(0, 12, SCREEN_WIDTH, 12);
// Speed and Distance
display.setCursor(0, 16);
display.println("spd: " + (speed != "--.--" ? speed : "N/A"));
display.setCursor(0, 26);
display.println("dis: " + (distance != "--.--" ? distance : "N/A"));
// Divider line
drawLine(0, 50, SCREEN_WIDTH, 36);
// Broadcasting message with scrolling
display.setCursor(-scrollOffset+15, 56); // Scrolling from left to right
display.println(message);
scrollOffset++;
if (scrollOffset > SCREEN_WIDTH) scrollOffset = 0; // Reset scroll
} */
// Draw the main UI page
void drawMainUI(String time, String carID, int batteryPercentage, String speed, String distance, String message) {
// Top section
display.setTextSize(1);
display.setCursor(0, 3);
display.print(time);
// Car ID with circle (toggle between filled and hollow)
display.setCursor(SCREEN_WIDTH / 2 - 15, 3);
display.print(carID);
drawCircleWithToggle(SCREEN_WIDTH / 2 + 18, 5, blinkState);
drawBatteryStatus(batteryPercentage);
// Divider line
drawLine(0, 12, SCREEN_WIDTH, 12);
// Speed and Distance
display.setCursor(5, 16);
display.setTextSize(2);
display.println("spd:" + (speed != "--.--" ? speed : "N/A"));
display.setCursor(5, 36);
display.println("dis:" + (distance != "--.--" ? distance : "N/A"));
display.setTextSize(1);
// Divider line
drawLine(0, 52, SCREEN_WIDTH, 36);
// Broadcasting message with scrolling
int messageWidth = message.length() * 6; // Approx. width of the message
display.setCursor(-scrollOffset, 55); // Scroll from left to right
display.print(message + " "); // Append a space for separation
display.print(message); // Repeat the message to create a loop
scrollOffset++;
if (scrollOffset > messageWidth) scrollOffset = 0; // Reset scroll when end of message is reached
drawLine(0, 63, SCREEN_WIDTH, 36);
}
// Draw Circle with toggle between hollow/filled
void drawCircleWithToggle(int x, int y, bool isFilled) {
if (isFilled) {
display.fillCircle(x, y, 4, SSD1306_WHITE); // Filled circle
} else {
display.drawCircle(x, y, 4, SSD1306_WHITE); // Hollow circle
}
}
// Draw Stopwatch and Elapsed Time combined UI
void drawStopwatchAndElapsedUI(String time, String carID, int batteryPercentage, String stopwatchTime, String elapsedTime, String message) {
// Top section
display.setTextSize(1);
display.setCursor(0, 0);
display.print(time);
// Car ID with circle (toggle between filled and hollow)
display.setCursor(SCREEN_WIDTH / 2 - 10, 0);
display.print(carID);
drawCircleWithToggle(SCREEN_WIDTH / 2 + 18, 4, blinkState);
drawBatteryStatus(batteryPercentage);
// Divider line
drawLine(0, 12, SCREEN_WIDTH, 12);
// Stopwatch and Elapsed Time
display.setCursor(0, 16);
display.println("SW: " + (stopwatchTime != "00:00:00" ? stopwatchTime : "N/A"));
display.setCursor(0, 26);
display.println("ET: " + (elapsedTime != "00:00:00" ? elapsedTime : "N/A"));
// Divider line
drawLine(0, 35, SCREEN_WIDTH, 36);
// Broadcasting message with scrolling
display.setCursor(-scrollOffset, 40); // Scrolling from left to right
display.println(message);
scrollOffset++;
if (scrollOffset > SCREEN_WIDTH) scrollOffset = 0; // Reset scroll
}
// Draw start/end time record page
void drawStartEndTimePage(String time, String carID, int batteryPercentage, String startTime, String endTime) {
// Top section
display.setTextSize(1);
display.setCursor(0, 0);
display.print(time);
// Car ID with circle (toggle between filled and hollow)
display.setCursor(SCREEN_WIDTH / 2 - 10, 0);
display.print(carID);
drawCircleWithToggle(SCREEN_WIDTH / 2 + 18, 4, blinkState);
drawBatteryStatus(batteryPercentage);
// Divider line
drawLine(0, 12, SCREEN_WIDTH, 12);
// Start and End times
display.setCursor(0, 16);
display.println("st: " + (startTime != "00:00:00" ? startTime : "N/A"));
display.setCursor(0, 26);
display.println("en: " + (endTime != "00:00:00" ? endTime : "N/A"));
}
// Draw overall results page
void drawOverallResultsPage(String time, String carID, int batteryPercentage, String avgSpeed, String totalTime, String penalties) {
// Top section
display.setTextSize(1);
display.setCursor(0, 0);
display.print(time);
// Car ID with circle (toggle between filled and hollow)
display.setCursor(SCREEN_WIDTH / 2 - 10, 0);
display.print(carID);
drawCircleWithToggle(SCREEN_WIDTH / 2 + 18, 4, blinkState);
drawBatteryStatus(batteryPercentage);
// Divider line
drawLine(0, 12, SCREEN_WIDTH, 12);
// Average speed, total time, and penalties
display.setCursor(0, 16);
display.println("avg spd: " + (avgSpeed != "--.--" ? avgSpeed : "N/A") + " km/h");
display.setCursor(0, 26);
display.println("total time: " + (totalTime != "00:00:00" ? totalTime : "N/A"));
display.setCursor(0, 36);
display.println("penalties: " + (penalties != "--" ? penalties : "N/A"));
}
// Function to draw Battery Status in a box and blink "low" when below 10%
void drawBatteryStatus(int batteryPercentage) {
display.setCursor(SCREEN_WIDTH - 25, 3);
display.println(String(batteryPercentage) + "%");
if (batteryPercentage < 10 && blinkState) {
display.setCursor(SCREEN_WIDTH - 30, 10);
display.println("LOW");
}
}
// Function to draw a dashed line
void drawLine(int x0, int y0, int x1, int y1) {
for (int x = x0; x < x1; x += 1) {
display.drawPixel(x, y0, SSD1306_WHITE);
}
}