-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUIController.cpp
More file actions
277 lines (252 loc) · 10.9 KB
/
Copy pathUIController.cpp
File metadata and controls
277 lines (252 loc) · 10.9 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
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
#include "UIController.h"
UIController::UIController(CoffeeMachineController &coffeeCtrl, LightningController &lightCtrl)
: coffeeController(coffeeCtrl), lightningController(lightCtrl) {}
void UIController::begin()
{
lightningController.setOnInvoicePaid(std::bind(&UIController::onInvoicePaid, this, std::placeholders::_1));
// Initialize SmartDisplay
smartdisplay_init();
// Get Default Display
__attribute__((unused)) auto disp = lv_disp_get_default();
// Set Rotation as needed
#if DISPLAY_HEIGHT > DISPLAY_WIDTH
lv_disp_set_rotation(disp, LV_DISP_ROT_90);
#endif
// Initialiez UI
ui_init();
lv_timer_handler();
vTaskDelay(500 / portTICK_PERIOD_MS);
#ifdef CONFIG_FREERTOS_UNICORE
BaseType_t app = 0;
#else
BaseType_t app = 1;
#endif
xTaskCreatePinnedToCore(task, "UIController", 10000, this, 1, NULL, app);
}
void UIController::task(void *pvParameters)
{
static_cast<UIController *>(pvParameters)->run();
}
void UIController::run()
{
log_i("UI Controller started");
static bool isBrewingBarAnimated = false;
static lv_timer_t *progressTimer = NULL;
static uint32_t progressStartTime = 0;
static const uint32_t PROGRESS_DURATION = 5000;
while (true)
{
if (lightningController.getIsConnected())
{
// Handle UI state
CoffeeMachineError currentError = coffeeController.getCurrentError();
switch (coffeeController.getCurrentState())
{
case CoffeeMachineState::Off:
coffeeController.sendOnCommand();
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_warning_png);
lv_label_set_text(ui_homeErrorTitle, "Ligando Cafeteira...");
lv_label_set_text(ui_homeErrorSubTitle, "Aguarde...\n");
break;
case CoffeeMachineState::WaitingForOn:
case CoffeeMachineState::TurningOn:
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_trash_png);
lv_label_set_text(ui_homeErrorTitle, "Ligando Cafeteira...");
lv_label_set_text(ui_homeErrorSubTitle, "Aguarde uns instantes.\n");
break;
case CoffeeMachineState::Loading:
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_trash_png);
lv_label_set_text(ui_homeErrorTitle, "Maquina aquecendo...");
lv_label_set_text(ui_homeErrorSubTitle, "Aguarde uns instantes.\n");
break;
case CoffeeMachineState::Ready:
if (isBrewingBarAnimated)
{
isBrewingBarAnimated = false;
}
lv_obj_clear_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
break;
case CoffeeMachineState::Error:
lv_obj_add_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
switch (currentError)
{
case CoffeeMachineError::NOWATER:
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_water_png);
lv_label_set_text(ui_homeErrorTitle, "Ops! O reservatório está vazio");
lv_label_set_text(ui_homeErrorSubTitle, "Por favor, adicione água para que possamos preparar o seu café");
break;
case CoffeeMachineError::TRASH:
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_trash_png);
lv_label_set_text(ui_homeErrorTitle, "Hora de uma limpeza!");
lv_label_set_text(ui_homeErrorSubTitle, "A borra de café precisa ser esvaziada para preparar seu pedido.");
break;
case CoffeeMachineError::GENERAL:
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_trash_png);
lv_label_set_text(ui_homeErrorTitle, "Ops! Estamos com problemas!");
lv_label_set_text(ui_homeErrorSubTitle, "Verifique se a bandeja está cheia ou se o café está no final.");
break;
}
break;
case CoffeeMachineState::Brewing:
if (!isBrewingBarAnimated)
{
isBrewingBarAnimated = true;
progressStartTime = lv_tick_get();
if (!progressTimer)
{
progressTimer = lv_timer_create([](_lv_timer_t *)
{
if (!isBrewingBarAnimated) {
_ui_screen_change(&ui_success, LV_SCR_LOAD_ANIM_MOVE_LEFT, 500, 0, &ui_success_screen_init);
lv_bar_set_value(ui_preparingProgress, 0, LV_ANIM_OFF);
lv_timer_del(progressTimer);
progressTimer = NULL;
return;
}
uint32_t elapsed = lv_tick_get() - progressStartTime;
if (elapsed >= PROGRESS_DURATION) {
// Reset
lv_bar_set_value(ui_preparingProgress, 0, LV_ANIM_OFF);
progressStartTime = lv_tick_get();
} else {
// Update progress
int32_t progress = (elapsed * 100) / PROGRESS_DURATION;
lv_bar_set_value(ui_preparingProgress, progress, LV_ANIM_OFF);
} },
50, NULL);
}
}
break;
default:
lv_obj_clear_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(ui_homeErrorSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
break;
}
}
else if (isInitialized)
{
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_wifi_png);
lv_label_set_text(ui_homeErrorTitle, "Conectando ao WiFi e LNBits...");
lv_label_set_text(ui_homeErrorSubTitle, "Aguarde...\n");
}
else
{
lv_obj_clear_flag(ui_homeErrorPanel, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeButton, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(ui_homeSubTitle, LV_OBJ_FLAG_HIDDEN);
lv_img_set_src(ui_homeErrorIcon, &ui_img_wifi_png);
lv_label_set_text(ui_homeErrorTitle, "Já configurou sua máquina café?");
lv_label_set_text(ui_homeErrorSubTitle, "Por favor, aguarde alguns segundos ou conecte-se ao AP para ajustar as configurações iniciais.");
}
delayLvgl = lv_timer_handler();
vTaskDelay(delayLvgl + 1 / portTICK_PERIOD_MS);
}
}
void UIController::setWaitingPaymentFor(uint8_t type)
{
if (type != 1 && type != 2)
{
log_e("Type %d is unknown", type);
return;
}
waitingPaymentFor = type;
}
void UIController::onInvoicePaid(uint8_t type)
{
if (waitingPaymentFor == type)
{
waitingPaymentFor = 0;
if (type == 1)
{
lv_img_set_src(ui_preparingImage, &ui_img_espresso_png);
lv_label_set_text(ui_homeErrorTitle, "Minerando seu\nEspresso To The Moon");
}
else
{
lv_img_set_src(ui_preparingImage, &ui_img_coffee_png);
lv_label_set_text(ui_homeErrorTitle, "Minerando seu\nBlock Coffee");
}
_ui_screen_change(&ui_preparing, LV_SCR_LOAD_ANIM_MOVE_LEFT, 500, 0, &ui_preparing_screen_init);
clearQrCode();
coffeeController.startOrder();
}
else
{
log_i("Invoice paid for: %d", type);
return;
}
}
void UIController::showQrCode(uint8_t buttonNumber, lv_coord_t size)
{
// Create and update QR code
log_d("Generating QRCode for button %d. %s", buttonNumber, buttonNumber == 1 ? lightningController.getButton1Lnurl() : lightningController.getButton2Lnurl());
auto ui_qrcode = lv_qrcode_create(ui_qrCodeContainer, size ? size : QRCODE_INITIAL_SIZE, lv_color_black(), lv_color_white());
String qrData = buttonNumber == 1 ? lightningController.getButton1Lnurl() : lightningController.getButton2Lnurl();
lv_qrcode_update(ui_qrcode, qrData.c_str(), qrData.length());
lv_obj_center(ui_qrcode);
}
void UIController::qrCodeTimerCallback(lv_timer_t *timer)
{
// Retrieve the class instance from user data
UIController *instance = static_cast<UIController *>(timer->user_data);
// Access member variables and methods
instance->qrCodeSize = lv_obj_get_content_width(ui_qrCodeContainer) - 5;
if (instance->qrCodeSize > QRCODE_INITIAL_SIZE)
{
instance->showQrCode(instance->timerButtonNumber, instance->qrCodeSize);
lv_timer_del(timer);
}
else
{
if (millis() - instance->startTime >= 2000) // 2-second timeout
{
lv_timer_del(timer);
instance->qrCodeSize = QRCODE_INITIAL_SIZE;
}
}
}
void UIController::generateQrCode(uint8_t buttonNumber)
{
if (qrCodeSize <= QRCODE_INITIAL_SIZE)
{
// Allocate and initialize timer data
startTime = millis();
timerButtonNumber = buttonNumber;
// Create a one-shot timer that will generate the QR code after the screen is loaded
// Create timer that will generate the QR code after the screen is loaded
__attribute__((unused)) lv_timer_t *timer = lv_timer_create(qrCodeTimerCallback, 100, this); // 100ms interval
}
else
{
showQrCode(buttonNumber, qrCodeSize);
}
}
void UIController::clearQrCode()
{
lv_timer_create([](lv_timer_t *timer)
{
lv_obj_clean(ui_qrCodeContainer);
lv_timer_del(timer); // Delete the timer after use
},
500, nullptr); // 500ms delay
}