From 7a3c5e6055fa569c7ecae73a249b3350194b7478 Mon Sep 17 00:00:00 2001 From: amarotica Date: Mon, 8 Jul 2024 10:06:30 -0700 Subject: [PATCH] Added proximity sensing support --- src/CAP1293.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ src/CAP1293.h | 26 +++++++++++++++++++++ src/CAP1293_Registers.h | 5 ++++ 3 files changed, 82 insertions(+) diff --git a/src/CAP1293.cpp b/src/CAP1293.cpp index 252b553..dbb1cfc 100644 --- a/src/CAP1293.cpp +++ b/src/CAP1293.cpp @@ -979,4 +979,55 @@ void CAP1293::writeRegisters(CAP1293_Register reg, byte *buffer, byte len) for (int i = 0; i < len; i++) _i2cPort->write(buffer[i]); _i2cPort->endTransmission(); // Stop transmitting +} + +void CAP1293::enableSignalGuard(uint8_t sensorInput, bool enable) { + SIGNAL_GUARD_ENABLE_REG reg; + reg.SIGNAL_GUARD_ENABLE_COMBINED = readRegister(SIGNAL_GUARD_ENABLE_REGISTER); + + if (sensorInput == 1) { + reg.SIGNAL_GUARD_ENABLE_FIELDS.CS1_SG_EN = enable ? 1 : 0; + } else if (sensorInput == 3) { + reg.SIGNAL_GUARD_ENABLE_FIELDS.CS3_SG_EN = enable ? 1 : 0; + } + + writeRegister(SIGNAL_GUARD_ENABLE_REGISTER, reg.SIGNAL_GUARD_ENABLE_COMBINED); +} + +int8_t CAP1293::readRawData(uint8_t sensorInput) { + + switch (sensorInput) { + case 1: + return readRegister(CS1_RAW_DATA_REGISTER); + break; + case 2: + return readRegister(CS2_RAW_DATA_REGISTER); + break; + case 3: + return readRegister(CS3_RAW_DATA_REGISTER); + break; + default: + return 0; // Invalid sensor input + } +} + +void CAP1293::setCalibrationSensitivity(uint8_t sensorInput, uint8_t gain) { + CALIBRATION_SENSITIVITY_REG reg; + reg.CALIBRATION_SENSITIVITY_COMBINED = readRegister(CALIBRATION_SENSITIVITY_REGISTER); + + switch (sensorInput) { + case 1: + reg.CALIBRATION_SENSITIVITY_FIELDS.CALSEN1 = gain; + break; + case 2: + reg.CALIBRATION_SENSITIVITY_FIELDS.CALSEN2 = gain; + break; + case 3: + reg.CALIBRATION_SENSITIVITY_FIELDS.CALSEN3 = gain; + break; + default: + return; // Invalid sensor input + } + + writeRegister(CALIBRATION_SENSITIVITY_REGISTER, reg.CALIBRATION_SENSITIVITY_COMBINED); } \ No newline at end of file diff --git a/src/CAP1293.h b/src/CAP1293.h index df6439d..e1c5b7f 100644 --- a/src/CAP1293.h +++ b/src/CAP1293.h @@ -213,6 +213,28 @@ typedef union uint8_t MULTIPLE_TOUCH_PATTERN_COMBINED; } MULTIPLE_TOUCH_PATTERN_REG; +// Structures to represent register fields +typedef union { + uint8_t SIGNAL_GUARD_ENABLE_COMBINED; + struct { + uint8_t CS1_SG_EN : 1; + uint8_t reserved : 1; + uint8_t CS3_SG_EN : 1; + uint8_t reserved2 : 5; + } SIGNAL_GUARD_ENABLE_FIELDS; +} SIGNAL_GUARD_ENABLE_REG; + +// Register address for calibration sensitivity configuration +typedef union { + uint8_t CALIBRATION_SENSITIVITY_COMBINED; + struct { + uint8_t CALSEN1 : 2; + uint8_t CALSEN2 : 2; + uint8_t CALSEN3 : 2; + uint8_t reserved : 2; + } CALIBRATION_SENSITIVITY_FIELDS; +} CALIBRATION_SENSITIVITY_REG; + //////////////////////////////// // CAP1293 Class Declaration // //////////////////////////////// @@ -291,6 +313,10 @@ class CAP1293 void setReleaseInterruptEnabled(); bool isReleaseInterruptEnabled(); + void enableSignalGuard(uint8_t sensorInput, bool enable); + int8_t readRawData(uint8_t sensorInput); + void setCalibrationSensitivity(uint8_t sensorInput, uint8_t gain); + private: TwoWire *_i2cPort = NULL; //The generic connection to user's chosen I2C hardware uint8_t _deviceAddress; //Keeps track of I2C address. setI2CAddress changes this. diff --git a/src/CAP1293_Registers.h b/src/CAP1293_Registers.h index cbb3586..0474356 100644 --- a/src/CAP1293_Registers.h +++ b/src/CAP1293_Registers.h @@ -56,6 +56,11 @@ enum CAP1293_Register PROD_ID = 0xFD, MANUFACTURE_ID = 0xFE, REVISION = 0xFF, + SIGNAL_GUARD_ENABLE_REGISTER = 0x29, + CS1_RAW_DATA_REGISTER = 0x10, + CS2_RAW_DATA_REGISTER = 0x11, + CS3_RAW_DATA_REGISTER = 0x12, + CALIBRATION_SENSITIVITY_REGISTER = 0x80, }; #endif // __CAP1293_REGISTERS_H__ \ No newline at end of file