2323String name;
2424unsigned long lastNotify = 0 ;
2525ScienceKitCarrier science_kit;
26+
27+ #ifdef ARDUINO_NANO_RP2040_CONNECT
2628rtos::Thread thread_update_sensors;
29+ #endif
30+
31+ #ifdef ESP32
32+ TaskHandle_t update_base;
33+ #endif
2734
2835bool ble_is_connected = false ;
2936
3037
38+
3139void setup (){
3240 science_kit.begin (NO_AUXILIARY_THREADS); // Doesn't start the BME688 and external temperature threads for the moment
3341
@@ -76,10 +84,14 @@ void setup(){
7684 service.addCharacteristic (humidityCharacteristic);
7785 /* _____________________________________________________________AIR_QUALITY */
7886 service.addCharacteristic (airQualityCharacteristic);
87+
88+ #ifdef ARDUINO_NANO_RP2040_CONNECT
7989 /* _________________________________________________________SOUND_INTENSITY */
8090 service.addCharacteristic (sndIntensityCharacteristic);
8191 /* _____________________________________________________________SOUND_PITCH */
8292 service.addCharacteristic (sndPitchCharacteristic);
93+ #endif
94+
8395 /* _________________________________________________________________INPUT_A */
8496 service.addCharacteristic (inputACharacteristic);
8597 /* _________________________________________________________________INPUT_B */
@@ -101,24 +113,39 @@ void setup(){
101113
102114 BLE.addService (service);
103115 BLE.advertise ();
104-
116+
105117 science_kit.startAuxiliaryThreads (); // start the BME688 and External Temperature Probe threads
106118
107- thread_update_sensors.start (update); // this thread updates sensors
119+ #ifdef ARDUINO_NANO_RP2040_CONNECT
120+ thread_update_sensors.start (update); // this thread updates sensors
121+ #endif
122+ #ifdef ESP32
123+ xTaskCreatePinnedToCore (&freeRTOSUpdate, " update_base" , 10000 , NULL , 1 , &update_base, 1 ); // starts the update sensors thread on core 1 (user)
124+ #endif
108125}
109126
110127
111128void update (void ){
112129 while (1 ){
113130 science_kit.update (ROUND_ROBIN_ENABLED);
114- rtos::ThisThread::sleep_for (25 );
131+ delay (25 );
115132 }
116133}
117134
135+ #ifdef ESP32
136+ static void freeRTOSUpdate (void * pvParameters){
137+ update ();
138+ }
139+ #endif
140+
141+
118142void loop (){
119143 BLEDevice central = BLE.central ();
120144 if (central) {
121145 ble_is_connected = true ;
146+ #ifdef ESP32
147+ science_kit.setStatusLed (STATUS_LED_BLE);
148+ #endif
122149 lastNotify=millis ();
123150 while (central.connected ()) {
124151 if (millis ()-lastNotify>10 ){
@@ -130,6 +157,9 @@ void loop(){
130157 else {
131158 delay (100 );
132159 ble_is_connected = false ;
160+ #ifdef ESP32
161+ science_kit.setStatusLed (STATUS_LED_PAIRING);
162+ #endif
133163 }
134164}
135165
@@ -199,7 +229,6 @@ void updateSubscribedCharacteristics(){
199229 /*
200230 * BME688
201231 */
202-
203232 /* _____________________________________________________________TEMPERATURE */
204233 if (temperatureCharacteristic.subscribed ()){
205234 temperatureCharacteristic.writeValue (science_kit.getTemperature ());
@@ -219,10 +248,11 @@ void updateSubscribedCharacteristics(){
219248 if (airQualityCharacteristic.subscribed ()){
220249 airQualityCharacteristic.writeValue (science_kit.getAirQuality ());
221250 }
222-
251+
223252 /*
224253 * MICROPHONE
225254 */
255+ #ifdef ARDUINO_NANO_RP2040_CONNECT
226256
227257 /* _________________________________________________________SOUND_INTENSITY */
228258 /* NOTE: raw value - value not in Db */
@@ -232,8 +262,9 @@ void updateSubscribedCharacteristics(){
232262
233263 /* _____________________________________________________________SOUND_PITCH */
234264 if (sndPitchCharacteristic.subscribed ()){
235- sndPitchCharacteristic.writeValue (science_kit. getExternalTemperature () );
265+ sndPitchCharacteristic.writeValue (0.0 );
236266 }
267+ #endif
237268
238269 /* _________________________________________________________________INPUT_A */
239270 if (inputACharacteristic.subscribed ()){
@@ -269,26 +300,16 @@ void updateSubscribedCharacteristics(){
269300
270301 /* ________________________________________________________________DISTANCE */
271302 if (distanceCharacteristic.subscribed ()){
272- if (science_kit.getUltrasonicIsConnected ()){
273- /* NOTE: getDistance() calls getMeters()
274- Requested value is in meters */
275- distanceCharacteristic.writeValue (science_kit.getDistance ());
276- }
277- else {
278- distanceCharacteristic.writeValue (-1.0 );
279- }
303+ /* NOTE: getDistance() calls getMeters() */
304+ /* Requested value is in meters */
305+ distanceCharacteristic.writeValue (science_kit.getDistance ());
280306 }
281307
282308 /* ____________________________________________________________________PING */
283309 if (pingCharacteristic.subscribed ()){
284- if (science_kit.getUltrasonicIsConnected ()){
285- /* NOTE: getTravelTime() returns micro seconds */
286- /* Converted to milliseconds (agreed with RF 20230719) */
287- pingCharacteristic.writeValue (science_kit.getTravelTime () * 1000.0 );
288- }
289- else {
290- pingCharacteristic.writeValue (-1.0 );
291- }
310+ /* NOTE: getTravelTime() returns micro seconds */
311+ /* Converted to milliseconds (agreed with RF 20230719) */
312+ pingCharacteristic.writeValue (science_kit.getTravelTime () * 1000.0 );
292313 }
293314}
294315
0 commit comments