From 490d8b578b21a655c2f91a6a44ea3a30752e7ebb Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 16 Jun 2023 13:23:33 +0200 Subject: [PATCH] range notifications --- main/ble.c | 12 +++++++++++- main/ble.h | 3 ++- main/power_profiler.c | 13 +++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/main/ble.c b/main/ble.c index 4e5e7c8..6b5c3cc 100644 --- a/main/ble.c +++ b/main/ble.c @@ -42,6 +42,7 @@ static uint16_t range_handle[RANGE_CHRS_NUM]; 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 ev_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] = { .uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_RANGE_CHAR), .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, .val_handle = &range_handle[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; case BLE_GAP_EVENT_SUBSCRIBE: ESP_LOGI(TAG, "Subscribe %d", event->subscribe.attr_handle); + for(int i = 0; i < INPUTS_NUM; i++){ if(event->subscribe.attr_handle == cs_handle[i]) cs_notify_state[i] = event->subscribe.cur_notify; if(event->subscribe.attr_handle == ev_handle[i]) 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; } @@ -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)); +} diff --git a/main/ble.h b/main/ble.h index 9439f51..0e223b6 100644 --- a/main/ble.h +++ b/main/ble.h @@ -15,4 +15,5 @@ struct ble_context { }; void initBLE(configuration* conf, measurements* meas, measurements_ctxt* meas_ctxt); -void notify_update(measurements* meas); \ No newline at end of file +void notify_update(measurements* meas); +void notify_range(configuration* conf); \ No newline at end of file diff --git a/main/power_profiler.c b/main/power_profiler.c index 2e72234..213bc7f 100644 --- a/main/power_profiler.c +++ b/main/power_profiler.c @@ -97,12 +97,15 @@ configuration main_conf = { .ranges_num = R_NUM, }; +uint8_t notify_range_f = 0; + static void IRAM_ATTR overrange_handler(void* arg) { if(!main_conf.auto_range) return; activate_range(ranges, 0, R_NUM - 1); main_conf.range = 0; + notify_range_f = 1; } void app_main(void){ @@ -188,9 +191,11 @@ void app_main(void){ if(meas_volts[UNDERRANGE_SRC] < UNDERRANGE_MV){ underrange_counter++; 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++; - 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; } } @@ -199,6 +204,10 @@ void app_main(void){ } notify_update(&meas); + if(notify_range_f){ + notify_range_f = 0; + notify_range(&main_conf); + } vTaskDelay(main_conf.refresh_delay / portTICK_PERIOD_MS); }