IoAbstraction
IoAbstraction.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
4  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
5  */
6 
7 #ifndef _BASIC_IO_ABSTRACTION_H_
8 #define _BASIC_IO_ABSTRACTION_H_
9 
16 #include "PlatformDetermination.h"
17 #include "BasicIoAbstraction.h"
18 
19 #define SHIFT_REGISTER_OUTPUT_CUTOVER 32
20 
21 #if defined(IOA_USE_MBED) || defined(BUILD_FOR_PICO_CMAKE)
22 #if defined(IOA_USE_MBED)
23 #include <mbed.h>
24 #endif
25 enum ShiftBitOrder { MSBFIRST, LSBFIRST };
26 uint8_t shiftIn(pinid_t dataPin, pinid_t clockPin, ShiftBitOrder bitOrder);
27 #else
28 #include <Arduino.h>
29 #endif
30 
32  pinid_t clock;
33  pinid_t data;
34  pinid_t latch;
35  uint8_t numDevices;
36 
37  ShiftRegConfig(pinid_t clock, pinid_t data, pinid_t latch, uint8_t numDevices) : clock(clock), data(data), latch(latch), numDevices(numDevices) {}
38  ShiftRegConfig() : clock(IO_PIN_NOT_DEFINED), data(IO_PIN_NOT_DEFINED), latch(IO_PIN_NOT_DEFINED), numDevices(0) {}
39 };
40 
50 private:
51  uint32_t toWrite;
52  uint32_t lastRead;
53  bool needsWrite;
54 
55  uint8_t numOfDevicesRead;
56  pinid_t readDataPin;
57  pinid_t readLatchPin;
58  pinid_t readClockPin;
59 
60  uint8_t numOfDevicesWrite;
61  pinid_t writeDataPin;
62  pinid_t writeLatchPin;
63  pinid_t writeClockPin;
64  bool needsInit;
65 public:
72  ShiftRegisterIoAbstraction(pinid_t readClockPin, pinid_t readDataPin, pinid_t readLatchPin,
73  pinid_t writeClockPin, pinid_t writeDataPin, pinid_t writeLatchPin, uint8_t numRead, uint8_t numWrite);
74  ShiftRegisterIoAbstraction(const ShiftRegConfig& readConfig, const ShiftRegConfig& writeConfig);
75  ~ShiftRegisterIoAbstraction() override { }
76  void initDevice();
77 
78  void pinDirection(pinid_t pin, uint8_t mode) override;
79  void writeValue(pinid_t pin, uint8_t value) override;
80  uint8_t readValue(pinid_t pin) override;
84  void attachInterrupt(pinid_t, RawIntHandler, uint8_t) override {;}
85  bool runLoop() override;
86 
90  void writePort(pinid_t port, uint8_t portVal) override;
91 
95  uint8_t readPort(pinid_t port) override;
96 };
97 
99 private:
100  uint32_t lastRead;
101  uint8_t numOfDevicesRead;
102  pinid_t readDataPin;
103  pinid_t readLatchPin;
104  pinid_t readClockPin;
105  bool needsInit;
106 
107 public:
114  ShiftRegisterIoAbstraction165In(pinid_t readClockPin, pinid_t readDataPin, pinid_t readLatchPin, pinid_t numRead);
116  ~ShiftRegisterIoAbstraction165In() override = default;
117  void initDevice();
118 
120  void pinDirection(pinid_t pin, uint8_t mode) override { }
121 
122  uint8_t readValue(pinid_t pin) override;
123  bool runLoop() override;
124  uint8_t readPort(pinid_t port) override;
125 
126  //
127  // Features not implemented on this abstaction
128  //
129  void writePort(pinid_t port, uint8_t portVal) override { }
130  void writeValue(pinid_t pin, uint8_t value) override { }
131  void attachInterrupt(pinid_t, RawIntHandler, uint8_t) override { }
132 
133  uint8_t shiftInFor165() const;
134 };
135 
155 IoAbstractionRef inputOutputFromShiftRegister(uint8_t readClockPin, uint8_t readDataPin, uint8_t readLatchPin, uint8_t numOfReadDevices,
156  uint8_t writeClockPin, uint8_t writeDataPin, uint8_t writeLatchPin, uint8_t numOfWriteDevices);
157 
175 IoAbstractionRef inputOutputFromShiftRegister(uint8_t readClockPin, uint8_t readDataPin, uint8_t readLatchPin,
176  uint8_t writeClockPin, uint8_t writeDataPin, uint8_t writeLatchPin);
177 
184 IoAbstractionRef inputOnlyFromShiftRegister(uint8_t readClockPin, uint8_t dataPin, uint8_t latchPin, uint8_t numOfDevicesRead = 1);
185 
192 IoAbstractionRef outputOnlyFromShiftRegister(uint8_t writeClockPin, uint8_t writeDataPin, uint8_t writeLatchPin, uint8_t numOfDevicesWrite = 1);
193 
203 IoAbstractionRef inputFrom74HC165ShiftRegister(pinid_t readClkPin, pinid_t dataPin, pinid_t latchPin, pinid_t numOfDevices = 1);
204 
205 // this defines the number of IOExpanders can be put into a multi IO expander.
206 #ifndef MAX_ALLOWABLE_DELEGATES
207 #define MAX_ALLOWABLE_DELEGATES 8
208 #endif // defined MAX_ALLOWABLE_DELEGATES
209 
210 typedef uint8_t (*ExpanderOpFn)(IoAbstractionRef ref, uint8_t pin, uint8_t val);
211 
224 private:
225  IoAbstractionRef delegates[MAX_ALLOWABLE_DELEGATES];
226  pinid_t limits[MAX_ALLOWABLE_DELEGATES];
227  uint8_t numDelegates;
228 public:
229  explicit MultiIoAbstraction(pinid_t arduinoPinsNeeded = 100);
230  ~MultiIoAbstraction() override;
231  void addIoExpander(IoAbstractionRef expander, pinid_t numOfPinsNeeded);
232  void addIoDevice(BasicIoAbstraction& expander, pinid_t pinsNeeded) { addIoExpander(&expander, pinsNeeded);}
233 
240  void pinDirection(pinid_t pin, uint8_t mode) override;
241 
248  void writeValue(pinid_t pin, uint8_t value) override;
249 
254  uint8_t readValue(pinid_t pin) override;
255 
262  void writePort(pinid_t port, uint8_t portVal) override;
263 
269  uint8_t readPort(pinid_t port) override;
270 
278  void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override;
279 
283  bool runLoop() override;
284 private:
285  uint8_t doExpanderOp(pinid_t pin, uint8_t aVal, ExpanderOpFn fn);
286 };
287 
293 
298 inline MultiIoAbstractionRef multiIoExpander(pinid_t arduinoPinRange) { return new MultiIoAbstraction(arduinoPinRange); }
299 
305 inline void multiIoAddExpander(MultiIoAbstractionRef multiIo, IoAbstractionRef expander, pinid_t pinRange) { multiIo->addIoExpander(expander, pinRange); }
306 
307 #include "TaskManagerIO.h"
308 #include "SwitchInput.h"
309 #include "IoLogging.h"
310 
311 #endif
Provides the core IoAbstraction interface and Arduino implementation of that interface.
IoAbstractionRef inputOutputFromShiftRegister(uint8_t readClockPin, uint8_t readDataPin, uint8_t readLatchPin, uint8_t numOfReadDevices, uint8_t writeClockPin, uint8_t writeDataPin, uint8_t writeLatchPin, uint8_t numOfWriteDevices)
Definition: IoAbstraction.cpp:194
IoAbstractionRef inputOnlyFromShiftRegister(uint8_t readClockPin, uint8_t dataPin, uint8_t latchPin, uint8_t numOfDevicesRead=1)
Definition: IoAbstraction.cpp:190
IoAbstractionRef inputFrom74HC165ShiftRegister(pinid_t readClkPin, pinid_t dataPin, pinid_t latchPin, pinid_t numOfDevices=1)
Definition: IoAbstraction.cpp:286
IoAbstractionRef outputOnlyFromShiftRegister(uint8_t writeClockPin, uint8_t writeDataPin, uint8_t writeLatchPin, uint8_t numOfDevicesWrite=1)
Definition: IoAbstraction.cpp:186
void multiIoAddExpander(MultiIoAbstractionRef multiIo, IoAbstractionRef expander, pinid_t pinRange)
Definition: IoAbstraction.h:305
MultiIoAbstraction * MultiIoAbstractionRef
Definition: IoAbstraction.h:292
MultiIoAbstractionRef multiIoExpander(pinid_t arduinoPinRange)
Definition: IoAbstraction.h:298
Switch input provides the button and rotary encoder input capabilities provided by this library....
Definition: BasicIoAbstraction.h:38
Definition: IoAbstraction.h:223
void writeValue(pinid_t pin, uint8_t value) override
Definition: IoAbstraction.cpp:335
void writePort(pinid_t port, uint8_t portVal) override
Definition: IoAbstraction.cpp:349
uint8_t readPort(pinid_t port) override
Definition: IoAbstraction.cpp:356
bool runLoop() override
Definition: IoAbstraction.cpp:378
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstraction.cpp:328
uint8_t readValue(pinid_t pin) override
Definition: IoAbstraction.cpp:342
void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override
Definition: IoAbstraction.cpp:362
Definition: IoAbstraction.h:98
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstraction.h:120
uint8_t readValue(pinid_t pin) override
Definition: IoAbstraction.cpp:252
void attachInterrupt(pinid_t, RawIntHandler, uint8_t) override
Definition: IoAbstraction.h:131
void writeValue(pinid_t pin, uint8_t value) override
Definition: IoAbstraction.h:130
ShiftRegisterIoAbstraction165In(pinid_t readClockPin, pinid_t readDataPin, pinid_t readLatchPin, pinid_t numRead)
Definition: IoAbstraction.cpp:214
bool runLoop() override
Definition: IoAbstraction.cpp:258
uint8_t readPort(pinid_t port) override
Definition: IoAbstraction.cpp:235
void writePort(pinid_t port, uint8_t portVal) override
Definition: IoAbstraction.h:129
Definition: IoAbstraction.h:49
ShiftRegisterIoAbstraction(pinid_t readClockPin, pinid_t readDataPin, pinid_t readLatchPin, pinid_t writeClockPin, pinid_t writeDataPin, pinid_t writeLatchPin, uint8_t numRead, uint8_t numWrite)
Definition: IoAbstraction.cpp:64
void writeValue(pinid_t pin, uint8_t value) override
Definition: IoAbstraction.cpp:102
void writePort(pinid_t port, uint8_t portVal) override
Definition: IoAbstraction.cpp:110
uint8_t readValue(pinid_t pin) override
Definition: IoAbstraction.cpp:148
void attachInterrupt(pinid_t, RawIntHandler, uint8_t) override
Definition: IoAbstraction.h:84
uint8_t readPort(pinid_t port) override
Definition: IoAbstraction.cpp:133
bool runLoop() override
Definition: IoAbstraction.cpp:152
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstraction.cpp:98
Definition: IoAbstraction.h:31