diff --git a/Middlewares/floatpump/Inc/InputChannel.h b/Middlewares/floatpump/Inc/InputChannel.h new file mode 100644 index 0000000..8d967c2 --- /dev/null +++ b/Middlewares/floatpump/Inc/InputChannel.h @@ -0,0 +1,67 @@ +// +// Created by robtor on 10.01.23. +// + +#ifndef FLOATPUMP_INPUTCHANNEL_H +#define FLOATPUMP_INPUTCHANNEL_H + +#include +#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 diff --git a/Middlewares/floatpump/Src/InputChannel.cpp b/Middlewares/floatpump/Src/InputChannel.cpp new file mode 100644 index 0000000..970692f --- /dev/null +++ b/Middlewares/floatpump/Src/InputChannel.cpp @@ -0,0 +1,11 @@ +// +// Created by robtor on 10.01.23. +// + +#include "InputChannel.h" + +namespace floatpump { + namespace io { + + } // floatpump +} // io \ No newline at end of file