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:
jonny_ji7
2022-09-12 11:18:06 +02:00
parent d2d85952df
commit 1f53fabd19
7 changed files with 38 additions and 53 deletions

View File

@@ -1,4 +1,6 @@
idf_component_register(
SRCS "gpio_evaluateSwitch.cpp"
SRCS
"gpio_evaluateSwitch.cpp"
"gpio_adc.cpp"
INCLUDE_DIRS "."
)

View 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;
}
}

View 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);