add examples

This commit is contained in:
s-light 2025-06-29 07:56:10 +02:00
parent 8febd00851
commit c96fc7877c
No known key found for this signature in database
GPG key ID: F0BFA86ED38D9F09
4 changed files with 383 additions and 6 deletions

View file

@ -57,25 +57,31 @@ bool Adafruit_MPR121::begin(uint8_t i2caddr, TwoWire *theWire,
}
i2c_dev = new Adafruit_I2CDevice(i2caddr, theWire);
Serial.println("Adafruit_I2CDevice setup..");
if (!i2c_dev->begin()) {
Serial.println("i2c_dev begin failed..");
return false;
}
Serial.println("done.");
Serial.println("send soft reset..");
// soft reset
writeRegister(MPR121_SOFTRESET, 0x63);
delay(1);
for (uint8_t i = 0; i < 0x7F; i++) {
// Serial.print("$"); Serial.print(i, HEX);
// Serial.print(": 0x"); Serial.println(readRegister8(i));
// Serial.print(": 0x"); Serial.println(readRegister8(i), HEX);
}
writeRegister(MPR121_ECR, 0x0);
Serial.print("read: MPR121_CONFIG2 ");
uint8_t c = readRegister8(MPR121_CONFIG2);
Serial.println(c, HEX);
if (c != 0x24)
return false;
Serial.println("write Configuration to sensor ...");
setThresholds(touchThreshold, releaseThreshold);
writeRegister(MPR121_MHDR, 0x01);
writeRegister(MPR121_NHDR, 0x01);
@ -101,7 +107,7 @@ bool Adafruit_MPR121::begin(uint8_t i2caddr, TwoWire *theWire,
// CL Calibration Lock: B10 = 5 bits for baseline tracking
// ELEPROX_EN proximity: disabled
// ELE_EN Electrode Enable: amount of electrodes running (12)
byte ECR_SETTING = B10000000 + 12;
byte ECR_SETTING = 0b10000000 + 12;
writeRegister(MPR121_ECR, ECR_SETTING); // start with above ECR setting
return true;
@ -144,8 +150,8 @@ void Adafruit_MPR121::setAutoconfig(boolean autoconfig) {
// BVA same as CL in ECR
// ARE Auto-Reconfiguration Enable
// ACE Auto-Configuration Enable
// 0x0B == B00001011
writeRegister(MPR121_AUTOCONFIG0, B00001011);
// 0x0B == 0b00001011
writeRegister(MPR121_AUTOCONFIG0, 0b00001011);
// details on values
// https://www.nxp.com/docs/en/application-note/AN3889.pdf#page=7&zoom=310,-42,792
@ -155,7 +161,7 @@ void Adafruit_MPR121::setAutoconfig(boolean autoconfig) {
writeRegister(MPR121_LOWLIMIT, 130); // UPLIMIT * 0.65
} else {
// really only disable ACE.
writeRegister(MPR121_AUTOCONFIG0, B00001010);
writeRegister(MPR121_AUTOCONFIG0, 0b00001010);
}
}

View file

@ -0,0 +1,111 @@
/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor
Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/
These sensors use I2C communicate, at least 2 pins are required
to interface
this sketch demonstrates the auto-config chip-funktionality.
for more information have a look at
https://github.com/adafruit/Adafruit_MPR121/issues/39
https://github.com/adafruit/Adafruit_MPR121/pull/43
based on MPR121test
modified & extended by Carter Nelson/caternuson
MIT license, all text above must be included in any redistribution
**********************************************************/
#include <Wire.h>
#include <Adafruit_MPR121.h>
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap = Adafruit_MPR121();
// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
void dump_regs() {
Serial.println("========================================");
Serial.println("CHAN 00 01 02 03 04 05 06 07 08 09 10 11");
Serial.println(" -- -- -- -- -- -- -- -- -- -- -- --");
// CDC
Serial.print("CDC: ");
for (int chan=0; chan<12; chan++) {
uint8_t reg = cap.readRegister8(0x5F+chan);
if (reg < 10) Serial.print(" ");
Serial.print(reg);
Serial.print(" ");
}
Serial.println();
// CDT
Serial.print("CDT: ");
for (int chan=0; chan<6; chan++) {
uint8_t reg = cap.readRegister8(0x6C+chan);
uint8_t cdtx = reg & 0b111;
uint8_t cdty = (reg >> 4) & 0b111;
if (cdtx < 10) Serial.print(" ");
Serial.print(cdtx);
Serial.print(" ");
if (cdty < 10) Serial.print(" ");
Serial.print(cdty);
Serial.print(" ");
}
Serial.println();
Serial.println("========================================");
}
void setup() {
delay(1000);
Serial.begin(115200);
while (!Serial);
Serial.println("MPR121 Autoconfiguration Test. (MPR121-AutoConfig.ino)");
Serial.println("startup `Wire`");
Wire.begin();
Serial.println("startup `Wire` done.");
delay(100);
Serial.println("cap.begin..");
if (!cap.begin(0x5A, &Wire)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
delay(100);
Serial.println("Initial CDC/CDT values:");
dump_regs();
cap.setAutoconfig(true);
Serial.println("After autoconfig CDC/CDT values:");
dump_regs();
}
void loop() {
// Get the currently touched pads
currtouched = cap.touched();
for (uint8_t i=0; i<12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" touched");
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" released");
}
}
// reset our state
lasttouched = currtouched;
}

