add firehazard.c

This commit is contained in:
2020-01-09 00:32:53 +01:00
parent ef7866a29f
commit d0d82ee8fb
14 changed files with 533 additions and 49 deletions
+92 -11
View File
@@ -3,7 +3,9 @@
#include <fileserver/token_subs.h>
#include <httpd_utils/captive.h>
#include <errno.h>
#include "firehazard.h"
#include "websrv.h"
#include "esp_http_server.h"
#include "utils.h"
@@ -40,14 +42,14 @@ static struct tpl_kv_list build_index_replacements_kv(void)
bool last_empty = true; // first is move
bool suc;
for (int i = 0; i < REG_HISTORY_LEN; i++) {
int x = i*5;
float x = i*2.5f;
if (reg_meas_history[i] == 0) {
snprintf(scratch1, SCRATCH_SIZE, "M%d,0", x);
snprintf(scratch2, SCRATCH_SIZE, "M%d,0", x);
snprintf(scratch1, SCRATCH_SIZE, "M%.1f,0", x);
snprintf(scratch2, SCRATCH_SIZE, "M%.1f,0", x);
last_empty = true;
} else {
snprintf(scratch1, SCRATCH_SIZE, "%c%d,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_meas_history[i]));
snprintf(scratch2, SCRATCH_SIZE, "%c%d,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_tset_history[i]));
snprintf(scratch1, SCRATCH_SIZE, "%c%.1f,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_meas_history[i]));
snprintf(scratch2, SCRATCH_SIZE, "%c%.1f,%d", last_empty ? 'M' : 'L', x, (int)(400 - reg_tset_history[i]));
last_empty = false;
}
@@ -60,8 +62,30 @@ static struct tpl_kv_list build_index_replacements_kv(void)
tpl_kv_add_heapstr(&kv, "ser-act", path1);
tpl_kv_add_heapstr(&kv, "ser-set", path2);
bool ena = fire_enabled();
tpl_kv_add(&kv, "fire_dis_ck", ena ? "" : "selected");
tpl_kv_add(&kv, "fire_en_ck", ena ? "selected" : "");
tpl_kv_add_int(&kv, "timeshift", history_counter);
snprintf(scratch1, SCRATCH_SIZE, "%.2f", analog_read());
tpl_kv_add(&kv, "temp", scratch1);
snprintf(scratch1, SCRATCH_SIZE, "%.0f", fire_get_setpoint(false));
tpl_kv_add(&kv, "tset", scratch1);
float kp, ki, kd;
fire_get_tuning(&kp, &ki, &kd);
snprintf(scratch1, SCRATCH_SIZE, "%.3f", kp);
tpl_kv_add(&kv, "kp", scratch1);
snprintf(scratch1, SCRATCH_SIZE, "%.3f", ki);
tpl_kv_add(&kv, "ki", scratch1);
snprintf(scratch1, SCRATCH_SIZE, "%.3f", kd);
tpl_kv_add(&kv, "kd", scratch1);
#undef SCRATCH_SIZE
return kv;
@@ -90,7 +114,7 @@ static esp_err_t handler_update(httpd_req_t *req)
/* Set a param */
static esp_err_t handler_set(httpd_req_t *req)
{
char buf[64];
char buf[200];
int n = httpd_req_recv(req, buf, 63);
if (n < 0) {
ESP_LOGW(TAG, "rx er");
@@ -98,13 +122,70 @@ static esp_err_t handler_set(httpd_req_t *req)
}
buf[n]=0;
char keybuf[20];
char valbuf[20];
if (ESP_OK != httpd_query_key_value(buf, "key", keybuf, 20)) goto err;
if (ESP_OK != httpd_query_key_value(buf, "value", valbuf, 20)) goto err;
char val[20];
// TODO handle
// select box 0/1
esp_err_t rv = httpd_query_key_value(buf, "fire", val, 20);
if (rv == ESP_OK) {
fire_enable(val[0] == '1');
}
rv = httpd_query_key_value(buf, "tset", val, 20);
if (rv == ESP_OK) {
errno = 0;
float f = atoff(val);
if (!errno) {
fire_setlevel(f);
}
}
float kp, ki, kd;
// sorry
// Kp
rv = httpd_query_key_value(buf, "kp", val, 20);
if (rv == ESP_OK) {
errno = 0;
kp = atoff(val);
if (!errno) {
// Ki
rv = httpd_query_key_value(buf, "ki", val, 20);
if (rv == ESP_OK) {
errno = 0;
ki = atoff(val);
if (!errno) {
// Kd
rv = httpd_query_key_value(buf, "kd", val, 20);
if (rv == ESP_OK) {
errno = 0;
kd = atoff(val);
if (!errno) {
fire_set_tuning(kp, ki, kd);
// save to NVS
nvs_handle nvs;
ESP_ERROR_CHECK(nvs_open("config", NVS_READWRITE, &nvs));
union uf32 ukp, uki, ukd;
ukp.f = kp;
uki.f = ki;
ukd.f = kd;
nvs_set_u32(nvs, "kp", ukp.u);
nvs_set_u32(nvs, "ki", uki.u);
nvs_set_u32(nvs, "kd", ukd.u);
nvs_commit(nvs);
nvs_close(nvs);
}
}
}
}
}
}
httpd_resp_set_hdr(req, "Location", "/");
httpd_resp_set_status(req, "302 Found");
return httpd_resp_send(req, NULL, 0);
err: