FEATURE: simple class for calibrating and calculating an input
This commit is contained in:
parent
ac2deb5001
commit
d8159087d9
67
Middlewares/floatpump/Inc/InputChannel.h
Normal file
67
Middlewares/floatpump/Inc/InputChannel.h
Normal file
@ -0,0 +1,67 @@
|
||||
//
|
||||
// Created by robtor on 10.01.23.
|
||||
//
|
||||
|
||||
#ifndef FLOATPUMP_INPUTCHANNEL_H
|
||||
#define FLOATPUMP_INPUTCHANNEL_H
|
||||
|
||||
#include <cstdint>
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
namespace floatpump {
|
||||
namespace io {
|
||||
|
||||
class InputChannel {
|
||||
|
||||
public:
|
||||
InputChannel(ADC_HandleTypeDef *adc, GPIO_TypeDef *gpio, uint16_t port) : m_adc(adc), m_gpio(gpio), m_gpio_port(port) {};
|
||||
|
||||
void poll() {
|
||||
//Enable measuring power
|
||||
HAL_GPIO_WritePin(m_gpio, m_gpio_port, GPIO_PIN_SET);
|
||||
HAL_Delay(m_cooldown);
|
||||
|
||||
uint16_t average = 0;
|
||||
|
||||
for(int i = 0; i < m_avg_size; i++) {
|
||||
HAL_Delay(m_avg_delay);
|
||||
HAL_ADC_Start(m_adc);
|
||||
HAL_ADC_PollForConversion(m_adc, m_avg_delay);
|
||||
average += HAL_ADC_GetValue(m_adc);
|
||||
}
|
||||
|
||||
m_raw = average / m_avg_size;
|
||||
|
||||
m_percent = ((m_raw - m_lowcalib)*100)/(m_highcalib-m_lowcalib);
|
||||
}
|
||||
|
||||
uint16_t getRaw() {
|
||||
return m_raw;
|
||||
}
|
||||
|
||||
int8_t getPercent() {
|
||||
return m_percent;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
const uint16_t m_avg_size = 50;
|
||||
const uint16_t m_avg_delay = 10;
|
||||
const uint16_t m_cooldown = 500;
|
||||
|
||||
uint16_t m_lowcalib = 0;
|
||||
uint16_t m_highcalib = 65535;
|
||||
|
||||
uint16_t m_raw = 0;
|
||||
int8_t m_percent = 0;
|
||||
|
||||
ADC_HandleTypeDef *m_adc;
|
||||
GPIO_TypeDef *m_gpio;
|
||||
uint16_t m_gpio_port;
|
||||
|
||||
};
|
||||
|
||||
} // floatpump
|
||||
} // io
|
||||
|
||||
#endif //FLOATPUMP_INPUTCHANNEL_H
|
11
Middlewares/floatpump/Src/InputChannel.cpp
Normal file
11
Middlewares/floatpump/Src/InputChannel.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by robtor on 10.01.23.
|
||||
//
|
||||
|
||||
#include "InputChannel.h"
|
||||
|
||||
namespace floatpump {
|
||||
namespace io {
|
||||
|
||||
} // floatpump
|
||||
} // io
|
Loading…
Reference in New Issue
Block a user