Skip to content

Commit 4655868

Browse files
committed
add: apds9999 support for new carrier revisions
add: new uuid for esp32 mod: sciencekit r3 to sciencekit
1 parent 0ada7db commit 4655868

File tree

6 files changed

+87
-14
lines changed

6 files changed

+87
-14
lines changed

examples/ScienceJournal/ScienceJournal.ino

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rtos::Thread thread_update_sensors;
3030

3131
#ifdef ESP32
3232
TaskHandle_t update_base;
33+
TaskHandle_t update_ble;
3334
#endif
3435

3536
bool ble_is_connected = false;
@@ -43,11 +44,17 @@ void setup(){
4344
while(1);
4445
}
4546

47+
//BLE.setConnectionInterval(6, 12);
48+
4649
String address = BLE.address();
4750

4851
address.toUpperCase();
49-
50-
name = "ScienceKit R3 - ";
52+
#ifdef ARDUINO_NANO_RP2040_CONNECT
53+
name = "ScienceKit R3 - ";
54+
#endif
55+
#ifdef ESP32
56+
name = "ScienceKit - ";
57+
#endif
5158
name += address[address.length() - 5];
5259
name += address[address.length() - 4];
5360
name += address[address.length() - 2];
@@ -121,6 +128,7 @@ void setup(){
121128
#endif
122129
#ifdef ESP32
123130
xTaskCreatePinnedToCore(&freeRTOSUpdate, "update_base", 10000, NULL, 1, &update_base, 1); // starts the update sensors thread on core 1 (user)
131+
xTaskCreatePinnedToCore(&freeRTOSble, "update_ble", 10000, NULL, 1, &update_ble, 0); // starts the ble thread on core 0 (internal)
124132
#endif
125133
}
126134

