diff --git a/main/eis.c b/main/eis.c index 4ba43cd..833e67a 100644 --- a/main/eis.c +++ b/main/eis.c @@ -25,6 +25,7 @@ static struct { /* cell constant K (cm⁻¹), cached from NVS */ static float cell_k_cached; +static float cl_factor_cached; /* open-circuit calibration data */ static struct { @@ -593,7 +594,8 @@ int eis_has_open_cal(void) return ocal.valid; } -#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) { @@ -619,3 +621,28 @@ void eis_load_cell_k(void) cell_k_cached = 0.0f; 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); +} diff --git a/main/eis.h b/main/eis.h index c4cd28c..8fbb4d3 100644 --- a/main/eis.h +++ b/main/eis.h @@ -67,4 +67,8 @@ void eis_set_cell_k(float k); float eis_get_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 diff --git a/main/eis4.c b/main/eis4.c index 5385e6d..dace30e 100644 --- a/main/eis4.c +++ b/main/eis4.c @@ -56,6 +56,7 @@ void app_main(void) eis_default_config(&cfg); eis_load_open_cal(); eis_load_cell_k(); + eis_load_cl_factor(); temp_init(); esp_netif_init(); @@ -214,6 +215,16 @@ void app_main(void) send_cell_k(eis_get_cell_k()); 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: { ClConfig cl_cfg; cl_cfg.v_cond = cmd.cl.v_cond; diff --git a/main/protocol.c b/main/protocol.c index 08a9c47..504a6ed 100644 --- a/main/protocol.c +++ b/main/protocol.c @@ -341,6 +341,18 @@ int send_cell_k(float k) 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 ---- */ int send_ref_frame(uint8_t mode, uint8_t rtia_idx) diff --git a/main/protocol.h b/main/protocol.h index 54b756b..8ab5709 100644 --- a/main/protocol.h +++ b/main/protocol.h @@ -26,6 +26,8 @@ #define CMD_START_REFS 0x30 #define CMD_GET_REFS 0x31 #define CMD_CLEAR_REFS 0x32 +#define CMD_SET_CL_FACTOR 0x33 +#define CMD_GET_CL_FACTOR 0x34 /* Session sync commands (0x4x) */ #define CMD_SESSION_CREATE 0x40 @@ -56,6 +58,7 @@ #define RSP_REF_LP_RANGE 0x21 #define RSP_REFS_DONE 0x22 #define RSP_REF_STATUS 0x23 +#define RSP_CL_FACTOR 0x24 /* Session sync responses (0x4x) */ #define RSP_SESSION_CREATED 0x40 @@ -81,6 +84,7 @@ typedef struct { struct { float stabilize_s; } ph; struct { float v_mv; float duration_s; } clean; float cell_k; + float cl_factor; struct { uint8_t name_len; char name[MAX_SESSION_NAME]; } session_create; struct { uint8_t id; } session_switch; 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 */ int send_cell_k(float k); +/* outbound: chlorine factor */ +int send_cl_factor(float f); + /* outbound: reference collection */ 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); diff --git a/main/wifi_transport.c b/main/wifi_transport.c index df02497..1d05ba4 100644 --- a/main/wifi_transport.c +++ b/main/wifi_transport.c @@ -166,6 +166,10 @@ static void parse_udp_sysex(const uint8_t *data, uint16_t len) if (len < 8) return; cmd.cell_k = decode_float(&data[3]); break; + case CMD_SET_CL_FACTOR: + if (len < 8) return; + cmd.cl_factor = decode_float(&data[3]); + break; case CMD_SESSION_CREATE: if (len < 5) return; 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_GET_TEMP: case CMD_GET_CELL_K: + case CMD_GET_CL_FACTOR: case CMD_START_REFS: case CMD_GET_REFS: case CMD_CLEAR_REFS: