|
1 |
| -#ifndef ROBOT_CONTROL_SRC_DIGITALPIN_HPP_ |
2 |
| -#define ROBOT_CONTROL_SRC_DIGITALPIN_HPP_ |
| 1 | +#ifndef GPIOPIN_HPP_ |
| 2 | +#define GPIOPIN_HPP_ |
3 | 3 |
|
4 | 4 | #include "GpioInterface.hpp"
|
5 | 5 | #include <Arduino.h>
|
6 | 6 |
|
| 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 | + */ |
7 | 14 | template<std::uint8_t PIN>
|
8 |
| -class DigitalPin: public GpioInterface { |
| 15 | +class GpioPin: public GpioInterface |
| 16 | +{ |
9 | 17 | public:
|
10 | 18 | static constexpr std::uint8_t pin = PIN;
|
11 | 19 |
|
12 | 20 | 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 | 21 | {
|
30 | 22 | return digitalRead(pin);
|
31 | 23 | }
|
32 |
| - static void setModeS(const std::uint8_t mode) |
| 24 | + |
| 25 | + virtual void setMode(const std::uint8_t mode) override |
33 | 26 | {
|
34 | 27 | pinMode(pin, mode);
|
35 | 28 | }
|
36 |
| - static void writeS(const std::uint8_t value) |
| 29 | + |
| 30 | + virtual void write(const std::uint8_t value) override |
37 | 31 | {
|
38 | 32 | digitalWrite(pin, value);
|
39 | 33 | }
|
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 |
41 | 40 | {
|
42 | 41 | analogWrite(pin, dutyCycle);
|
43 | 42 | }
|
44 | 43 | };
|
45 | 44 |
|
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) |
51 | 50 |
|
| 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 | + */ |
52 | 60 | inline constexpr int digitalPinToInterruptWrapper(const int pinNumber)
|
53 | 61 | {
|
54 | 62 | return digitalPinToInterrupt(pinNumber);
|
55 | 63 | }
|
56 | 64 |
|
| 65 | +/* Undefine macro in order to enable function declaration with the same name. */ |
57 | 66 | #undef digitalPinToInterrupt
|
58 | 67 |
|
| 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 | + */ |
59 | 75 | inline constexpr int digitalPinToInterrupt(const int pinNumber)
|
60 | 76 | {
|
61 | 77 | return digitalPinToInterruptWrapper(pinNumber);
|
62 | 78 | }
|
63 | 79 |
|
| 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 | + */ |
64 | 96 | template<std::uint8_t PIN>
|
65 |
| -int constexpr digitalPinToInterrupt(const DigitalPin<PIN>& gpio) |
| 97 | +int constexpr digitalPinToInterrupt(const GpioPin<PIN>& gpio) |
66 | 98 | {
|
67 |
| - return digitalPinToInterruptWrapper(gpio.pin); |
| 99 | + return digitalPinToInterrupt(gpio.pin); |
68 | 100 | }
|
69 | 101 |
|
| 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 | + */ |
70 | 110 | 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) |
72 | 112 | {
|
73 | 113 | attachInterrupt(digitalPinToInterrupt(gpio.pin), interruptServiceRoutine, mode);
|
74 | 114 | }
|
75 | 115 |
|
| 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 */ |
76 | 129 |
|
77 |
| -#endif /* ROBOT_CONTROL_SRC_DIGITALPIN_HPP_ */ |
| 130 | +#endif /* GPIOPIN_HPP_ */ |
0 commit comments