#include #include #include "BLE_UUID.h" #include "esp_attr.h" #include "esp_err.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/projdefs.h" #include "freertos/task.h" #include "freertos/portmacro.h" #include "host/ble_uuid.h" #include "nvs_flash.h" #include "sdkconfig.h" #include "BLEh.h" #include "simple_gatt_value_server.h" #include "BLE.h" #include "measure.h" #define TAG "Main" static uint32_t switching_frequency = CONFIG_SW_FREQ; 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){ case SWITCHING_FREQUENCY_CHR_ID: *value = &switching_frequency; *value_size = sizeof(switching_frequency); break; case DUTY_CYCLE_CHR_ID: *value = &duty_cycle; *value_size = sizeof(duty_cycle); break; } break; } } 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; switch(svc_ind){ case CONFIGURATION_SVC_ID: switch (chr_ind){ case SWITCHING_FREQUENCY_CHR_ID: ; rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL); switching_frequency = val; // TODO update running val break; case DUTY_CYCLE_CHR_ID: rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL); duty_cycle = val; // TODO update running val break; } break; } return rc; } void app_main(void){ init_adc(adc_pins); init_adc_cali(); adc_start(); 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); set_gatt_services(gatt_svcs, SVCS_NUM); initBLE("ALIM"); while(1){ fetch_measures(); get_measures_raw_mV(raw_mv_meas); get_measures_calc(calc_meas); 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); vTaskDelay(pdMS_TO_TICKS(CONFIG_UPD_DELAY)); } }