alim_1000w_soft/main/Alim1000W.c
2023-06-26 17:34:13 +02:00

103 lines
2.6 KiB
C

#include <stdint.h>
#include <stdio.h>
#include "BLEh.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 "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){
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){
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];
uint32_t calc_meas[CONFIG_ADC_IN_PIN_NUM];
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);
vTaskDelay(pdMS_TO_TICKS(CONFIG_UPD_DELAY));
}
}