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
44 #define MPR121_USE_BITFIELDS true
47 #define MPR121_SAVE_MEMORY true
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)))
55 #define DEPRECATED(func, msg) func __attribute__ ((deprecated))
58 #warning "Please implement DEPRECATED for your compiler"
59 #define DEPRECATED(func, msg) func
76 #define MPR_ELEPROX 12
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
97 static byte i2cReadBuf[MPR121_I2C_BUFLEN];
99 #if MPR121_SAVE_MEMORY
100 static short electrodeDataBuf[13];
101 static byte electrodeBaselineBuf[13];
102 static byte electrodeCDCBuf[13];
104 #if !MPR121_USE_BITFIELDS
105 bool electrodeTouchBuf[13];
106 static bool electrodeOORBuf[15];
108 #else // MPR121_SAVE_MEMORY
109 short electrodeDataBuf[13];
110 byte electrodeBaselineBuf[13];
111 byte electrodeCDCBuf[13];
113 #if !MPR121_USE_BITFIELDS
114 bool electrodeTouchBuf[13];
115 bool electrodeOORBuf[15];
117 #endif // MPR121_SAVE_MEMORY
119 #if MPR121_USE_BITFIELDS
120 short electrodeTouchCache;
122 unsigned long electrodeTouchCacheMicros;
140 return readRegister(addr, 1)[0];
149 bool checkElectrodeNum(
byte &electrode,
byte &count);
156 bool checkElectrodeNum(
byte &electrode);
163 bool checkGPIOPinNum(
byte &pin,
byte &count);
170 bool checkGPIOPinNum(
byte &pin);
177 void setElectrodeThresholds(
byte electrode,
byte count,
byte touchThreshold,
byte releaseThreshold);
183 void setElectrodeThresholds(
byte electrode,
byte touchThreshold,
byte releaseThreshold) {
184 setElectrodeThresholds(electrode, 1, touchThreshold, releaseThreshold);
193 void setMHD(
byte rising,
byte falling);
200 void setNHD(
byte rising,
byte falling,
byte touched);
206 void setNCL(
byte rising,
byte falling,
byte touched);
212 void setFDL(
byte rising,
byte falling,
byte touched);
220 void setMHDProx(
byte rising,
byte falling);
227 void setNHDProx(
byte rising,
byte falling,
byte touched);
233 void setNCLProx(
byte rising,
byte falling,
byte touched);
239 void setFDLProx(
byte rising,
byte falling,
byte touched);
247 void setDebounce(
byte touchNumber,
byte releaseNumber);
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);
302 void setPWM(
byte pin,
byte count,
byte value);
311 mpr121(
byte addr = 0, TwoWire *wire = &Wire);
387 #if MPR121_USE_BITFIELDS
575 void begin(
unsigned long clock=400000);
582 void start(
byte electrodes);
586 DEPRECATED(
void startMPR(
byte electrodes),
"Renamed to `start`.") {
598 DEPRECATED(
void stopMPR(),
"Renamed to `stop`.") {
mpr121FilterSFI SFI
"Second Filter Iterations" (number of samples taken for the second level of filtering)
void writeGPIODigital(byte pin, byte count, bool value)
Writes a digital value to consecutive GPIO pins.
void setGPIOMode(byte pin, byte count, mpr121GPIOMode mode)
Sets pin mode for consecutive GPIO pins.
mpr121FilterCDT * readElectrodeCDT(byte electrode, byte count)
Reads per-electrode "Charge Discharge Time" (μs) for consecutive electrodes.
void writeElectrodeCDT(byte electrode, byte count, mpr121FilterCDT value)
Writes per-electrode "Charge Discharge Time" (μs) for consecutive electrodes.
void begin(unsigned long clock=400000)
Optional alternative to using Wire.begin() and Wire.setClock().
byte autoConfigLSL
"Low-Side Limit" for auto calibration – if not set when starting, this will be automatically set base...
mpr121ElectrodeConfigCL calLock
"Calibration Lock" (baseline tracking and initial value settings)
mpr121AutoConfigRetry
possible auto-configuration "retry" settings
void clearOverCurrent()
Clears the over current flag.
mpr121FilterCDT readElectrodeCDT(byte electrode)
Reads per-electrode "Charge Discharge Time" (μs) for a single electrode.
byte autoConfigTL
"Target Level" for auto calibration – if not set when starting, this will be automatically set based ...
byte releaseThresholds[13]
Release detection thresholds for ELE0-ELE11 and ELEPROX.
byte NHDrising
"Noise Half Delta" rising baseline adjustment value (AN3891) – max: 63
byte FDLrisingProx
"Filter Delay Limit" rising value for proximity detection (AN3891/AN3893)
bool autoConfigSkipChargeTime
"Skip Charge Time Search" will skip searching for charge time and use the already-set per-electrode o...
byte globalCDC
Global "Charge Discharge Current" (μA), not used if autoconfig is enabled – max 63.
byte NCLtouched
"Noise Count Limit" touched baseline adjustment value (AN3891)
byte FDLtouchedProx
"Filter Delay Limit" touched value for proximity detection (AN3891/AN3893)
bool autoConfigInterruptARF
"Auto-reconfiguration fail interrupt enable" will trigger an interrupt when auto-reconfiguration fail...
void writeGPIODigital(byte pin, bool value)
Writes a digital value to a single GPIO pin.
byte NCLrising
"Noise Count Limit" rising baseline adjustment value (AN3891)
mpr121FilterSFI
possible "second filter iterations" settings
void writeGPIOAnalog(byte pin, byte count, byte value)
Writes an "analog" (PWM) value to consecutive GPIO pins.
void writeElectrodeBaseline(byte electrode, byte count, byte value)
Writes a baseline value to consecutive electrodes.
byte NHDfalling
"Noise Half Delta" falling baseline adjustment value (AN3891) – max: 63
bool checkRunning()
Checks if the MPR121 is in run mode.
mpr121FilterFFI
possible "first filter iterations" settings
byte autoConfigUSL
"Up-Side Limit" for auto calibration – if not set when starting, this will be automatically set to th...
byte readElectrodeBaseline(byte electrode)
Reads the baseline value for a single electrode.
mpr121FilterCDT
possible "charge discharge time" settings
mpr121ElectrodeConfigProx proxEnable
ELEPROX_EN: sets what electrodes will be used for proximity detection.
void softReset()
Resets the MPR121.
short readElectrodeData(byte electrode)
Reads filtered analog data for a single electrode.
byte NCLfallingProx
"Noise Count Limit" falling value for proximity detection (AN3891/AN3893)
short * readElectrodeData(byte electrode, byte count)
Reads filtered analog data for consecutive electrodes.
byte NHDrisingProx
"Noise Half Delta" rising value for proximity detection (AN3891/AN3893) – max: 63
void writeElectrodeCDT(byte electrode, mpr121FilterCDT value)
Writes per-electrode "Charge Discharge Time" (μs) for a single electrode.
void stop()
Exits run mode.
bool autoConfigInterruptACF
"Auto-configuration fail interrupt enable" will trigger an interrupt when auto-configuration fails
byte NCLfalling
"Noise Count Limit" falling baseline adjustment value (AN3891)
void setAllThresholds(byte touched, byte released, bool prox)
A quick way to set all ::touchThresholds and ::releaseThresholds.
mpr121AutoConfigBVA autoConfigBaselineAdjust
"Baseline Value Adjust" changes how the baseline registers will be set after auto-configuration compl...
mpr121AutoConfigRetry autoConfigRetry
Number of retries for failed auto-config before out of range will be set.
byte NCLtouchedProx
"Noise Count Limit" touched value for proximity detection (AN3891/AN3893)
byte * readElectrodeCDC(byte electrode, byte count)
Reads per-electrode "Charge Discharge Current" (μA) for consecutive electrodes.
bool readOverCurrent()
Reads the over current flag.
byte debounceRelease
Set "Debounce" count for releases (times a detection must be sampled) – max: 7.
byte readElectrodeCDC(byte electrode)
Reads per-electrode "Charge Discharge Current" (μA) for a single electrode.
byte NCLrisingProx
"Noise Count Limit" rising value for proximity detection (AN3891/AN3893)
bool autoConfigEnableReconfig
"Automatic Reconfiguration Enable" will try to reconfigure out of range (failed) channels every sampl...
byte MHDrisingProx
"Max Half Delta" rising value for proximity detection (AN3891/AN3893) – max: 63
mpr121FilterCDT globalCDT
Global "Charge Discharge Time" (μs), not used if autoconfig is enabled.
mpr121ElectrodeConfigProx
possible "proximity enable" settings
byte NHDfallingProx
"Noise Half Delta" falling value for proximity detection (AN3891/AN3893) – max: 63
short readTouchState()
Reads the 13 touch state bits.
byte MHDfalling
"Max Half Delta" falling baseline adjustment value (AN3891) – max: 63
short readOORState()
Reads the 15 out of range bits.
byte FDLfallingProx
"Filter Delay Limit" falling value for proximity detection (AN3891/AN3893)
byte touchThresholds[13]
Touch detection thresholds for ELE0-ELE11 and ELEPROX.
byte FDLfalling
"Filter Delay Limit" falling baseline adjustment value (AN3891)
mpr121GPIOMode
GPIO pin modes.
byte NHDtouched
"Noise Half Delta" touched baseline adjustment value (AN3891) – max: 63
byte debounceTouch
Set "Debounce" count for touches (times a detection must be sampled) – max: 7.
void setGPIOMode(byte pin, mpr121GPIOMode mode)
Sets pin mode for a single GPIO pin.
byte MHDrising
"Max Half Delta" rising baseline adjustment value (AN3891) – max: 63
mpr121FilterESI
possible "electrode sample interval" settings
void writeElectrodeCDC(byte electrode, byte value)
Writes per-electrode "Charge Discharge Current" (μA) for a single electrode.
byte FDLtouched
"Filter Delay Limit" touched baseline adjustment value (AN3891)
byte FDLrising
"Filter Delay Limit" rising baseline adjustment value (AN3891)
mpr121(byte addr=0, TwoWire *wire=&Wire)
Creates an MPR121 device with sane default settings.
mpr121ElectrodeConfigCL
possible "calibration lock" settings
byte NHDtouchedProx
"Noise Half Delta" touched value for proximity detection (AN3891/AN3893) – max: 63
byte MHDfallingProx
"Max Half Delta" falling value for proximity detection (AN3891/AN3893) – max: 63
mpr121AutoConfigBVA
possible auto-configuration "baseline value adjust" settings
mpr121FilterESI ESI
"Electrode Sample Interval" (ms)
mpr121Register
all MPR121 registers
byte * readElectrodeBaseline(byte electrode, byte count)
Reads baseline values for consecutive electrodes.
void writeElectrodeBaseline(byte electrode, byte value)
Writes the baseline value for a single electrode.
void writeGPIOAnalog(byte pin, byte value)
Writes an "analog" (PWM) value to a single GPIO pin.
mpr121FilterFFI FFI
"First Filter Iterations" (number of samples taken for the first level of filtering)
void start(byte electrodes)
Applies settings and enters run mode with a given number of electrodes.
bool autoConfigEnableCalibration
"Automatic Configuration Enable" will enable/disable auto-configuration when entering run mode
void writeElectrodeCDC(byte electrode, byte count, byte value)
Writes per-electrode "Charge Discharge Current" (μA) for consecutive electrodes.
bool autoConfigInterruptOOR
"Out-of-range interrupt enable" will trigger an interrupt when a channel is determined to be out of r...