-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.cpp
More file actions
194 lines (168 loc) · 6.5 KB
/
tasks.cpp
File metadata and controls
194 lines (168 loc) · 6.5 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
#include "tasks.h"
#include "display_handler.h"
#include "secrets.h"
#include "calendar_handler.h"
#include "time_manager.h"
#include "animations.h"
#include "bin_type.h"
#include "bindicator.h"
#include <WiFi.h>
uint8_t Matrix_Data[8][8];
uint8_t RGB_Data1[64][3];
QueueHandle_t commandQueue;
const uint32_t CALENDAR_CHECK_INTERVAL_MS = 3600000; // 1 hour
extern CalendarHandler calendar;
void animationTask(void* parameter) {
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = pdMS_TO_TICKS(30);
Serial.println("Animation task started");
static bool isLoading = true;
static bool isSetupMode = false;
static bool isError = false;
static bool isBin = false;
static bool isComplete = false;
static Color color = Animations::DEFAULT_BLUE;
extern DisplayHandler display;
while(true) {
Command cmd = CMD_NONE;
if (xQueueReceive(commandQueue, &cmd, 0) == pdTRUE) {
Serial.printf("Animation received command: %d\n", cmd);
isLoading = false;
isSetupMode = false;
isError = false;
isBin = false;
isComplete = false;
switch(cmd) {
case CMD_SHOW_RECYCLING:
Serial.println("Switching to green (recycling)");
isBin = true;
color = Animations::RECYCLING_GREEN;
break;
case CMD_SHOW_RUBBISH:
Serial.println("Switching to brown (rubbish)");
isBin = true;
color = Animations::RUBBISH_BROWN;
break;
case CMD_SHOW_NEITHER:
Serial.println("Switching to blue (neither)");
color = Animations::DEFAULT_BLUE;
break;
case CMD_SHOW_COMPLETED:
Serial.println("Showing completed animation");
isComplete = true;
color = Animations::COMPLETE_GREEN;
break;
case CMD_SHOW_LOADING:
Serial.println("Showing loading animation");
isLoading = true;
break;
case CMD_SHOW_SETUP_MODE:
Serial.println("Showing setup mode");
isSetupMode = true;
break;
case CMD_SHOW_ERROR_API:
Serial.println("Showing API error");
isError = true;
Animations::drawError(display, ErrorType::API);
break;
case CMD_SHOW_ERROR_WIFI:
Serial.println("Showing WiFi error");
isError = true;
Animations::drawError(display, ErrorType::WIFI);
break;
case CMD_SHOW_ERROR_OTHER:
Serial.println("Showing other error");
isError = true;
Animations::drawError(display, ErrorType::OTHER);
break;
default:
Serial.printf("Unknown command: %d\n", cmd);
break;
}
}
display.matrix.clear();
if (isError) {
Animations::drawError(display, ErrorType::API);
} else if (isLoading) {
Animations::drawLoading(display);
} else if (isSetupMode) {
Animations::drawSetupMode(display);
} else if (isBin) {
Animations::drawBinImage(display, color);
} else if (isComplete) {
Animations::drawComplete(display, color);
} else {
Animations::drawPulse(display, color);
}
display.matrix.show();
vTaskDelayUntil(&xLastWakeTime, xFrequency);
}
}
void calendarTask(void* parameter) {
const TickType_t xDelay = pdMS_TO_TICKS(CALENDAR_CHECK_INTERVAL_MS);
const TickType_t WIFI_RETRY_DELAY = pdMS_TO_TICKS(10000);
const int TIME_SYNC_MAX_RETRIES = 10;
const TickType_t TIME_SYNC_RETRY_DELAY = pdMS_TO_TICKS(30000);
Command cmd = CMD_SHOW_LOADING;
xQueueSend(commandQueue, &cmd, 0);
// Initial delay to allow system to stabilize and load state
vTaskDelay(pdMS_TO_TICKS(5000));
int timeSyncAttempts = 0;
while (timeSyncAttempts < TIME_SYNC_MAX_RETRIES) {
if (setupTime()) {
Serial.println("Time synced successfully");
break;
}
timeSyncAttempts++;
if (timeSyncAttempts < TIME_SYNC_MAX_RETRIES) {
Serial.printf("Time sync attempt %d failed, retrying in 30 seconds...\n", timeSyncAttempts);
vTaskDelay(TIME_SYNC_RETRY_DELAY);
} else {
Serial.println("Failed to sync time after maximum retries");
Bindicator::setErrorState(ErrorType::API);
vTaskDelay(xDelay);
continue;
}
}
while (true) {
bool wasInErrorState = Bindicator::isInErrorState();
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected, attempting to reconnect...");
WiFi.reconnect();
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 5) {
vTaskDelay(WIFI_RETRY_DELAY);
attempts++;
}
}
if (!Bindicator::shouldCheckCalendar()) {
Serial.println("Skipping calendar check - bin already taken out or wrong time");
vTaskDelay(xDelay);
continue;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Checking calendar...");
bool hasRecycling = false;
bool hasRubbish = false;
if (calendar.checkForBinEvents(hasRecycling, hasRubbish)) {
if (wasInErrorState) {
Bindicator::clearErrorState();
}
CollectionState state = CollectionState::NO_COLLECTION;
if (hasRecycling) {
state = CollectionState::RECYCLING_DUE;
} else if (hasRubbish) {
state = CollectionState::RUBBISH_DUE;
}
Bindicator::updateFromCalendar(state);
} else {
Serial.println("Failed to check calendar");
Bindicator::setErrorState(ErrorType::API);
}
} else {
Serial.println("WiFi not connected, setting error state");
Bindicator::setErrorState(ErrorType::WIFI);
}
vTaskDelay(xDelay);
}
}