From f7705b4bcfd742242d63d418e5674576d977a9fa Mon Sep 17 00:00:00 2001 From: marqdevx Date: Thu, 18 Feb 2021 20:15:55 +0100 Subject: [PATCH 1/6] Added interrupt feature --- src/Arduino_APDS9960.cpp | 92 +++++++++++++++++++++++++++++++++++++++- src/Arduino_APDS9960.h | 18 ++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/Arduino_APDS9960.cpp b/src/Arduino_APDS9960.cpp index bb56701..d2a2f41 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,11 +433,94 @@ 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); +} + + #if defined(APDS9960_INT_PIN) APDS9960 APDS(APDS9960_WIRE_INSTANCE, APDS9960_INT_PIN); #elif defined(ARDUINO_ARDUINO_NANO33BLE) diff --git a/src/Arduino_APDS9960.h b/src/Arduino_APDS9960.h index f81f05a..4777b15 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; From d564a3660cba83df4eb386ae8466d1b5648250eb Mon Sep 17 00:00:00 2001 From: marqdevx Date: Fri, 19 Mar 2021 11:42:45 +0100 Subject: [PATCH 2/6] Add files via upload --- examples/InterruptLight/InterruptLight.ino | 69 +++++++++++++++++++ .../InterruptProximity/InterruptProximity.ino | 67 ++++++++++++++++++ keywords.txt | 15 ++++ library.properties | 2 +- src/Arduino_APDS9960.cpp | 15 +++- 5 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 examples/InterruptLight/InterruptLight.ino create mode 100644 examples/InterruptProximity/InterruptProximity.ino diff --git a/examples/InterruptLight/InterruptLight.ino b/examples/InterruptLight/InterruptLight.ino new file mode 100644 index 0000000..9b151f7 --- /dev/null +++ b/examples/InterruptLight/InterruptLight.ino @@ -0,0 +1,69 @@ +/* + 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 + then if the measure its 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 clear"); + delay(5000); + } + + delay(100); +} + +void assert_flag() { + interrupt_flag = 1; +} \ No newline at end of file diff --git a/examples/InterruptProximity/InterruptProximity.ino b/examples/InterruptProximity/InterruptProximity.ino new file mode 100644 index 0000000..c628a1c --- /dev/null +++ b/examples/InterruptProximity/InterruptProximity.ino @@ -0,0 +1,67 @@ +/* + 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 then if + the measure its 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; +} \ No newline at end of file diff --git a/keywords.txt b/keywords.txt index 48ec020..3cc928c 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 6f102f6..e5a1b76 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 APDS9960 sensor diff --git a/src/Arduino_APDS9960.cpp b/src/Arduino_APDS9960.cpp index d2a2f41..6859fde 100644 --- a/src/Arduino_APDS9960.cpp +++ b/src/Arduino_APDS9960.cpp @@ -66,8 +66,8 @@ bool APDS9960::begin() { //Clear remaining interrupts if(!setPERS(0b00010001)) return false; - clearLightInterrupt(); - clearProximityInterrupt(); + /*clearLightInterrupt(); + clearProximityInterrupt();*/ if (_intPin > -1) { pinMode(_intPin, INPUT); @@ -511,13 +511,24 @@ void APDS9960::clearLightInterrupt(){ } void APDS9960::setLightLowThreshold(uint16_t newThold){ + Serial.print("set to "); + Serial.println(newThold); setAILTL(newThold); setAIHTL(newThold >> 8); + + } void APDS9960::setLightHighThreshold(uint16_t newThold){ + setAILTL(newThold); setAIHTL(newThold >> 8); + + + uint8_t final; + getENABLE(&final); + Serial.print("data: "); + Serial.println(final,BIN); } From 71fd0c416854d480a0ed0a03746d6ebb41908b15 Mon Sep 17 00:00:00 2001 From: marqdevx Date: Mon, 7 Jun 2021 11:00:46 +0200 Subject: [PATCH 3/6] Beautfied code --- examples/InterruptLight/InterruptLight.ino | 2 +- src/Arduino_APDS9960.cpp | 8 +------- src/Arduino_APDS9960.h | 4 ++-- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/examples/InterruptLight/InterruptLight.ino b/examples/InterruptLight/InterruptLight.ino index 9b151f7..b7ad3c1 100644 --- a/examples/InterruptLight/InterruptLight.ino +++ b/examples/InterruptLight/InterruptLight.ino @@ -57,7 +57,7 @@ void loop() { interrupt_flag = 0; APDS.clearLightInterrupt(); - Serial.println("Flag and interrupt clear"); + Serial.println("Flag and interrupt cleared"); delay(5000); } diff --git a/src/Arduino_APDS9960.cpp b/src/Arduino_APDS9960.cpp index 28d1c4b..41b9ee4 100644 --- a/src/Arduino_APDS9960.cpp +++ b/src/Arduino_APDS9960.cpp @@ -440,7 +440,6 @@ int APDS9960::readProximity() { // Interrupts related functions - // Proximity Interrupt void APDS9960::enableProximityInterrupt(){ uint8_t data; @@ -515,27 +514,22 @@ void APDS9960::setLightLowThreshold(uint16_t newThold){ Serial.println(newThold); setAILTL(newThold); setAIHTL(newThold >> 8); - - } void APDS9960::setLightHighThreshold(uint16_t newThold){ - setAILTL(newThold); setAIHTL(newThold >> 8); - uint8_t final; getENABLE(&final); Serial.print("data: "); Serial.println(final,BIN); } - #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 4777b15..902bd74 100644 --- a/src/Arduino_APDS9960.h +++ b/src/Arduino_APDS9960.h @@ -55,7 +55,7 @@ class APDS9960 { bool setLEDBoost(uint8_t boost); - //Interrupts + // Interrupts // Proximity interrupt void enableProximityInterrupt(); void disableProximityInterrupt(); @@ -168,4 +168,4 @@ class APDS9960 { extern APDS9960 APDS; -#endif // ARDUINO_APDS9960 +#endif // ARDUINO_APDS9960 \ No newline at end of file From d046f50ecd2bccd2612d2368c9b5e3f1d38a84e2 Mon Sep 17 00:00:00 2001 From: marqdevx Date: Mon, 7 Jun 2021 12:07:16 +0200 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: per1234 --- examples/InterruptLight/InterruptLight.ino | 7 +++-- .../InterruptProximity/InterruptProximity.ino | 7 +++-- keywords.txt | 28 +++++++++---------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/examples/InterruptLight/InterruptLight.ino b/examples/InterruptLight/InterruptLight.ino index 9b151f7..c136f8d 100644 --- a/examples/InterruptLight/InterruptLight.ino +++ b/examples/InterruptLight/InterruptLight.ino @@ -2,8 +2,9 @@ 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 - then if the measure its outside the interrupt thresholds it will trigger an interrupt. + 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 @@ -66,4 +67,4 @@ void loop() { void assert_flag() { interrupt_flag = 1; -} \ No newline at end of file +} diff --git a/examples/InterruptProximity/InterruptProximity.ino b/examples/InterruptProximity/InterruptProximity.ino index c628a1c..27ecf2e 100644 --- a/examples/InterruptProximity/InterruptProximity.ino +++ b/examples/InterruptProximity/InterruptProximity.ino @@ -2,8 +2,9 @@ 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 then if - the measure its outside the interrupt thresholds it will trigger an interrupt. + 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 @@ -64,4 +65,4 @@ void loop() { void assert_flag() { interrupt_flag = 1; -} \ No newline at end of file +} diff --git a/keywords.txt b/keywords.txt index 3cc928c..0b41a00 100644 --- a/keywords.txt +++ b/keywords.txt @@ -25,20 +25,20 @@ 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 +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 From 6bb9e72527c2666b59b2df4045fbc3786e9bdf11 Mon Sep 17 00:00:00 2001 From: marqdevx Date: Mon, 7 Jun 2021 12:11:39 +0200 Subject: [PATCH 5/6] Removed Serial for debugging --- src/Arduino_APDS9960.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Arduino_APDS9960.cpp b/src/Arduino_APDS9960.cpp index 41b9ee4..be3e56d 100644 --- a/src/Arduino_APDS9960.cpp +++ b/src/Arduino_APDS9960.cpp @@ -510,8 +510,6 @@ void APDS9960::clearLightInterrupt(){ } void APDS9960::setLightLowThreshold(uint16_t newThold){ - Serial.print("set to "); - Serial.println(newThold); setAILTL(newThold); setAIHTL(newThold >> 8); } @@ -522,8 +520,6 @@ void APDS9960::setLightHighThreshold(uint16_t newThold){ uint8_t final; getENABLE(&final); - Serial.print("data: "); - Serial.println(final,BIN); } #if defined(APDS9960_INT_PIN) From bb0a9b40fab9a5e1f4cbf98513bf2c8ee95adfa4 Mon Sep 17 00:00:00 2001 From: marqdevx Date: Mon, 7 Jun 2021 12:21:29 +0200 Subject: [PATCH 6/6] update ignore-words --- .codespellrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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