From c19133a085ed859d980f01c69f467e5e0c6279cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesse=20Sadow=C3=BD?= <101496888+Jim-23@users.noreply.github.com> Date: Wed, 12 Mar 2025 15:35:23 +0100 Subject: [PATCH] Added third ID and support for both Wire and Wire1 In this version I added a new ID that the library recognise (for some kind of cheap chinese sensor) and I also Added support for Wire1 so you can create 2 instances like this: SparkFun_APDS9960 s1(Wire); // First sensor on Wire (SDA/SCL) SparkFun_APDS9960 s2(Wire1); // Second sensor on Wire1 (SDA1/SCL1) --- src/SparkFun_APDS9960.cpp | 49 +++---- src/SparkFun_APDS9960.h | 260 +++++++++++++++++++------------------- 2 files changed, 156 insertions(+), 153 deletions(-) diff --git a/src/SparkFun_APDS9960.cpp b/src/SparkFun_APDS9960.cpp index 530c1f6..8663d6f 100644 --- a/src/SparkFun_APDS9960.cpp +++ b/src/SparkFun_APDS9960.cpp @@ -24,10 +24,11 @@ /** * @brief Constructor - Instantiates SparkFun_APDS9960 object */ -SparkFun_APDS9960::SparkFun_APDS9960() +SparkFun_APDS9960::SparkFun_APDS9960(TwoWire &wirePort) { - gesture_ud_delta_ = 0; - gesture_lr_delta_ = 0; + i2cPort = &wirePort; + gesture_ud_delta_ = 0; + gesture_lr_delta_ = 0; gesture_ud_count_ = 0; gesture_lr_count_ = 0; @@ -57,13 +58,13 @@ bool SparkFun_APDS9960::init() uint8_t id; /* Initialize I2C */ - Wire.begin(); - - /* Read ID register and check against known values for APDS-9960 */ + i2cPort->begin(); + + /* Read ID register and check against known values for APDS-9960 */ if( !wireReadDataByte(APDS9960_ID, id) ) { return false; } - if( !(id == APDS9960_ID_1 || id == APDS9960_ID_2) ) { + if( !(id == APDS9960_ID_1 || id == APDS9960_ID_2 || id == ADPS9960_ID_3) ) { return false; } @@ -2120,9 +2121,9 @@ bool SparkFun_APDS9960::setGestureMode(uint8_t mode) */ bool SparkFun_APDS9960::wireWriteByte(uint8_t val) { - Wire.beginTransmission(APDS9960_I2C_ADDR); - Wire.write(val); - if( Wire.endTransmission() != 0 ) { + i2cPort->beginTransmission(APDS9960_I2C_ADDR); + i2cPort->write(val); + if( i2cPort->endTransmission() != 0 ) { return false; } @@ -2138,10 +2139,10 @@ bool SparkFun_APDS9960::wireWriteByte(uint8_t val) */ bool SparkFun_APDS9960::wireWriteDataByte(uint8_t reg, uint8_t val) { - Wire.beginTransmission(APDS9960_I2C_ADDR); - Wire.write(reg); - Wire.write(val); - if( Wire.endTransmission() != 0 ) { + i2cPort->beginTransmission(APDS9960_I2C_ADDR); + i2cPort->write(reg); + i2cPort->write(val); + if( i2cPort->endTransmission() != 0 ) { return false; } @@ -2162,12 +2163,12 @@ bool SparkFun_APDS9960::wireWriteDataBlock( uint8_t reg, { unsigned int i; - Wire.beginTransmission(APDS9960_I2C_ADDR); - Wire.write(reg); + i2cPort->beginTransmission(APDS9960_I2C_ADDR); + i2cPort->write(reg); for(i = 0; i < len; i++) { - Wire.beginTransmission(val[i]); + i2cPort->beginTransmission(val[i]); } - if( Wire.endTransmission() != 0 ) { + if( i2cPort->endTransmission() != 0 ) { return false; } @@ -2190,9 +2191,9 @@ bool SparkFun_APDS9960::wireReadDataByte(uint8_t reg, uint8_t &val) } /* Read from register */ - Wire.requestFrom(APDS9960_I2C_ADDR, 1); - while (Wire.available()) { - val = Wire.read(); + i2cPort->requestFrom(APDS9960_I2C_ADDR, 1); + while (i2cPort->available()) { + val = i2cPort->read(); } return true; @@ -2218,12 +2219,12 @@ int SparkFun_APDS9960::wireReadDataBlock( uint8_t reg, } /* Read block data */ - Wire.requestFrom(APDS9960_I2C_ADDR, len); - while (Wire.available()) { + i2cPort->requestFrom(APDS9960_I2C_ADDR, len); + while (i2cPort->available()) { if (i >= len) { return -1; } - val[i] = Wire.read(); + val[i] = i2cPort->read(); i++; } diff --git a/src/SparkFun_APDS9960.h b/src/SparkFun_APDS9960.h index 8266b45..080aa46 100644 --- a/src/SparkFun_APDS9960.h +++ b/src/SparkFun_APDS9960.h @@ -32,7 +32,8 @@ /* Acceptable device IDs */ #define APDS9960_ID_1 0xAB -#define APDS9960_ID_2 0x9C +#define APDS9960_ID_2 0x9C +#define ADPS9960_ID_3 0x9E /* Misc parameters */ #define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads @@ -211,145 +212,146 @@ enum { /* Container for gesture data */ typedef struct gesture_data_type { - uint8_t u_data[32]; - uint8_t d_data[32]; - uint8_t l_data[32]; - uint8_t r_data[32]; - uint8_t index; - uint8_t total_gestures; - uint8_t in_threshold; - uint8_t out_threshold; + uint8_t u_data[32]; + uint8_t d_data[32]; + uint8_t l_data[32]; + uint8_t r_data[32]; + uint8_t index; + uint8_t total_gestures; + uint8_t in_threshold; + uint8_t out_threshold; } gesture_data_type; /* APDS9960 Class */ class SparkFun_APDS9960 { public: - /* Initialization methods */ - SparkFun_APDS9960(); - ~SparkFun_APDS9960(); - bool init(); - uint8_t getStatusRegister(); - uint8_t getMode(); - bool setMode(uint8_t mode, uint8_t enable); - - /* Turn the APDS-9960 on and off */ - bool enablePower(); - bool disablePower(); - - /* Enable or disable specific sensors */ - bool enableLightSensor(bool interrupts = false); - bool disableLightSensor(); - bool enableProximitySensor(bool interrupts = false); - bool disableProximitySensor(); - bool enableGestureSensor(bool interrupts = true); - bool disableGestureSensor(); - - /* LED drive strength control */ - uint8_t getLEDDrive(); - bool setLEDDrive(uint8_t drive); - uint8_t getGestureLEDDrive(); - bool setGestureLEDDrive(uint8_t drive); - - /* Gain control */ - uint8_t getAmbientLightGain(); - bool setAmbientLightGain(uint8_t gain); - uint8_t getProximityGain(); - bool setProximityGain(uint8_t gain); - uint8_t getGestureGain(); - bool setGestureGain(uint8_t gain); - - /* Get and set light interrupt thresholds */ - bool getLightIntLowThreshold(uint16_t &threshold); - bool setLightIntLowThreshold(uint16_t threshold); - bool getLightIntHighThreshold(uint16_t &threshold); - bool setLightIntHighThreshold(uint16_t threshold); - - /* Get and set proximity interrupt thresholds */ - bool getProximityIntLowThreshold(uint8_t &threshold); - bool setProximityIntLowThreshold(uint8_t threshold); - bool getProximityIntHighThreshold(uint8_t &threshold); - bool setProximityIntHighThreshold(uint8_t threshold); - - /* Get and set interrupt enables */ - uint8_t getAmbientLightIntEnable(); - bool setAmbientLightIntEnable(uint8_t enable); - uint8_t getProximityIntEnable(); - bool setProximityIntEnable(uint8_t enable); - uint8_t getGestureIntEnable(); - bool setGestureIntEnable(uint8_t enable); - - /* Clear interrupts */ - bool clearAmbientLightInt(); - bool clearProximityInt(); - - /* Ambient light methods */ - bool readAmbientLight(uint16_t &val); - bool readRedLight(uint16_t &val); - bool readGreenLight(uint16_t &val); - bool readBlueLight(uint16_t &val); - - /* Proximity methods */ - bool readProximity(uint8_t &val); - - /* Gesture methods */ - bool isGestureAvailable(); - int readGesture(); - + /* Initialization methods */ + SparkFun_APDS9960(TwoWire &wirePort = Wire); + ~SparkFun_APDS9960(); + bool init(); + uint8_t getStatusRegister(); + uint8_t getMode(); + bool setMode(uint8_t mode, uint8_t enable); + + /* Turn the APDS-9960 on and off */ + bool enablePower(); + bool disablePower(); + + /* Enable or disable specific sensors */ + bool enableLightSensor(bool interrupts = false); + bool disableLightSensor(); + bool enableProximitySensor(bool interrupts = false); + bool disableProximitySensor(); + bool enableGestureSensor(bool interrupts = true); + bool disableGestureSensor(); + + /* LED drive strength control */ + uint8_t getLEDDrive(); + bool setLEDDrive(uint8_t drive); + uint8_t getGestureLEDDrive(); + bool setGestureLEDDrive(uint8_t drive); + + /* Gain control */ + uint8_t getAmbientLightGain(); + bool setAmbientLightGain(uint8_t gain); + uint8_t getProximityGain(); + bool setProximityGain(uint8_t gain); + uint8_t getGestureGain(); + bool setGestureGain(uint8_t gain); + + /* Get and set light interrupt thresholds */ + bool getLightIntLowThreshold(uint16_t &threshold); + bool setLightIntLowThreshold(uint16_t threshold); + bool getLightIntHighThreshold(uint16_t &threshold); + bool setLightIntHighThreshold(uint16_t threshold); + + /* Get and set proximity interrupt thresholds */ + bool getProximityIntLowThreshold(uint8_t &threshold); + bool setProximityIntLowThreshold(uint8_t threshold); + bool getProximityIntHighThreshold(uint8_t &threshold); + bool setProximityIntHighThreshold(uint8_t threshold); + + /* Get and set interrupt enables */ + uint8_t getAmbientLightIntEnable(); + bool setAmbientLightIntEnable(uint8_t enable); + uint8_t getProximityIntEnable(); + bool setProximityIntEnable(uint8_t enable); + uint8_t getGestureIntEnable(); + bool setGestureIntEnable(uint8_t enable); + + /* Clear interrupts */ + bool clearAmbientLightInt(); + bool clearProximityInt(); + + /* Ambient light methods */ + bool readAmbientLight(uint16_t &val); + bool readRedLight(uint16_t &val); + bool readGreenLight(uint16_t &val); + bool readBlueLight(uint16_t &val); + + /* Proximity methods */ + bool readProximity(uint8_t &val); + + /* Gesture methods */ + bool isGestureAvailable(); + int readGesture(); + private: + TwoWire *i2cPort; - /* Gesture processing */ - void resetGestureParameters(); - bool processGestureData(); - bool decodeGesture(); + /* Gesture processing */ + void resetGestureParameters(); + bool processGestureData(); + bool decodeGesture(); - /* Proximity Interrupt Threshold */ - uint8_t getProxIntLowThresh(); - bool setProxIntLowThresh(uint8_t threshold); - uint8_t getProxIntHighThresh(); - bool setProxIntHighThresh(uint8_t threshold); - - /* LED Boost Control */ - uint8_t getLEDBoost(); - bool setLEDBoost(uint8_t boost); - - /* Proximity photodiode select */ - uint8_t getProxGainCompEnable(); - bool setProxGainCompEnable(uint8_t enable); - uint8_t getProxPhotoMask(); - bool setProxPhotoMask(uint8_t mask); - - /* Gesture threshold control */ - uint8_t getGestureEnterThresh(); - bool setGestureEnterThresh(uint8_t threshold); - uint8_t getGestureExitThresh(); - bool setGestureExitThresh(uint8_t threshold); - - /* Gesture LED, gain, and time control */ - uint8_t getGestureWaitTime(); - bool setGestureWaitTime(uint8_t time); - - /* Gesture mode */ - uint8_t getGestureMode(); - bool setGestureMode(uint8_t mode); + /* Proximity Interrupt Threshold */ + uint8_t getProxIntLowThresh(); + bool setProxIntLowThresh(uint8_t threshold); + uint8_t getProxIntHighThresh(); + bool setProxIntHighThresh(uint8_t threshold); + + /* LED Boost Control */ + uint8_t getLEDBoost(); + bool setLEDBoost(uint8_t boost); + + /* Proximity photodiode select */ + uint8_t getProxGainCompEnable(); + bool setProxGainCompEnable(uint8_t enable); + uint8_t getProxPhotoMask(); + bool setProxPhotoMask(uint8_t mask); + + /* Gesture threshold control */ + uint8_t getGestureEnterThresh(); + bool setGestureEnterThresh(uint8_t threshold); + uint8_t getGestureExitThresh(); + bool setGestureExitThresh(uint8_t threshold); + + /* Gesture LED, gain, and time control */ + uint8_t getGestureWaitTime(); + bool setGestureWaitTime(uint8_t time); + + /* Gesture mode */ + uint8_t getGestureMode(); + bool setGestureMode(uint8_t mode); - /* Raw I2C Commands */ - bool wireWriteByte(uint8_t val); - bool wireWriteDataByte(uint8_t reg, uint8_t val); - bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len); - bool wireReadDataByte(uint8_t reg, uint8_t &val); - int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len); + /* Raw I2C Commands */ + bool wireWriteByte(uint8_t val); + bool wireWriteDataByte(uint8_t reg, uint8_t val); + bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len); + bool wireReadDataByte(uint8_t reg, uint8_t &val); + int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len); - /* Members */ - gesture_data_type gesture_data_; - int gesture_ud_delta_; - int gesture_lr_delta_; - int gesture_ud_count_; - int gesture_lr_count_; - int gesture_near_count_; - int gesture_far_count_; - int gesture_state_; - int gesture_motion_; + /* Members */ + gesture_data_type gesture_data_; + int gesture_ud_delta_; + int gesture_lr_delta_; + int gesture_ud_count_; + int gesture_lr_count_; + int gesture_near_count_; + int gesture_far_count_; + int gesture_state_; + int gesture_motion_; }; #endif \ No newline at end of file