Skip to content

issue on wemos d1 r2 mini #12

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 2 commits into
base: master
Choose a base branch
from
Open
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
83 changes: 6 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,11 @@
SparkFun APDS9960 RGB and Gesture Sensor Arduino Library
SparkFun APDS9960 RGB and Gesture Sensor Arduino Library -- Modified for esp8266
=========================================================

![Avago APDS-9960 Breakout Board - SEN-12787 ](https://cdn.sparkfun.com/r/92-92/assets/parts/9/6/0/3/12787-01.jpg)
Made a few changes to get this to work with esp8266.

[*Avago APDS-9960 Breakout Board (SEN-12787)*](https://www.sparkfun.com/products/12787)
The main two are:
* Removing wire.begin() from SparkFun_APDS9960.cpp and moving it into the examples so that the pins it uses can be specified in your sketch

Getting Started
---------------
* Changed the LED_BOOST_300 to LED_BOOST_100 in SparkFun_APDS9960.cpp as I couldn't get the gesture sensor to work without changing this

* Download the Git repository as a ZIP ("Download ZIP" button)
* Unzip
* Copy the entire library directory (APDS-9960_RGB_and_Gesture_Sensor_Arduino_Library
) to \<Arduino installation directory\>/libraries
* Open the Arduino program
* Select File -> Examples -> SparkFun_APDS9960 -> GestureTest
* Plug in your Arduino and APDS-9960 with the following connections

*-OR-*

* Use the library manager

| Arduino Pin | APDS-9960 Board | Function |
|---|---|---|
| 3.3V | VCC | Power |
| GND | GND | Ground |
| A4 | SDA | I2C Data |
| A5 | SCL | I2C Clock |
| 2 | INT | Interrupt |

* Go to Tools -> Board and select your Arduino board
* Go to Tools -> Serial Port and select the COM port of your Arduino board
* Click "Upload"
* Go to Tools -> Serial Monitor
* Ensure the baud rate is set at 9600 baud
* Swipe your hand over the sensor in various directions!

Repository Contents
-------------------

* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
* **/src** - Source files for the library (.cpp, .h).
* **library.properties** - General library properties for the Arduino package manager.

Documentation
--------------

* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
* **[Product Repository](https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor)** - Main repository (including hardware files) for the SparkFun_APDS9960 RGB and Gesture Sensor.
* **[Hookup Guide](https://learn.sparkfun.com/tutorials/apds-9960-rgb-and-gesture-sensor-hookup-guide)** - Basic hookup guide for the sensor.

Products that use this Library
---------------------------------

* [SEN-12787](https://www.sparkfun.com/products/12787)- Avago APDS-9960

Version History
---------------
* [V_1.4.1](https://github.com/sparkfun/SparkFun_APDS-9960_Sensor_Arduino_Library/tree/V_1.4.1) - Removing blank files, updating library.properties file.
* [V_1.4.0](https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor_Arduino_Library/tree/V_1.4.0) - Updated to new library structure
* V_1.3.0 - Implemented disableProximitySensor(). Thanks to jmg5150 for catching that!
* V_1.2.0 - Added pinMode line to GestureTest demo to fix interrupt bug with some Arduinos
* V_1.1.0 - Updated GestureTest demo to not freeze with fast swipes
* V_1.0.0: Initial release
* Ambient and RGB light sensing implemented
* Ambient light interrupts working
* Proximity sensing implemented
* Proximity interrupts working
* Gesture (UP, DOWN, LEFT, RIGHT, NEAR, FAR) sensing implemented

License Information
-------------------

This product is _**open source**_!

The **code** is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!

Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.

Distributed as-is; no warranty is given.

- Your friends at SparkFun.
See Sparkfun's original library here https://github.com/sparkfun/SparkFun_APDS-9960_Sensor_Arduino_Library for usage.
32 changes: 20 additions & 12 deletions examples/AmbientLightInterrupt/AmbientLightInterrupt.ino
Original file line number Diff line number Diff line change
@@ -16,14 +16,14 @@ Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Arduino Pin APDS-9960 Board Function
Wemos Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
2 INT Interrupt
13 - LED
D3 SDA I2C Data
D1 SCL I2C Clock
D6 INT Interrupt
D7 - LED
Resources:
Include Wire.h and SparkFun_APDS-9960.h
@@ -37,14 +37,19 @@ employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
Modified for ESP8266 by Jon Ulmer Nov 2016
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin
#define LED_PIN 13 // LED for showing interrupt
// Pins on wemos D1 mini
#define APDS9960_INT D6 //AKA GPIO12 -- Interupt pin
#define APDS9960_SDA D3 //AKA GPIO0
#define APDS9960_SCL D1 //AKA GPIO5
#define LED_PIN D7 //AKA GPIO13 -- LED for showing interrupt


// Constants
#define LIGHT_INT_HIGH 1000 // High light level for interrupt
@@ -56,24 +61,27 @@ uint16_t ambient_light = 0;
uint16_t red_light = 0;
uint16_t green_light = 0;
uint16_t blue_light = 0;
int isr_flag = 0;
volatile bool isr_flag = 0;
uint16_t threshold = 0;

void setup() {

//Start I2C with pins defined above
Wire.begin(APDS9960_SDA,APDS9960_SCL);

// Set LED as output
pinMode(LED_PIN, OUTPUT);
pinMode(APDS9960_INT, INPUT);

// Initialize Serial port
Serial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("-------------------------------------"));
Serial.println(F("SparkFun APDS-9960 - Light Interrupts"));
Serial.println(F("-------------------------------------"));

// Initialize interrupt service routine
attachInterrupt(0, interruptRoutine, FALLING);
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);

// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
@@ -158,4 +166,4 @@ void loop() {

void interruptRoutine() {
isr_flag = 1;
}
}
19 changes: 14 additions & 5 deletions examples/ColorSensor/ColorSensor.ino
Original file line number Diff line number Diff line change
@@ -14,12 +14,12 @@ Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Arduino Pin APDS-9960 Board Function
Wemos Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
D3 SDA I2C Data
D1 SCL I2C Clock
Resources:
Include Wire.h and SparkFun_APDS-9960.h
@@ -33,11 +33,17 @@ employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
Modified for ESP8266 by Jon Ulmer Nov 2016
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins on wemos D1 mini
#define APDS9960_SDA D3 //AKA GPIO0
#define APDS9960_SCL D1 //AKA GPIO5

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint16_t ambient_light = 0;
@@ -46,9 +52,12 @@ uint16_t green_light = 0;
uint16_t blue_light = 0;

void setup() {

//Start I2C with pins defined above
Wire.begin(APDS9960_SDA,APDS9960_SCL);

// Initialize Serial port
Serial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - ColorSensor"));
@@ -93,4 +102,4 @@ void loop() {

// Wait 1 second before next reading
delay(1000);
}
}
33 changes: 20 additions & 13 deletions examples/GestureTest/GestureTest.ino
Original file line number Diff line number Diff line change
@@ -23,13 +23,14 @@ Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Arduino Pin APDS-9960 Board Function
Wemos Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
2 INT Interrupt
D3 SDA I2C Data
D1 SCL I2C Clock
D6 INT Interrupt
D7 - LED
Resources:
Include Wire.h and SparkFun_APDS-9960.h
@@ -43,34 +44,40 @@ employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
Modified for ESP8266 by Jon Ulmer Nov 2016
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin

// Pins on wemos D1 mini
#define APDS9960_INT D6 //AKA GPIO12 -- Interupt pin
#define APDS9960_SDA D3 //AKA GPIO0
#define APDS9960_SCL D1 //AKA GPIO5
// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
volatile bool isr_flag = 0;

void setup() {

//Start I2C with pins defined above
Wire.begin(APDS9960_SDA,APDS9960_SCL);

// Set interrupt pin as input
pinMode(APDS9960_INT, INPUT);

// Initialize Serial port
Serial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - GestureTest"));
Serial.println(F("--------------------------------"));

// Initialize interrupt service routine
attachInterrupt(0, interruptRoutine, FALLING);
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);

// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
@@ -89,10 +96,10 @@ void setup() {

void loop() {
if( isr_flag == 1 ) {
detachInterrupt(0);
detachInterrupt(APDS9960_INT);
handleGesture();
isr_flag = 0;
attachInterrupt(0, interruptRoutine, FALLING);
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);
}
}

@@ -125,4 +132,4 @@ void handleGesture() {
Serial.println("NONE");
}
}
}
}
31 changes: 19 additions & 12 deletions examples/ProximityInterrupt/ProximityInterrupt.ino
Original file line number Diff line number Diff line change
@@ -14,14 +14,14 @@ Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Arduino Pin APDS-9960 Board Function
Wemos Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
2 INT Interrupt
13 - LED
D3 SDA I2C Data
D1 SCL I2C Clock
D6 INT Interrupt
D7 - LED
Resources:
Include Wire.h and SparkFun_APDS-9960.h
@@ -35,14 +35,18 @@ employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
Modified for ESP8266 by Jon Ulmer Nov 2016
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin
#define LED_PIN 13 // LED for showing interrupt
// Pins on wemos D1 mini
#define APDS9960_INT D6 //AKA GPIO12 -- Interupt pin
#define APDS9960_SDA D3 //AKA GPIO0
#define APDS9960_SCL D1 //AKA GPIO5
#define LED_PIN D7 //AKA GPIO13 -- LED for showing interrupt

// Constants
#define PROX_INT_HIGH 50 // Proximity level for interrupt
@@ -51,23 +55,26 @@ Distributed as-is; no warranty is given.
// Global variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint8_t proximity_data = 0;
int isr_flag = 0;
volatile bool isr_flag = 0;

void setup() {

//Start I2C with pins defined above
Wire.begin(APDS9960_SDA,APDS9960_SCL);

// Set LED as output
pinMode(LED_PIN, OUTPUT);
pinMode(APDS9960_INT, INPUT);

// Initialize Serial port
Serial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("---------------------------------------"));
Serial.println(F("SparkFun APDS-9960 - ProximityInterrupt"));
Serial.println(F("---------------------------------------"));

// Initialize interrupt service routine
attachInterrupt(0, interruptRoutine, FALLING);
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);

// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
@@ -126,4 +133,4 @@ void loop() {

void interruptRoutine() {
isr_flag = 1;
}
}
19 changes: 14 additions & 5 deletions examples/ProximitySensor/ProximitySensor.ino
Original file line number Diff line number Diff line change
@@ -13,12 +13,12 @@ Hardware Connections:
IMPORTANT: The APDS-9960 can only accept 3.3V!
Arduino Pin APDS-9960 Board Function
Wemos Pin APDS-9960 Board Function
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
D3 SDA I2C Data
D1 SCL I2C Clock
Resources:
Include Wire.h and SparkFun_APDS-9960.h
@@ -32,19 +32,28 @@ employee) at the local, and you've found our code helpful, please
buy us a round!
Distributed as-is; no warranty is given.
Modified for ESP8266 by Jon Ulmer Nov 2016
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins on wemos D1 mini
#define APDS9960_SDA D3 //AKA GPIO0
#define APDS9960_SCL D1 //AKA GPIO5

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint8_t proximity_data = 0;

void setup() {

//Start I2C with pins defined above
Wire.begin(APDS9960_SDA,APDS9960_SCL);

// Initialize Serial port
Serial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("------------------------------------"));
Serial.println(F("SparkFun APDS-9960 - ProximitySensor"));
@@ -82,4 +91,4 @@ void loop() {

// Wait 250 ms before next reading
delay(250);
}
}
498 changes: 265 additions & 233 deletions src/SparkFun_APDS9960.cpp

