Skip to content

Commit 67c5d56

Browse files
committed
first commit
0 parents  commit 67c5d56

File tree

9 files changed

+791
-0
lines changed

9 files changed

+791
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

LICENSE

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Arduino_APDS9999

examples/all_data/all_data.ino

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "Arduino_APDS9999.h"
13+
14+
Arduino_APDS9999 apds9999(Wire1);
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
while (!Serial);
19+
20+
if (!apds9999.begin()){
21+
Serial.println("There is an error into your connection");
22+
}
23+
24+
apds9999.enableColorSensor();
25+
apds9999.enableProximitySensor();
26+
}
27+
28+
void loop() {
29+
Serial.print(apds9999.getRed());
30+
Serial.print("\t");
31+
Serial.print(apds9999.getGreen());
32+
Serial.print("\t");
33+
Serial.print(apds9999.getBlue());
34+
Serial.print("\t");
35+
Serial.print(apds9999.getIR());
36+
Serial.print("\t");
37+
Serial.println(apds9999.getProximity());
38+
39+
delay(10);
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "Arduino_APDS9999.h"
13+
14+
Arduino_APDS9999 apds9999(Wire1);
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
while (!Serial);
19+
20+
if (!apds9999.begin()){
21+
Serial.println("There is an error into your connection");
22+
}
23+
24+
apds9999.enableColorSensor();
25+
}
26+
27+
void loop() {
28+
Serial.print(apds9999.getRed());
29+
Serial.print("\t");
30+
Serial.print(apds9999.getGreen());
31+
Serial.print("\t");
32+
Serial.println(apds9999.getBlue());
33+
34+
delay(10);
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "Arduino_APDS9999.h"
13+
14+
Arduino_APDS9999 apds9999(Wire1);
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
while (!Serial);
19+
20+
if (!apds9999.begin()){
21+
Serial.println("There is an error into your connection");
22+
}
23+
24+
apds9999.enableProximitySensor();
25+
}
26+
27+
void loop() {
28+
Serial.println(apds9999.getProximity());
29+
delay(10);
30+
}

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Arduino_APDS9999
2+
version=0.0.3
3+
author=Arduino, Giovanni Bruno
4+
maintainer=Arduino <[email protected]>
5+
sentence=APDS9999 library
6+
paragraph=Ambient light, color and proximity sensor
7+
category=Sensors
8+
url=https://github.com/arduino-libraries/Arduino_APDS9999
9+
architectures=*
10+
includes=Arduino_APDS9999.h

