range notifications

This commit is contained in:
leo 2023-06-16 13:23:33 +02:00
parent 776291df07
commit 490d8b578b
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
3 changed files with 24 additions and 4 deletions

View File

@ -42,6 +42,7 @@ static uint16_t range_handle[RANGE_CHRS_NUM];
static uint8_t cs_notify_state[INPUTS_NUM]; static uint8_t cs_notify_state[INPUTS_NUM];
static uint8_t ev_notify_state[INPUTS_NUM]; static uint8_t ev_notify_state[INPUTS_NUM];
static uint8_t range_notify_state;
static struct ble_context ev_ctxt[INPUTS_NUM]; static struct ble_context ev_ctxt[INPUTS_NUM];
static struct ble_context cs_ctxt[INPUTS_NUM]; static struct ble_context cs_ctxt[INPUTS_NUM];
@ -122,7 +123,7 @@ static struct ble_gatt_svc_def gatt_svr_svcs[] = {
[CURRENT_RANGE_ID] = { [CURRENT_RANGE_ID] = {
.uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_RANGE_CHAR), .uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_RANGE_CHAR),
.access_cb = gatt_char_access_range, .access_cb = gatt_char_access_range,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
.min_key_size = 0, .min_key_size = 0,
.val_handle = &range_handle[CURRENT_RANGE_ID], .val_handle = &range_handle[CURRENT_RANGE_ID],
.arg = &range_ctxt[CURRENT_RANGE_ID], .arg = &range_ctxt[CURRENT_RANGE_ID],
@ -230,12 +231,15 @@ static int ble_gap_event(struct ble_gap_event *event, void* arg){
break; break;
case BLE_GAP_EVENT_SUBSCRIBE: case BLE_GAP_EVENT_SUBSCRIBE:
ESP_LOGI(TAG, "Subscribe %d", event->subscribe.attr_handle); ESP_LOGI(TAG, "Subscribe %d", event->subscribe.attr_handle);
for(int i = 0; i < INPUTS_NUM; i++){ for(int i = 0; i < INPUTS_NUM; i++){
if(event->subscribe.attr_handle == cs_handle[i]) if(event->subscribe.attr_handle == cs_handle[i])
cs_notify_state[i] = event->subscribe.cur_notify; cs_notify_state[i] = event->subscribe.cur_notify;
if(event->subscribe.attr_handle == ev_handle[i]) if(event->subscribe.attr_handle == ev_handle[i])
ev_notify_state[i] = event->subscribe.cur_notify; ev_notify_state[i] = event->subscribe.cur_notify;
} }
if(event->subscribe.attr_handle == range_handle[CURRENT_RANGE_ID])
range_notify_state = event->subscribe.cur_notify;
break; break;
} }
@ -314,3 +318,9 @@ void notify_update(measurements* meas){
} }
} }
} }
void notify_range(configuration* conf){
ESP_LOGI(TAG, "notify range");
struct os_mbuf* om = ble_hs_mbuf_from_flat(&conf->range, sizeof(conf->range));
ESP_ERROR_CHECK(ble_gattc_notify_custom(conn_handle, range_handle[CURRENT_RANGE_ID], om));
}

View File

@ -15,4 +15,5 @@ struct ble_context {
}; };
void initBLE(configuration* conf, measurements* meas, measurements_ctxt* meas_ctxt); void initBLE(configuration* conf, measurements* meas, measurements_ctxt* meas_ctxt);
void notify_update(measurements* meas); void notify_update(measurements* meas);
void notify_range(configuration* conf);

View File

@ -97,12 +97,15 @@ configuration main_conf = {
.ranges_num = R_NUM, .ranges_num = R_NUM,
}; };
uint8_t notify_range_f = 0;
static void IRAM_ATTR overrange_handler(void* arg) static void IRAM_ATTR overrange_handler(void* arg)
{ {
if(!main_conf.auto_range) return; if(!main_conf.auto_range) return;
activate_range(ranges, 0, R_NUM - 1); activate_range(ranges, 0, R_NUM - 1);
main_conf.range = 0; main_conf.range = 0;
notify_range_f = 1;
} }
void app_main(void){ void app_main(void){
@ -188,9 +191,11 @@ void app_main(void){
if(meas_volts[UNDERRANGE_SRC] < UNDERRANGE_MV){ if(meas_volts[UNDERRANGE_SRC] < UNDERRANGE_MV){
underrange_counter++; underrange_counter++;
if(underrange_counter > UNDERRANGE_NUM && main_conf.auto_range){ if(underrange_counter > UNDERRANGE_NUM && main_conf.auto_range){
if(main_conf.range < R_NUM - 1) if(main_conf.range < R_NUM - 1){
main_conf.range++; main_conf.range++;
activate_range(ranges, main_conf.range, R_NUM - 1); activate_range(ranges, main_conf.range, R_NUM - 1);
notify_range_f = 1;
}
underrange_counter = 0; underrange_counter = 0;
} }
} }
@ -199,6 +204,10 @@ void app_main(void){
} }
notify_update(&meas); notify_update(&meas);
if(notify_range_f){
notify_range_f = 0;
notify_range(&main_conf);
}
vTaskDelay(main_conf.refresh_delay / portTICK_PERIOD_MS); vTaskDelay(main_conf.refresh_delay / portTICK_PERIOD_MS);
} }