From 5420b39dd2edd7b9026e3c1cb7cd780bb46c32d4 Mon Sep 17 00:00:00 2001 From: Robin Dietzel Date: Tue, 10 Jan 2023 14:04:49 +0100 Subject: [PATCH] FEATURE: simple input channel support --- Core/Src/main.cpp | 19 +++++++++---- Middlewares/floatpump/Inc/GPIChannel.h | 36 ++++++++++++++++++++++++ Middlewares/floatpump/Src/GPIChannel.cpp | 29 +++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 Middlewares/floatpump/Inc/GPIChannel.h create mode 100644 Middlewares/floatpump/Src/GPIChannel.cpp diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index dad9c04..b043252 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -16,6 +16,7 @@ #include "Menu_Controller.h" #include "PressureChannel.h" #include "RelayChannel.h" +#include "GPIChannel.h" #define SLAVE_ADDRESS_LCD 0x4e @@ -122,17 +123,22 @@ int main(void) { cdd.linkConfig(&curCooldown); Menu_Entry entry6(cdd, "Cooldown"); - mainmenu.addEntry(entry6); + Menu_Entry_Type_Checkable tr(false); + bool inputTest; + tr.linkConfig(&inputTest); + Menu_Entry entry7(tr, "Input"); + + mainmenu.addEntry(entry7); Menu_Controller controller(&mainmenu, display); - HAL_GPIO_WritePin(MPWR0_GPIO_Port, MPWR0_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(MPWR2_GPIO_Port, MPWR2_Pin, GPIO_PIN_SET); - - floatpump::io::PressureChannel channel(&hadc1, MPWR0_GPIO_Port, MPWR0_Pin); floatpump::io::RelayChannel relay(OCHAN0_GPIO_Port, OCHAN0_Pin, false); + floatpump::io::GPIChannel swimmer(GPI0_GPIO_Port, GPI0_Pin, false); + + + relay.setCooldown(10000); static int old_pos = 0; @@ -172,11 +178,14 @@ int main(void) { prescaler++; + + swimmer.poll(); if(prescaler % 100 == 0) channel.poll(); current = channel.getRaw(); cur_perc = channel.getPercent(); + inputTest = swimmer.getStateBool(); /*if(cur_perc > 50) { relay.switchRelay(floatpump::io::RelayChannel::state::ON); diff --git a/Middlewares/floatpump/Inc/GPIChannel.h b/Middlewares/floatpump/Inc/GPIChannel.h new file mode 100644 index 0000000..51e2e0a --- /dev/null +++ b/Middlewares/floatpump/Inc/GPIChannel.h @@ -0,0 +1,36 @@ +// +// Created by robtor on 10.01.23. +// + +#ifndef FLOATPUMP_GPICHANNEL_H +#define FLOATPUMP_GPICHANNEL_H + +#include "stm32f4xx_hal.h" + +namespace floatpump::io { + + class GPIChannel { + public: + enum state { + ON, OFF + }; + + GPIChannel(GPIO_TypeDef *gpio, uint16_t pin, bool inverted = false); + + void poll(); + + [[nodiscard]] state getState() const; + + [[nodiscard]] bool getStateBool() const; + + private: + GPIO_TypeDef *m_gpio; + uint16_t m_gpio_pin; + bool m_inverted; + + state m_state = state::OFF; + }; + + } // io + +#endif //FLOATPUMP_GPICHANNEL_H diff --git a/Middlewares/floatpump/Src/GPIChannel.cpp b/Middlewares/floatpump/Src/GPIChannel.cpp new file mode 100644 index 0000000..cdb9240 --- /dev/null +++ b/Middlewares/floatpump/Src/GPIChannel.cpp @@ -0,0 +1,29 @@ +// +// Created by robtor on 10.01.23. +// + +#include "GPIChannel.h" + +namespace floatpump { + namespace io { + GPIChannel::GPIChannel(GPIO_TypeDef *gpio, uint16_t pin, bool inverted) : m_gpio(gpio), m_gpio_pin(pin), m_inverted(inverted) { + + } + + void GPIChannel::poll() { + if(HAL_GPIO_ReadPin(m_gpio, m_gpio_pin) == GPIO_PIN_SET) { + m_state = (m_inverted)? state::OFF : state::ON; + } else { + m_state = (m_inverted)? state::ON : state::OFF; + } + } + + GPIChannel::state GPIChannel::getState() const { + return m_state; + } + + bool GPIChannel::getStateBool() const { + return (m_state == state::ON); + } + } // floatpump +} // io \ No newline at end of file