This repository has been archived on 2024-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
zamek-hswro/ArduinoGpio.cpp

53 lines
1.0 KiB
C++
Raw Permalink Normal View History

2017-12-17 11:31:13 +00:00
#include "ArduinoGpio.h"
#include <Arduino.h>
ArduinoGpio::ArduinoGpio(int pin, bool initialValue, IGpio::Mode initialMode):
pin(pin)
{
/* There is no protection to prevent from creation of multiple instances
* of single pin, as Arduino does not support C++ exceptions.
* Only one parent object may own instance of a pin.
*/
write(initialValue);
setMode((initialMode));
}
ArduinoGpio::~ArduinoGpio()
{
setMode(Mode::Input);
write(false);
}
bool ArduinoGpio::read()
{
return (digitalRead(pin) != 0);
}
void ArduinoGpio::write(bool newValue)
{
digitalWrite(pin, newValue ? HIGH : LOW);
}
IGpio::Mode ArduinoGpio::getMode()
{
return mode;
}
void ArduinoGpio::setMode(IGpio::Mode newMode)
{
mode = newMode;
pinMode(pin, toAdruinoMode(newMode));
}
int ArduinoGpio::toAdruinoMode(IGpio::Mode mode)
{
switch(mode) {
case Mode::Input:
return INPUT;
case Mode::InputPullup:
return INPUT_PULLUP;
case Mode::Output:
return OUTPUT;
}
}