Skip to content

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

Open
wants to merge 8 commits 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
2 changes: 1 addition & 1 deletion .codespellrc
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
70 changes: 70 additions & 0 deletions examples/InterruptLight/InterruptLight.ino
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;
}
68 changes: 68 additions & 0 deletions examples/InterruptProximity/InterruptProximity.ino
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;
}
15 changes: 15 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
###########################################
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino_APDS9960
version=1.0.3
version=1.1.0
author=Arduino
maintainer=Arduino <[email protected]>
sentence=A library for the APDS-9960 sensor
Expand Down
95 changes: 92 additions & 3 deletions src/Arduino_APDS9960.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

@Rocketct Rocketct Jul 26, 2021

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


if (_intPin > -1) {
pinMode(_intPin, INPUT);
}
Expand Down Expand Up @@ -428,15 +433,99 @@ int APDS9960::readProximity() {
return -1;
}

disableProximity();
//disableProximity();

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line here

20 changes: 19 additions & 1 deletion src/Arduino_APDS9960.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -72,6 +89,7 @@ class APDS9960 {
bool enableGesture();
bool disableGesture();


private:
TwoWire& _wire;
int _intPin;
Expand Down Expand Up @@ -150,4 +168,4 @@ class APDS9960 {

extern APDS9960 APDS;

#endif // ARDUINO_APDS9960
#endif // ARDUINO_APDS9960

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line here