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

Commit 125e65e

Browse files
committed
Document GpioInterface and GpioPin.
1 parent 578f8ce commit 125e65e

File tree

2 files changed

+139
-33
lines changed

2 files changed

+139
-33
lines changed

robot-control-src/GpioInterface.hpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,70 @@
33

44
#include <cstdint>
55

6-
class GpioInterface {
6+
/**
7+
* Interface for digital input and output pins.
8+
*
9+
* Implementations may provide access to the pins of the microprocessor or
10+
* external pins of IO-expanders.
11+
*
12+
* Parameters and return values are compatible to the Arduino library.
13+
*/
14+
class GpioInterface
15+
{
716
public:
17+
/**
18+
* Sets output value of the pin.
19+
* @param value may be HIGH or LOW (see Arduino.h)
20+
* @pre Configure the mode to OUTPUT using setMode().
21+
*/
822
virtual void write(const std::uint8_t value) = 0;
23+
24+
/**
25+
* Reads the value of the pin.
26+
* @return Either HIGH or LOW (see Arduino.h).
27+
* @pre Configure the mode to INPUT or INPUT_PULLUP using setMode().
28+
*/
929
virtual int read() = 0;
30+
31+
/**
32+
* Configures the pin to be either input or output.
33+
* @param mode INPUT, OUTPUT, or INPUT_PULLUP (see Arduino.h).
34+
*/
1035
virtual void setMode(const std::uint8_t mode) = 0;
1136

12-
virtual ~GpioInterface() {};
37+
virtual ~GpioInterface() = default;
1338
};
1439

40+
/** @name Digital I/O function overload for GpioInterface objects.
41+
*
42+
* These functions provide an interface to GpioInterface objects compatible
43+
* to the Arduino library digital I/O functions.
44+
*/
45+
/** @{ */ /* Start group for digital I/O */
46+
47+
/**
48+
* Sets output value of the pin.
49+
* @param gpio digital output pin
50+
* @param value may be HIGH or LOW (see Arduino.h)
51+
* @pre Configure the mode to OUTPUT using pinMode().
52+
*/
1553
void digitalWrite(GpioInterface& gpio, const std::uint8_t value);
54+
55+
/**
56+
* Reads the value of the pin.
57+
* @param gpio digital input pin
58+
* @return Either HIGH or LOW (see Arduino.h).
59+
* @pre Configure the mode to INPUT or INPUT_PULLUP using pinMode().
60+
*/
1661
int digitalRead(GpioInterface& gpio);
62+
63+
/**
64+
* Configures the pin to be either input or output.
65+
* @param gpio digital pin
66+
* @param mode INPUT, OUTPUT, or INPUT_PULLUP (see Arduino.h).
67+
*/
1768
void pinMode(GpioInterface& gpio, const std::uint8_t mode);
1869

70+
/** @} */ /* End group for digital I/O */
71+
1972
#endif /* GPIOINTERFACE_HPP_ */

robot-control-src/GpioPin.hpp

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,130 @@
1-
#ifndef ROBOT_CONTROL_SRC_DIGITALPIN_HPP_
2-
#define ROBOT_CONTROL_SRC_DIGITALPIN_HPP_
1+
#ifndef GPIOPIN_HPP_
2+
#define GPIOPIN_HPP_
33

44
#include "GpioInterface.hpp"
55
#include <Arduino.h>
66

7+
/**
8+
* Extension of GpioInterface for the processors general purpose input/output pins.
9+
*
10+
* Adds method to set PWM duty cycle.
11+
*
12+
* @tparam PIN is the Arduino pin number
13+
*/
714
template<std::uint8_t PIN>
8-
class DigitalPin: public GpioInterface {
15+
class GpioPin: public GpioInterface
16+
{
917
public:
1018
static constexpr std::uint8_t pin = PIN;
1119

1220
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()
2921
{
3022
return digitalRead(pin);
3123
}
32-
static void setModeS(const std::uint8_t mode)
24+
25+
virtual void setMode(const std::uint8_t mode) override
3326
{
3427
pinMode(pin, mode);
3528
}
36-
static void writeS(const std::uint8_t value)
29+
30+
virtual void write(const std::uint8_t value) override
3731
{
3832
digitalWrite(pin, value);
3933
}
40-
static void setDutyCycleS(const int dutyCycle)
34+
35+
/**
36+
* Sets PWM duty cycle.
37+
* @param dutyCycle between 0 and 255
38+
*/
39+
void setDutyCycle(const int dutyCycle) const
4140
{
4241
analogWrite(pin, dutyCycle);
4342
}
4443
};
4544

46-
template<std::uint8_t PIN>
47-
void analogWrite(const DigitalPin<PIN>& gpio, const int dutyCycle)
48-
{
49-
analogWrite(gpio.pin, dutyCycle);
50-
}
45+
/* If digitalPinToInterrupt is defined as C preprocessor macro it needs to be
46+
* undefined. digitalPinToInterrupt must be overloaded for GpioPin. This is
47+
* not possible for macros.
48+
*/
49+
#if defined(digitalPinToInterrupt)
5150

51+
/**
52+
* Wrapper for digitalPinToInterrupt.
53+
*
54+
* Encapsulates the digitalPinToInterrupt functionality before it is undefined.
55+
* In contrast to the C preprocessor macro this function can be overridden.
56+
*
57+
* @param pinNumber is the Arduino pin number
58+
* @return interrupt number
59+
*/
5260
inline constexpr int digitalPinToInterruptWrapper(const int pinNumber)
5361
{
5462
return digitalPinToInterrupt(pinNumber);
5563
}
5664

65+
/* Undefine macro in order to enable function declaration with the same name. */
5766
#undef digitalPinToInterrupt
5867

68+
/**
69+
* Getter for the interrupt number for a digital input pin.
70+
*
71+
* This is an adapter to digitalPinToInterruptWrapper().
72+
* @param pinNumber is the Arduino pin number
73+
* @return interrupt number
74+
*/
5975
inline constexpr int digitalPinToInterrupt(const int pinNumber)
6076
{
6177
return digitalPinToInterruptWrapper(pinNumber);
6278
}
6379

80+
#endif /* defined(digitalPinToInterrupt) */
81+
82+
/** @name Function overload for GpioPin objects.
83+
*
84+
* These functions provide an interface to GpioPin objects compatible
85+
* to the Arduino library functions.
86+
*/
87+
/** @{ */ /* Start group for function overloads */
88+
89+
/**
90+
* Getter for the interrupt number for a digital input pin.
91+
*
92+
* This is an overload to digitalPinToInterrupt().
93+
* @tparam PIN is the Arduino pin number
94+
* @return interrupt number
95+
*/
6496
template<std::uint8_t PIN>
65-
int constexpr digitalPinToInterrupt(const DigitalPin<PIN>& gpio)
97+
int constexpr digitalPinToInterrupt(const GpioPin<PIN>& gpio)
6698
{
67-
return digitalPinToInterruptWrapper(gpio.pin);
99+
return digitalPinToInterrupt(gpio.pin);
68100
}
69101

102+
/**
103+
* Attach function to be called on external interrupt.
104+
*
105+
* @param gpio is the input pin to attach interrupt to
106+
* @param interruptServiceRoutine is the function to be called when interrupt occurs
107+
* @param mode may be LOW, CHANGE, RISING, FALLING (see Arduino.h)
108+
* @tparam PIN is the Arduino pin number
109+
*/
70110
template<std::uint8_t PIN>
71-
void attachInterrupt(const DigitalPin<PIN>& gpio, void (* const interruptServiceRoutine)(void), const int mode)
111+
void attachInterrupt(const GpioPin<PIN>& gpio, void (* const interruptServiceRoutine)(void), const int mode)
72112
{
73113
attachInterrupt(digitalPinToInterrupt(gpio.pin), interruptServiceRoutine, mode);
74114
}
75115

116+
/**
117+
* Sets PWM duty cycle to pin.
118+
* @tparam PIN is the Arduino pin number
119+
* @param gpio output pin to set duty cycle to.
120+
* @param dutyCycle between 0 and 255
121+
*/
122+
template<std::uint8_t PIN>
123+
void analogWrite(const GpioPin<PIN>& gpio, const int dutyCycle)
124+
{
125+
analogWrite(gpio.pin, dutyCycle);
126+
}
127+
128+
/** @} */ /* End group for function overloads */
76129

77-
#endif /* ROBOT_CONTROL_SRC_DIGITALPIN_HPP_ */
130+
#endif /* GPIOPIN_HPP_ */

0 commit comments

Comments
 (0)