diff --git a/.codespellrc b/.codespellrc index 101edae..3306024 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,7 +1,7 @@ # See: https://github.com/codespell-project/codespell#using-a-config-file [codespell] # In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: -ignore-words-list = , +ignore-words-list = als, check-filenames = check-hidden = skip = ./.git diff --git a/examples/InterruptLight/InterruptLight.ino b/examples/InterruptLight/InterruptLight.ino new file mode 100644 index 0000000..f9ef6e8 --- /dev/null +++ b/examples/InterruptLight/InterruptLight.ino @@ -0,0 +1,70 @@ +/* + APDS9960 - Light/Color Sensor, Interrupt + + This example reads the light (C) and color (RGB) data from the on-board APDS9960 + sensor of the Nano 33 BLE Sense and prints the proximity value to the Serial Monitor. + If the measure is outside the interrupt thresholds it will trigger an interrupt. + + + The circuit: + - Arduino Nano 33 BLE Sense + + This example code is in the public domain. +*/ + +#include + +int interrupt_pin = 2; // pin connected to the Interrupt pin from the sensor + +// Thresholds to trigger the interrupt +unsigned int lower_threshold = 0; +unsigned int higher_threshold = 500; + +// Flag to know if the interrupt has been triggered +int interrupt_flag = 0; + +void setup() { + + Serial.begin(9600); + while (!Serial); + + // Attach interrupt + attachInterrupt(interrupt_pin, assert_flag, FALLING); + + if (!APDS.begin()) { + Serial.println("Error initializing APDS9960 sensor!"); + } + + //Enable the Light Interrupt generation on the Interrupt pin + APDS.enableLightInterrupt(); + // Config the Thresholds + APDS.setLightLowThreshold(lower_threshold); + APDS.setLightHighThreshold(higher_threshold); +} + +void loop() { + // Output the proximity + int r,g,b,c; + if (APDS.colorAvailable()) { + APDS.readColor(r, g, b,c); + } + Serial.println(c); + + // If the interrupt flag has been asserted, clear the interrupt of the sensor + if ( interrupt_flag == 1 ) { + Serial.println("Flagged!"); + + delay(1000); + + interrupt_flag = 0; + APDS.clearLightInterrupt(); + Serial.println("Flag and interrupt cleared"); + delay(5000); + } + + delay(100); +} + +void assert_flag() { + interrupt_flag = 1; +} diff --git a/examples/InterruptProximity/InterruptProximity.ino b/examples/InterruptProximity/InterruptProximity.ino new file mode 100644 index 0000000..27ecf2e --- /dev/null +++ b/examples/InterruptProximity/InterruptProximity.ino @@ -0,0 +1,68 @@ +/* + APDS9960 - Proximity Sensor, Interrupt + + This example reads proximity data from the on-board APDS9960 sensor of the + Nano 33 BLE Sense and prints the proximity value to the Serial Monitor. If + the measure is outside the interrupt thresholds it will trigger an interrupt. + + + The circuit: + - Arduino Nano 33 BLE Sense + + This example code is in the public domain. +*/ + +#include + +int interrupt_pin = 2; // pin connected to the Interrupt pin from the sensor + +// Thresholds to trigger the interrupt +unsigned int lower_threshold = 0; +unsigned int higher_threshold = 150; + +// Flag to know if the interrupt has been triggered +int interrupt_flag = 0; + +void setup() { + + Serial.begin(9600); + while (!Serial); + + // Attach interrupt + attachInterrupt(interrupt_pin, assert_flag, FALLING); + + if (!APDS.begin()) { + Serial.println("Error initializing APDS9960 sensor!"); + } + + //Enable the proximity Interrupt generation on the Interrupt pin + APDS.enableProximityInterrupt(); + // Config the Thresholds + APDS.setProximityLowThreshold(lower_threshold); + APDS.setProximityHighThreshold(higher_threshold); +} + +void loop() { + // Output the proximity + if (APDS.proximityAvailable()) { + Serial.println(APDS.readProximity()); + } + + // If the interrupt flag has been asserted, clear the interrupt of the sensor + if ( interrupt_flag == 1 ) { + Serial.println("Flagged!"); + + delay(1000); + + interrupt_flag = 0; + APDS.clearProximityInterrupt(); + Serial.println("Flag and interrupt clear"); + delay(500); + } + + delay(100); +} + +void assert_flag() { + interrupt_flag = 1; +} diff --git a/keywords.txt b/keywords.txt index 48ec020..0b41a00 100644 --- a/keywords.txt +++ b/keywords.txt @@ -25,6 +25,21 @@ setGestureSensitivity KEYWORD2 setInterruptPin KEYWORD2 setLEDBoost KEYWORD2 +setInterruptPin KEYWORD2 +enableProximityInterrupt KEYWORD2 +disableProximityInterrupt KEYWORD2 +proximityInterrupt KEYWORD2 +clearProximityInterrupt KEYWORD2 +setProximityLowThreshold KEYWORD2 +setProximityHighThreshold KEYWORD2 + +enableLightInterrupt KEYWORD2 +disableLightInterrupt KEYWORD2 +lightInterrupt KEYWORD2 +clearLightInterrupt KEYWORD2 +setLightLowThreshold KEYWORD2 +setLightHighThreshold KEYWORD2 + ########################################### # Constants ########################################### diff --git a/library.properties b/library.properties index fd9ca36..cba5820 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arduino_APDS9960 -version=1.0.3 +version=1.1.0 author=Arduino maintainer=Arduino sentence=A library for the APDS-9960 sensor diff --git a/src/Arduino_APDS9960.cpp b/src/Arduino_APDS9960.cpp index 2cb343b..be3e56d 100644 --- a/src/Arduino_APDS9960.cpp +++ b/src/Arduino_APDS9960.cpp @@ -52,7 +52,7 @@ bool APDS9960::begin() { if (!setWTIME(0xFF)) return false; if (!setGPULSE(0x8F)) return false; // 16us, 16 pulses // default is: 0x40 = 8us, 1 pulse if (!setPPULSE(0x8F)) return false; // 16us, 16 pulses // default is: 0x40 = 8us, 1 pulse - if (!setGestureIntEnable(true)) return false; + //if (!setGestureIntEnable(true)) return false; if (!setGestureMode(true)) return false; if (!enablePower()) return false; if (!enableWait()) return false; @@ -64,6 +64,11 @@ bool APDS9960::begin() { // enable power if (!enablePower()) return false; + //Clear remaining interrupts + if(!setPERS(0b00010001)) return false; + /*clearLightInterrupt(); + clearProximityInterrupt();*/ + if (_intPin > -1) { pinMode(_intPin, INPUT); } @@ -428,15 +433,99 @@ int APDS9960::readProximity() { return -1; } - disableProximity(); + //disableProximity(); return (255 - r); } + +// Interrupts related functions +// Proximity Interrupt +void APDS9960::enableProximityInterrupt(){ + uint8_t data; + getENABLE(&data); + data &= 0b01111111; //MSB reserved + data |= 0b1; //PowerON + data |= 0b00100000; //Int prox + setENABLE(data); +} + +void APDS9960::disableProximityInterrupt(){ + uint8_t data; + getENABLE(&data); + data &= 0b01111111; //MSB reserved + data |= 0b1; //PowerON + data &= 0b01011111; //Int prox disable + setENABLE(data); +} + +bool APDS9960::proximityInterrupt(){ + uint8_t data; + getENABLE(&data); + data &= 0b100000; + return bool(data >> 5); +} + +void APDS9960::clearProximityInterrupt(){ + setPICLEAR(0b1); +} + +void APDS9960::setProximityLowThreshold(uint8_t newThold){ + setPILT(newThold); +} + +void APDS9960::setProximityHighThreshold(uint8_t newThold){ + setPIHT(newThold); +} + +// Light interrupt (Clear channel from RGBC) +void APDS9960::enableLightInterrupt(){ + uint8_t data; + getENABLE(&data); + data &= 0b01111111; //MSB reserved + data |= 0b1; //PowerON + data |= 0b00010000; //ALS + setENABLE(data); +} + +void APDS9960::disableLightInterrupt(){ + uint8_t data; + getENABLE(&data); + data &= 0b01111111; //MSB reserved + data |= 0b1; //PowerON + data &= 0b01101111; //ALS disable + setENABLE(data); +} + +bool APDS9960::lightInterrupt(){ + uint8_t data; + getSTATUS(&data); + data &= 0b010000; + return bool(data >> 4); +} + +void APDS9960::clearLightInterrupt(){ + //Only non gesture ones + setAICLEAR(0b1); +} + +void APDS9960::setLightLowThreshold(uint16_t newThold){ + setAILTL(newThold); + setAIHTL(newThold >> 8); +} + +void APDS9960::setLightHighThreshold(uint16_t newThold){ + setAILTL(newThold); + setAIHTL(newThold >> 8); + + uint8_t final; + getENABLE(&final); +} + #if defined(APDS9960_INT_PIN) APDS9960 APDS(APDS9960_WIRE_INSTANCE, APDS9960_INT_PIN); #elif defined(ARDUINO_ARDUINO_NANO33BLE) APDS9960 APDS(Wire1, PIN_INT_APDS); #else APDS9960 APDS(Wire, -1); -#endif +#endif \ No newline at end of file diff --git a/src/Arduino_APDS9960.h b/src/Arduino_APDS9960.h index f81f05a..902bd74 100644 --- a/src/Arduino_APDS9960.h +++ b/src/Arduino_APDS9960.h @@ -55,6 +55,23 @@ class APDS9960 { bool setLEDBoost(uint8_t boost); + // Interrupts + // Proximity interrupt + void enableProximityInterrupt(); + void disableProximityInterrupt(); + bool proximityInterrupt(); + void clearProximityInterrupt(); + void setProximityLowThreshold(uint8_t newThold); + void setProximityHighThreshold(uint8_t newThold); + + // Light interrupt (Clear channel from RGBC) + void enableLightInterrupt(); + void disableLightInterrupt(); + bool lightInterrupt(); + void clearLightInterrupt(); + void setLightLowThreshold(uint16_t newThold); + void setLightHighThreshold(uint16_t newThold); + private: bool setGestureIntEnable(bool en); bool setGestureMode(bool en); @@ -72,6 +89,7 @@ class APDS9960 { bool enableGesture(); bool disableGesture(); + private: TwoWire& _wire; int _intPin; @@ -150,4 +168,4 @@ class APDS9960 { extern APDS9960 APDS; -#endif // ARDUINO_APDS9960 +#endif // ARDUINO_APDS9960 \ No newline at end of file