IoAbstraction
IoAbstractionWire.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 
12 #ifndef _IOABSTRACTION_IOABSTRACTIONWIRE_H_
13 #define _IOABSTRACTION_IOABSTRACTIONWIRE_H_
14 
15 //
16 // An addition mode for devices that support LED controlled output, where the output is a controlled current source
17 //
18 #define LED_CURRENT_OUTPUT 0x99
19 #define AW9523_LED_OUTPUT LED_CURRENT_OUTPUT
20 
21 #include "PlatformDeterminationWire.h"
22 #include "IoAbstraction.h"
24 
32 public:
33  enum { NEEDS_WRITE_FLAG, PINS_CONFIGURED_READ_FLAG, PCF8575_16BIT_FLAG, INVERTED_LOGIC };
34 private:
35  WireType wireImpl;
36  uint8_t address;
37  uint8_t lastRead[2];
38  uint8_t toWrite[2];
39  uint8_t flags;
40  uint8_t interruptPin;
41 public:
50  PCF8574IoAbstraction(uint8_t addr, uint8_t interruptPin, WireType wireInstance = nullptr, bool mode16bit = false, bool invertedLogic = false);
51  virtual ~PCF8574IoAbstraction() { }
52 
54  void overrideReadFlag() { bitWrite(flags, PINS_CONFIGURED_READ_FLAG, true); }
55 
60  void pinDirection(pinid_t pin, uint8_t mode) override;
61 
65  void writeValue(pinid_t pin, uint8_t value) override;
66 
70  uint8_t readValue(pinid_t pin) override;
71 
75  void writePort(pinid_t pin, uint8_t port) override;
76 
80  uint8_t readPort(pinid_t pin) override;
81 
87  void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override;
88 
92  bool runLoop() override;
93 };
94 
96 protected:
97  uint16_t lastRead;
98  uint16_t toWrite;
99  uint8_t flags;
100  virtual void initDevice()=0;
101 public:
103  ~Standard16BitDevice() override = default;
104 
105  void writeValue(pinid_t pin, uint8_t value) override;
106  uint8_t readValue(pinid_t pin) override;
107  void writePort(pinid_t pin, uint8_t port) override;
108  uint8_t readPort(pinid_t pin) override;
109  void clearChangeFlags();
110  void setReadPort(int port);
111  bool isReadPortSet(int port) const;
112  bool isWritePortSet(int port) const;
113  bool isInitNeeded() const;
114  void markInitialised();
115 };
116 
122  NOT_ENABLED = 0, ACTIVE_HIGH_OPEN = 0b110, ACTIVE_LOW_OPEN = 0b100, ACTIVE_HIGH = 0b010, ACTIVE_LOW = 0b000
123 };
124 
131 private:
132  WireType wireImpl;
133  uint8_t address;
134  pinid_t intPinA;
135  pinid_t intPinB;
136  uint8_t intMode;
137 public:
143  MCP23017IoAbstraction(uint8_t address, Mcp23xInterruptMode intMode, pinid_t intPinA, pinid_t intPinB, WireType wireImpl = nullptr);
148  explicit MCP23017IoAbstraction(uint8_t address, WireType wireImpl = nullptr);
149 
155  MCP23017IoAbstraction(uint8_t address, Mcp23xInterruptMode intMode, pinid_t intPinA, WireType wireImpl = nullptr);
156 
157 
158  ~MCP23017IoAbstraction() override = default;
159 
165  void pinDirection(pinid_t pin, uint8_t mode) override;
166 
171  void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override;
172 
176  bool runLoop() override;
177 
188  void setInvertInputPin(pinid_t pin, bool shouldInvert);
189 
196  void resetDevice(int resetPin);
197 
198 private:
199  void initDevice() override;
200 };
201 
209 private:
210  WireType wireImpl;
211  uint8_t i2cAddress;
212  pinid_t interruptPin;
213 public:
214  enum AW9523CurrentControl: uint8_t { FULL_CURRENT = 0, CURRENT_THREE_QUARTER = 1, CURRENT_HALF = 2, CURRENT_QUARTER = 3 };
215 
225  explicit AW9523IoAbstraction(uint8_t addr, pinid_t intPin = IO_PIN_NOT_DEFINED, WireType wirePtr = nullptr);
226  ~AW9523IoAbstraction() override = default;
227 
232  uint8_t deviceId();
233 
234 
242  void pinDirection(pinid_t pin, uint8_t mode) override;
243 
248  void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override;
249 
253  bool runLoop() override;
254 
261  void setPinLedCurrent(pinid_t pin, uint8_t pwr);
262 
270  void writeGlobalControl(bool pushPullP0, AW9523CurrentControl maxCurrentMode = FULL_CURRENT);
271 
275  void softwareReset();
276 private:
277  void initDevice() override;
278 };
279 
286 private:
287  AW9523IoAbstraction& theAbstraction;
288 public:
289  explicit AW9523AnalogAbstraction(AW9523IoAbstraction &abs): theAbstraction(abs) {}
290 
291  int getMaximumRange(AnalogDirection direction, pinid_t pin) override { return 255; }
292 
293  int getBitDepth(AnalogDirection direction, pinid_t pin) override { return 8; }
294 
295  void initPin(pinid_t pin, AnalogDirection direction) override;
296 
297  unsigned int getCurrentValue(pinid_t pin) override { return -1; }
298 
299  float getCurrentFloat(pinid_t pin) override { return NAN; }
300 
301  void setCurrentValue(pinid_t pin, unsigned int newValue) override { theAbstraction.setPinLedCurrent(pin, newValue); }
302 
303  void setCurrentFloat(pinid_t pin, float newValue) override { theAbstraction.setPinLedCurrent(pin, (uint8_t)(newValue * 255.0F)); }
304 };
305 
306 //
307 // These definitions help with accessing the various registers, and provide default values and layouts for the MPR121
308 // You can use many of them with the read/write register functions if you need to directly access the chips registers.
309 // Only a subset of the available operations are directly handled within IoAbstraction directly, but this gives you
310 // access to the rest.
311 //
312 #define MPR121_FIRST_GPIO 4
313 #define MPR121_TOTAL_PINS 13
314 #define MPR121_PROXIMITY_PIN 12
315 
316 #define MPR121_TOUCH_STATUS_16 0x00
317 #define MPR121_OOR_STATUS_16 0x02
318 #define MPR121_ELECTRODE_DATA_2ND 0x04
319 #define MPR121_BASELINE_DATA_3RD 0x1E
320 #define MPR121_MHD_RISING 0x2B
321 #define MPR121_NHD_RISING 0x2C
322 #define MPR121_NCL_RISING 0x2D
323 #define MPR121_FDL_RISING 0x2E
324 #define MPR121_MHD_FALLING 0x2F
325 #define MPR121_NHD_FALLING 0x30
326 #define MPR121_NCL_FALLING 0x31
327 #define MPR121_FDL_FALLING 0x32
328 #define MPR121_NHD_TOUCHED 0x33
329 #define MPR121_NCL_TOUCHED 0x34
330 #define MPR121_FDL_TOUCHED 0x35
331 #define MPR121_TCH_REL_THRESHOLD 0x41
332 #define MPR121_DEBOUNCE_REG 0x5B
333 #define MPR121_AFE_CONFIG_1 0x5C
334 #define MPR121_AFE_CONFIG_2 0x5D
335 #define MPR121_ELECTRODE_CONFIG 0x5E
336 #define MPR121_ELECTRODE_CURRENT_0 0x5F
337 #define MPR121_CHARGE_TIME_0 0x6C
338 #define MPR121_GPIO_CONTROL_0 0x73
339 #define MPR121_GPIO_CONTROL_1 0x74
340 #define MPR121_GPIO_DATA 0x75
341 #define MPR121_GPIO_DIRECTION_0 0x76
342 #define MPR121_GPIO_ENABLE 0x77
343 #define MPR121_GPIO_DATA_SET 0x78
344 #define MPR121_GPIO_DATA_CLEAR 0x79
345 #define MPR121_GPIO_DATA_TOGGLE 0x7A
346 #define MPR121_AUTO_CONFIG_0 0x7B
347 #define MPR121_AUTO_CONFIG_1 0x7C
348 #define MPR121_UPPER_LIMIT 0x7D
349 #define MPR121_LOWER_LIMIT 0x7E
350 #define MPR121_TARGET_LIMIT 0x7F
351 #define MPR121_SOFT_RESET 0x80
352 #define MPR121_SOFT_RESET_VALUE 0x63
353 #define MPR121_LED_PWM_0 0x81
354 #define MPR121_LED_PWM_1 0x82
355 #define MPR121_LED_PWM_2 0x83
356 #define MPR121_LED_PWM_3 0x84
357 
361 enum MPR121ConfigType: uint8_t { MPR121_MANUAL_CONFIG, MPR121_AUTO_CONFIG };
362 
374 private:
375  WireType wireImpl;
376  uint8_t i2cAddress;
377  pinid_t interruptPin;
378  pinid_t maximumTouchPin = 0;
379 public:
389  explicit MPR121IoAbstraction(uint8_t addr, pinid_t intPin = IO_PIN_NOT_DEFINED, WireType wirePtr = nullptr);
390  ~MPR121IoAbstraction() override = default;
391 
402  void begin(int maxTouchPin, MPR121ConfigType configType, uint8_t configReg1 = 0x10, uint8_t configReg2 = 0x20);
403 
411  void pinDirection(pinid_t pin, uint8_t mode) override;
412 
417  void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override;
418 
422  bool runLoop() override;
423 
435  void electrodeSettingsForPin(pinid_t pin, uint8_t touchThreshold, uint8_t releaseThreshold, uint8_t current=0, uint8_t chargeTime=0);
436 
440  uint16_t getOutOfRangeRegister();
441 
449  void configureDebounce(uint8_t debounceTouch, uint8_t debounceRelease);
450 
457  uint16_t read2ndFilteredData(uint8_t pin);
458 
465  void setPinLedCurrent(pinid_t pin, uint8_t pwr);
466 
470  void softwareReset();
471  void initDevice() override { /* ignored */ }
472 
473  void writeReg8(uint8_t reg, uint8_t data);
474  void writeReg16(uint8_t reg, uint16_t data);
475  uint8_t readReg8(uint8_t reg);
476  uint16_t readReg16(uint8_t reg);
477 protected:
478  void enableTouchSupportOnPin(pinid_t pin);
479 };
480 
487 private:
488  MPR121IoAbstraction& theAbstraction;
489 public:
490  explicit MPR121AnalogAbstraction(MPR121IoAbstraction &abs): theAbstraction(abs) {}
491 
492  int getMaximumRange(AnalogDirection direction, pinid_t pin) override { return 255; }
493 
494  int getBitDepth(AnalogDirection direction, pinid_t pin) override { return 8; }
495 
496  void initPin(pinid_t pin, AnalogDirection direction) override;
497 
498  unsigned int getCurrentValue(pinid_t pin) override { return theAbstraction.read2ndFilteredData(pin) >> 2; }
499 
500  float getCurrentFloat(pinid_t pin) override { return theAbstraction.read2ndFilteredData(pin) / 1024.0F; }
501 
502  void setCurrentValue(pinid_t pin, unsigned int newValue) override { theAbstraction.setPinLedCurrent(pin, newValue >> 4); }
503 
504  void setCurrentFloat(pinid_t pin, float newValue) override { theAbstraction.setPinLedCurrent(pin, (uint8_t)(newValue * 15.0F)); }
505 };
506 
507 // to remain compatible with old code
508 #define ioFrom8754 ioFrom8575
509 
521 IoAbstractionRef ioFrom8574(uint8_t addr, pinid_t interruptPin, WireType wireImpl, bool invertedLogic = false);
522 
534 IoAbstractionRef ioFrom8575(uint8_t addr, pinid_t interruptPin, WireType wireImpl, bool invertedLogic = false);
535 
545 IoAbstractionRef ioFrom23017(pinid_t addr, WireType wireImpl);
546 
558 IoAbstractionRef ioFrom23017(uint8_t addr, Mcp23xInterruptMode intMode, pinid_t interruptPin, WireType wireImpl);
559 
572 IoAbstractionRef ioFrom23017IntPerPort(pinid_t addr, Mcp23xInterruptMode intMode, pinid_t interruptPinA, pinid_t interruptPinB, WireType wireImpl);
573 
574 inline IoAbstractionRef ioFrom8574(uint8_t addr, pinid_t interruptPin = 0xff, bool invertedLogic = false) {
575  return ioFrom8574(addr, interruptPin, defaultWireTypePtr, invertedLogic);
576 };
577 
578 inline IoAbstractionRef ioFrom8575(uint8_t addr, pinid_t interruptPin = 0xff, bool invertedLogic = false) {
579  return ioFrom8575(addr, interruptPin, defaultWireTypePtr, invertedLogic);
580 };
581 
582 inline IoAbstractionRef ioFrom23017IntPerPort(uint8_t addr, Mcp23xInterruptMode intMode, pinid_t interruptPinA, pinid_t interruptPinB) {
583  return ioFrom23017IntPerPort(addr, intMode, interruptPinA, interruptPinB, defaultWireTypePtr);
584 }
585 
586 inline IoAbstractionRef ioFrom23017(uint8_t addr, Mcp23xInterruptMode intMode, pinid_t interruptPin) {
587  return ioFrom23017(addr, intMode, interruptPin, defaultWireTypePtr);
588 }
589 
590 inline IoAbstractionRef ioFrom23017(pinid_t addr) {
591  return ioFrom23017(addr, defaultWireTypePtr);
592 }
593 
594 #endif /* _IOABSTRACTION_IOABSTRACTIONWIRE_H_ */
Contains a series of helper classes for dealing with analog devices, these are compatible across a wi...
AnalogDirection
Definition: AnalogDeviceAbstraction.h:22
Using basic IoFacilities allows one to abstract away the use of IoExpanders, such that the switching ...
IoAbstractionRef ioFrom8574(uint8_t addr, pinid_t interruptPin, WireType wireImpl, bool invertedLogic=false)
Definition: IoAbstractionWire.cpp:88
MPR121ConfigType
Definition: IoAbstractionWire.h:361
IoAbstractionRef ioFrom23017IntPerPort(pinid_t addr, Mcp23xInterruptMode intMode, pinid_t interruptPinA, pinid_t interruptPinB, WireType wireImpl)
Definition: IoAbstractionWire.cpp:245
Mcp23xInterruptMode
Definition: IoAbstractionWire.h:121
IoAbstractionRef ioFrom23017(pinid_t addr, WireType wireImpl)
Definition: IoAbstractionWire.cpp:237
IoAbstractionRef ioFrom8575(uint8_t addr, pinid_t interruptPin, WireType wireImpl, bool invertedLogic=false)
Definition: IoAbstractionWire.cpp:92
Definition: IoAbstractionWire.h:285
int getBitDepth(AnalogDirection direction, pinid_t pin) override
Definition: IoAbstractionWire.h:293
void setCurrentFloat(pinid_t pin, float newValue) override
Definition: IoAbstractionWire.h:303
int getMaximumRange(AnalogDirection direction, pinid_t pin) override
Definition: IoAbstractionWire.h:291
unsigned int getCurrentValue(pinid_t pin) override
Definition: IoAbstractionWire.h:297
void setCurrentValue(pinid_t pin, unsigned int newValue) override
Definition: IoAbstractionWire.h:301
void initPin(pinid_t pin, AnalogDirection direction) override
Definition: IoAbstractionWire.cpp:448
Definition: IoAbstractionWire.h:208
void setPinLedCurrent(pinid_t pin, uint8_t pwr)
Definition: IoAbstractionWire.cpp:426
void writeGlobalControl(bool pushPullP0, AW9523CurrentControl maxCurrentMode=FULL_CURRENT)
Definition: IoAbstractionWire.cpp:442
AW9523IoAbstraction(uint8_t addr, pinid_t intPin=IO_PIN_NOT_DEFINED, WireType wirePtr=nullptr)
Definition: IoAbstractionWire.cpp:342
void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override
Definition: IoAbstractionWire.cpp:363
void softwareReset()
Definition: IoAbstractionWire.cpp:434
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstractionWire.cpp:380
bool runLoop() override
Definition: IoAbstractionWire.cpp:399
uint8_t deviceId()
Definition: IoAbstractionWire.cpp:438
Definition: AnalogDeviceAbstraction.h:31
Definition: BasicIoAbstraction.h:38
Definition: IoAbstractionWire.h:130
void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override
Definition: IoAbstractionWire.cpp:203
MCP23017IoAbstraction(uint8_t address, Mcp23xInterruptMode intMode, pinid_t intPinA, pinid_t intPinB, WireType wireImpl=nullptr)
Definition: IoAbstractionWire.cpp:121
void setInvertInputPin(pinid_t pin, bool shouldInvert)
Definition: IoAbstractionWire.cpp:226
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstractionWire.cpp:166
void resetDevice(int resetPin)
Definition: IoAbstractionWire.cpp:230
bool runLoop() override
Definition: IoAbstractionWire.cpp:175
Definition: IoAbstractionWire.h:486
unsigned int getCurrentValue(pinid_t pin) override
Definition: IoAbstractionWire.h:498
int getMaximumRange(AnalogDirection direction, pinid_t pin) override
Definition: IoAbstractionWire.h:492
void setCurrentFloat(pinid_t pin, float newValue) override
Definition: IoAbstractionWire.h:504
void setCurrentValue(pinid_t pin, unsigned int newValue) override
Definition: IoAbstractionWire.h:502
int getBitDepth(AnalogDirection direction, pinid_t pin) override
Definition: IoAbstractionWire.h:494
void initPin(pinid_t pin, AnalogDirection direction) override
Definition: IoAbstractionWire.cpp:606
Definition: IoAbstractionWire.h:373
uint16_t getOutOfRangeRegister()
Definition: IoAbstractionWire.cpp:512
void configureDebounce(uint8_t debounceTouch, uint8_t debounceRelease)
Definition: IoAbstractionWire.cpp:493
bool runLoop() override
Definition: IoAbstractionWire.cpp:576
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstractionWire.cpp:516
void begin(int maxTouchPin, MPR121ConfigType configType, uint8_t configReg1=0x10, uint8_t configReg2=0x20)
Definition: IoAbstractionWire.cpp:537
void softwareReset()
Definition: IoAbstractionWire.cpp:461
MPR121IoAbstraction(uint8_t addr, pinid_t intPin=IO_PIN_NOT_DEFINED, WireType wirePtr=nullptr)
Definition: IoAbstractionWire.cpp:456
uint16_t read2ndFilteredData(uint8_t pin)
Definition: IoAbstractionWire.cpp:508
void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override
Definition: IoAbstractionWire.cpp:598
void electrodeSettingsForPin(pinid_t pin, uint8_t touchThreshold, uint8_t releaseThreshold, uint8_t current=0, uint8_t chargeTime=0)
Definition: IoAbstractionWire.cpp:498
void setPinLedCurrent(pinid_t pin, uint8_t pwr)
Definition: IoAbstractionWire.cpp:467
Definition: IoAbstractionWire.h:31
void attachInterrupt(pinid_t pin, RawIntHandler intHandler, uint8_t mode) override
Definition: IoAbstractionWire.cpp:79
uint8_t readValue(pinid_t pin) override
Definition: IoAbstractionWire.cpp:36
uint8_t readPort(pinid_t pin) override
Definition: IoAbstractionWire.cpp:41
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: IoAbstractionWire.cpp:19
void overrideReadFlag()
Definition: IoAbstractionWire.h:54
bool runLoop() override
Definition: IoAbstractionWire.cpp:52
void writeValue(pinid_t pin, uint8_t value) override
Definition: IoAbstractionWire.cpp:30
PCF8574IoAbstraction(uint8_t addr, uint8_t interruptPin, WireType wireInstance=nullptr, bool mode16bit=false, bool invertedLogic=false)
Definition: IoAbstractionWire.cpp:9
void writePort(pinid_t pin, uint8_t port) override
Definition: IoAbstractionWire.cpp:46
Definition: IoAbstractionWire.h:95
uint8_t readValue(pinid_t pin) override
Definition: IoAbstractionWire.cpp:269
void writePort(pinid_t pin, uint8_t port) override
Definition: IoAbstractionWire.cpp:285
void writeValue(pinid_t pin, uint8_t value) override
Definition: IoAbstractionWire.cpp:273
uint8_t readPort(pinid_t pin) override
Definition: IoAbstractionWire.cpp:281