Large diffs are not rendered by default.

75 changes: 45 additions & 30 deletions src/SparkFun_APDS9960.h
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
* relies on the Arduino Wire (I2C) library. to use the library, instantiate an
* APDS9960 object, call init(), and call the appropriate functions.
*/

#ifndef SparkFun_APDS9960_H
#define SparkFun_APDS9960_H

@@ -32,7 +32,7 @@

/* Acceptable device IDs */
#define APDS9960_ID_1 0xAB
#define APDS9960_ID_2 0x9C
#define APDS9960_ID_2 0x9C

/* Misc parameters */
#define FIFO_PAUSE_TIME 30 // Wait period (ms) between FIFO reads
@@ -106,7 +106,7 @@
#define POWER 0
#define AMBIENT_LIGHT 1
#define PROXIMITY 2
#define WAIT 3
#define WAITING_TIME 3
#define AMBIENT_LIGHT_INT 4
#define PROXIMITY_INT 5
#define GESTURE 6
@@ -140,7 +140,7 @@
#define LED_BOOST_100 0
#define LED_BOOST_150 1
#define LED_BOOST_200 2
#define LED_BOOST_300 3
#define LED_BOOST_300 3

/* Gesture wait time values */
#define GWTIME_0MS 0
@@ -158,7 +158,7 @@
#define DEFAULT_PROX_PPULSE 0x87 // 16us, 8 pulses
#define DEFAULT_GESTURE_PPULSE 0x89 // 16us, 10 pulses
#define DEFAULT_POFFSET_UR 0 // 0 offset
#define DEFAULT_POFFSET_DL 0 // 0 offset
#define DEFAULT_POFFSET_DL 0 // 0 offset
#define DEFAULT_CONFIG1 0x60 // No 12x wait (WTIME) factor
#define DEFAULT_LDRIVE LED_DRIVE_100MA
#define DEFAULT_PGAIN PGAIN_4X
@@ -168,13 +168,15 @@
#define DEFAULT_AILT 0xFFFF // Force interrupt for calibration
#define DEFAULT_AIHT 0
#define DEFAULT_PERS 0x11 // 2 consecutive prox or ALS for int.
#define DEFAULT_CONFIG2 0x01 // No saturation interrupts or LED boost
#define DEFAULT_CONFIG2 0x01 // No saturation interrupts or LED boost
#define DEFAULT_CONFIG3 0 // Enable all photodiodes, no SAI
#define DEFAULT_GPENTH 40 // Threshold for entering gesture mode
#define DEFAULT_GEXTH 30 // Threshold for exiting gesture mode
#define DEFAULT_GEXTH 30 // Threshold for exiting gesture mode
#define DEFAULT_GCONF1 0x40 // 4 gesture events for int., 1 for exit
#define DEFAULT_GGAIN GGAIN_4X
#define DEFAULT_GLDRIVE LED_DRIVE_100MA
//#define DEFAULT_GGAIN GGAIN_2X
//#define DEFAULT_GLDRIVE LED_DRIVE_100MA
#define DEFAULT_GLDRIVE LED_DRIVE_25MA
#define DEFAULT_GWTIME GWTIME_2_8MS
#define DEFAULT_GOFFSET 0 // No offset scaling for gesture mode
#define DEFAULT_GPULSE 0xC9 // 32us, 10 pulses
@@ -195,7 +197,7 @@ enum {

/* State definitions */
enum {
NA_STATE,
APDS_NA_STATE,
NEAR_STATE,
FAR_STATE,
ALL_STATE
@@ -223,70 +225,71 @@ class SparkFun_APDS9960 {
bool init();
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();

//Jon int readGesture();
int16_t readGesture();

private:

/* Gesture processing */
@@ -299,40 +302,43 @@ class SparkFun_APDS9960 {
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);
// Jon bool wireWriteDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
bool wireWriteDataBlock(uint8_t reg, uint8_t *val, uint16_t len);
bool wireReadDataByte(uint8_t reg, uint8_t &val);
int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
// Jon int wireReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
int16_t wireReadDataBlock(uint8_t reg, uint8_t *val, uint16_t len);

/* Members */
gesture_data_type gesture_data_;
/* Jon
int gesture_ud_delta_;
int gesture_lr_delta_;
int gesture_ud_count_;
@@ -341,6 +347,15 @@ class SparkFun_APDS9960 {
int gesture_far_count_;
int gesture_state_;
int gesture_motion_;
*/
int16_t gesture_ud_delta_;
int16_t gesture_lr_delta_;
int16_t gesture_ud_count_;
int16_t gesture_lr_count_;
int16_t gesture_near_count_;
int16_t gesture_far_count_;
int16_t gesture_state_;
int16_t gesture_motion_;
};

#endif
#endif