Skip to content

Commit

Permalink
add auto-polling
Browse files Browse the repository at this point in the history
  • Loading branch information
GLinnik21 committed Aug 27, 2023
1 parent 8a9e9a0 commit 37b5c9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
10 changes: 9 additions & 1 deletion lib/mh_z19_uart_tools/mh_z19_uart_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ extern "C" {
#define MH_Z19_COMMAND_SIZE (9U)
#define MH_Z19_START_BYTE (0xFFU)

/**
* @brief Interval for polling MH-Z19 sensor (in ms).
*
* Set to 2 minutes based on the sensor's T90 response time (< 120s).
* Ensures stable, accurate readings while optimizing resource use.
*/
#define MH_Z19_RECOMMENDED_READ_INTERVAL 120000

typedef enum {
MhZ19UartCommandCO2Concentraion = 0x86,
MhZ19UartCommandCalibrateZERO = 0x87,
Expand Down Expand Up @@ -105,4 +113,4 @@ int16_t mh_z19_decode_co2_concentration(const uint8_t* data);

#ifdef __cplusplus
}
#endif
#endif
28 changes: 13 additions & 15 deletions mh_z19_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@

#include "mh_z19_uart_tools.h"

#define MH_Z19_APP_POLL_INTERVAL_MS (5000U) // 5 seconds

static void mh_z19_app_run(MhZ19App* const app) {
for(bool isRunning = true; isRunning;) {
InputEvent event;
const FuriStatus status =
furi_message_queue_get(app->event_queue, &event, FuriWaitForever);

if((status != FuriStatusOk) || (event.type != InputTypeShort)) {
continue;
}
furi_message_queue_get(app->event_queue, &event, MH_Z19_APP_POLL_INTERVAL_MS);

static uint8_t data[9] = {0};
mh_z19_uart_read_co2(data);

switch(event.key) {
case InputKeyBack:
isRunning = false;
break;
case InputKeyOk:
furi_hal_uart_tx(app->uart.channel, data, sizeof(data));
break;
default:
break;
furi_hal_uart_tx(app->uart.channel, data, sizeof(data));

if((status == FuriStatusOk) && (event.type = InputTypeShort)) {
switch(event.key) {
case InputKeyBack:
isRunning = false;
break;
default:
break;
}
}
view_port_update(app->gui_data.view_port);
}
Expand Down

0 comments on commit 37b5c9b

Please sign in to comment.