-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
244 lines (191 loc) · 6.02 KB
/
main.cpp
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
#include <stdarg.h>
#include <stdlib.h>
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <input/input.h>
#include <memory>
#define LOG_LEVEL 5
#include "scd30_logging.h"
#include "err.h"
#include "main.h"
#include "scd30.h"
constexpr static int FPS = 2;
std::unique_ptr<State> state;
static void input_cb(InputEvent *event, FuriMessageQueue *queue) {
furi_assert(event);
furi_assert(queue);
EventMessage message {EventID::Key, *event};
furi_message_queue_put(queue, &message, FuriWaitForever);
}
static void draw_cb(Canvas *canvas, void *ctx) {
auto *mutex = reinterpret_cast<FuriMutex *>(ctx);
if (furi_mutex_acquire(mutex, 0) != FuriStatusOk) {
ERROR(errs[(ERR_MUTEX_BLOCK)]);
goto bail;
}
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
if (state->hasRead) {
if (state->lastReadResult) {
char buffer[46];
auto &scd30 = *state->scd30;
int row = 0;
constexpr uint8_t x = 70;
auto print = [&](const char *label, float value) {
snprintf(buffer, sizeof(buffer), "%d.%.3d", int(value), int((value - int(value)) * 1'000));
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 0, row * 10, AlignLeft, AlignTop, label);
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, x, row++ * 10 + 1, AlignLeft, AlignTop, buffer);
};
print("CO2", scd30.carbonDioxide);
print("Temperature", scd30.temperature);
print("Rel Humidity", scd30.relativeHumidity);
} else {
canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "Reading failed.");
}
} else {
canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "Waiting for reading...");
}
goto release;
bail:
canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "Couldn't acquire state.");
release:
if (furi_mutex_release(mutex) != FuriStatusOk) {
canvas_clear(canvas);
canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "Couldn't release mutex.");
}
}
static void timer_cb(FuriMessageQueue *queue) {
furi_assert(queue);
EventMessage message {EventID::Tick};
furi_message_queue_put(queue, &message, 0);
}
extern "C" int32_t scd30_main() {
err_t error = static_cast<err_t>(0);
Gui *gui = nullptr;
ViewPort *viewport = nullptr;
FuriMutex *mutex = furi_mutex_alloc(FuriMutexTypeNormal);
FuriMessageQueue *queue = nullptr;
constexpr uint32_t queue_size = 8;
EventMessage message;
FuriStatus status = FuriStatusOk;
if (!(queue = furi_message_queue_alloc(queue_size, sizeof(message)))) {
ERROR(errs[(error = ERR_MALLOC_QUEUE)]);
goto bail;
}
if (!(gui = reinterpret_cast<Gui *>(furi_record_open("gui")))) {
ERROR(errs[(error = ERR_NO_GUI)]);
goto bail;
}
if (!(state = std::make_unique<State>())) {
ERROR(errs[(error = ERR_MALLOC_STATE)]);
goto bail;
}
if (mutex == nullptr) {
ERROR(errs[(error = ERR_NO_MUTEX)]);
goto bail;
}
if (!(viewport = view_port_alloc())) {
ERROR(errs[(error = ERR_MALLOC_VIEW)]);
goto bail;
}
if (!state->init(queue)) {
ERROR(errs[(error = ERR_INIT_STATE)]);
}
if (!state->scd30 || !state->scd30->begin()) {
ERROR("Couldn't begin SCD30");
}
view_port_input_callback_set(viewport, input_cb, queue);
view_port_draw_callback_set(viewport, draw_cb, &mutex);
gui_add_view_port(gui, viewport, GuiLayerFullscreen);
if (!(state->timer = furi_timer_alloc(timer_cb, FuriTimerTypePeriodic, queue))) {
ERROR(errs[(error = ERR_NO_TIMER)]);
goto bail;
}
state->timerFrequency = furi_kernel_get_tick_frequency();
if ((status = furi_timer_start(state->timer, state->timerFrequency / FPS)) != FuriStatusOk) {
ERROR("Couldn't start timer (%d)", status);
goto bail;
}
INFO("Initialized");
for (bool run = true; run;) {
FuriStatus status = FuriStatusErrorTimeout;
while ((status = furi_message_queue_get(queue, &message, 60000)) == FuriStatusErrorTimeout);
if (status != FuriStatusOk) {
switch (status) {
case FuriStatusErrorTimeout: DEBUG(errs[DEBUG_QUEUE_TIMEOUT]); break;
case FuriStatusError: ERROR(errs[(error = ERR_QUEUE_RTOS)]); goto bail;
case FuriStatusErrorResource: ERROR(errs[(error = ERR_QUEUE_RESOURCE)]); goto bail;
case FuriStatusErrorParameter: ERROR(errs[(error = ERR_QUEUE_BADPRM)]); goto bail;
case FuriStatusErrorNoMemory: ERROR(errs[(error = ERR_QUEUE_NOMEM)]); goto bail;
case FuriStatusErrorISR: ERROR(errs[(error = ERR_QUEUE_ISR)]); goto bail;
default: ERROR(errs[(error = ERR_QUEUE_UNK)]); goto bail;
}
} else {
if (!furi_mutex_acquire(mutex, 0)) {
ERROR(errs[(error = ERR_MUTEX_BLOCK)]);
goto bail;
}
if (message.id == EventID::Tick) {
state->readData();
} else if (message.id == EventID::Key) {
if (message.event.type == InputTypeRelease && message.event.key == InputKeyBack)
run = false;
} else
WARN("Unknown message ID [%d]", static_cast<int>(message.id));
view_port_update(viewport);
if (!furi_mutex_release(mutex)) {
ERROR(errs[(error = ERR_MUTEX_RELEASE)]);
goto bail;
}
}
}
bail:
if (state->timer) {
furi_timer_stop(state->timer);
furi_timer_free(state->timer);
state->timer = nullptr;
}
gui_remove_view_port(gui, viewport);
if (viewport) {
view_port_enabled_set(viewport, false);
view_port_free(viewport);
viewport = nullptr;
}
state.reset();
furi_record_close("gui");
furi_mutex_free(mutex);
if (queue) {
furi_message_queue_free(queue);
queue = nullptr;
}
return 0;
}
bool State::init(FuriMessageQueue *queue) {
furi_assert(this);
if (!(timer = furi_timer_alloc(timer_cb, FuriTimerTypePeriodic, queue))) {
ERROR(errs[ERR_NO_TIMER]);
return false;
}
if (!(scd30 = std::make_unique<SCD30>())) {
ERROR(errs[ERR_NO_SCD30]);
return false;
}
return true;
}
bool State::readData() {
hasRead = true;
if (scd30 == nullptr) {
ERROR("scd30 is null");
return false;
}
if (scd30->dataReady()) {
lastReadResult = (scd30->beginIfNecessary() && scd30->readData());
if (!lastReadResult)
scd30->begun = false;
}
return lastReadResult;
}