add cl_factor NVS storage and protocol commands to firmware

This commit is contained in:
jess 2026-04-02 18:19:04 -07:00
parent 1abf46f0c3
commit 4c914a5101
6 changed files with 67 additions and 1 deletions

View File

@ -25,6 +25,7 @@ static struct {
/* cell constant K (cm⁻¹), cached from NVS */ /* cell constant K (cm⁻¹), cached from NVS */
static float cell_k_cached; static float cell_k_cached;
static float cl_factor_cached;
/* open-circuit calibration data */ /* open-circuit calibration data */
static struct { static struct {
@ -594,6 +595,7 @@ int eis_has_open_cal(void)
} }
#define NVS_CELLK_KEY "cell_k" #define NVS_CELLK_KEY "cell_k"
#define NVS_CLFACTOR_KEY "cl_factor"
void eis_set_cell_k(float k) void eis_set_cell_k(float k)
{ {
@ -619,3 +621,28 @@ void eis_load_cell_k(void)
cell_k_cached = 0.0f; cell_k_cached = 0.0f;
nvs_close(h); nvs_close(h);
} }
void eis_set_cl_factor(float f)
{
cl_factor_cached = f;
nvs_handle_t h;
if (nvs_open(NVS_OCAL_NS, NVS_READWRITE, &h) != ESP_OK) return;
nvs_set_blob(h, NVS_CLFACTOR_KEY, &f, sizeof(f));
nvs_commit(h);
nvs_close(h);
}
float eis_get_cl_factor(void)
{
return cl_factor_cached;
}
void eis_load_cl_factor(void)
{
nvs_handle_t h;
if (nvs_open(NVS_OCAL_NS, NVS_READONLY, &h) != ESP_OK) return;
size_t len = sizeof(cl_factor_cached);
if (nvs_get_blob(h, NVS_CLFACTOR_KEY, &cl_factor_cached, &len) != ESP_OK || len != sizeof(cl_factor_cached))
cl_factor_cached = 0.0f;
nvs_close(h);
}

View File

@ -67,4 +67,8 @@ void eis_set_cell_k(float k);
float eis_get_cell_k(void); float eis_get_cell_k(void);
void eis_load_cell_k(void); void eis_load_cell_k(void);
void eis_set_cl_factor(float f);
float eis_get_cl_factor(void);
void eis_load_cl_factor(void);
#endif #endif

View File

@ -56,6 +56,7 @@ void app_main(void)
eis_default_config(&cfg); eis_default_config(&cfg);
eis_load_open_cal(); eis_load_open_cal();
eis_load_cell_k(); eis_load_cell_k();
eis_load_cl_factor();
temp_init(); temp_init();
esp_netif_init(); esp_netif_init();
@ -214,6 +215,16 @@ void app_main(void)
send_cell_k(eis_get_cell_k()); send_cell_k(eis_get_cell_k());
break; break;
case CMD_SET_CL_FACTOR:
eis_set_cl_factor(cmd.cl_factor);
send_cl_factor(cmd.cl_factor);
printf("Cl factor set: %.6f\n", cmd.cl_factor);
break;
case CMD_GET_CL_FACTOR:
send_cl_factor(eis_get_cl_factor());
break;
case CMD_START_CL: { case CMD_START_CL: {
ClConfig cl_cfg; ClConfig cl_cfg;
cl_cfg.v_cond = cmd.cl.v_cond; cl_cfg.v_cond = cmd.cl.v_cond;

View File

@ -341,6 +341,18 @@ int send_cell_k(float k)
return send_sysex(sx, p); return send_sysex(sx, p);
} }
/* ---- outbound: chlorine factor ---- */
int send_cl_factor(float f)
{
uint8_t sx[12];
uint16_t p = 0;
sx[p++] = 0xF0; sx[p++] = 0x7D; sx[p++] = RSP_CL_FACTOR;
encode_float(f, &sx[p]); p += 5;
sx[p++] = 0xF7;
return send_sysex(sx, p);
}
/* ---- outbound: reference collection ---- */ /* ---- outbound: reference collection ---- */
int send_ref_frame(uint8_t mode, uint8_t rtia_idx) int send_ref_frame(uint8_t mode, uint8_t rtia_idx)

View File

@ -26,6 +26,8 @@
#define CMD_START_REFS 0x30 #define CMD_START_REFS 0x30
#define CMD_GET_REFS 0x31 #define CMD_GET_REFS 0x31
#define CMD_CLEAR_REFS 0x32 #define CMD_CLEAR_REFS 0x32
#define CMD_SET_CL_FACTOR 0x33
#define CMD_GET_CL_FACTOR 0x34
/* Session sync commands (0x4x) */ /* Session sync commands (0x4x) */
#define CMD_SESSION_CREATE 0x40 #define CMD_SESSION_CREATE 0x40
@ -56,6 +58,7 @@
#define RSP_REF_LP_RANGE 0x21 #define RSP_REF_LP_RANGE 0x21
#define RSP_REFS_DONE 0x22 #define RSP_REFS_DONE 0x22
#define RSP_REF_STATUS 0x23 #define RSP_REF_STATUS 0x23
#define RSP_CL_FACTOR 0x24
/* Session sync responses (0x4x) */ /* Session sync responses (0x4x) */
#define RSP_SESSION_CREATED 0x40 #define RSP_SESSION_CREATED 0x40
@ -81,6 +84,7 @@ typedef struct {
struct { float stabilize_s; } ph; struct { float stabilize_s; } ph;
struct { float v_mv; float duration_s; } clean; struct { float v_mv; float duration_s; } clean;
float cell_k; float cell_k;
float cl_factor;
struct { uint8_t name_len; char name[MAX_SESSION_NAME]; } session_create; struct { uint8_t name_len; char name[MAX_SESSION_NAME]; } session_create;
struct { uint8_t id; } session_switch; struct { uint8_t id; } session_switch;
struct { uint8_t id; uint8_t name_len; char name[MAX_SESSION_NAME]; } session_rename; struct { uint8_t id; uint8_t name_len; char name[MAX_SESSION_NAME]; } session_rename;
@ -132,6 +136,9 @@ int send_temp(float temp_c);
/* outbound: cell constant */ /* outbound: cell constant */
int send_cell_k(float k); int send_cell_k(float k);
/* outbound: chlorine factor */
int send_cl_factor(float f);
/* outbound: reference collection */ /* outbound: reference collection */
int send_ref_frame(uint8_t mode, uint8_t rtia_idx); int send_ref_frame(uint8_t mode, uint8_t rtia_idx);
int send_ref_lp_range(uint8_t mode, uint8_t low_idx, uint8_t high_idx); int send_ref_lp_range(uint8_t mode, uint8_t low_idx, uint8_t high_idx);

View File

@ -166,6 +166,10 @@ static void parse_udp_sysex(const uint8_t *data, uint16_t len)
if (len < 8) return; if (len < 8) return;
cmd.cell_k = decode_float(&data[3]); cmd.cell_k = decode_float(&data[3]);
break; break;
case CMD_SET_CL_FACTOR:
if (len < 8) return;
cmd.cl_factor = decode_float(&data[3]);
break;
case CMD_SESSION_CREATE: case CMD_SESSION_CREATE:
if (len < 5) return; if (len < 5) return;
cmd.session_create.name_len = data[3] & 0x7F; cmd.session_create.name_len = data[3] & 0x7F;
@ -192,6 +196,7 @@ static void parse_udp_sysex(const uint8_t *data, uint16_t len)
case CMD_STOP_AMP: case CMD_STOP_AMP:
case CMD_GET_TEMP: case CMD_GET_TEMP:
case CMD_GET_CELL_K: case CMD_GET_CELL_K:
case CMD_GET_CL_FACTOR:
case CMD_START_REFS: case CMD_START_REFS:
case CMD_GET_REFS: case CMD_GET_REFS:
case CMD_CLEAR_REFS: case CMD_CLEAR_REFS: