Modified GpioPin to offer PinState reading

This commit is contained in:
Dario 2023-05-16 22:19:08 +01:00
parent 38bdbbe526
commit 4eb1053ffd
4 changed files with 23 additions and 3 deletions

View File

@ -37,6 +37,13 @@ namespace sta
* @param state Output state * @param state Output state
*/ */
virtual void setState(GpioPinState state) = 0; virtual void setState(GpioPinState state) = 0;
/**
* @brief Get pin input state.
*
* @param state Input state
*/
virtual GpioPinState getState() = 0;
}; };

View File

@ -31,6 +31,8 @@ namespace sta
void setState(GpioPinState state) override; void setState(GpioPinState state) override;
GpioPinState getState() override;
static RaspiGpioPin * DUMMY_GPIO; static RaspiGpioPin * DUMMY_GPIO;
private: private:

View File

@ -6,7 +6,7 @@
namespace sta namespace sta
{ {
RaspiGpioPin::RaspiGpioPin(uint8_t pin, GpioMode mode) : pin_{pin}, mode_{mode} RaspiGpioPin::RaspiGpioPin(uint8_t pin, GpioMode mode) : pin_{ pin }, mode_{ mode }
{ {
pinMode(pin, mode == GpioMode::GPIO_INPUT ? INPUT : OUTPUT); pinMode(pin, mode == GpioMode::GPIO_INPUT ? INPUT : OUTPUT);
} }
@ -16,11 +16,22 @@ namespace sta
digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH); digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH);
} }
class DummyGpioPin : public RaspiGpioPin { GpioPinState RaspiGpioPin::getState()
{
return digitalRead(pin_) == LOW ? GpioPinState::GPIO_LOW : GpioPinState::GPIO_HIGH;
}
class DummyGpioPin : public RaspiGpioPin
{
public: public:
DummyGpioPin() : RaspiGpioPin { 0, GpioMode::GPIO_INPUT } {} DummyGpioPin() : RaspiGpioPin { 0, GpioMode::GPIO_INPUT } {}
void setState(GpioPinState state) override {} void setState(GpioPinState state) override {}
GpioPinState getState() override {
// This should really never be called since the code might rely on the returned value to be accurate.
STA_UNREACHABLE();
}
}; };
static DummyGpioPin dummyGpio; static DummyGpioPin dummyGpio;

View File

@ -133,7 +133,7 @@ namespace sta
if (result == -1) { if (result == -1) {
printf("Sending failed with error '%s'! \n", strerror(errno)); printf("Sending failed with error '%s'! \n", strerror(errno));
} }
STA_DEBUG_IOCTL_SEND(result); STA_DEBUG_IOCTL_SEND(result);
} }