firmware: NVS-backed cell constant K — set/get over BLE for app sync
This commit is contained in:
parent
f5dd536ca4
commit
4e0dfecce0
15
main/ble.c
15
main/ble.c
|
|
@ -186,10 +186,15 @@ static void parse_one_sysex(const uint8_t *midi, uint16_t mlen)
|
||||||
cmd.clean.v_mv = ble_decode_float(&midi[3]);
|
cmd.clean.v_mv = ble_decode_float(&midi[3]);
|
||||||
cmd.clean.duration_s = ble_decode_float(&midi[8]);
|
cmd.clean.duration_s = ble_decode_float(&midi[8]);
|
||||||
break;
|
break;
|
||||||
|
case CMD_SET_CELL_K:
|
||||||
|
if (mlen < 8) return;
|
||||||
|
cmd.cell_k = ble_decode_float(&midi[3]);
|
||||||
|
break;
|
||||||
case CMD_START_SWEEP:
|
case CMD_START_SWEEP:
|
||||||
case CMD_GET_CONFIG:
|
case CMD_GET_CONFIG:
|
||||||
case CMD_STOP_AMP:
|
case CMD_STOP_AMP:
|
||||||
case CMD_GET_TEMP:
|
case CMD_GET_TEMP:
|
||||||
|
case CMD_GET_CELL_K:
|
||||||
case CMD_START_REFS:
|
case CMD_START_REFS:
|
||||||
case CMD_GET_REFS:
|
case CMD_GET_REFS:
|
||||||
case CMD_CLEAR_REFS:
|
case CMD_CLEAR_REFS:
|
||||||
|
|
@ -857,6 +862,16 @@ int ble_send_temp(float temp_c)
|
||||||
return send_sysex(sx, p);
|
return send_sysex(sx, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ble_send_cell_k(float k)
|
||||||
|
{
|
||||||
|
uint8_t sx[12];
|
||||||
|
uint16_t p = 0;
|
||||||
|
sx[p++] = 0xF0; sx[p++] = 0x7D; sx[p++] = RSP_CELL_K;
|
||||||
|
encode_float(k, &sx[p]); p += 5;
|
||||||
|
sx[p++] = 0xF7;
|
||||||
|
return send_sysex(sx, p);
|
||||||
|
}
|
||||||
|
|
||||||
int ble_send_ref_frame(uint8_t mode, uint8_t rtia_idx)
|
int ble_send_ref_frame(uint8_t mode, uint8_t rtia_idx)
|
||||||
{
|
{
|
||||||
uint8_t sx[] = { 0xF0, 0x7D, RSP_REF_FRAME, mode & 0x7F, rtia_idx & 0x7F, 0xF7 };
|
uint8_t sx[] = { 0xF0, 0x7D, RSP_REF_FRAME, mode & 0x7F, rtia_idx & 0x7F, 0xF7 };
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@
|
||||||
#define CMD_START_CLEAN 0x25
|
#define CMD_START_CLEAN 0x25
|
||||||
#define CMD_OPEN_CAL 0x26
|
#define CMD_OPEN_CAL 0x26
|
||||||
#define CMD_CLEAR_OPEN_CAL 0x27
|
#define CMD_CLEAR_OPEN_CAL 0x27
|
||||||
|
#define CMD_SET_CELL_K 0x28
|
||||||
|
#define CMD_GET_CELL_K 0x29
|
||||||
#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
|
||||||
|
|
@ -41,6 +43,7 @@
|
||||||
#define RSP_CL_END 0x0E
|
#define RSP_CL_END 0x0E
|
||||||
#define RSP_PH_RESULT 0x0F
|
#define RSP_PH_RESULT 0x0F
|
||||||
#define RSP_TEMP 0x10
|
#define RSP_TEMP 0x10
|
||||||
|
#define RSP_CELL_K 0x11
|
||||||
#define RSP_REF_FRAME 0x20
|
#define RSP_REF_FRAME 0x20
|
||||||
#define RSP_REF_LP_RANGE 0x21
|
#define RSP_REF_LP_RANGE 0x21
|
||||||
#define RSP_REFS_DONE 0x22
|
#define RSP_REFS_DONE 0x22
|
||||||
|
|
@ -58,6 +61,7 @@ typedef struct {
|
||||||
struct { float v_cond, t_cond_ms, v_free, v_total, t_dep_ms, t_meas_ms; uint8_t lp_rtia; } cl;
|
struct { float v_cond, t_cond_ms, v_free, v_total, t_dep_ms, t_meas_ms; uint8_t lp_rtia; } cl;
|
||||||
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;
|
||||||
};
|
};
|
||||||
} BleCommand;
|
} BleCommand;
|
||||||
|
|
||||||
|
|
@ -103,6 +107,9 @@ int ble_send_ph_result(float v_ocp_mv, float ph, float temp_c);
|
||||||
/* outbound: temperature */
|
/* outbound: temperature */
|
||||||
int ble_send_temp(float temp_c);
|
int ble_send_temp(float temp_c);
|
||||||
|
|
||||||
|
/* outbound: cell constant */
|
||||||
|
int ble_send_cell_k(float k);
|
||||||
|
|
||||||
/* outbound: reference collection */
|
/* outbound: reference collection */
|
||||||
int ble_send_ref_frame(uint8_t mode, uint8_t rtia_idx);
|
int ble_send_ref_frame(uint8_t mode, uint8_t rtia_idx);
|
||||||
int ble_send_ref_lp_range(uint8_t mode, uint8_t low_idx, uint8_t high_idx);
|
int ble_send_ref_lp_range(uint8_t mode, uint8_t low_idx, uint8_t high_idx);
|
||||||
|
|
|
||||||
30
main/eis.c
30
main/eis.c
|
|
@ -23,6 +23,9 @@ static struct {
|
||||||
uint32_t dertia_reg;
|
uint32_t dertia_reg;
|
||||||
} ctx;
|
} ctx;
|
||||||
|
|
||||||
|
/* cell constant K (cm⁻¹), cached from NVS */
|
||||||
|
static float cell_k_cached;
|
||||||
|
|
||||||
/* open-circuit calibration data */
|
/* open-circuit calibration data */
|
||||||
static struct {
|
static struct {
|
||||||
fImpCar_Type y[EIS_MAX_POINTS]; /* admittance at each freq */
|
fImpCar_Type y[EIS_MAX_POINTS]; /* admittance at each freq */
|
||||||
|
|
@ -585,3 +588,30 @@ int eis_has_open_cal(void)
|
||||||
{
|
{
|
||||||
return ocal.valid;
|
return ocal.valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define NVS_CELLK_KEY "cell_k"
|
||||||
|
|
||||||
|
void eis_set_cell_k(float k)
|
||||||
|
{
|
||||||
|
cell_k_cached = k;
|
||||||
|
nvs_handle_t h;
|
||||||
|
if (nvs_open(NVS_OCAL_NS, NVS_READWRITE, &h) != ESP_OK) return;
|
||||||
|
nvs_set_blob(h, NVS_CELLK_KEY, &k, sizeof(k));
|
||||||
|
nvs_commit(h);
|
||||||
|
nvs_close(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
float eis_get_cell_k(void)
|
||||||
|
{
|
||||||
|
return cell_k_cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
void eis_load_cell_k(void)
|
||||||
|
{
|
||||||
|
nvs_handle_t h;
|
||||||
|
if (nvs_open(NVS_OCAL_NS, NVS_READONLY, &h) != ESP_OK) return;
|
||||||
|
size_t len = sizeof(cell_k_cached);
|
||||||
|
if (nvs_get_blob(h, NVS_CELLK_KEY, &cell_k_cached, &len) != ESP_OK || len != sizeof(cell_k_cached))
|
||||||
|
cell_k_cached = 0.0f;
|
||||||
|
nvs_close(h);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,4 +63,8 @@ void eis_clear_open_cal(void);
|
||||||
int eis_has_open_cal(void);
|
int eis_has_open_cal(void);
|
||||||
void eis_load_open_cal(void);
|
void eis_load_open_cal(void);
|
||||||
|
|
||||||
|
void eis_set_cell_k(float k);
|
||||||
|
float eis_get_cell_k(void);
|
||||||
|
void eis_load_cell_k(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
11
main/eis4.c
11
main/eis4.c
|
|
@ -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();
|
||||||
temp_init();
|
temp_init();
|
||||||
|
|
||||||
esp_netif_init();
|
esp_netif_init();
|
||||||
|
|
@ -207,6 +208,16 @@ void app_main(void)
|
||||||
printf("Open-circuit cal cleared\n");
|
printf("Open-circuit cal cleared\n");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case CMD_SET_CELL_K:
|
||||||
|
eis_set_cell_k(cmd.cell_k);
|
||||||
|
ble_send_cell_k(cmd.cell_k);
|
||||||
|
printf("Cell K set: %.4f cm^-1\n", cmd.cell_k);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CMD_GET_CELL_K:
|
||||||
|
ble_send_cell_k(eis_get_cell_k());
|
||||||
|
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;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue