feat(display): add air quality pages, history and display settings
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#include "ui_model.h"
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
static portMUX_TYPE model_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
#define LOCK() portENTER_CRITICAL(&model_mux)
|
||||
#define UNLOCK() portEXIT_CRITICAL(&model_mux)
|
||||
#else
|
||||
#define LOCK() ((void)0)
|
||||
#define UNLOCK() ((void)0)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint64_t bucket;
|
||||
float sum[UI_METRIC_COUNT];
|
||||
uint16_t count[UI_METRIC_COUNT];
|
||||
bool used;
|
||||
} history_bin_t;
|
||||
static ui_sample_t latest;
|
||||
static history_bin_t bins[UI_HISTORY_POINTS];
|
||||
|
||||
void ui_model_publish(const ui_sample_t *sample)
|
||||
{
|
||||
if (!sample || !sample->available) return;
|
||||
const uint64_t bucket = sample->timestamp_ms / UI_HISTORY_BUCKET_MS;
|
||||
LOCK();
|
||||
latest = *sample;
|
||||
history_bin_t *bin = &bins[bucket % UI_HISTORY_POINTS];
|
||||
if (!bin->used || bin->bucket != bucket) {
|
||||
*bin = (history_bin_t){.bucket = bucket, .used = true};
|
||||
}
|
||||
for (unsigned i = 0; i < UI_METRIC_COUNT; ++i) {
|
||||
if (isfinite(sample->values[i]) && bin->count[i] < UINT16_MAX) {
|
||||
bin->sum[i] += sample->values[i];
|
||||
++bin->count[i];
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
}
|
||||
|
||||
void ui_model_snapshot(ui_sample_t *sample)
|
||||
{
|
||||
LOCK();
|
||||
*sample = latest;
|
||||
UNLOCK();
|
||||
}
|
||||
|
||||
void ui_model_history(ui_metric_t metric, uint64_t now_ms, float *values)
|
||||
{
|
||||
const uint64_t last_bucket = now_ms / UI_HISTORY_BUCKET_MS;
|
||||
LOCK();
|
||||
for (unsigned i = 0; i < UI_HISTORY_POINTS; ++i) {
|
||||
const unsigned age = UI_HISTORY_POINTS - 1 - i;
|
||||
values[i] = NAN;
|
||||
if ((unsigned)metric >= UI_METRIC_COUNT || last_bucket < age) continue;
|
||||
const uint64_t bucket = last_bucket - age;
|
||||
const history_bin_t *bin = &bins[bucket % UI_HISTORY_POINTS];
|
||||
if (bin->used && bin->bucket == bucket && bin->count[metric]) {
|
||||
values[i] = bin->sum[metric] / bin->count[metric];
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
}
|
||||
Reference in New Issue
Block a user