src/Arduino_APDS9999.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "Arduino_APDS9999.h"
13+
14+
Arduino_APDS9999::Arduino_APDS9999(TwoWire & wire):_wire(wire){
15+
16+
}
17+
18+
int Arduino_APDS9999::begin(){
19+
_wire.begin();
20+
if (getID()!=APDS9999_DEFAULT_ID){
21+
return 0;
22+
}
23+
writeRegister(APDS9999_MAIN_CTRL,APDS9999_SOFT_RST);
24+
delay(500);
25+
writeRegister(APDS9999_MAIN_CTRL, 0x00);
26+
return 1;
27+
}
28+
29+
void Arduino_APDS9999::end(){
30+
_wire.end();
31+
}
32+
33+
void Arduino_APDS9999::writeRegister(uint8_t reg, uint8_t value) {
34+
_wire.beginTransmission(APDS9999_ADDRESS);
35+
_wire.write(reg);
36+
_wire.write(value);
37+
_wire.endTransmission();
38+
}
39+
40+
uint8_t Arduino_APDS9999::readRegister(uint8_t reg) {
41+
_wire.beginTransmission(APDS9999_ADDRESS);
42+
_wire.write(reg);
43+
_wire.endTransmission();
44+
45+
_wire.requestFrom(APDS9999_ADDRESS, 1);
46+
if (_wire.available()) {
47+
return _wire.read();
48+
}
49+
return 0;
50+
}
51+
52+
uint32_t Arduino_APDS9999::readRegister24(uint8_t reg) {
53+
_wire.beginTransmission(APDS9999_ADDRESS);
54+
_wire.write(reg);
55+
_wire.endTransmission();
56+
57+
_wire.requestFrom(APDS9999_ADDRESS, 3);
58+
if (_wire.available() >= 3) {
59+
uint32_t value = _wire.read();
60+
value |= (_wire.read() << 8);
61+
value |= (_wire.read() << 16);
62+
return value;
63+
}
64+
return 0;
65+
}
66+
67+
//--------------------------------------------------------------------------//
68+
// Sensor Data //
69+
//--------------------------------------------------------------------------//
70+
71+
72+
void Arduino_APDS9999::enableLightSensor(){
73+
uint8_t ctrl = readRegister(APDS9999_MAIN_CTRL);
74+
writeRegister(APDS9999_MAIN_CTRL, ctrl | APDS9999_LS_ENABLE);
75+
delay(20);
76+
}
77+
78+
void Arduino_APDS9999::disableLightSensor(){
79+
uint8_t ctrl = readRegister(APDS9999_MAIN_CTRL);
80+
writeRegister(APDS9999_MAIN_CTRL, ctrl & ~APDS9999_LS_ENABLE);
81+
}
82+
83+
void Arduino_APDS9999::enableColorSensor(){
84+
enableLightSensor();
85+
uint8_t ctrl = readRegister(APDS9999_MAIN_CTRL);
86+
writeRegister(APDS9999_MAIN_CTRL, ctrl | APDS9999_CS_ENABLE);
87+
delay(20);
88+
}
89+
90+
void Arduino_APDS9999::disableColorSensor(){
91+
uint8_t ctrl = readRegister(APDS9999_MAIN_CTRL);
92+
writeRegister(APDS9999_MAIN_CTRL, ctrl & ~APDS9999_CS_ENABLE);
93+
}
94+
95+
void Arduino_APDS9999::enableProximitySensor(){
96+
uint8_t ctrl = readRegister(APDS9999_MAIN_CTRL);
97+
writeRegister(APDS9999_MAIN_CTRL, ctrl | APDS9999_PS_ENABLE);
98+
delay(20);
99+
}
100+
101+
void Arduino_APDS9999::disableProximitySensor(){
102+
uint8_t ctrl = readRegister(APDS9999_MAIN_CTRL);
103+
writeRegister(APDS9999_MAIN_CTRL, ctrl & ~APDS9999_PS_ENABLE);
104+
}
105+
106+
void Arduino_APDS9999::setGain(const uint8_t gain){
107+
uint8_t ctrl = readRegister(APDS9999_LS_GAIN);
108+
writeRegister(APDS9999_LS_GAIN, (ctrl & 0xF8) | gain);
109+
}
110+
111+
void Arduino_APDS9999::setLSResolution(const uint8_t resolution){
112+
uint8_t ctrl = readRegister(APDS9999_LS_MEAS_R);
113+
writeRegister(APDS9999_LS_MEAS_R, (ctrl & 0x8F) | (resolution<<4));
114+
}
115+
116+
void Arduino_APDS9999::setLSRate(const uint8_t rate){
117+
uint8_t ctrl = readRegister(APDS9999_LS_MEAS_R);
118+
writeRegister(APDS9999_LS_MEAS_R, (ctrl & 0xF8) | rate);
119+
}
120+
121+
void Arduino_APDS9999::setPSResolution(const uint8_t resolution){
122+
uint8_t ctrl = readRegister(APDS9999_PS_MEAS_R);
123+
writeRegister(APDS9999_PS_MEAS_R, (ctrl & 0xE7) | (resolution<<3));
124+
}
125+
126+
void Arduino_APDS9999::setPSRate(const uint8_t rate){
127+
uint8_t ctrl = readRegister(APDS9999_PS_MEAS_R);
128+
writeRegister(APDS9999_PS_MEAS_R, (ctrl & 0xF8) | rate);
129+
}
130+
131+
132+
int Arduino_APDS9999::getIR(){
133+
return readRegister24(APDS9999_IR_DATA);
134+
}
135+
136+
int Arduino_APDS9999::getRed(){
137+
return readRegister24(APDS9999_RED_DATA);
138+
}
139+
140+
int Arduino_APDS9999::getGreen(){
141+
return readRegister24(APDS9999_GREEN_DATA);
142+
}
143+
144+
int Arduino_APDS9999::getBlue(){
145+
return readRegister24(APDS9999_BLUE_DATA);
146+
}
147+
148+
void Arduino_APDS9999::readColor(int & red, int & green, int & blue){
149+
red = getRed();
150+
green = getGreen();
151+
blue = getBlue();
152+
}
153+
154+
void Arduino_APDS9999::readColor(int & red, int & green, int & blue, int & ir){
155+
red = getRed();
156+
green = getGreen();
157+
blue = getBlue();
158+
ir = getIR();
159+
}
160+
161+
int Arduino_APDS9999::getProximity(){
162+
uint8_t lsb = readRegister(APDS9999_PS_DATA_L);
163+
uint8_t msb =readRegister(APDS9999_PS_DATA_H);
164+
165+
bool overflow = msb & 0x08; // Check overflow bit in PS_DATA_1
166+
167+
if (overflow) {
168+
return -1;
169+
}
170+
171+
return (msb & 0x07) << 8 | lsb;
172+
}
173+
174+
uint8_t Arduino_APDS9999::getID(){
175+
return readRegister(APDS9999_ID);
176+
}
177+
178+
179+

0 commit comments

Comments
 (0)