Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ArduinoGpio.hxx
Go to the documentation of this file.
1
35#ifndef _DRIVERS_ARDUINOGPIO_HXX_
36#define _DRIVERS_ARDUINOGPIO_HXX_
37
38#include "os/Gpio.hxx"
39#include "GpioWrapper.hxx"
40#if defined(ESP32)
41#include <esp32-hal.h>
42#include <driver/gpio.h>
43#else
44#include <Arduino.h>
45#endif
46
54template <int PIN_NUM>
56{
57public:
59 static void set_output()
60 {
61 pinMode(PIN_NUM, OUTPUT);
62 }
63
65 static void set_input()
66 {
67 pinMode(PIN_NUM, INPUT);
68 }
69
71 static void set_pullup_on()
72 {
73 digitalWrite(PIN_NUM, HIGH);
74 }
75
77 static void set_pullup_off()
78 {
79 digitalWrite(PIN_NUM, LOW);
80 }
81
83 static void set_on()
84 {
85 digitalWrite(PIN_NUM, HIGH);
86 }
87
89 static void set_off()
90 {
91 digitalWrite(PIN_NUM, LOW);
92 }
93
95 static bool get()
96 {
97 return digitalRead(PIN_NUM);
98 }
99
101 static void set(bool value)
102 {
103 if (value)
104 {
105 set_on();
106 }
107 else
108 {
109 set_off();
110 }
111 }
112
114 static void toggle()
115 {
116 set(!get());
117 }
118
120 static bool is_output()
121 {
122#if defined(ESP32)
123 if(digitalPinIsValid(PIN_NUM) && digitalPinCanOutput(PIN_NUM))
124 {
125 // pins 32 and below use the first GPIO controller
126 if(PIN_NUM < 32)
127 {
128 return GPIO.enable_w1ts & ((uint32_t)1 << (PIN_NUM & 31));
129 }
130 else
131 {
132 return GPIO.enable1_w1ts.val & ((uint32_t)1 << (PIN_NUM & 31));
133 }
134 }
135#endif
136 return false;
137 }
138
141 static constexpr const Gpio *instance()
142 {
144 }
145};
146
147template <class Base, bool SAFE_VALUE, bool INVERT = false>
148struct GpioOutputPin : public Base
149{
150public:
152 static void hw_init()
153 {
154 Base::set(SAFE_VALUE);
155 Base::set_output();
156 Base::set(SAFE_VALUE);
157 }
159 static void hw_set_to_safe()
160 {
161 Base::set(SAFE_VALUE);
162 }
165 static void set(bool value)
166 {
167 if (INVERT)
168 {
169 Base::set(!value);
170 }
171 else
172 {
173 Base::set(value);
174 }
175 }
176};
177
181template <class Defs>
182struct GpioOutputSafeLow : public GpioOutputPin<Defs, false>
183{
184};
185
190template <class Defs>
191struct GpioOutputSafeLowInvert : public GpioOutputPin<Defs, false, true>
192{
193};
194
198template <class Defs>
199struct GpioOutputSafeHigh : public GpioOutputPin<Defs, true>
200{
201};
202
207template <class Defs>
208struct GpioOutputSafeHighInvert : public GpioOutputPin<Defs, true, true>
209{
210};
211
216template <class Base, bool PUEN> struct GpioInputPar : public Base
217{
218public:
220 static void hw_init()
221 {
222 Base::set_input();
223 if (PUEN)
224 {
225 Base::set_pullup_on();
226 }
227 else
228 {
229 Base::set_pullup_off();
230 }
231 }
233 static void hw_set_to_safe()
234 {
235 hw_init();
236 }
237};
238
242template <class Defs> struct GpioInputNP : public GpioInputPar<Defs, false>
243{
244};
245
249template <class Defs> struct GpioInputPU : public GpioInputPar<Defs, true>
250{
251};
252
269#define GPIO_PIN(NAME, BaseClass, NUM) \
270 typedef BaseClass<ArduinoStaticGpio<NUM>> NAME##_Pin
271
272#endif // _DRIVERS_ARDUINOGPIO_HXX_
Defines a GPIO output pin.
static constexpr const Gpio * instance()
static void set(bool value)
Set output pin level.
static bool is_output()
static void set_on()
Sets output to HIGH.
static void set_input()
Sets pin to input.
static void set_output()
Sets pin to output.
static void set_off()
Sets output to LOW.
static bool get()
static void set_pullup_off()
Turns off pullup.
static void set_pullup_on()
Turns on pullup.
static void toggle()
Toggles output pin value.
Creates an implementation of an os-independent Gpio object from a hardware-specific static Gpio struc...
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
Defines a GPIO input pin.
Defines a GPIO input pin with pull-up.
Parametric GPIO input class.
static void hw_init()
Initializes the hardware pin.
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
Parametric GPIO output class.
static void hw_init()
Initializes the hardware pin.
static void set(bool value)
Sets the output pinm.
static void hw_set_to_safe()
Sets the hardware pin to a safe value.
Defines a GPIO output pin, initialized to be an output pin with high level.
Defines a GPIO output pin, initialized to be an output pin with high level.
Defines a GPIO output pin, initialized to be an output pin with low level.
Defines a GPIO output pin, initialized to be an output pin with low level.