Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit bcba139

Browse files
committed
Added GPIO interface and implementation for pin and MCP23017.
2 parents da76607 + da354ee commit bcba139

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

robot-control-src/DigitalPin.hpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#ifndef ROBOT_CONTROL_SRC_DIGITALPIN_HPP_
2+
#define ROBOT_CONTROL_SRC_DIGITALPIN_HPP_
3+
4+
#include "GpioInterface.hpp"
5+
#include <Arduino.h>
6+
7+
template<std::uint8_t PIN>
8+
class DigitalPin: public GpioInterface {
9+
public:
10+
static constexpr std::uint8_t pin = PIN;
11+
12+
virtual int read() override
13+
{
14+
return readS();
15+
}
16+
virtual void setMode(const std::uint8_t mode) override
17+
{
18+
setModeS(mode);
19+
}
20+
virtual void write(const std::uint8_t value) override
21+
{
22+
writeS(value);
23+
}
24+
void setDutyCycle(const int dutyCycle) const
25+
{
26+
setDutyCycleS(dutyCycle);
27+
}
28+
static int readS()
29+
{
30+
return digitalRead(pin);
31+
}
32+
static void setModeS(const std::uint8_t mode)
33+
{
34+
pinMode(pin, mode);
35+
}
36+
static void writeS(const std::uint8_t value)
37+
{
38+
digitalWrite(pin, value);
39+
}
40+
static void setDutyCycleS(const int dutyCycle)
41+
{
42+
analogWrite(pin, dutyCycle);
43+
}
44+
};
45+
46+
template<std::uint8_t PIN>
47+
void analogWrite(const DigitalPin<PIN>& gpio, const int dutyCycle)
48+
{
49+
analogWrite(gpio.pin, dutyCycle);
50+
}
51+
52+
inline constexpr int digitalPinToInterruptWrapper(const int pinNumber)
53+
{
54+
return digitalPinToInterrupt(pinNumber);
55+
}
56+
57+
#undef digitalPinToInterrupt
58+
59+
inline constexpr int digitalPinToInterrupt(const int pinNumber)
60+
{
61+
return digitalPinToInterruptWrapper(pinNumber);
62+
}
63+
64+
template<std::uint8_t PIN>
65+
int constexpr digitalPinToInterrupt(const DigitalPin<PIN>& gpio)
66+
{
67+
return digitalPinToInterruptWrapper(gpio.pin);
68+
}
69+
70+
template<std::uint8_t PIN>
71+
void attachInterrupt(const DigitalPin<PIN>& gpio, void (* const interruptServiceRoutine)(void), const int mode)
72+
{
73+
attachInterrupt(digitalPinToInterrupt(gpio.pin), interruptServiceRoutine, mode);
74+
}
75+
76+
77+
#endif /* ROBOT_CONTROL_SRC_DIGITALPIN_HPP_ */

robot-control-src/GpioInterface.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "GpioInterface.hpp"
2+
3+
void digitalWrite(GpioInterface& gpio, const std::uint8_t value)
4+
{
5+
gpio.write(value);
6+
}
7+
8+
int digitalRead(GpioInterface& gpio)
9+
{
10+
return gpio.read();
11+
}
12+
13+
void pinMode(GpioInterface& gpio, const std::uint8_t mode)
14+
{
15+
gpio.setMode(mode);
16+
}

robot-control-src/GpioInterface.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef GPIOINTERFACE_HPP_
2+
#define GPIOINTERFACE_HPP_
3+
4+
#include <cstdint>
5+
6+
class GpioInterface {
7+
public:
8+
virtual void write(const std::uint8_t value) = 0;
9+
virtual int read() = 0;
10+
virtual void setMode(const std::uint8_t mode) = 0;
11+
12+
virtual ~GpioInterface() {};
13+
};
14+
15+
void digitalWrite(GpioInterface& gpio, const std::uint8_t value);
16+
int digitalRead(GpioInterface& gpio);
17+
void pinMode(GpioInterface& gpio, const std::uint8_t mode);
18+
19+
#endif /* GPIOINTERFACE_HPP_ */

robot-control-src/MCP23017Pin.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "MCP23017Pin.hpp"
2+
#include <MCP23017.h>
3+
4+
MCP23017Pin::MCP23017Pin(MCP23017 &ioExpander, std::uint8_t const pin) :ioExpander(ioExpander), pin(pin) {
5+
}
6+
7+
MCP23017Pin::~MCP23017Pin() {
8+
}
9+
10+
int MCP23017Pin::read() {
11+
return ioExpander.digitalRead(pin);
12+
}
13+
14+
void MCP23017Pin::setMode(std::uint8_t const mode) {
15+
setMode(mode, false);
16+
}
17+
18+
void MCP23017Pin::setMode(std::uint8_t const mode, bool const inverted) {
19+
ioExpander.pinMode(pin, mode, inverted);
20+
}
21+
22+
void MCP23017Pin::write(std::uint8_t const value) {
23+
ioExpander.digitalWrite(pin, value);
24+
}

robot-control-src/MCP23017Pin.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef MCP23017PIN_HPP_
2+
#define MCP23017PIN_HPP_
3+
4+
#include "GpioInterface.hpp"
5+
6+
class MCP23017;
7+
8+
class MCP23017Pin: public GpioInterface {
9+
public:
10+
MCP23017Pin(MCP23017& ioExpander, const std::uint8_t pin);
11+
virtual ~MCP23017Pin();
12+
virtual int read() override;
13+
virtual void setMode(const std::uint8_t mode) override;
14+
void setMode(const std::uint8_t mode, const bool inverted);
15+
virtual void write(const std::uint8_t value) override;
16+
private:
17+
MCP23017& ioExpander;
18+
const std::uint8_t pin;
19+
};
20+
21+
#endif /* MCP23017PIN_HPP_ */

0 commit comments

Comments
 (0)