View file

@ -0,0 +1,190 @@
[Channels]
channel\1\color=@Variant(\0\0\0\x43\x1\xff\xff\xff\xff\xff\xff\0\0\0\0)
channel\1\gain=1
channel\1\gainEnabled=false
channel\1\name=0 touched
channel\1\offset=0
channel\1\offsetEnabled=false
channel\1\visible=true
channel\10\color=@Variant(\0\0\0\x43\x1\xff\xffUUUU\xff\xff\0\0)
channel\10\gain=1
channel\10\gainEnabled=false
channel\10\name=3 touched
channel\10\offset=0
channel\10\offsetEnabled=false
channel\10\visible=true
channel\11\color=@Variant(\0\0\0\x43\x1\xff\xff\x87\x87\x87\x87\xff\xff\0\0)
channel\11\gain=1
channel\11\gainEnabled=false
channel\11\name=3 baseline
channel\11\offset=0
channel\11\offsetEnabled=false
channel\11\visible=true
channel\12\color="@Variant(\0\0\0\x43\x1\xff\xffhh==;;\0\0)"
channel\12\gain=1
channel\12\gainEnabled=false
channel\12\name=3 filtered
channel\12\offset=0
channel\12\offsetEnabled=false
channel\12\visible=true
channel\13\color=@Variant(\0\0\0\x43\x1\xff\xff\0\0\0\0\xff\xff\0\0)
channel\13\gain=1
channel\13\gainEnabled=false
channel\13\name=4 touched
channel\13\offset=0
channel\13\offsetEnabled=false
channel\13\visible=true
channel\14\color=@Variant(\0\0\0\x43\x1\xff\xffUUUU\xff\xff\0\0)
channel\14\gain=1
channel\14\gainEnabled=false
channel\14\name=4 baseline
channel\14\offset=0
channel\14\offsetEnabled=false
channel\14\visible=true
channel\15\color=@Variant(\0\0\0\x43\x1\xff\xffUUUU\xff\xff\0\0)
channel\15\gain=1
channel\15\gainEnabled=false
channel\15\name=4 filtered
channel\15\offset=0
channel\15\offsetEnabled=false
channel\15\visible=true
channel\2\color=@Variant(\0\0\0\x43\x1\xff\xff\xff\xff\xaa\xaa\0\0\0\0)
channel\2\gain=1
channel\2\gainEnabled=false
channel\2\name=0 baseline
channel\2\offset=0
channel\2\offsetEnabled=false
channel\2\visible=true
channel\3\color=@Variant(\0\0\0\x43\x1\xff\xff\xff\xffUU\0\0\0\0)
channel\3\gain=1
channel\3\gainEnabled=false
channel\3\name=0 filtered
channel\3\offset=0
channel\3\offsetEnabled=false
channel\3\visible=true
channel\4\color=@Variant(\0\0\0\x43\x1\xff\xff\0\0\0\0\xff\xff\0\0)
channel\4\gain=1
channel\4\gainEnabled=false
channel\4\name=1 touched
channel\4\offset=0
channel\4\offsetEnabled=false
channel\4\visible=true
channel\5\color=@Variant(\0\0\0\x43\x1\xff\xff\0\0\0\0\x7f\x7f\0\0)
channel\5\gain=1
channel\5\gainEnabled=true
channel\5\name=1 baseline
channel\5\offset=0
channel\5\offsetEnabled=false
channel\5\visible=true
channel\6\color=@Variant(\0\0\0\x43\x1\xff\xff\0\0\xaa\xaa\xff\xff\0\0)
channel\6\gain=1
channel\6\gainEnabled=false
channel\6\name=1 filtered
channel\6\offset=0
channel\6\offsetEnabled=false
channel\6\visible=true
channel\7\color=@Variant(\0\0\0\x43\x1\xff\xff\x87\x87\xff\xffKK\0\0)
channel\7\gain=1
channel\7\gainEnabled=false
channel\7\name=2 touched
channel\7\offset=0
channel\7\offsetEnabled=false
channel\7\visible=true
channel\8\color=@Variant(\0\0\0\x43\x1\xff\xff\xbc\xbc\xff\xff\x9b\x9b\0\0)
channel\8\gain=1
channel\8\gainEnabled=false
channel\8\name=2 baseline
channel\8\offset=0
channel\8\offsetEnabled=false
channel\8\visible=true
channel\9\color=@Variant(\0\0\0\x43\x1\xff\xff\0\0\0\0\xff\xff\0\0)
channel\9\gain=1
channel\9\gainEnabled=false
channel\9\name=2 filtered
channel\9\offset=0
channel\9\offsetEnabled=false
channel\9\visible=true
channel\size=15
[Commands]
command\1\data=
command\1\name=Command 1
command\1\type=ascii
command\size=1
[DataFormat]
format=ascii
[DataFormat_ASCII]
customDelimiter=";"
delimiter=","
filterMode=disabled
filterPrefix=m/s^2
hex=false
numOfChannels=15
[DataFormat_Binary]
endianness=little
numOfChannels=1
numberFormat=uint8
[DataFormat_CustomFrame]
checksum=false
debugMode=false
endianness=little
fixedSize=field1byte
frameSize=1
frameStart=AA BB
numOfChannels=1
numberFormat=uint8
[MainWindow]
activePanel=Plot
hidePanels=false
maximized=true
pos=@Point(0 0)
size=@Size(3072 1659)
state=@ByteArray(\0\0\0\xff\0\0\0\0\xfd\0\0\0\0\0\0\f\0\0\0\x6\v\0\0\0\x4\0\0\0\x4\0\0\0\b\0\0\0\b\xfc\0\0\0\x1\0\0\0\x2\0\0\0\x3\0\0\0\f\0t\0\x62\0P\0l\0o\0t\x1\0\0\0\0\xff\xff\xff\xff\0\0\0\0\0\0\0\0\0\0\0\x1a\0t\0\x62\0P\0o\0r\0t\0\x43\0o\0n\0t\0r\0o\0l\x1\0\0\0\xdb\xff\xff\xff\xff\0\0\0\0\0\0\0\0\0\0\0\x10\0t\0\x62\0R\0\x65\0\x63\0o\0r\0\x64\x1\0\0\x2\x19\xff\xff\xff\xff\0\0\0\0\0\0\0\0)
[Plot]
autoScale=false
darkBackground=true
decimals=2
grid=true
indexAsX=true
legend=true
legendPos=bottomright
lineThickness=2
minorGrid=true
multiPlot=false
numLines=1000
numOfSamples=1000
plotWidth=1000
symbols=auto
xMax=100
xMin=0
yMax=750
yMin=550
[Port]
baudRate=115200
dataBits=8
flowControl=none
parity=none
selectedPort=ttyACM0
stopBits=1
[Record]
autoIncrement=true
decimals=2
disableBuffering=false
header=true
recordPaused=true
separator=","
stopOnClose=true
timestamp=true
timestampFormat=seconds_with_precision
[UpdateCheck]
lastCheck=2025-06-29
periodicCheck=true

