-
Notifications
You must be signed in to change notification settings - Fork 25
Add interrupt feature #15
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
base: master
Are you sure you want to change the base?
Changes from all commits
f7705b4
d564a36
95cbcc6
71fd0c4
d046f50
c21b743
6bb9e72
bb0a9b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Arduino_APDS9960.h> | ||
|
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Arduino_APDS9960.h> | ||
|
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not fine, if useless remove the cmment otherwise add and fix incompatibility |
||
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();*/ | ||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
if (_intPin > -1) { | ||
pinMode(_intPin, INPUT); | ||
} | ||
|
@@ -428,15 +433,99 @@ int APDS9960::readProximity() { | |
return -1; | ||
} | ||
|
||
disableProximity(); | ||
//disableProximity(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
return (255 - r); | ||
} | ||
|
||
|
||
// Interrupts related functions | ||
// Proximity Interrupt | ||
void APDS9960::enableProximityInterrupt(){ | ||
uint8_t data; | ||
getENABLE(&data); | ||
data &= 0b01111111; //MSB reserved | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no number create macros and give a meanings to each action |
||
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 | ||
Comment on lines
+456
to
+458
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no number create macros and give a meanings to each action |
||
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 | ||
Comment on lines
+485
to
+487
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no number create macros and give a meanings to each action |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add new line here |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add new line here |
Uh oh!
There was an error while loading. Please reload this page.