From 1f53fabd1989ae450145a626216376c5d696ffc9 Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Mon, 12 Sep 2022 11:18:06 +0200 Subject: [PATCH] Create gpio_adc component - outsource readAdc() remove duplicate code: function readAdc was used in multiple files, outsourced this to gpio component --- components/gpio/CMakeLists.txt | 4 +++- components/gpio/gpio_adc.cpp | 21 +++++++++++++++++++++ components/gpio/gpio_adc.hpp | 9 +++++++++ main/control.cpp | 25 ++----------------------- main/control.hpp | 1 + main/switchesAnalog.cpp | 25 +------------------------ main/switchesAnalog.hpp | 6 +----- 7 files changed, 38 insertions(+), 53 deletions(-) create mode 100644 components/gpio/gpio_adc.cpp create mode 100644 components/gpio/gpio_adc.hpp diff --git a/components/gpio/CMakeLists.txt b/components/gpio/CMakeLists.txt index 386d539..129e877 100644 --- a/components/gpio/CMakeLists.txt +++ b/components/gpio/CMakeLists.txt @@ -1,4 +1,6 @@ idf_component_register( - SRCS "gpio_evaluateSwitch.cpp" + SRCS + "gpio_evaluateSwitch.cpp" + "gpio_adc.cpp" INCLUDE_DIRS "." ) diff --git a/components/gpio/gpio_adc.cpp b/components/gpio/gpio_adc.cpp new file mode 100644 index 0000000..2e2580f --- /dev/null +++ b/components/gpio/gpio_adc.cpp @@ -0,0 +1,21 @@ +#include "gpio_adc.hpp" + + +//============================= +//========= readAdc =========== +//============================= +//function for multisampling an anlog input +int gpio_readAdc(adc1_channel_t adc_channel, bool inverted) { + //make multiple measurements + int adc_reading = 0; + for (int i = 0; i < 32; i++) { + adc_reading += adc1_get_raw(adc_channel); + } + adc_reading = adc_reading / 32; + //return original or inverted result + if (inverted) { + return 4095 - adc_reading; + } else { + return adc_reading; + } +} diff --git a/components/gpio/gpio_adc.hpp b/components/gpio/gpio_adc.hpp new file mode 100644 index 0000000..57c54d7 --- /dev/null +++ b/components/gpio/gpio_adc.hpp @@ -0,0 +1,9 @@ +#pragma once +#include +#include "driver/adc.h" + +//function for multisampling an anlog input +//measures 30 times and returns average +//if invertion is used currently 11bit resolution is assumed (subtracts from 4095) +//TODO: rework this function to be more universal +int gpio_readAdc(adc1_channel_t adc_channel, bool inverted = false); diff --git a/main/control.cpp b/main/control.cpp index 2a69869..06b8a9b 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -23,27 +23,6 @@ QueueHandle_t init_encoder(rotary_encoder_info_t * info){ } -//============================= -//========= readAdc =========== -//============================= -//function for multisampling an anlog input -int readAdc(adc1_channel_t adc_channel, bool inverted = false) { - //make multiple measurements - int adc_reading = 0; - for (int i = 0; i < 16; i++) { - adc_reading += adc1_get_raw(adc_channel); - } - adc_reading = adc_reading / 16; - //return original or inverted result - if (inverted) { - return 4095 - adc_reading; - } else { - return adc_reading; - } -} - - - //==================== @@ -218,7 +197,7 @@ void task_control(void *pvParameter) //set target length to poti position when SET switch is pressed if (SW_SET.state == true) { //read adc - potiRead = readAdc(ADC_CHANNEL_POTI); //0-4095 + potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095 //scale to target length range int lengthTargetNew = (float)potiRead / 4095 * 30000; //apply hysteresis and round to whole meters //TODO optimize this @@ -326,7 +305,7 @@ void task_control(void *pvParameter) case MANUAL: //manually control motor via preset buttons + poti //read poti value - potiRead = readAdc(ADC_CHANNEL_POTI); //0-4095 + potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095 //scale poti to speed levels 0-3 uint8_t level = round( (float)potiRead / 4095 * 3 ); //exit manual mode if preset2 released diff --git a/main/control.hpp b/main/control.hpp index 921a021..16f7724 100644 --- a/main/control.hpp +++ b/main/control.hpp @@ -18,6 +18,7 @@ extern "C" #include "config.hpp" #include "gpio_evaluateSwitch.hpp" +#include "gpio_adc.hpp" #include "buzzer.hpp" #include "vfd.hpp" #include "display.hpp" diff --git a/main/switchesAnalog.cpp b/main/switchesAnalog.cpp index 6ab1fb5..d9add49 100644 --- a/main/switchesAnalog.cpp +++ b/main/switchesAnalog.cpp @@ -4,29 +4,6 @@ -//============================= -//========= readAdc =========== -//============================= -//TODO add this to gpio library - used multiple times in this project -//function for multisampling an anlog input -int readAdc(adc1_channel_t adc_channel, bool inverted = false) { - //make multiple measurements - int adc_reading = 0; - for (int i = 0; i < 16; i++) { - adc_reading += adc1_get_raw(adc_channel); - } - adc_reading = adc_reading / 16; - //return original or inverted result - if (inverted) { - return 4095 - adc_reading; - } else { - return adc_reading; - } -} - - - - //===================== //===== Variables ===== @@ -66,7 +43,7 @@ const int lookup_voltages[] = { //handle demuxing of 4 switches from 1 adc (has to be run repeatedly) void handle(){ //read current voltage - adcValue = readAdc(ADC_CHANNEL_BUTTONS); + adcValue = gpio_readAdc(ADC_CHANNEL_4SW_TO_ANALOG); ESP_LOGI(TAG, "voltage read: %d", adcValue); //find closest match in lookup table diff --git a/main/switchesAnalog.hpp b/main/switchesAnalog.hpp index 84bf0cb..8919952 100644 --- a/main/switchesAnalog.hpp +++ b/main/switchesAnalog.hpp @@ -6,14 +6,10 @@ extern "C" #include "esp_log.h" #include "driver/adc.h" #include - } #include "config.hpp" - - -#define GPIO_BUTTONS GPIO_NUM_34 -#define ADC_CHANNEL_BUTTONS ADC1_CHANNEL_6 //gpio 34 +#include "gpio_adc.hpp" //get current state of certain switch