Full dashboard - make LEDs and graph overridable

This commit is contained in:
cpq committed 2026-12-02 11:24:54 +00:00
1 parent 364c979a1d
commit 37db92bfc2
7 files changed
+294 -69

No files matched your search

+75 -27
View File
@@ -3,6 +3,43 @@
#include "mongoose.h"
// Maps v from the lo..hi range to the real-time graph's 0..100. For example,
// a signed 16-bit sensor reading: GRAPH_SCALE(v, -32768, 32767)
#define GRAPH_SCALE(v, lo, hi) \
((int) (((int64_t) (v) - (lo)) * 100 / ((int64_t) (hi) - (lo))))
// Board hooks. To wire the dashboard to real hardware, a board builds with
// -DDASHBOARD_CONFIG and overrides them in its own dashboard_config.h, next
// to its main.c. The defaults keep the built-in demo behaviour
#ifdef DASHBOARD_CONFIG
#include "dashboard_config.h"
#endif
#ifndef LED1_READ
#define LED1_READ() s_led1 // Returns the current LED1 state
#endif
#ifndef LED1_WRITE
#define LED1_WRITE(on) (void) (on) // Turns LED1 on or off
#endif
#ifndef LED2_READ
#define LED2_READ() s_led2
#endif
#ifndef LED2_WRITE
#define LED2_WRITE(on) (void) (on)
#endif
#ifndef LED3_READ
#define LED3_READ() s_led3
#endif
#ifndef LED3_WRITE
#define LED3_WRITE(on) (void) (on)
#endif
#ifndef GRAPH_READ
#define GRAPH_READ(v) graph_demo_read(v) // Fills int v[3]: x, y, z, 0..100
#endif
#ifndef GRAPH_PERIOD_MS
#define GRAPH_PERIOD_MS 25 // How often to sample and send x, y, z
#endif
static struct mg_dash s_dash;
static int authenticate(char *user, size_t userlen, const char *pass);
@@ -34,6 +71,16 @@ static struct mg_field_set set_actions = {"actions", fields_actions, actions_fn,
static bool s_led1, s_led2, s_led3;
static bool leds_fn(enum mg_dash_op op, struct mg_dash_user *u) {
(void) u;
if (op == MG_DASH_READ) {
s_led1 = LED1_READ();
s_led2 = LED2_READ();
s_led3 = LED3_READ();
}
if (op == MG_DASH_WRITE) {
LED1_WRITE(s_led1);
LED2_WRITE(s_led2);
LED3_WRITE(s_led3);
}
return op == MG_DASH_READ || op == MG_DASH_WRITE;
}
static struct mg_field fields_leds[] = {
@@ -77,6 +124,7 @@ static int s_ota_interval = 30;
static char s_ota_version[] = "1.1.2";
static char s_ota_status[40] = "No scans yet";
static char s_ota_url[100] = "https://my-product.com/ota.json";
static bool s_graph_report = true; // Send real-time graph values
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u) {
if (u->level < 3) return false;
@@ -101,6 +149,7 @@ static struct mg_field fields_settings[] = {
{"ota_status", MG_VAL_STR, &s_ota_status, sizeof(s_ota_status)},
{"ota_url", MG_VAL_STR, &s_ota_url, sizeof(s_ota_url)},
{"ota_interval", MG_VAL_INT, &s_ota_interval, sizeof(s_ota_interval)},
{"graph_report", MG_VAL_BOOL, &s_graph_report, sizeof(s_graph_report)},
{NULL, MG_VAL_INT, NULL, 0},
};
@@ -152,41 +201,38 @@ static bool graph1_fn(enum mg_dash_op op, struct mg_dash_user *u) {
static struct mg_field_set set_graph1 = {"graph1", fields_graph1, graph1_fn,
NULL, NULL};
#define NUM_POINTS_GRAPH2 100 // How many graph2 data points to send
static char s_graph2_data[NUM_POINTS_GRAPH2 * 4 + 2 + 1];
static bool s_graph2_report = true;
// Real-time graph: the latest x, y, z values. The device sends each new
// set, the browser keeps the rolling window, see GRAPH_WINDOW in the HTML
static int s_graph[3];
// Adjust t += 7, i * 9, and i * 3
void read_graph2(void) {
static uint16_t t;
size_t i, len = 0;
len = mg_snprintf(s_graph2_data, sizeof(s_graph2_data), "[");
t += 23;
for (i = 0; i < NUM_POINTS_GRAPH2; i++) {
uint16_t x = (uint16_t) (i * 7 + t);
uint16_t y = (uint16_t) (i * 3 - t / 2);
// Demo data source: x, y, z of a fake signal, each 3..97. Not static, so a
// board that overrides GRAPH_READ gets no unused function warning
void graph_demo_read(int *v) {
static uint16_t n;
int i;
for (i = 0; i < 3; i++) {
uint16_t t = (uint16_t) (n + i * 40); // Shift y and z along the signal
uint16_t x = (uint16_t) (t * 7), y = (uint16_t) (t * 3);
uint8_t a = (uint8_t) (x ^ (x >> 3)) & 255;
uint8_t b = (uint8_t) (y ^ (y >> 4)) & 255;
int v =
70 + ((a < 128 ? a : 255 - a) >> 1) + ((b < 128 ? b : 255 - b) >> 2);
len += mg_snprintf(s_graph2_data + len, sizeof(s_graph2_data) - len,
"%s%hhu", i > 0 ? "," : "", (uint8_t) v);
int s = ((a < 128 ? a : 255 - a) >> 1) + ((b < 128 ? b : 255 - b) >> 2);
v[i] = GRAPH_SCALE(s, -3, 97); // s is 0..94, so 3..97, off the edges
}
len += mg_snprintf(s_graph2_data + len, sizeof(s_graph2_data) - len, "]");
n++;
}
static struct mg_field fields_graph2[] = {
{"data", MG_VAL_RAW, s_graph2_data, sizeof(s_graph2_data)},
{"report", MG_VAL_BOOL, &s_graph2_report, sizeof(s_graph2_report)},
static struct mg_field fields_graph[] = {
{"x", MG_VAL_INT, &s_graph[0], sizeof(s_graph[0])},
{"y", MG_VAL_INT, &s_graph[1], sizeof(s_graph[1])},
{"z", MG_VAL_INT, &s_graph[2], sizeof(s_graph[2])},
{NULL, MG_VAL_INT, NULL, 0},
};
static bool graph2_fn(enum mg_dash_op op, struct mg_dash_user *u) {
static bool graph_fn(enum mg_dash_op op, struct mg_dash_user *u) {
(void) u;
if (op == MG_DASH_READ) read_graph2();
return op == MG_DASH_READ || op == MG_DASH_WRITE;
return op == MG_DASH_READ; // Values are set in mg_dash_poll()
}
static struct mg_field_set set_graph2 = {"graph2", fields_graph2, graph2_fn,
static struct mg_field_set set_graph = {"graph", fields_graph, graph_fn,
NULL, NULL};
static int authenticate(char *user, size_t userlen, const char *pass) {
@@ -238,7 +284,7 @@ void mg_dash_init(struct mg_mgr *mgr) {
MG_DASH_ADD_FIELD_SET(&s_dash, &set_metrics);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_settings);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph1);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph2);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_graph);
MG_DASH_ADD_FIELD_SET(&s_dash, &set_actions);
#if MG_ARCH == MG_ARCH_UNIX
@@ -262,8 +308,10 @@ void mg_dash_poll(struct mg_mgr *mgr) {
if (mg_timer_expired(&timer1, 30000, mg_now())) {
mg_dash_send_change(mgr, &set_metrics);
}
if (mg_timer_expired(&timer2, 120, mg_now()) && s_graph2_report) {
mg_dash_send_change(mgr, &set_graph2);
if (mg_timer_expired(&timer2, GRAPH_PERIOD_MS, mg_now()) && s_graph_report) {
// Sample here, not in graph_fn(): that runs once per connected client
GRAPH_READ(s_graph);
mg_dash_send_change(mgr, &set_graph);
}
if (s_action2 == true && s_action2_timeout < mg_now()) {
s_action2 = false;