IoAbstraction
BasicIoAbstraction.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef _IO_ABSTRACTION_CORE_TYPES
7 #define _IO_ABSTRACTION_CORE_TYPES
8 
13 #include "PlatformDetermination.h"
14 #include <TaskManagerIO.h>
15 
16 #define IO_PIN_NOT_DEFINED 0xFF
17 
18 #if defined(IOA_USE_MBED)
19 # include "mbed/MbedDigitalIO.h"
20 #elif defined(ESP32) && defined(IOA_USE_ESP32_EXTRAS)
21 # include "esp32/ESP32DigitalIO.h"
22 #elif defined(BUILD_FOR_PICO_CMAKE)
23 # include "pico/PicoDigitalIO.h"
24 #else
25 # include <Arduino.h>
26 #endif //IOA_USE_MBED
27 
38 class BasicIoAbstraction : public InterruptAbstraction {
39 private:
40 #ifdef IOA_USE_MBED
41  BtreeList<uint32_t, GpioWrapper> pinCache;
42 
43  GpioWrapper *allocatePinIfNeedBe(uint8_t pinToAlloc);
44 #endif //IOA_USE_MBED
45 public:
46  virtual ~BasicIoAbstraction() = default;
47 
54  uint8_t digitalRead(pinid_t p) { return readValue(p); }
55 
63  void digitalWrite(pinid_t p, uint8_t v) { return writeValue(p, v); }
64 
70  uint8_t digitalReadS(pinid_t p) { runLoop(); return readValue(p); }
71 
80  void digitalWriteS(pinid_t p, uint8_t v) { writeValue(p, v); sync(); }
81 
88  void writePortS(pinid_t p, uint8_t v) { writePort(p, v); sync(); }
89 
96  uint8_t readPortS(pinid_t p) { sync(); return readPort(p); }
97 
104  void pinMode(pinid_t pin, uint8_t mode) { pinDirection(pin, mode); }
105 
112  bool sync() { return runLoop(); }
113 
119  virtual void pinDirection(pinid_t pin, uint8_t mode);
120 
126  virtual void writeValue(pinid_t pin, uint8_t value);
127 
132  virtual uint8_t readValue(pinid_t pin);
133 
141  virtual void attachInterrupt(pinid_t pin, RawIntHandler interruptHandler, uint8_t mode);
142 
147  virtual bool runLoop() { return true; }
148 
155  virtual void writePort(pinid_t pin, uint8_t portVal);
156 
163  virtual uint8_t readPort(pinid_t pin);
164 };
165 
171 
175 #ifdef IOA_USE_MBED
176 #define pgm_read_byte_near(x) (*(x))
177 #elif defined(IOA_USE_ARDUINO)
178 #define ioUsingArduino internalDigitalIo
179 #endif
180 
188 
189 extern BasicIoAbstraction internalIoAbstraction;
190 
195 #define internalDigitalDevice() internalIoAbstraction
196 
204 inline void ioDevicePinMode(IoAbstractionRef ioDev, pinid_t pin, uint8_t dir) { ioDev->pinDirection(pin, dir); }
205 
214 inline uint8_t ioDeviceDigitalRead(IoAbstractionRef ioDev, pinid_t pin) { return ioDev->readValue(pin); }
215 
225 inline void ioDeviceDigitalWrite(IoAbstractionRef ioDev, pinid_t pin, uint8_t val) { ioDev->writeValue(pin, (val)); }
226 
233 inline bool ioDeviceSync(IoAbstractionRef ioDev) { return ioDev->runLoop(); }
234 
245 inline void ioDeviceAttachInterrupt(IoAbstractionRef ioDev, pinid_t pin, RawIntHandler intHandler, uint8_t mode) {ioDev->attachInterrupt(pin, intHandler, mode) ;}
246 
255 inline uint8_t ioDeviceDigitalReadS(IoAbstractionRef ioDev, pinid_t pin) { ioDev->runLoop(); return ioDev->readValue(pin); }
256 
266 inline bool ioDeviceDigitalWriteS(IoAbstractionRef ioDev, pinid_t pin, uint8_t val) { ioDev->writeValue(pin, (val)); return ioDev->runLoop(); }
267 
280 inline bool ioDeviceDigitalWritePortS(IoAbstractionRef ioDev, pinid_t pinOnPort, uint8_t portVal) { ioDev->writePort(pinOnPort, portVal); return ioDev->runLoop(); }
281 
294 inline uint8_t ioDeviceDigitalReadPortS(IoAbstractionRef ioDev, pinid_t pinOnPort) { ioDev->runLoop(); return ioDev->readPort(pinOnPort); }
295 
307 inline void ioDeviceDigitalWritePort(IoAbstractionRef ioDev, pinid_t pinOnPort, uint8_t portVal) { ioDev->writePort(pinOnPort, portVal); }
308 
321 inline uint8_t ioDeviceDigitalReadPort(IoAbstractionRef ioDev, pinid_t pinOnPort) { return ioDev->readPort(pinOnPort); }
322 
323 #define asIoRef(x) (&(x))
324 
325 #endif // _IO_ABSTRACTION_CORE_TYPES
uint8_t ioDeviceDigitalReadPort(IoAbstractionRef ioDev, pinid_t pinOnPort)
Definition: BasicIoAbstraction.h:321
bool ioDeviceDigitalWriteS(IoAbstractionRef ioDev, pinid_t pin, uint8_t val)
Definition: BasicIoAbstraction.h:266
void ioDeviceDigitalWrite(IoAbstractionRef ioDev, pinid_t pin, uint8_t val)
Definition: BasicIoAbstraction.h:225
uint8_t ioDeviceDigitalRead(IoAbstractionRef ioDev, pinid_t pin)
Definition: BasicIoAbstraction.h:214
void ioDevicePinMode(IoAbstractionRef ioDev, pinid_t pin, uint8_t dir)
Definition: BasicIoAbstraction.h:204
IoAbstractionRef internalDigitalIo()
bool ioDeviceDigitalWritePortS(IoAbstractionRef ioDev, pinid_t pinOnPort, uint8_t portVal)
Definition: BasicIoAbstraction.h:280
bool ioDeviceSync(IoAbstractionRef ioDev)
Definition: BasicIoAbstraction.h:233
uint8_t ioDeviceDigitalReadPortS(IoAbstractionRef ioDev, pinid_t pinOnPort)
Definition: BasicIoAbstraction.h:294
BasicIoAbstraction * IoAbstractionRef
Definition: BasicIoAbstraction.h:170
void ioDeviceAttachInterrupt(IoAbstractionRef ioDev, pinid_t pin, RawIntHandler intHandler, uint8_t mode)
Definition: BasicIoAbstraction.h:245
void ioDeviceDigitalWritePort(IoAbstractionRef ioDev, pinid_t pinOnPort, uint8_t portVal)
Definition: BasicIoAbstraction.h:307
uint8_t ioDeviceDigitalReadS(IoAbstractionRef ioDev, pinid_t pin)
Definition: BasicIoAbstraction.h:255
Definition: BasicIoAbstraction.h:38
void writePortS(pinid_t p, uint8_t v)
Definition: BasicIoAbstraction.h:88
uint8_t digitalReadS(pinid_t p)
Definition: BasicIoAbstraction.h:70
virtual void pinDirection(pinid_t pin, uint8_t mode)
virtual void writePort(pinid_t pin, uint8_t portVal)
void pinMode(pinid_t pin, uint8_t mode)
Definition: BasicIoAbstraction.h:104
void digitalWriteS(pinid_t p, uint8_t v)
Definition: BasicIoAbstraction.h:80
virtual uint8_t readValue(pinid_t pin)
void digitalWrite(pinid_t p, uint8_t v)
Definition: BasicIoAbstraction.h:63
uint8_t digitalRead(pinid_t p)
Definition: BasicIoAbstraction.h:54
virtual void writeValue(pinid_t pin, uint8_t value)
uint8_t readPortS(pinid_t p)
Definition: BasicIoAbstraction.h:96
virtual void attachInterrupt(pinid_t pin, RawIntHandler interruptHandler, uint8_t mode)
virtual bool runLoop()
Definition: BasicIoAbstraction.h:147
bool sync()
Definition: BasicIoAbstraction.h:112
virtual uint8_t readPort(pinid_t pin)