From d9087b12743a14aac9f0813c5d000a9fb604082b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Mon, 28 Nov 2022 03:23:53 +0100 Subject: [PATCH] push --- main/Peripherals.cpp | 106 ++++++++++++++++++------------------------- main/Peripherals.h | 73 +++++++++++++++++++++++++---- main/main.cpp | 16 ------- 3 files changed, 108 insertions(+), 87 deletions(-) diff --git a/main/Peripherals.cpp b/main/Peripherals.cpp index 9b2cf62..ae38db4 100644 --- a/main/Peripherals.cpp +++ b/main/Peripherals.cpp @@ -1,13 +1,5 @@ #include "Peripherals.h" -gpio_config_t btn_config = { - .pin_bit_mask = (uint64_t) 0, - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = GPIO_PULLDOWN_ENABLE, - .intr_type = GPIO_INTR_ANYEDGE -}; - Potentiometer::Potentiometer(pot_update_callback_t update_ntfy) : Potentiometer(ADC_UNIT_1, ADC_CHANNEL_1, 0, update_ntfy) {} @@ -18,64 +10,18 @@ Potentiometer::Potentiometer(const adc_unit_t unit, const adc_channel_t channel, offset_fact(offset_fact), prev_raw(0) { - static bool adc_first_init = init_adc(unit, channel); -} - -bool Potentiometer::init_adc(const adc_unit_t unit, const adc_channel_t channel) { - check_efuse(); - //Configure ADC - if (unit == ADC_UNIT_1) { - adc1_config_width(width); - adc1_config_channel_atten((adc1_channel_t)channel, atten); - } else { - adc2_config_channel_atten((adc2_channel_t)channel, atten); - } - - //Characterize ADC - esp_adc_cal_characteristics_t* adc_chars = (esp_adc_cal_characteristics_t*) calloc(1, sizeof(esp_adc_cal_characteristics_t)); - esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, VREF, adc_chars); - print_char_val_type(val_type); - return true; -} - -void Potentiometer::check_efuse() { - //Check if TP is burned into eFuse - if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { - printf("eFuse Two Point: Supported\n"); - } else { - printf("eFuse Two Point: NOT supported\n"); - } - //Check Vref is burned into eFuse - if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) { - printf("eFuse Vref: Supported\n"); - } else { - printf("eFuse Vref: NOT supported\n"); - } - if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { - printf("eFuse Two Point: Supported\n"); - } else { - printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n"); - } -} - -void Potentiometer::print_char_val_type(esp_adc_cal_value_t val_type) { - if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { - printf("Characterized using Two Point Value\n"); - } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { - printf("Characterized using eFuse Vref\n"); - } else { - printf("Characterized using Default Vref\n"); - } + init_adc(unit, channel); } int Potentiometer::get_raw() { - uint32_t sum = 0; + int sum = 0; for (int i = 0; i < SAMPLES; i++) { if (adc_unit == ADC_UNIT_1) { sum += adc1_get_raw((adc1_channel_t)adc_channel); - }else { + } + else { int raw; - adc2_get_raw((adc2_channel_t)adc_channel, width, &raw); + adc2_get_raw((adc2_channel_t)adc_channel, WIDTH, &raw); sum += raw; } } @@ -88,7 +34,7 @@ float Potentiometer::get_percent() { } void Potentiometer::update() { - uint32_t current = get_raw(); + int current = get_raw(); if (on_change != NULL) { //Check threshold and maybe send update to function if (abs((int) current - prev_raw) > DIFF_THRESHOLD || ((current % 4095 == 0) && (prev_raw % 4095 != 0))) { @@ -103,13 +49,18 @@ void Potentiometer::update() { prev_raw = current; } } - } Button::Button(gpio_num_t gpio_pin, btn_update_callback_t update_ntfy) : on_change(update_ntfy), gpio_pin(gpio_pin) { - gpio_config_t config = btn_config; + gpio_config_t config = { + .pin_bit_mask = (uint64_t) 0, + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_ENABLE, + .intr_type = GPIO_INTR_ANYEDGE + }; config.pin_bit_mask = (1 << gpio_pin); gpio_config(&config); } @@ -130,3 +81,34 @@ void Button::update() { (*on_change)(); } } + +BatteryMonitor::BatteryMonitor(const adc_unit_t unit, const adc_channel_t channel, float zero) + : adc_unit(unit), + adc_channel(channel), + zero(zero) { + + init_adc(unit, channel); +} + +int BatteryMonitor::get_raw() { + int sum = 0; + for (int i = 0; i < SAMPLES; i++) { + if (adc_unit == ADC_UNIT_1) { + sum += adc1_get_raw((adc1_channel_t)adc_channel); + } + else { + int raw; + adc2_get_raw((adc2_channel_t)adc_channel, WIDTH, &raw); + sum += raw; + } + } + return sum / SAMPLES; +} + +float BatteryMonitor::get_percentage() { + int current = get_raw(); + // Do some zeroing here + return (((float)current / (4095)) * 100); +} + + diff --git a/main/Peripherals.h b/main/Peripherals.h index 5670f86..52c791d 100644 --- a/main/Peripherals.h +++ b/main/Peripherals.h @@ -8,18 +8,69 @@ #define VREF 1100 #define DIFF_THRESHOLD 10 -#define atten ADC_ATTEN_DB_11 -#define width ADC_WIDTH_BIT_12 +#define ATTEN ADC_ATTEN_DB_11 +#define WIDTH ADC_WIDTH_BIT_12 typedef void(*pot_update_callback_t)(float); typedef void(*btn_update_callback_t)(void); +static bool check_efuse() { + //Check if TP is burned into eFuse + if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { + printf("eFuse Two Point: Supported\n"); + } else { + printf("eFuse Two Point: NOT supported\n"); + } + //Check Vref is burned into eFuse + if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) { + printf("eFuse Vref: Supported\n"); + } else { + printf("eFuse Vref: NOT supported\n"); + } + if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { + printf("eFuse Two Point: Supported\n"); + } else { + printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n"); + } + printf("REMOVETHISFLAG"); + return true; +} +static bool print_char_val_type(esp_adc_cal_value_t val_type) { + if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { + printf("Characterized using Two Point Value\n"); + } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { + printf("Characterized using eFuse Vref\n"); + } else { + printf("Characterized using Default Vref\n"); + } + printf("REMOVETHISFLAG"); + return true; +} +static bool init_adc(const adc_unit_t unit, const adc_channel_t channel) { + static bool some_check = check_efuse(); + + //Configure ADC + if (unit == ADC_UNIT_1) { + adc1_config_width(WIDTH); + adc1_config_channel_atten((adc1_channel_t)channel, ATTEN); + } else { + adc2_config_channel_atten((adc2_channel_t)channel, ATTEN); + } + + //Characterize ADC + static esp_adc_cal_characteristics_t* adc_chars = (esp_adc_cal_characteristics_t*) calloc(1, sizeof(esp_adc_cal_characteristics_t)); + static esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, ATTEN, WIDTH, VREF, adc_chars); + static bool some_print = print_char_val_type(val_type); + return true; +} + + class Potentiometer { public: Potentiometer(pot_update_callback_t = NULL); - Potentiometer(const adc_unit_t, const adc_channel_t, float offset_fact, pot_update_callback_t = NULL); + Potentiometer(const adc_unit_t, const adc_channel_t, float, pot_update_callback_t = NULL); int get_raw(); void update(); float get_percent(); @@ -30,11 +81,6 @@ private: pot_update_callback_t on_change; float offset_fact; int prev_raw; - - - static bool init_adc(const adc_unit_t, const adc_channel_t); - static void check_efuse(); - static void print_char_val_type(esp_adc_cal_value_t); }; class Button { @@ -49,5 +95,14 @@ private: gpio_num_t gpio_pin; }; - +class BatteryMonitor { +public: + BatteryMonitor(const adc_unit_t, const adc_channel_t, float); + int get_raw(); + float get_percentage(); +private: + adc_unit_t adc_unit; + adc_channel_t adc_channel; + float zero; +}; #endif \ No newline at end of file diff --git a/main/main.cpp b/main/main.cpp index 39212c1..c695ae7 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -258,22 +258,6 @@ void update_display(al_event_cb_t event, al_event_cb_param_t *param) } } -bool input_timer_cb(void *args) -{ - int8_t msg = INPUT; - BaseType_t high_task_awoken = pdFALSE; - xQueueSendFromISR(event_queue, &msg, &high_task_awoken); - return high_task_awoken == pdTRUE; -} - -bool display_timer_cb(void *args) -{ - int8_t msg = DISPLAY; - BaseType_t high_task_awoken = pdFALSE; - xQueueSendFromISR(event_queue, &msg, &high_task_awoken); - return high_task_awoken == pdTRUE; -} - void pot_volume_update_cb(float val) { printf("Volume changed %.2f\n", val); -- 2.30.2