View file

@ -0,0 +1,70 @@
/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor
Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/
These sensors use I2C communicate, at least 2 pins are required
to interface
based on MPR121test
modified & extended by Stefan Krüger
**********************************************************/
#include <Adafruit_MPR121.h>
#include <Wire.h>
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap = Adafruit_MPR121();
const uint8_t channelFirst = 0;
const uint8_t channelLast = 5; // max 13
uint16_t currTouched;
void plotValues() {
currTouched = cap.touched();
for (int chan = channelFirst; chan <= channelLast; chan++) {
if (currTouched & _BV(chan)) {
Serial.print("550,");
} else {
Serial.print("0,");
}
Serial.print(cap.filteredData(chan));
Serial.print(",");
Serial.print(cap.filteredData(chan));
Serial.print(", ");
// uint8_t reg = cap.readRegister8(0x5F + chan);
// if (reg < 10)
// Serial.print(" ");
// Serial.print(reg);
}
Serial.println();
}
void setup() {
delay(1000);
Serial.begin(115200);
while (!Serial);
Serial.println("MPR121 Autoconfiguration Test. (MPR121-AutoConfig.ino)");
Wire.begin();
if (!cap.begin(0x5A, &Wire)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
cap.setAutoconfig(true);
}
void loop() {
plotValues();
delay(10);
}