Skip to content

Conversation

@s-light
Copy link
Contributor

@s-light s-light commented Jun 11, 2025

fixes #42
i think my changes are fully backwards-compatible.
so old use-cases are not at risk.

please @wystewart test it.
i have no board at hand at the moment.

@caternuson
Copy link
Contributor

@s-light
Copy link
Contributor Author

s-light commented Jun 11, 2025

thanks caternuson!
i just found exactly that guide ;-)
and it seems i have got it working ;-)

@caternuson
Copy link
Contributor

This seems to work with a basic functional check. Here's the test sketch:

#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() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("MPR121 Configuration Dump.");

  if (!cap.begin(0x5A)) {
    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;
}

And resulting output:
Screenshot from 2025-06-13 09-59-07

Only the CDC values are changed, but maybe that's expected.

It'd be good if this could be tested (@wystewart ?) in a more realistic setup to see if it has the desired benefits there.

@caternuson
Copy link
Contributor

Going to go ahead and merge. This PR will also fix the compile warn brought up in #45.

@caternuson caternuson merged commit 94f3a35 into adafruit:master Jul 8, 2025
1 check passed
@s-light s-light deleted the autoconfig branch July 9, 2025 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autoconfig function

2 participants