@@ -136,10 +144,16 @@ void update(void){
136144
static void freeRTOSUpdate(void * pvParameters){
137145
update();
138146
}
139-
#endif
140147

148+
static void freeRTOSble(void * pvParameters){
149+
while(1){
150+
updateBle();
151+
delay(1);
152+
}
153+
}
154+
#endif
141155

142-
void loop(){
156+
void updateBle(){
143157
BLEDevice central = BLE.central();
144158
if (central) {
145159
ble_is_connected = true;
@@ -151,6 +165,9 @@ void loop(){
151165
if (millis()-lastNotify>10){
152166
updateSubscribedCharacteristics();
153167
lastNotify=millis();
168+
#ifdef ESP32
169+
delay(1);
170+
#endif
154171
}
155172
}
156173
}
@@ -163,6 +180,13 @@ void loop(){
163180
}
164181
}
165182

183+
184+
void loop(){
185+
#ifdef ARDUINO_NANO_RP2040_CONNECT
186+
updateBle();
187+
#endif
188+
}
189+
166190
void updateSubscribedCharacteristics(){
167191
/* ________________________________________________________________CURRENT */
168192
if(currentCharacteristic.subscribed()){

examples/ScienceJournal/ble_config.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ const int VERSION = 0x00000002;
5353
#define BLE_CH____PING "1020"
5454
#define BLE_CH_FUNGEN2 "1021"
5555

56-
56+
#ifdef ARDUINO_NANO_RP2040_CONNECT
5757
#define SCIENCE_KIT_UUID(val) ("555a0003-" val "-467a-9538-01f0652c74e8")
58+
#endif
59+
#ifdef ESP32
60+
#define SCIENCE_KIT_UUID(val) ("555a0004-" val "-467a-9538-01f0652c74e8")
61+
#endif
62+
5863

5964
/*
6065
* SERVICE, VERSION

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ category=Communication
88
url=https://github.com/arduino-libraries/Arduino_ScienceKitCarrier
99
architectures=mbed,mbed_nano,esp32
1010
includes=Arduino_ScienceKitCarrier.h
11-
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,bsec2,OneWireNg
11+
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,bsec2,OneWireNg,Arduino_APDS9999

src/Arduino_ScienceKitCarrier.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ScienceKitCarrier::ScienceKitCarrier(){
3737
board_resolution = BOARD_RESOLUTION;
3838

3939
apds9960 = new APDS9960(Wire,INT_APDS9960);
40+
apds9999 = new Arduino_APDS9999(Wire);
4041
proximity=0;
4142
r=0;
4243
g=0;
@@ -275,23 +276,57 @@ int ScienceKitCarrier::getInputB(){
275276

276277

277278
/********************************************************************/
278-
/* APDS9960 */
279+
/* APDS99xx */
279280
/********************************************************************/
280281

281282
int ScienceKitCarrier::beginAPDS(){
282-
if (!apds9960->begin()) {
283-
return ERR_BEGIN_APDS;
283+
if (!apds9999->begin()){
284+
if (!apds9960->begin()) {
285+
return ERR_BEGIN_APDS;
286+
}
287+
else{
288+
color_sensor_used = APDS9960_VERSION;
289+
}
284290
}
291+
else{
292+
apds9999->enableColorSensor();
293+
apds9999->enableProximitySensor();
294+
apds9999->setGain(APDS9999_GAIN_3X);
295+
apds9999->setLSResolution(APDS9999_LS_RES_16B);
296+
apds9999->setLSRate(APDS9999_LS_RATE_25MS);
297+
color_sensor_used = APDS9999_VERSION;
298+
}
299+
#ifdef ESP32
300+
for(int i=0; i<=color_sensor_used; i++){
301+
digitalWrite(LED_GREEN, LOW);
302+
delay(100);
303+
digitalWrite(LED_GREEN, HIGH);
304+
delay(100);
305+
}
306+
digitalWrite(LED_GREEN, HIGH);
307+
#endif
285308
return 0;
286309
}
287310

288311
void ScienceKitCarrier::updateAPDS(){
289312
wire_lock;
290-
if (apds9960->proximityAvailable()){
291-
proximity=apds9960->readProximity();
313+
if (color_sensor_used==APDS9960_VERSION){
314+
if (apds9960->proximityAvailable()){
315+
proximity=apds9960->readProximity();
316+
}
317+
if (apds9960->colorAvailable()){
318+
apds9960->readColor(r,g,b,c);
319+
}
292320
}
293-
if (apds9960->colorAvailable()){
294-
apds9960->readColor(r,g,b,c);
321+
if (color_sensor_used==APDS9999_VERSION){
322+
r = apds9999->getRed()*4097/65535.0;
323+
g = apds9999->getGreen()*4097/262144.0;
324+
b = apds9999->getBlue()*4097/131072.0;
325+
c = apds9999->getIR()*4097/4096.0;
326+
proximity = 255 - apds9999->getProximity();
327+
if (proximity>255){
328+
proximity = 0;
329+
}
295330
}
296331
wire_unlock;
297332
}
@@ -778,7 +813,7 @@ void ScienceKitCarrier::retriveUltrasonicUpdate(){
778813
}
779814

780815
float ScienceKitCarrier::getDistance(){
781-
return distance;
816+
return distance/1000.0;
782817
}
783818

784819
float ScienceKitCarrier::getTravelTime(){

src/Arduino_ScienceKitCarrier.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
#include <Arduino.h>
2424

25+
#if !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ESP32)
26+
#error "This product is compatible only with Arduino® Nano RP2040 Connect and Arduino® Nano ESP32
27+
#endif
28+
2529
#ifdef ARDUINO_NANO_RP2040_CONNECT
2630
#include "WiFiNINA.h"
2731
#include "mbed.h"
@@ -31,6 +35,7 @@
3135

3236
#include <Wire.h>
3337
#include "Arduino_APDS9960.h"
38+
#include "Arduino_APDS9999.h"
3439
#include "INA.h"
3540

3641
#include "bsec2.h"
@@ -75,7 +80,9 @@ class ScienceKitCarrier{
7580
uint8_t timer_inputA;
7681

7782
APDS9960 * apds9960;
83+
Arduino_APDS9999 * apds9999;
7884
int r,g,b,c, proximity;
85+
int color_sensor_used;
7986

8087
INA_Class * ina;
8188
float voltage, current;

src/utils/Arduino_ScienceKitCarrier_definitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
// APDS9960
3939
#define INT_APDS9960 9
40+
#define APDS9960_VERSION 0
41+
#define APDS9999_VERSION 1
4042

4143
// INA
4244
const uint32_t SHUNT_MICRO_OHM{100000}; // check schematic R20

0 commit comments

Comments
 (0)