-
Notifications
You must be signed in to change notification settings - Fork 1
/
aiq_sensors_app.c
524 lines (446 loc) · 16 KB
/
aiq_sensors_app.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <furi.h>
#include <furi_hal.h>
#include <gui/icon_i.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/menu.h>
#include <gui/modules/popup.h>
/* generated by fbt from .png files in images folder */
#include <aiq_sensors_app_icons.h>
// Drivers
#include "sensors/scd4x/scd4x_i2c.h"
#include "sensors/scd4x/sensirion_i2c_hal.h"
#include "sensors/ens160/ens160_i2c.h"
typedef enum {
AiqSensorsAppScene_MainMenu,
AiqSensorsAppScene_SensorData,
AiqSensorsAppScene_count
} AiqSensorsAppScene;
typedef enum {
AiqSensorsAppView_Menu,
AiqSensorsAppView_SCD4x,
AiqSensorsAppView_ENS160
} AiqSensorsAppView;
typedef struct {
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
Menu* menu;
View* view;
FuriTimer* timer;
FuriMutex* mutex;
} AiqSensorsApp;
typedef enum {
AiqSensorsAppEvent_ShowSCD4xView,
AiqSensorsAppEvent_UpdateSCD4xView,
AiqSensorsAppEvent_ShowENS160View,
AiqSensorsAppEvent_UpdateENS160View
} AiqSensorsAppEvent;
typedef enum {
AiqSensorsAppMenuSelection_SCD4x,
AiqSensorsAppMenuSelection_ENS160,
} AiqSensorsAppMenuSelection;
typedef struct {
uint16_t co2;
int32_t temp;
int32_t hum;
uint8_t aiq_index;
uint16_t eco2;
uint16_t tvoc;
uint8_t error;
AiqSensorsAppView selected_view;
} AiqSensorsAppModel;
void aiq_sensors_app_main_menu_callback(void* context, uint32_t index) {
AiqSensorsApp* app = context;
switch(index) {
case AiqSensorsAppMenuSelection_SCD4x:
scene_manager_handle_custom_event(app->scene_manager, AiqSensorsAppEvent_ShowSCD4xView);
break;
case AiqSensorsAppMenuSelection_ENS160:
scene_manager_handle_custom_event(app->scene_manager, AiqSensorsAppEvent_ShowENS160View);
break;
default:
break;
}
}
void aiq_sensors_app_enter_main_menu_scene(void* context) {
AiqSensorsApp* app = context;
menu_reset(app->menu);
menu_add_item(
app->menu,
"Read SCD4x",
NULL,
AiqSensorsAppMenuSelection_SCD4x,
aiq_sensors_app_main_menu_callback,
app);
menu_add_item(
app->menu,
"Read ENS160",
NULL,
AiqSensorsAppMenuSelection_ENS160,
aiq_sensors_app_main_menu_callback,
app);
view_dispatcher_switch_to_view(app->view_dispatcher, AiqSensorsAppView_Menu);
}
void aiq_sensors_app_scd4x_view(Canvas* canvas, AiqSensorsAppModel* model) {
FuriString* xstr = furi_string_alloc();
canvas_draw_str(canvas, 3, 13, "SCD4x");
canvas_draw_icon(canvas, 3, 19, &I_weather_temperature_16x16);
canvas_draw_icon(canvas, 2, 42, &I_weather_humidity_11x16);
canvas_draw_icon(canvas, 58, 20, &I_plant_16x16);
furi_string_printf(xstr, "%.f C", (double)model->temp / 1000);
canvas_draw_str(canvas, 22, 31, furi_string_get_cstr(xstr));
furi_string_printf(xstr, "%.2f%%", (double)model->hum / 1000);
canvas_draw_str(canvas, 22, 57, furi_string_get_cstr(xstr));
furi_string_printf(xstr, "%u ppm", model->co2);
canvas_draw_str(canvas, 76, 33, furi_string_get_cstr(xstr));
furi_string_printf(xstr, "E: %d", model->error);
canvas_draw_str(canvas, 64, 56, furi_string_get_cstr(xstr));
}
void aiq_sensors_app_ens160_view(Canvas* canvas, AiqSensorsAppModel* model) {
FuriString* xstr = furi_string_alloc();
canvas_draw_str(canvas, 3, 13, "ENS160");
canvas_draw_icon(canvas, 3, 19, &I_star_15x16);
canvas_draw_icon(canvas, 2, 42, &I_weather_wind_15x16);
canvas_draw_icon(canvas, 58, 20, &I_plant_16x16);
furi_string_printf(xstr, "aiq: %d", model->aiq_index);
canvas_draw_str(canvas, 22, 31, furi_string_get_cstr(xstr));
furi_string_printf(xstr, "%u ppb", model->tvoc);
canvas_draw_str(canvas, 22, 57, furi_string_get_cstr(xstr));
furi_string_printf(xstr, "%u ppm", model->eco2);
canvas_draw_str(canvas, 76, 33, furi_string_get_cstr(xstr));
furi_string_printf(xstr, "E: %d", model->error);
canvas_draw_str(canvas, 64, 56, furi_string_get_cstr(xstr));
}
void aiq_sensors_app_view_draw_callback(Canvas* canvas, void* model) {
AiqSensorsAppModel* current_model = model;
canvas_set_bitmap_mode(canvas, true);
canvas_set_font(canvas, FontPrimary);
switch(current_model->selected_view) {
case AiqSensorsAppView_SCD4x:
aiq_sensors_app_scd4x_view(canvas, model);
break;
case AiqSensorsAppView_ENS160:
aiq_sensors_app_ens160_view(canvas, model);
break;
default:
break;
}
}
bool aiq_sensors_app_on_main_menu_scene_event(void* context, SceneManagerEvent event) {
AiqSensorsApp* app = context;
bool consumed = false;
switch(event.type) {
case SceneManagerEventTypeCustom:
switch(event.event) {
case AiqSensorsAppEvent_ShowSCD4xView:
with_view_model(
app->view,
AiqSensorsAppModel * model,
{ model->selected_view = AiqSensorsAppView_SCD4x; },
false);
scene_manager_next_scene(app->scene_manager, AiqSensorsAppScene_SensorData);
consumed = true;
break;
case AiqSensorsAppEvent_ShowENS160View:
with_view_model(
app->view,
AiqSensorsAppModel * model,
{ model->selected_view = AiqSensorsAppView_ENS160; },
false);
consumed = true;
scene_manager_next_scene(app->scene_manager, AiqSensorsAppScene_SensorData);
break;
default:
break;
}
break;
default:
consumed = false;
break;
}
return consumed;
}
void aiq_sensors_app_exit_main_menu_scene(void* context) {
AiqSensorsApp* app = context;
menu_reset(app->menu);
}
void aiq_sensors_app_scd4x_view_timer_callback(void* context) {
AiqSensorsApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, AiqSensorsAppEvent_UpdateSCD4xView);
}
void aiq_sensors_app_ens160_view_timer_callback(void* context) {
AiqSensorsApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, AiqSensorsAppEvent_UpdateENS160View);
}
void aiq_sensors_app_init_scd4x_view(AiqSensorsApp* app) {
furi_mutex_acquire(app->mutex, FuriWaitForever);
if(sensirion_i2c_hal_init()) {
uint8_t error = scd4x_wake_up();
error += scd4x_stop_periodic_measurement();
error += scd4x_reinit();
error += scd4x_start_periodic_measurement();
with_view_model(
app->view,
AiqSensorsAppModel * model,
{
model->error = error;
model->co2 = 0;
model->temp = 0;
model->hum = 0;
},
false);
} else {
with_view_model(
app->view,
AiqSensorsAppModel * model,
{
model->error = -4;
model->co2 = 0;
model->temp = 0;
model->hum = 0;
},
false);
}
sensirion_i2c_hal_free();
furi_mutex_release(app->mutex);
}
void aiq_sensors_app_init_ens160_view(AiqSensorsApp* app) {
furi_mutex_acquire(app->mutex, FuriWaitForever);
if(ens160_i2c_hal_init()) {
uint8_t error = ens160_init();
with_view_model(
app->view,
AiqSensorsAppModel * model,
{
model->error = error;
model->eco2 = 0;
model->tvoc = 0;
model->aiq_index = 0;
},
false);
} else {
with_view_model(
app->view,
AiqSensorsAppModel * model,
{
model->error = -4;
model->eco2 = 0;
model->tvoc = 0;
model->aiq_index = 0;
},
false);
}
ens160_i2c_hal_free();
furi_mutex_release(app->mutex);
}
void aiq_sensors_app_enter_sensor_data_scene(void* context) {
AiqSensorsApp* app = context;
furi_assert(app->timer == NULL);
uint32_t period = furi_ms_to_ticks(10000);
AiqSensorsAppModel* model = view_get_model(app->view);
switch(model->selected_view) {
case AiqSensorsAppView_SCD4x:
app->timer = furi_timer_alloc(
aiq_sensors_app_scd4x_view_timer_callback, FuriTimerTypePeriodic, context);
aiq_sensors_app_init_scd4x_view(app);
furi_timer_start(app->timer, period);
view_dispatcher_switch_to_view(app->view_dispatcher, AiqSensorsAppView_SCD4x);
break;
case AiqSensorsAppView_ENS160:
app->timer = furi_timer_alloc(
aiq_sensors_app_ens160_view_timer_callback, FuriTimerTypePeriodic, context);
aiq_sensors_app_init_ens160_view(app);
furi_timer_start(app->timer, period);
view_dispatcher_switch_to_view(app->view_dispatcher, AiqSensorsAppView_ENS160);
break;
default:
break;
}
}
void aiq_sensors_app_update_scd4x_view(AiqSensorsApp* app) {
uint16_t co2 = 0;
int32_t temp = 0;
int32_t hum = 0;
uint8_t error = 0;
furi_mutex_acquire(app->mutex, FuriWaitForever);
sensirion_i2c_hal_init();
error = scd4x_read_measurement(&co2, &temp, &hum);
bool redraw = true;
with_view_model(
app->view,
AiqSensorsAppModel * model,
{
model->co2 = co2;
model->temp = temp;
model->hum = hum;
model->error = error;
},
redraw);
sensirion_i2c_hal_free();
furi_mutex_release(app->mutex);
}
void aiq_sensors_app_update_ens160_view(AiqSensorsApp* app) {
uint16_t eco2 = 0;
uint16_t tvoc = 0;
uint8_t aiq_index = 0;
uint8_t error = 0;
furi_mutex_acquire(app->mutex, FuriWaitForever);
ens160_i2c_hal_init();
error = ens160_read_measurement(&aiq_index, &eco2, &tvoc);
bool redraw = true;
with_view_model(
app->view,
AiqSensorsAppModel * model,
{
model->eco2 = eco2;
model->tvoc = tvoc;
model->aiq_index = aiq_index;
model->error = error;
},
redraw);
ens160_i2c_hal_free();
furi_mutex_release(app->mutex);
}
bool aiq_sensors_app_on_sensor_data_scene_event(void* context, SceneManagerEvent event) {
AiqSensorsApp* app = context;
switch(event.type) {
case SceneManagerEventTypeCustom:
switch(event.event) {
case AiqSensorsAppEvent_UpdateSCD4xView: {
aiq_sensors_app_update_scd4x_view(app);
return true;
} break;
case AiqSensorsAppEvent_UpdateENS160View: {
aiq_sensors_app_update_ens160_view(app);
return true;
} break;
default:
break;
}
break;
default:
break;
}
return false;
}
void aiq_sensors_app_exit_scd4x_view(AiqSensorsApp* app) {
furi_mutex_acquire(app->mutex, FuriWaitForever);
furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
scd4x_stop_periodic_measurement();
scd4x_power_down();
furi_hal_i2c_release(&furi_hal_i2c_handle_external);
furi_mutex_release(app->mutex);
}
void aiq_sensors_app_exit_sensor_data_scene(void* context) {
AiqSensorsApp* app = context;
furi_timer_stop(app->timer);
furi_timer_free(app->timer);
app->timer = NULL;
AiqSensorsAppModel* model = view_get_model(app->view);
switch(model->selected_view) {
case AiqSensorsAppView_SCD4x:
aiq_sensors_app_exit_scd4x_view(app);
break;
case AiqSensorsAppView_ENS160:
break;
case AiqSensorsAppView_Menu:
break;
}
}
/** collection of all scene on_enter handlers - in the same order as their enum */
void (*const aiq_sensors_app_scene_on_enter_handlers[])(void*) = {
aiq_sensors_app_enter_main_menu_scene,
aiq_sensors_app_enter_sensor_data_scene,
};
/** collection of all scene on event handlers - in the same order as their enum */
bool (*const aiq_sensors_app_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
aiq_sensors_app_on_main_menu_scene_event,
aiq_sensors_app_on_sensor_data_scene_event,
};
/** collection of all scene on exit handlers - in the same order as their enum */
void (*const aiq_sensors_app_scene_on_exit_handlers[])(void*) = {
aiq_sensors_app_exit_main_menu_scene,
aiq_sensors_app_exit_sensor_data_scene,
};
/** collection of all on_enter, on_event, on_exit handlers */
const SceneManagerHandlers aiq_sensors_app_scene_event_handlers = {
.on_enter_handlers = aiq_sensors_app_scene_on_enter_handlers,
.on_event_handlers = aiq_sensors_app_scene_on_event_handlers,
.on_exit_handlers = aiq_sensors_app_scene_on_exit_handlers,
.scene_num = AiqSensorsAppScene_count};
/** custom event handler - passes the event to the scene manager */
bool aiq_sensors_app_scene_manager_custom_event_callback(void* context, uint32_t custom_event) {
furi_assert(context);
AiqSensorsApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, custom_event);
}
/** navigation event handler - passes the event to the scene manager */
bool aiq_sensors_app_scene_manager_navigation_event_callback(void* context) {
furi_assert(context);
AiqSensorsApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
void aiq_sensors_app_scene_manager_init(AiqSensorsApp* app) {
app->scene_manager = scene_manager_alloc(&aiq_sensors_app_scene_event_handlers, app);
}
void aiq_sensors_app_view_dispatcher_init(AiqSensorsApp* app) {
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
// allocate each view
app->menu = menu_alloc();
app->view = view_alloc();
view_set_draw_callback(app->view, aiq_sensors_app_view_draw_callback);
view_allocate_model(app->view, ViewModelTypeLockFree, sizeof(AiqSensorsAppModel));
AiqSensorsAppModel* model = view_get_model(app->view);
model->co2 = 0;
model->temp = 0;
model->hum = 0;
model->eco2 = 0;
model->tvoc = 0;
model->aiq_index = 0;
model->error = 0;
model->selected_view = AiqSensorsAppView_Menu;
// assign callback that pass events from views to the scene manager
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, aiq_sensors_app_scene_manager_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, aiq_sensors_app_scene_manager_navigation_event_callback);
// add views to the dispatcher, indexed by their enum value
view_dispatcher_add_view(
app->view_dispatcher, AiqSensorsAppView_Menu, menu_get_view(app->menu));
view_dispatcher_add_view(app->view_dispatcher, AiqSensorsAppView_SCD4x, app->view);
view_dispatcher_add_view(app->view_dispatcher, AiqSensorsAppView_ENS160, app->view);
}
AiqSensorsApp* aiq_sensors_app_init() {
AiqSensorsApp* app = malloc(sizeof(AiqSensorsApp));
app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
aiq_sensors_app_scene_manager_init(app);
aiq_sensors_app_view_dispatcher_init(app);
return app;
}
void aiq_sensors_app_free(AiqSensorsApp* app) {
scene_manager_free(app->scene_manager);
view_dispatcher_remove_view(app->view_dispatcher, AiqSensorsAppView_Menu);
view_dispatcher_remove_view(app->view_dispatcher, AiqSensorsAppView_SCD4x);
view_dispatcher_remove_view(app->view_dispatcher, AiqSensorsAppView_ENS160);
view_dispatcher_free(app->view_dispatcher);
menu_free(app->menu);
view_free(app->view);
furi_mutex_free(app->mutex);
free(app);
}
int32_t aiq_sensors_app_main(void* p) {
UNUSED(p);
AiqSensorsApp* app = aiq_sensors_app_init();
// set the scene and launch the main loop
Gui* gui = furi_record_open(RECORD_GUI);
view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
scene_manager_next_scene(app->scene_manager, AiqSensorsAppScene_MainMenu);
view_dispatcher_run(app->view_dispatcher);
// free all memory
furi_record_close(RECORD_GUI);
aiq_sensors_app_free(app);
return 0;
}