diff --git a/main/ble.c b/main/ble.c index a834c08..faa6748 100644 --- a/main/ble.c +++ b/main/ble.c @@ -35,15 +35,18 @@ static struct ble_gap_adv_params adv_params = { static uint16_t conn_handle; +static uint16_t csf_handle; static uint16_t cs_handle[INPUTS_NUM]; static uint16_t ev_handle[INPUTS_NUM]; static uint16_t settings_handle[SETTINGS_CHRS_NUM]; static uint16_t range_handle[RANGE_CHRS_NUM]; +static uint8_t csf_notify_state; static uint8_t cs_notify_state[INPUTS_NUM]; static uint8_t ev_notify_state[INPUTS_NUM]; static uint8_t range_notify_state; +static struct ble_context csf_ctxt; static struct ble_context ev_ctxt[INPUTS_NUM]; static struct ble_context cs_ctxt[INPUTS_NUM]; static struct ble_context settings_ctxt[SETTINGS_CHRS_NUM]; @@ -104,6 +107,29 @@ static struct ble_gatt_dsc_def rvs_descr_array[INPUTS_NUM + 1][3]; static struct ble_gatt_svc_def gatt_svr_svcs[] = { + [CSF_SVC_ID] = { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(METROLOGY_SERVICE), + .characteristics = (struct ble_gatt_chr_def[]){ + [0] = { + .uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_CHAR), + .access_cb = gatt_char_access_csf, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, + .min_key_size = 0, + .val_handle = &csf_handle, + .arg = &csf_ctxt, + .descriptors = (struct ble_gatt_dsc_def[]){ + [0] = { + .uuid = BLE_UUID16_DECLARE(CHAR_PRES_FORMAT), + .access_cb = gatt_char_access_csf, + .att_flags = BLE_ATT_F_READ, + }, + { 0 }, + }, + }, + {0}, + }, + }, [CS_SVC_ID] = { .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = BLE_UUID16_DECLARE(METROLOGY_SERVICE), @@ -257,6 +283,8 @@ static int ble_gap_event(struct ble_gap_event *event, void* arg){ case BLE_GAP_EVENT_SUBSCRIBE: ESP_LOGI(TAG, "Subscribe %d", event->subscribe.attr_handle); + if(event->subscribe.attr_handle == csf_handle) + csf_notify_state = event->subscribe.cur_notify; for(int i = 0; i < INPUTS_NUM; i++){ if(event->subscribe.attr_handle == cs_handle[i]) cs_notify_state[i] = event->subscribe.cur_notify; @@ -319,6 +347,11 @@ static void generate_svc_defs(configuration* conf, measurements* meas, measureme range_ctxt[i].meas = meas; range_ctxt[i].meas_ctxt = meas_ctxt; } + + csf_ctxt.handle = 0; + csf_ctxt.conf = conf; + csf_ctxt.meas = meas; + csf_ctxt.meas_ctxt = meas_ctxt; } void initBLE(configuration* conf, measurements* meas, measurements_ctxt* meas_ctxt){ @@ -335,6 +368,12 @@ void initBLE(configuration* conf, measurements* meas, measurements_ctxt* meas_ct } void notify_update(measurements* meas){ + if(csf_notify_state){ + ESP_LOGI(TAG, "notify current fused"); + uint32_t data = meas->amperes_f; + struct os_mbuf* om = ble_hs_mbuf_from_flat(&data, sizeof(data)); + ESP_ERROR_CHECK(ble_gattc_notify_custom(conn_handle, csf_handle, om)); + } for(int i = 0; i < INPUTS_NUM; i++){ if(cs_notify_state[i]){ ESP_LOGI(TAG, "notify current %d", i); diff --git a/main/gatt_svcs.c b/main/gatt_svcs.c index cb02de9..d900eb1 100644 --- a/main/gatt_svcs.c +++ b/main/gatt_svcs.c @@ -269,3 +269,24 @@ int gatt_char_access_range(uint16_t conn_handle, uint16_t attr_handle, struct bl return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0; } + +int gatt_char_access_csf(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg){ + int rc = 0; + + switch(ctxt->op){ + case BLE_GATT_ACCESS_OP_READ_CHR: + ; + struct ble_context *c_ctxt = arg; + + uint32_t data = c_ctxt->meas->amperes_f; + rc = os_mbuf_append(ctxt->om, &data, sizeof(data)); + break; + case BLE_GATT_ACCESS_OP_READ_DSC: + ; + rc = os_mbuf_append(ctxt->om, ¤t_char_pres_format, CHAR_PRESENTATION_FORMAT_SIZE); + break; + } + + return rc ? BLE_ATT_ERR_INSUFFICIENT_RES : 0; +} + diff --git a/main/gatt_svcs.h b/main/gatt_svcs.h index ae36802..e11008f 100644 --- a/main/gatt_svcs.h +++ b/main/gatt_svcs.h @@ -2,7 +2,7 @@ #include "host/ble_gatt.h" -enum ble_services {CS_SVC_ID, RVS_SVC_ID, RANGE_SVC_ID, SETTINGS_SVC_ID, SVCS_NUM}; +enum ble_services {CSF_SVC_ID, CS_SVC_ID, RVS_SVC_ID, RANGE_SVC_ID, SETTINGS_SVC_ID, SVCS_NUM}; enum settings_chars {RFRSH_RATE_ID, ZEROS_CALI_ID, ZEROS_CALI_RESET, ZEROS_CALI_NSAMP_ID, SETTINGS_CHRS_NUM}; enum range_characteristics {AUTO_RANGE_ID, CURRENT_RANGE_ID, RANGE_CHRS_NUM}; @@ -17,6 +17,7 @@ struct char_pres_format { uint8_t descrL; }; +int gatt_char_access_csf(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); int gatt_char_access_cs(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); int gatt_char_access_ev(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); int gatt_char_access_sampling_rate(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg); diff --git a/main/measure.h b/main/measure.h index bd99a6a..a3b5930 100644 --- a/main/measure.h +++ b/main/measure.h @@ -14,6 +14,7 @@ typedef struct { typedef struct { uint32_t *raw_volt; uint32_t *amperes; + uint32_t amperes_f; } measurements; typedef struct { diff --git a/main/power_profiler.c b/main/power_profiler.c index fcd6b45..3591c15 100644 --- a/main/power_profiler.c +++ b/main/power_profiler.c @@ -31,6 +31,18 @@ // number of consecutive times the input has to be below the threshold before switching #define UNDERRANGE_NUM 3 + +// default source for the fused current measure +#define DEFAULT_SRC X10 +// valeur pour laquelle on passe a la source de haute precision (mV) +#define DEFAULT_SRC_SW_MIN 10 +// source de plus haute precision +#define HIGH_PRECISION_SRC X100 +// valeur pour laquelle on passe a la source de plus basse precision (mV) +#define DEFAULT_SRC_SW_MAX 700 +// source de plus haute precision +#define LOW_PRECISION_SRC X1 + resistor_range ranges[] = { [R1] = { .pin = GPIO_NUM_6, @@ -187,6 +199,13 @@ void app_main(void){ meas_amp[i] = buff; } + if(meas_volts[DEFAULT_SRC] < DEFAULT_SRC_SW_MIN) + meas.amperes_f = meas_amp[HIGH_PRECISION_SRC]; + else if(meas_volts[DEFAULT_SRC] > DEFAULT_SRC_SW_MAX) + meas.amperes_f = meas_amp[LOW_PRECISION_SRC]; + else + meas.amperes_f = meas_amp[DEFAULT_SRC]; + if(meas_volts[UNDERRANGE_SRC] < UNDERRANGE_MV){ underrange_counter++; if(underrange_counter > UNDERRANGE_NUM && main_conf.auto_range){