diff --git a/components/BLEh b/components/BLEh index a15c1ca..01063a0 160000 --- a/components/BLEh +++ b/components/BLEh @@ -1 +1 @@ -Subproject commit a15c1ca68e8bc601ae82f5b9184a5742e741ccee +Subproject commit 01063a014cc00c4ac1569f812cad0311ba758306 diff --git a/main/Alim1000W.c b/main/Alim1000W.c index 1478725..e2ab70b 100644 --- a/main/Alim1000W.c +++ b/main/Alim1000W.c @@ -1,8 +1,7 @@ #include #include -#include "BLE_UUID.h" -#include "esp_attr.h" +#include "BLEh.h" #include "esp_err.h" #include "esp_log.h" @@ -16,9 +15,6 @@ #include "sdkconfig.h" -#include "BLEh.h" -#include "simple_gatt_value_server.h" - #include "BLE.h" #include "measure.h" @@ -30,7 +26,6 @@ static uint32_t duty_cycle = 50000000; static uint8_t adc_pins[] = {CONFIG_OUT_VOLT_PIN, CONFIG_OUT_CURR_PIN, CONFIG_PHASE1_CURR_PIN, CONFIG_PHASE2_CURR_PIN, CONFIG_PHASE3_CURR_PIN}; void on_char_read_handler(int svc_ind, int chr_ind, void** value, size_t* value_size){ - ESP_LOGI(TAG, "char read handler"); switch(svc_ind){ case CONFIGURATION_SVC_ID: switch (chr_ind){ @@ -48,7 +43,6 @@ void on_char_read_handler(int svc_ind, int chr_ind, void** value, size_t* value_ } int on_char_write_handler(int svc_ind, int chr_ind, struct os_mbuf* os){ - ESP_LOGI(TAG, "char write handler"); uint32_t val; int rc = 0; @@ -82,58 +76,23 @@ void app_main(void){ nvs_flash_init(); uint32_t raw_mv_meas[CONFIG_ADC_IN_PIN_NUM]; - - struct char_pres_format mv_char_pres_format = { - .format = FORMAT_UINT32, - .unit = VOLTS_UNIT_UUID, - .exponent = -3, - .namespc = 1, - .descrH = NSP_DESC_MAIN & 0xff, - .descrL = (NSP_DESC_MAIN>>8) & 0xff, - }; - - gatt_value_server_handle_t raw_mv_serve_handle = simple_gatt_identical_values_server(raw_mv_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, &mv_char_pres_format, &gatt_svcs[RAW_VALUES_SVC_ID], RAW_VALUES_SVC_ID, BLE_UUID16_DECLARE(VOLTAGE_CHAR)); - uint32_t calc_meas[CONFIG_ADC_IN_PIN_NUM]; - struct char_pres_format mA_char_pres_format = { - .format = FORMAT_UINT32, - .unit = AMPERE_UNIT_UUID, - .exponent = -3, - .namespc = 1, - .descrH = NSP_DESC_MAIN & 0xff, - .descrL = (NSP_DESC_MAIN>>8) & 0xff, - }; - - struct char_pres_format* calc_meas_formats[CONFIG_ADC_IN_PIN_NUM]; - - ble_uuid_t* calc_meas_char_uuid[CONFIG_ADC_IN_PIN_NUM]; - - for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){ - switch(adc_pins[i]){ - case CONFIG_OUT_VOLT_PIN: - calc_meas_formats[i] = &mv_char_pres_format; - calc_meas_char_uuid[i] = BLE_UUID16_DECLARE(VOLTAGE_CHAR); - break; - default: - calc_meas_formats[i] = &mA_char_pres_format; - calc_meas_char_uuid[i] = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_CHAR); - } - } - - gatt_value_server_handle_t calc_meas_serve_handle = simple_gatt_value_server(calc_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, calc_meas_formats, &gatt_svcs[CALC_VALUES_SVC_ID], CALC_VALUES_SVC_ID, calc_meas_char_uuid); - - set_on_char_read_handler(on_char_read_handler); - set_on_char_write_handler(on_char_write_handler); + gatt_value_server_handle_t raw_mv_serve_handle, calc_meas_serve_handle; + init_ble_app_services(raw_mv_meas, calc_meas, adc_pins, &raw_mv_serve_handle, &calc_meas_serve_handle); set_gatt_services(gatt_svcs, SVCS_NUM); - + + set_on_char_read_handler(on_char_read_handler); + set_on_char_write_handler(on_char_write_handler); + initBLE("ALIM"); while(1){ fetch_measures(); get_measures_raw_mV(raw_mv_meas); get_measures_calc(calc_meas); + ESP_LOGI(TAG, "meas %lu %lu %lu %lu %lu", raw_mv_meas[0], raw_mv_meas[1], raw_mv_meas[2], raw_mv_meas[3], raw_mv_meas[4]); simple_gatt_value_server_notify(raw_mv_serve_handle, CONFIG_ADC_IN_PIN_NUM); simple_gatt_value_server_notify(calc_meas_serve_handle, CONFIG_ADC_IN_PIN_NUM); diff --git a/main/BLE.c b/main/BLE.c index b7b850e..e9d674e 100644 --- a/main/BLE.c +++ b/main/BLE.c @@ -1,11 +1,20 @@ -#include "BLE.h" -#include "BLE_UUID.h" +#include + +#include "esp_log.h" -#include "BLEh.h" #include "host/ble_att.h" #include "host/ble_gatt.h" #include "host/ble_uuid.h" +#include "sdkconfig.h" + +#include "BLE_UUID.h" +#include "BLEh.h" +#include "simple_gatt_value_server.h" + +#include "BLE.h" + +#define TAG "BLE" struct char_pres_format frequency_char_pres_format = { .format = FORMAT_UINT32, @@ -82,4 +91,48 @@ struct ble_gatt_svc_def gatt_svcs[] = { }, }, { 0 }, -}; \ No newline at end of file +}; + +static struct char_pres_format mv_char_pres_format = { + .format = FORMAT_UINT32, + .unit = VOLTS_UNIT_UUID, + .exponent = -3, + .namespc = 1, + .descrH = NSP_DESC_MAIN & 0xff, + .descrL = (NSP_DESC_MAIN>>8) & 0xff, +}; + +static struct char_pres_format mA_char_pres_format = { + .format = FORMAT_UINT32, + .unit = AMPERE_UNIT_UUID, + .exponent = -3, + .namespc = 1, + .descrH = NSP_DESC_MAIN & 0xff, + .descrL = (NSP_DESC_MAIN>>8) & 0xff, +}; + +ble_uuid_t* voltage_char_uuid = BLE_UUID16_DECLARE(VOLTAGE_CHAR); +ble_uuid_t* current_char_uuid = BLE_UUID16_DECLARE(ELECTRIC_CURRENT_CHAR); + +void init_ble_app_services(uint32_t raw_mv_meas[], uint32_t calc_meas[], uint8_t adc_pins[], gatt_value_server_handle_t* raw_mv_serve_handle, gatt_value_server_handle_t* calc_meas_serve_handle){ + + *raw_mv_serve_handle = simple_gatt_identical_values_server(raw_mv_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, &mv_char_pres_format, &gatt_svcs[RAW_VALUES_SVC_ID], RAW_VALUES_SVC_ID, voltage_char_uuid); + + struct char_pres_format* calc_meas_formats[CONFIG_ADC_IN_PIN_NUM]; + + ble_uuid_t* calc_meas_char_uuid[CONFIG_ADC_IN_PIN_NUM]; + + for(int i = 0; i < CONFIG_ADC_IN_PIN_NUM; i++){ + switch(adc_pins[i]){ + case CONFIG_OUT_VOLT_PIN: + calc_meas_formats[i] = &mv_char_pres_format; + calc_meas_char_uuid[i] = voltage_char_uuid; + break; + default: + calc_meas_formats[i] = &mA_char_pres_format; + calc_meas_char_uuid[i] = current_char_uuid; + } + } + + *calc_meas_serve_handle = simple_gatt_value_server(calc_meas, sizeof(uint32_t), CONFIG_ADC_IN_PIN_NUM, calc_meas_formats, &gatt_svcs[CALC_VALUES_SVC_ID], CALC_VALUES_SVC_ID, calc_meas_char_uuid); +} \ No newline at end of file diff --git a/main/BLE.h b/main/BLE.h index 6cf9891..a42f461 100644 --- a/main/BLE.h +++ b/main/BLE.h @@ -1,10 +1,13 @@ #pragma once -#include "BLE_UUID.h" #include "host/ble_gatt.h" +#include "simple_gatt_value_server.h" + enum {RAW_VALUES_SVC_ID, CALC_VALUES_SVC_ID, CONFIGURATION_SVC_ID, SVCS_NUM}; enum {SWITCHING_FREQUENCY_CHR_ID, DUTY_CYCLE_CHR_ID, CONFIGURATION_SVC_CHR_NUM}; extern struct ble_gatt_svc_def gatt_svcs[SVCS_NUM + 1]; + +void init_ble_app_services(uint32_t raw_mv_meas[], uint32_t calc_meas[], uint8_t adc_pins[], gatt_value_server_handle_t* raw_mv_serve_handle, gatt_value_server_handle_t* calc_meas_serve_handle);