Create gpio_adc component - outsource readAdc()
remove duplicate code: function readAdc was used in multiple files, outsourced this to gpio component
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
idf_component_register(
|
||||
SRCS "gpio_evaluateSwitch.cpp"
|
||||
SRCS
|
||||
"gpio_evaluateSwitch.cpp"
|
||||
"gpio_adc.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
)
|
||||
|
||||
21
components/gpio/gpio_adc.cpp
Normal file
21
components/gpio/gpio_adc.cpp
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
9
components/gpio/gpio_adc.hpp
Normal file
9
components/gpio/gpio_adc.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
Reference in New Issue
Block a user