-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairmon.c
343 lines (285 loc) · 10 KB
/
airmon.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
#include <gui/gui.h>
#include <gui/view_port.h>
#include <power/power_service/power.h>
#include <notification/notification_messages.h>
#include <core/thread.h>
#include <core/kernel.h>
#include "airmon_icons.h"
#include "airmon_pms.h"
#include "airmon_aqi.h"
#define TAG "Airmon"
#define STR_BUF_SIZE 32
typedef enum {
AirmonDisplayModePm1_0 = 0,
AirmonDisplayModePm2_5,
AirmonDisplayModePm10,
AirmonDisplayModeAqi,
AirmonDisplayModeCount
} AirmonDisplayMode;
typedef struct {
Gui* gui;
ViewPort* view_port;
Power* power;
PowerInfo power_info;
FuriMessageQueue* event_queue;
NotificationApp* notifications;
AirmonDisplayMode display_mode;
uint32_t last_data_timestamp;
AirmonPmsContext* pms_context;
} AirmonContext;
typedef enum {
AirmonEventTypeKey,
} AirmonEventType;
typedef struct {
AirmonEventType type;
InputEvent input;
} AirmonEvent;
static const NotificationMessage message_green_165 = {
.type = NotificationMessageTypeLedGreen,
.data.led.value = 165,
};
static const NotificationMessage message_red_128 = {
.type = NotificationMessageTypeLedRed,
.data.led.value = 128,
};
static const NotificationMessage message_blue_128 = {
.type = NotificationMessageTypeLedBlue,
.data.led.value = 128,
};
const NotificationSequence sequence_blink_orange_100 = {
&message_red_255,
&message_green_165,
&message_delay_100,
NULL,
};
const NotificationSequence sequence_blink_purple_100 = {
&message_red_128,
&message_blue_128,
&message_delay_100,
NULL,
};
const NotificationSequence sequence_blink_maroon_100 = {
&message_red_128,
&message_delay_100,
NULL,
};
static void airmon_blink(AirmonContext* ctx, int aqi_value) {
int aqi_level = airmon_aqi_level(aqi_value);
const NotificationSequence *sequence;
switch(aqi_level) {
case 0:
// Good - Green
sequence = &sequence_blink_green_100;
break;
case 1:
// Moderate - Yellow
sequence = &sequence_blink_yellow_100;
break;
case 2:
// Unhealthy for Sensitive Groups - Orange
sequence = &sequence_blink_orange_100;
break;
case 3:
// Unhealthy - Red
sequence = &sequence_blink_red_100;
break;
case 4:
// Very Unhealthy - Purple
sequence = &sequence_blink_purple_100;
break;
default:
// Hazardous - Maroon
sequence = &sequence_blink_maroon_100;
break;
}
notification_message_block(ctx->notifications, sequence);
}
static void airmon_draw_battery(Canvas* canvas, AirmonContext* ctx) {
// Sourced from https://github.com/flipperdevices/flipperzero-firmware/blob/09edf66a/applications/services/power/power_service/power.c#L9
if(!ctx->power_info.gauge_is_ok) {
return;
}
canvas_draw_icon(canvas, 100, 0, &I_Battery_26x8);
canvas_draw_box(canvas, 102, 2, (ctx->power_info.charge + 4) / 5, 4);
}
static void airmon_draw_callback(Canvas* canvas, void* ctx) {
static char buffer[STR_BUF_SIZE];
AirmonContext* context = ctx;
AirmonPmsContext* pms_context = context->pms_context;
const uint8_t canvas_h = canvas_height(canvas);
const uint8_t canvas_w = canvas_width(canvas);
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
uint16_t pm_value;
uint16_t aqi_value;
uint32_t data_timestamp;
// Obtain PM/AQI values
furi_mutex_acquire(pms_context->mutex, FuriWaitForever);
switch(context->display_mode) {
case AirmonDisplayModePm1_0:
pm_value = pms_context->pms_data.pm1_0at;
break;
case AirmonDisplayModePm2_5:
pm_value = pms_context->pms_data.pm2_5at;
break;
case AirmonDisplayModePm10:
pm_value = pms_context->pms_data.pm10at;
break;
default:
pm_value = 0;
break;
}
aqi_value = airmon_aqi(pms_context->pms_data.pm2_5at, pms_context->pms_data.pm10at);
data_timestamp = pms_context->pms_data_timestamp;
furi_mutex_release(pms_context->mutex);
bool data_is_valid = (furi_get_tick() - data_timestamp < furi_ms_to_ticks(5000));
// Draw PM/AQI value
canvas_set_font(canvas, FontBigNumbers);
if(data_is_valid) {
snprintf(buffer, STR_BUF_SIZE, "%d",
(context->display_mode == AirmonDisplayModeAqi) ? aqi_value : pm_value);
} else {
snprintf(buffer, STR_BUF_SIZE, "--");
}
canvas_draw_str_aligned(canvas, canvas_w / 2, canvas_h / 2, AlignCenter, AlignBottom, buffer);
// Draw PM/AQI value legend (density units of measurement / air quality level description)
const uint8_t legend_y = canvas_h / 2;
if(context->display_mode == AirmonDisplayModeAqi) {
canvas_set_font(canvas, FontSecondary);
snprintf(buffer, STR_BUF_SIZE, "%s", data_is_valid ? airmon_aqi_category(aqi_value) : "Unknown");
canvas_draw_str_aligned(canvas, canvas_w / 2, legend_y + 5, AlignCenter, AlignTop, buffer);
} else {
const Icon* density_icon = &I_Density_33x11;
const uint8_t density_icon_w = icon_get_width(density_icon);
canvas_draw_icon(canvas, (canvas_w - density_icon_w) / 2, legend_y + 3, density_icon);
}
// Draw mode bottom menu
canvas_set_font(canvas, FontSecondary);
const uint8_t label_w = canvas_w / AirmonDisplayModeCount;
const uint8_t label_h = canvas_current_font_height(canvas);
const uint8_t label_y = canvas_h - label_h - 1;
for(AirmonDisplayMode mode = 0; mode < AirmonDisplayModeCount; mode++) {
uint8_t label_x = mode * label_w;
bool mode_active = mode == context->display_mode;
if(mode_active) {
canvas_draw_box(canvas, label_x, label_y, label_w, label_h);
canvas_invert_color(canvas);
}
const char* label_str;
switch(mode) {
case AirmonDisplayModePm1_0:
label_str = "PM1.0";
break;
case AirmonDisplayModePm2_5:
label_str = "PM2.5";
break;
case AirmonDisplayModePm10:
label_str = "PM10";
break;
case AirmonDisplayModeAqi:
label_str = "AQI";
break;
default:
label_str = "";
break;
}
const uint8_t label_str_w = canvas_string_width(canvas, label_str);
canvas_draw_str(
canvas, label_x + (label_w - label_str_w) / 2, label_y + label_h - 2, label_str);
if(mode_active) {
canvas_invert_color(canvas);
}
}
airmon_draw_battery(canvas, context);
// Blink LED if data was updated
if(data_timestamp > context->last_data_timestamp) {
airmon_blink(context, aqi_value);
context->last_data_timestamp = data_timestamp;
}
}
static void airmon_input_callback(InputEvent* input_event, void* ctx) {
AirmonContext* context = ctx;
AirmonEvent event = {.type = AirmonEventTypeKey, .input = *input_event};
furi_message_queue_put(context->event_queue, &event, FuriWaitForever);
}
static AirmonContext* airmon_context_alloc() {
AirmonContext* ctx = malloc(sizeof(AirmonContext));
ctx->pms_context = airmon_pms_context_alloc();
if(!ctx->pms_context) {
free(ctx);
return NULL;
}
ctx->last_data_timestamp = furi_get_tick();
ctx->display_mode = AirmonDisplayModePm2_5;
ctx->event_queue = furi_message_queue_alloc(8, sizeof(AirmonEvent));
ctx->view_port = view_port_alloc();
view_port_draw_callback_set(ctx->view_port, airmon_draw_callback, ctx);
view_port_input_callback_set(ctx->view_port, airmon_input_callback, ctx);
ctx->gui = furi_record_open(RECORD_GUI);
ctx->power = furi_record_open(RECORD_POWER);
ctx->notifications = furi_record_open(RECORD_NOTIFICATION);
gui_add_view_port(ctx->gui, ctx->view_port, GuiLayerFullscreen);
return ctx;
}
static void airmon_run(AirmonContext* ctx) {
airmon_pms_init(ctx->pms_context);
AirmonEvent event;
bool processing = true;
while(processing) {
FuriStatus event_status = furi_message_queue_get(ctx->event_queue, &event, 1000);
if(event_status == FuriStatusOk) {
// press events
if(event.type == AirmonEventTypeKey) {
if(event.input.type == InputTypePress) {
switch(event.input.key) {
case InputKeyBack:
processing = false;
break;
case InputKeyLeft:
if(ctx->display_mode-- == 0) {
ctx->display_mode = AirmonDisplayModeCount - 1;
}
break;
case InputKeyRight:
if(++ctx->display_mode >= AirmonDisplayModeCount) {
ctx->display_mode -= AirmonDisplayModeCount;
}
break;
default:
break;
}
}
}
} else {
FURI_LOG_D(TAG, "FuriMessageQueue: event timeout");
}
power_get_info(ctx->power, &ctx->power_info);
view_port_update(ctx->view_port);
}
airmon_pms_deinit(ctx->pms_context);
}
static void airmon_context_free(AirmonContext* ctx) {
view_port_enabled_set(ctx->view_port, false);
gui_remove_view_port(ctx->gui, ctx->view_port);
view_port_free(ctx->view_port);
furi_message_queue_free(ctx->event_queue);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_POWER);
furi_record_close(RECORD_NOTIFICATION);
airmon_pms_context_free(ctx->pms_context);
free(ctx);
}
/* The application's entry point. Execution starts from here. */
int32_t airmon_app(void* p) {
UNUSED(p);
// Allocate all of the necessary structures
AirmonContext* ctx = airmon_context_alloc();
if(!ctx) {
return 255;
}
// Start the applicaton's main loop. It won't return until the application was requested to exit
airmon_run(ctx);
// Release all resources
airmon_context_free(ctx);
return 0;
}