From: Phonekeo, Lila Date: Thu, 2 Oct 2025 00:29:58 +0000 (-0400) Subject: Update 06-Keypad.ino X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=d484defeeafe77fb487ddf2f65e0f9ce71cd81a2;p=gt-ece4180-lab02.git Update 06-Keypad.ino --- diff --git a/06-Keypad/06-Keypad.ino b/06-Keypad/06-Keypad.ino index 74f966a..658e858 100644 --- a/06-Keypad/06-Keypad.ino +++ b/06-Keypad/06-Keypad.ino @@ -1,6 +1,94 @@ +#include +#include + +#ifndef _BV +#define _BV(bit) (1 << (bit)) +#endif + +Adafruit_MPR121 cap = Adafruit_MPR121(); + +#define SDA_PIN 21 +#define SCL_PIN 22 + +// 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() { //example helper function + 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(SDA_PIN, SCL_PIN); + 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; }