QuickMpr121
QuickMpr121.h
1 
31 #pragma once
32 #include "Arduino.h"
33 #include "Wire.h"
34 #include "QuickMpr121Enums.h"
35 
36 
37 // please also update `PREDEFINED` in Doxyfile if changing any defines
38 
39 #ifndef MPR121_I2C_BUFLEN
40 #define MPR121_I2C_BUFLEN 26 // note: arduino Wire library defines BUFFER_LENGTH as 32, so much larger values won't work
41 #endif
42 
43 // use bitfields (stored in short) instead electrodeTouchBuf and electrodeOORBuf
44 #define MPR121_USE_BITFIELDS true
45 
46 // make some buffers static (shared between instances) to save memory
47 #define MPR121_SAVE_MEMORY true
48 
49 
50 // define DEPRECATED so the same syntax can be used for any compiler without issues
51 #if __GNUC__
52  # if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) // GCC 4.5+ supports deprecated with msg
53  #define DEPRECATED(func, msg) func __attribute__ ((deprecated(msg)))
54  #else // GCC 4.5+
55  #define DEPRECATED(func, msg) func __attribute__ ((deprecated))
56  #endif // GCC 4.5+
57 #else // __GNUC__
58  #warning "Please implement DEPRECATED for your compiler"
59  #define DEPRECATED(func, msg) func
60 #endif // __GNUC__
61 
62 
63 // pin names (mainly useful for LED pins)
64 #define MPR_ELE0 0
65 #define MPR_ELE1 1
66 #define MPR_ELE2 2
67 #define MPR_ELE3 3
68 #define MPR_ELE4 4
69 #define MPR_ELE5 5
70 #define MPR_ELE6 6
71 #define MPR_ELE7 7
72 #define MPR_ELE8 8
73 #define MPR_ELE9 9
74 #define MPR_ELE10 10
75 #define MPR_ELE11 11
76 #define MPR_ELEPROX 12
77 
78 #define MPR_LED0 MPR_ELE4
79 #define MPR_LED1 MPR_ELE5
80 #define MPR_LED2 MPR_ELE6
81 #define MPR_LED3 MPR_ELE7
82 #define MPR_LED4 MPR_ELE8
83 #define MPR_LED5 MPR_ELE9
84 #define MPR_LED6 MPR_ELE10
85 #define MPR_LED7 MPR_ELE11
86 
87 
92 class mpr121 {
93 private:
94  byte i2cAddr;
95  TwoWire* i2cWire;
96 
97  static byte i2cReadBuf[MPR121_I2C_BUFLEN];
98 
99  #if MPR121_SAVE_MEMORY
100  static short electrodeDataBuf[13];
101  static byte electrodeBaselineBuf[13];
102  static byte electrodeCDCBuf[13];
103  static mpr121FilterCDT electrodeCDTBuf[13];
104  #if !MPR121_USE_BITFIELDS
105  bool electrodeTouchBuf[13];
106  static bool electrodeOORBuf[15];
107  #endif
108  #else // MPR121_SAVE_MEMORY
109  short electrodeDataBuf[13];
110  byte electrodeBaselineBuf[13];
111  byte electrodeCDCBuf[13];
112  mpr121FilterCDT electrodeCDTBuf[13];
113  #if !MPR121_USE_BITFIELDS
114  bool electrodeTouchBuf[13];
115  bool electrodeOORBuf[15];
116  #endif
117  #endif // MPR121_SAVE_MEMORY
118 
119  #if MPR121_USE_BITFIELDS
120  short electrodeTouchCache;
121  #endif
122  unsigned long electrodeTouchCacheMicros;
123 
124 
128  void writeRegister(mpr121Register addr, byte value);
129 
134  byte* readRegister(mpr121Register addr, byte count);
135 
139  byte readRegister(mpr121Register addr) {
140  return readRegister(addr, 1)[0];
141  }
142 
143 
149  bool checkElectrodeNum(byte &electrode, byte &count);
150 
156  bool checkElectrodeNum(byte &electrode);
157 
163  bool checkGPIOPinNum(byte &pin, byte &count);
164 
170  bool checkGPIOPinNum(byte &pin);
171 
172 
177  void setElectrodeThresholds(byte electrode, byte count, byte touchThreshold, byte releaseThreshold);
178 
183  void setElectrodeThresholds(byte electrode, byte touchThreshold, byte releaseThreshold) {
184  setElectrodeThresholds(electrode, 1, touchThreshold, releaseThreshold);
185  }
186 
187 
193  void setMHD(byte rising, byte falling);
194 
200  void setNHD(byte rising, byte falling, byte touched);
201 
206  void setNCL(byte rising, byte falling, byte touched);
207 
212  void setFDL(byte rising, byte falling, byte touched);
213 
214 
220  void setMHDProx(byte rising, byte falling);
221 
227  void setNHDProx(byte rising, byte falling, byte touched);
228 
233  void setNCLProx(byte rising, byte falling, byte touched);
234 
239  void setFDLProx(byte rising, byte falling, byte touched);
240 
241 
247  void setDebounce(byte touchNumber, byte releaseNumber);
248 
249 
262  void setFilterConfig(mpr121FilterFFI FFI, byte CDC, mpr121FilterCDT CDT, mpr121FilterSFI SFI, mpr121FilterESI ESI);
263 
264 
272  void setElectrodeConfiguration(mpr121ElectrodeConfigCL CL, mpr121ElectrodeConfigProx ELEPROX_EN, byte ELE_EN);
273 
274 
291  void setAutoConfig(byte USL, byte LSL, byte TL, mpr121AutoConfigRetry RETRY, mpr121AutoConfigBVA BVA, bool ARE, bool ACE, bool SCTS, bool OORIE, bool ARFIE, bool ACFIE);
292 
293 
302  void setPWM(byte pin, byte count, byte value);
303 
304 public:
311  mpr121(byte addr = 0, TwoWire *wire = &Wire);
312 
313  byte touchThresholds[13];
314  byte releaseThresholds[13];
315 
316  byte MHDrising;
317  byte MHDfalling;
318 
319  byte NHDrising;
320  byte NHDfalling;
321  byte NHDtouched;
322 
323  byte NCLrising;
324  byte NCLfalling;
325  byte NCLtouched;
326 
327  byte FDLrising;
328  byte FDLfalling;
329  byte FDLtouched;
330 
331 
334 
338 
342 
346 
347 
350 
351 
353  byte globalCDC;
357 
358 
361 
369 
376 
377 
385  bool readTouchState(byte electrode);
386 
387  #if MPR121_USE_BITFIELDS
388 
392  short readTouchState();
393 
398  short readOORState();
399  #else
400 
404  bool* readTouchState();
405 
410  bool* readOORState();
411  #endif
412 
417  bool readOverCurrent();
418 
423  void clearOverCurrent();
424 
428  short* readElectrodeData(byte electrode, byte count);
429 
433  short readElectrodeData(byte electrode) {
434  return readElectrodeData(electrode, 1)[0];
435  }
436 
440  byte* readElectrodeBaseline(byte electrode, byte count);
441 
445  byte readElectrodeBaseline(byte electrode) {
446  return readElectrodeBaseline(electrode, 1)[0];
447  }
448 
452  void writeElectrodeBaseline(byte electrode, byte count, byte value);
453 
457  void writeElectrodeBaseline(byte electrode, byte value) {
458  writeElectrodeBaseline(electrode, 1, value);
459  }
460 
466  void setAllThresholds(byte touched, byte released, bool prox);
467 
468 
472  byte* readElectrodeCDC(byte electrode, byte count);
473 
477  byte readElectrodeCDC(byte electrode) {
478  return readElectrodeCDC(electrode, 1)[0];
479  }
480 
485  void writeElectrodeCDC(byte electrode, byte count, byte value);
486 
491  void writeElectrodeCDC(byte electrode, byte value) {
492  writeElectrodeCDC(electrode, 1, value);
493  }
494 
498  mpr121FilterCDT* readElectrodeCDT(byte electrode, byte count);
499 
504  return readElectrodeCDT(electrode, 1)[0];
505  }
506 
510  void writeElectrodeCDT(byte electrode, byte count, mpr121FilterCDT value);
511 
515  void writeElectrodeCDT(byte electrode, mpr121FilterCDT value) {
516  writeElectrodeCDT(electrode, 1, value);
517  }
518 
519 
526  void setGPIOMode(byte pin, byte count, mpr121GPIOMode mode);
527 
534  void setGPIOMode(byte pin, mpr121GPIOMode mode) {
535  setGPIOMode(pin, 1, mode);
536  }
537 
541  void writeGPIODigital(byte pin, byte count, bool value);
542 
546  void writeGPIODigital(byte pin, bool value) {
547  writeGPIODigital(pin, 1, value);
548  }
549 
557  void writeGPIOAnalog(byte pin, byte count, byte value);
558 
566  void writeGPIOAnalog(byte pin, byte value) {
567  writeGPIOAnalog(pin, 1, value);
568  }
569 
570 
575  void begin(unsigned long clock=400000);
576 
577 
582  void start(byte electrodes);
583 
584  #ifndef NO_DOXYGEN
585  // (deprecated) alias for start
586  DEPRECATED(void startMPR(byte electrodes), "Renamed to `start`.") {
587  start(electrodes);
588  }
589  #endif
590 
594  void stop();
595 
596  #ifndef NO_DOXYGEN
597  // (deprecated) alias for stop
598  DEPRECATED(void stopMPR(), "Renamed to `stop`.") {
599  stop();
600  }
601  #endif
602 
603 
607  bool checkRunning();
608 
609 
613  void softReset();
614 };
615 
mpr121::SFI
mpr121FilterSFI SFI
"Second Filter Iterations" (number of samples taken for the second level of filtering)
Definition: QuickMpr121.h:355
mpr121::writeGPIODigital
void writeGPIODigital(byte pin, byte count, bool value)
Writes a digital value to consecutive GPIO pins.
Definition: QuickMpr121.cpp:719
mpr121::setGPIOMode
void setGPIOMode(byte pin, byte count, mpr121GPIOMode mode)
Sets pin mode for consecutive GPIO pins.
Definition: QuickMpr121.cpp:662
mpr121::readElectrodeCDT
mpr121FilterCDT * readElectrodeCDT(byte electrode, byte count)
Reads per-electrode "Charge Discharge Time" (μs) for consecutive electrodes.
Definition: QuickMpr121.cpp:613
mpr121::writeElectrodeCDT
void writeElectrodeCDT(byte electrode, byte count, mpr121FilterCDT value)
Writes per-electrode "Charge Discharge Time" (μs) for consecutive electrodes.
Definition: QuickMpr121.cpp:633
mpr121::begin
void begin(unsigned long clock=400000)
Optional alternative to using Wire.begin() and Wire.setClock().
Definition: QuickMpr121.cpp:767
mpr121::autoConfigLSL
byte autoConfigLSL
"Low-Side Limit" for auto calibration – if not set when starting, this will be automatically set base...
Definition: QuickMpr121.h:363
mpr121::calLock
mpr121ElectrodeConfigCL calLock
"Calibration Lock" (baseline tracking and initial value settings)
Definition: QuickMpr121.h:359
mpr121AutoConfigRetry
mpr121AutoConfigRetry
possible auto-configuration "retry" settings
Definition: QuickMpr121Enums.h:66
mpr121::clearOverCurrent
void clearOverCurrent()
Clears the over current flag.
Definition: QuickMpr121.cpp:529
mpr121::readElectrodeCDT
mpr121FilterCDT readElectrodeCDT(byte electrode)
Reads per-electrode "Charge Discharge Time" (μs) for a single electrode.
Definition: QuickMpr121.h:503
mpr121::autoConfigTL
byte autoConfigTL
"Target Level" for auto calibration – if not set when starting, this will be automatically set based ...
Definition: QuickMpr121.h:364
mpr121::releaseThresholds
byte releaseThresholds[13]
Release detection thresholds for ELE0-ELE11 and ELEPROX.
Definition: QuickMpr121.h:314
mpr121::NHDrising
byte NHDrising
"Noise Half Delta" rising baseline adjustment value (AN3891) – max: 63
Definition: QuickMpr121.h:319
mpr121::FDLrisingProx
byte FDLrisingProx
"Filter Delay Limit" rising value for proximity detection (AN3891/AN3893)
Definition: QuickMpr121.h:343
mpr121::autoConfigSkipChargeTime
bool autoConfigSkipChargeTime
"Skip Charge Time Search" will skip searching for charge time and use the already-set per-electrode o...
Definition: QuickMpr121.h:370
mpr121::globalCDC
byte globalCDC
Global "Charge Discharge Current" (μA), not used if autoconfig is enabled – max 63.
Definition: QuickMpr121.h:353
mpr121::NCLtouched
byte NCLtouched
"Noise Count Limit" touched baseline adjustment value (AN3891)
Definition: QuickMpr121.h:325
mpr121::FDLtouchedProx
byte FDLtouchedProx
"Filter Delay Limit" touched value for proximity detection (AN3891/AN3893)
Definition: QuickMpr121.h:345
mpr121::autoConfigInterruptARF
bool autoConfigInterruptARF
"Auto-reconfiguration fail interrupt enable" will trigger an interrupt when auto-reconfiguration fail...
Definition: QuickMpr121.h:374
mpr121::writeGPIODigital
void writeGPIODigital(byte pin, bool value)
Writes a digital value to a single GPIO pin.
Definition: QuickMpr121.h:546
mpr121::NCLrising
byte NCLrising
"Noise Count Limit" rising baseline adjustment value (AN3891)
Definition: QuickMpr121.h:323
mpr121FilterSFI
mpr121FilterSFI
possible "second filter iterations" settings
Definition: QuickMpr121Enums.h:30
mpr121::writeGPIOAnalog
void writeGPIOAnalog(byte pin, byte count, byte value)
Writes an "analog" (PWM) value to consecutive GPIO pins.
Definition: QuickMpr121.cpp:744
mpr121::writeElectrodeBaseline
void writeElectrodeBaseline(byte electrode, byte count, byte value)
Writes a baseline value to consecutive electrodes.
Definition: QuickMpr121.cpp:563
mpr121::NHDfalling
byte NHDfalling
"Noise Half Delta" falling baseline adjustment value (AN3891) – max: 63
Definition: QuickMpr121.h:320
mpr121::checkRunning
bool checkRunning()
Checks if the MPR121 is in run mode.
Definition: QuickMpr121.cpp:842
mpr121FilterFFI
mpr121FilterFFI
possible "first filter iterations" settings
Definition: QuickMpr121Enums.h:10
mpr121::autoConfigUSL
byte autoConfigUSL
"Up-Side Limit" for auto calibration – if not set when starting, this will be automatically set to th...
Definition: QuickMpr121.h:362
mpr121::readElectrodeBaseline
byte readElectrodeBaseline(byte electrode)
Reads the baseline value for a single electrode.
Definition: QuickMpr121.h:445
mpr121FilterCDT
mpr121FilterCDT
possible "charge discharge time" settings
Definition: QuickMpr121Enums.h:18
mpr121::proxEnable
mpr121ElectrodeConfigProx proxEnable
ELEPROX_EN: sets what electrodes will be used for proximity detection.
Definition: QuickMpr121.h:360
mpr121::softReset
void softReset()
Resets the MPR121.
Definition: QuickMpr121.cpp:848
mpr121::readElectrodeData
short readElectrodeData(byte electrode)
Reads filtered analog data for a single electrode.
Definition: QuickMpr121.h:433
mpr121::NCLfallingProx
byte NCLfallingProx
"Noise Count Limit" falling value for proximity detection (AN3891/AN3893)
Definition: QuickMpr121.h:340
mpr121::readElectrodeData
short * readElectrodeData(byte electrode, byte count)
Reads filtered analog data for consecutive electrodes.
Definition: QuickMpr121.cpp:535
mpr121::NHDrisingProx
byte NHDrisingProx
"Noise Half Delta" rising value for proximity detection (AN3891/AN3893) – max: 63
Definition: QuickMpr121.h:335
mpr121::writeElectrodeCDT
void writeElectrodeCDT(byte electrode, mpr121FilterCDT value)
Writes per-electrode "Charge Discharge Time" (μs) for a single electrode.
Definition: QuickMpr121.h:515
mpr121::stop
void stop()
Exits run mode.
Definition: QuickMpr121.cpp:835
mpr121::autoConfigInterruptACF
bool autoConfigInterruptACF
"Auto-configuration fail interrupt enable" will trigger an interrupt when auto-configuration fails
Definition: QuickMpr121.h:375
mpr121::NCLfalling
byte NCLfalling
"Noise Count Limit" falling baseline adjustment value (AN3891)
Definition: QuickMpr121.h:324
mpr121::setAllThresholds
void setAllThresholds(byte touched, byte released, bool prox)
A quick way to set all ::touchThresholds and ::releaseThresholds.
Definition: QuickMpr121.cpp:574
mpr121::autoConfigBaselineAdjust
mpr121AutoConfigBVA autoConfigBaselineAdjust
"Baseline Value Adjust" changes how the baseline registers will be set after auto-configuration compl...
Definition: QuickMpr121.h:366
mpr121::autoConfigRetry
mpr121AutoConfigRetry autoConfigRetry
Number of retries for failed auto-config before out of range will be set.
Definition: QuickMpr121.h:365
mpr121::NCLtouchedProx
byte NCLtouchedProx
"Noise Count Limit" touched value for proximity detection (AN3891/AN3893)
Definition: QuickMpr121.h:341
mpr121::readElectrodeCDC
byte * readElectrodeCDC(byte electrode, byte count)
Reads per-electrode "Charge Discharge Current" (μA) for consecutive electrodes.
Definition: QuickMpr121.cpp:588
mpr121::readOverCurrent
bool readOverCurrent()
Reads the over current flag.
Definition: QuickMpr121.cpp:523
mpr121::debounceRelease
byte debounceRelease
Set "Debounce" count for releases (times a detection must be sampled) – max: 7.
Definition: QuickMpr121.h:349
mpr121::readElectrodeCDC
byte readElectrodeCDC(byte electrode)
Reads per-electrode "Charge Discharge Current" (μA) for a single electrode.
Definition: QuickMpr121.h:477
mpr121::NCLrisingProx
byte NCLrisingProx
"Noise Count Limit" rising value for proximity detection (AN3891/AN3893)
Definition: QuickMpr121.h:339
mpr121::autoConfigEnableReconfig
bool autoConfigEnableReconfig
"Automatic Reconfiguration Enable" will try to reconfigure out of range (failed) channels every sampl...
Definition: QuickMpr121.h:367
mpr121::MHDrisingProx
byte MHDrisingProx
"Max Half Delta" rising value for proximity detection (AN3891/AN3893) – max: 63
Definition: QuickMpr121.h:332
mpr121::globalCDT
mpr121FilterCDT globalCDT
Global "Charge Discharge Time" (μs), not used if autoconfig is enabled.
Definition: QuickMpr121.h:354
mpr121ElectrodeConfigProx
mpr121ElectrodeConfigProx
possible "proximity enable" settings
Definition: QuickMpr121Enums.h:58
mpr121::NHDfallingProx
byte NHDfallingProx
"Noise Half Delta" falling value for proximity detection (AN3891/AN3893) – max: 63
Definition: QuickMpr121.h:336
mpr121::readTouchState
short readTouchState()
Reads the 13 touch state bits.
Definition: QuickMpr121.cpp:453
mpr121
Main mpr121 class.
Definition: QuickMpr121.h:92
mpr121::MHDfalling
byte MHDfalling
"Max Half Delta" falling baseline adjustment value (AN3891) – max: 63
Definition: QuickMpr121.h:317
mpr121::readOORState
short readOORState()
Reads the 15 out of range bits.
Definition: QuickMpr121.cpp:461
mpr121::FDLfallingProx
byte FDLfallingProx
"Filter Delay Limit" falling value for proximity detection (AN3891/AN3893)
Definition: QuickMpr121.h:344
mpr121::touchThresholds
byte touchThresholds[13]
Touch detection thresholds for ELE0-ELE11 and ELEPROX.
Definition: QuickMpr121.h:313
mpr121::FDLfalling
byte FDLfalling
"Filter Delay Limit" falling baseline adjustment value (AN3891)
Definition: QuickMpr121.h:328
mpr121GPIOMode
mpr121GPIOMode
GPIO pin modes.
Definition: QuickMpr121Enums.h:84
mpr121::NHDtouched
byte NHDtouched
"Noise Half Delta" touched baseline adjustment value (AN3891) – max: 63
Definition: QuickMpr121.h:321
mpr121::debounceTouch
byte debounceTouch
Set "Debounce" count for touches (times a detection must be sampled) – max: 7.
Definition: QuickMpr121.h:348
mpr121::setGPIOMode
void setGPIOMode(byte pin, mpr121GPIOMode mode)
Sets pin mode for a single GPIO pin.
Definition: QuickMpr121.h:534
mpr121::MHDrising
byte MHDrising
"Max Half Delta" rising baseline adjustment value (AN3891) – max: 63
Definition: QuickMpr121.h:316
mpr121FilterESI
mpr121FilterESI
possible "electrode sample interval" settings
Definition: QuickMpr121Enums.h:38
QuickMpr121Enums.h
mpr121::writeElectrodeCDC
void writeElectrodeCDC(byte electrode, byte value)
Writes per-electrode "Charge Discharge Current" (μA) for a single electrode.
Definition: QuickMpr121.h:491
mpr121::FDLtouched
byte FDLtouched
"Filter Delay Limit" touched baseline adjustment value (AN3891)
Definition: QuickMpr121.h:329
mpr121::FDLrising
byte FDLrising
"Filter Delay Limit" rising baseline adjustment value (AN3891)
Definition: QuickMpr121.h:327
mpr121::mpr121
mpr121(byte addr=0, TwoWire *wire=&Wire)
Creates an MPR121 device with sane default settings.
Definition: QuickMpr121.cpp:308
mpr121ElectrodeConfigCL
mpr121ElectrodeConfigCL
possible "calibration lock" settings
Definition: QuickMpr121Enums.h:50
mpr121::NHDtouchedProx
byte NHDtouchedProx
"Noise Half Delta" touched value for proximity detection (AN3891/AN3893) – max: 63
Definition: QuickMpr121.h:337
mpr121::MHDfallingProx
byte MHDfallingProx
"Max Half Delta" falling value for proximity detection (AN3891/AN3893) – max: 63
Definition: QuickMpr121.h:333
mpr121AutoConfigBVA
mpr121AutoConfigBVA
possible auto-configuration "baseline value adjust" settings
Definition: QuickMpr121Enums.h:76
mpr121::ESI
mpr121FilterESI ESI
"Electrode Sample Interval" (ms)
Definition: QuickMpr121.h:356
mpr121Register
mpr121Register
all MPR121 registers
Definition: QuickMpr121Enums.h:98
mpr121::readElectrodeBaseline
byte * readElectrodeBaseline(byte electrode, byte count)
Reads baseline values for consecutive electrodes.
Definition: QuickMpr121.cpp:549
mpr121::writeElectrodeBaseline
void writeElectrodeBaseline(byte electrode, byte value)
Writes the baseline value for a single electrode.
Definition: QuickMpr121.h:457
mpr121::writeGPIOAnalog
void writeGPIOAnalog(byte pin, byte value)
Writes an "analog" (PWM) value to a single GPIO pin.
Definition: QuickMpr121.h:566
mpr121::FFI
mpr121FilterFFI FFI
"First Filter Iterations" (number of samples taken for the first level of filtering)
Definition: QuickMpr121.h:352
mpr121::start
void start(byte electrodes)
Applies settings and enters run mode with a given number of electrodes.
Definition: QuickMpr121.cpp:779
mpr121::autoConfigEnableCalibration
bool autoConfigEnableCalibration
"Automatic Configuration Enable" will enable/disable auto-configuration when entering run mode
Definition: QuickMpr121.h:368
mpr121::writeElectrodeCDC
void writeElectrodeCDC(byte electrode, byte count, byte value)
Writes per-electrode "Charge Discharge Current" (μA) for consecutive electrodes.
Definition: QuickMpr121.cpp:603
mpr121::autoConfigInterruptOOR
bool autoConfigInterruptOOR
"Out-of-range interrupt enable" will trigger an interrupt when a channel is determined to be out of r...
Definition: QuickMpr121.h:373