-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
209 lines (182 loc) · 8.53 KB
/
Copy pathmain.cpp
File metadata and controls
209 lines (182 loc) · 8.53 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
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
/*
* Copyright (C) 2026 by scriptwriter13
*
* Dieses Programm ist freie Software: Sie können es unter den Bedingungen der
* GNU General Public License, wie von der Free Software Foundation veröffentlicht,
* entweder Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren
* Version, weiterverbreiten und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, sogar ohne die implizite Gewährleistung der
* MARKTGÄNGIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK. Siehe die
* GNU General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
// FILE: src/main.cpp
// STATUS: FINAL STABLE - POWER-TIMER FIX + GESTURE DEBOUNCE + LIGHT SLEEP + WAKEUP FIX
// DATE: 2026-03-10
// CHANGE: Entfernung von esp_sleep_enable_bt_wakeup() für C3/S3 Kompatibilität
// CHANGE: Casual Greeting angepasst
// STATUS: Lizenz-Header geprüft und bestätigt.
#include <Arduino.h>
#include <Arduino_GFX_Library.h>
#include <CST816S.h>
#include <Wire.h>
#include <Preferences.h>
#include <esp_sleep.h>
#include "config.h"
#include "layout_config.h"
#include "logic.h"
#include "graphics.h"
#include "ble_handler.h"
// --- GLOBALE VARIABLEN ---
String displayText = "warte auf App", macAddr = "", streetNumber = "";
String pointNumber = "";
String nextStreet = "";
String metaDist = "", metaTime = "", metaEta = "", metaSpd = "";
float currentDist = 0, startDist = 0, nextDist = 0, angleExtraDist = 0;
float distSinceAction = 0;
float currentHeading = 0;
float lastKmh = 0;
unsigned long lastSpdTime = 0, arrivalTime = 0, powerTimer = 0;
unsigned long lastPktTime = 0, lastCalcTime = 0, lastPreviewUpdateTime = 0;
unsigned long lastGestureTime = 0; // Debounce Timer für Gesten
bool previewPendingClear = false, isConnected = false, displayOn = true, isRerouting = false;
bool actionActive = false, isReverse = false, justWokeUp = false;
int navIcon = 0, turnMod = 0, nextNavIcon = 0, nextTurnMod = 0;
int brightness = 200, displayRot = 0, spdWaitCount = 0;
Preferences prefs;
// Hardware Objekte
Arduino_DataBus *bus = new Arduino_ESP32SPI(DC_PIN, CS_PIN, SCK_PIN, MOSI_PIN, MISO_PIN);
Arduino_GFX *gfx = new Arduino_GC9A01(bus, RST_PIN, 0, true);
Arduino_Canvas *canvas = new Arduino_Canvas(240, 240, gfx);
CST816S touch(TOUCH_SDA, TOUCH_SCL, TOUCH_RST, TOUCH_INT);
// --- SYSTEM FUNKTIONEN ---
void logLogicStatus() {
Serial.printf(">>> [REPORT]: Icon:%d | Dist:%.0fm | Pt:%s | Text:[%s]\n",
navIcon, currentDist, pointNumber.c_str(), displayText.c_str());
}
void wakeup() {
if (!displayOn) {
displayOn = true;
justWokeUp = true;
gfx->displayOn();
pinMode(TFT_BL, OUTPUT);
analogWrite(TFT_BL, brightness);
Serial.println(">>> [SYSTEM]: Wakeup (Display ON)");
}
powerTimer = millis(); // Timer immer zurücksetzen, wenn berührt
}
void setup() {
Serial.begin(115200);
prefs.begin("bikenav", false);
displayRot = prefs.getInt("rot", 0);
brightness = prefs.getInt("bright", 200);
// Hardware Init
pinMode(RST_PIN, OUTPUT); digitalWrite(RST_PIN, LOW); delay(200); digitalWrite(RST_PIN, HIGH);
gfx->begin(); gfx->setRotation(displayRot); canvas->begin();
// Initiales Backlight Setup
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
analogWrite(TFT_BL, brightness);
Wire.begin(TOUCH_SDA, TOUCH_SCL); touch.begin();
// Wakeup Konfiguration für Light Sleep (Kompatibel mit C3/S3)
esp_sleep_enable_gpio_wakeup();
gpio_wakeup_enable((gpio_num_t)TOUCH_INT, GPIO_INTR_LOW_LEVEL);
// BLE Setup
setupBLE("BikeNav_C3");
macAddr = getBLEAddress();
Serial.println("\n--- Hey, BikeNav_C3 ist bereit! ---");
Serial.print(">>> DEVICE MAC: "); Serial.println(macAddr);
powerTimer = millis();
lastCalcTime = millis();
}
void loop() {
unsigned long now = millis();
// 1. Touch-Handler mit Debouncing
if (touch.available()) {
// Geste auslesen
String g = touch.gesture();
// Wenn Display aus, nur aufwecken
if (!displayOn) {
wakeup();
}
// Wenn Display an, nur auf Gesten reagieren (nicht auf einfache Taps)
else if (!justWokeUp && (now - lastGestureTime > 500)) {
// Timer zurücksetzen bei Interaktion
powerTimer = now;
if (g.indexOf("LEFT") >= 0) {
displayRot = (displayRot + 1) % 4;
gfx->setRotation(displayRot);
prefs.putInt("rot", displayRot);
lastGestureTime = now;
}
else if (g.indexOf("RIGHT") >= 0) {
displayRot = (displayRot > 0) ? displayRot - 1 : 3;
gfx->setRotation(displayRot);
prefs.putInt("rot", displayRot);
lastGestureTime = now;
}
else if (g.indexOf("UP") >= 0) {
brightness = min(255, brightness + 30);
analogWrite(TFT_BL, brightness);
prefs.putInt("bright", brightness);
lastGestureTime = now;
}
else if (g.indexOf("DOWN") >= 0) {
brightness = max(10, brightness - 30);
analogWrite(TFT_BL, brightness);
prefs.putInt("bright", brightness);
lastGestureTime = now;
}
}
}
// 2. Power-Save Logik (Stabilisiert)
if (displayOn) {
// Sicherung: Falls gerade aufgewacht, Timer synchronisieren
if (justWokeUp) {
powerTimer = now;
justWokeUp = false;
}
// Timeout Check (90 Sekunden)
if (now - powerTimer > 90000) {
displayOn = false;
analogWrite(TFT_BL, 0);
digitalWrite(TFT_BL, LOW);
gfx->displayOff();
Serial.println(">>> [SYSTEM]: Standby nach Timeout");
}
}
// Wenn Display aus, CPU in Light Sleep versetzen
if (!displayOn) {
esp_light_sleep_start();
return;
}
// 3. Preview-Auto-Clear
if (previewPendingClear && (now - lastPreviewUpdateTime > 5000)) {
nextNavIcon = 0; nextTurnMod = 0; nextDist = 0; nextStreet = ""; previewPendingClear = false;
}
// 4. SPD Deduction (Golden Rule 2)
if (isConnected && lastKmh > 0.5f && (now - lastCalcTime >= 2000)) {
float dDelta = (lastKmh / 3.6f) * ((float)(now - lastCalcTime) / 1000.0f);
if (currentDist > 0) {
currentDist = max(0.0f, currentDist - dDelta);
if (currentDist <= 0 && arrivalTime == 0) arrivalTime = now;
}
lastCalcTime = now;
}
// 5. Rendering
canvas->fillScreen(0);
if (displayText == "warte auf App" && !isRerouting) {
renderStartScreen(canvas, isConnected, macAddr);
} else {
renderNavScreen(canvas, navIcon, turnMod, currentDist, startDist, angleExtraDist,
displayText, streetNumber, isConnected, isRerouting, arrivalTime,
currentHeading, isReverse, lastPktTime, nextNavIcon, nextTurnMod);
}
canvas->flush();
yield();
delay(35);
}