Skip to content

Added third ID and support for both Wire and Wire1 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions src/SparkFun_APDS9960.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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++;
}

Expand Down
260 changes: 131 additions & 129 deletions src/SparkFun_APDS9960.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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