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  ARDUINO_NANO_ESP32
32+ TaskHandle_t update_base;
33+ TaskHandle_t update_ble;
34+ #endif 
2735
2836bool  ble_is_connected = false ;
2937
3038
39+ 
3140void  setup (){
3241  science_kit.begin (NO_AUXILIARY_THREADS); //  Doesn't start the BME688 and external temperature threads for the moment
3342
3443  if  (!BLE.begin ()){
3544    while (1 );
3645  }
3746
47+   // BLE.setConnectionInterval(6, 12);
48+ 
3849  String address = BLE.address ();
3950
4051  address.toUpperCase ();
41- 
42-   name = " ScienceKit R3 - "  ;
52+   #ifdef  ARDUINO_NANO_RP2040_CONNECT
53+     name = " ScienceKit R3 - "  ;
54+   #endif 
55+   #ifdef  ARDUINO_NANO_ESP32
56+     name = " ScienceKit - "  ;
57+   #endif 
4358  name += address[address.length () - 5 ];
4459  name += address[address.length () - 4 ];
4560  name += address[address.length () - 2 ];
@@ -76,10 +91,14 @@ void setup(){
7691  service.addCharacteristic (humidityCharacteristic);
7792  /*  _____________________________________________________________AIR_QUALITY */ 
7893  service.addCharacteristic (airQualityCharacteristic);
94+ 
95+   #ifdef  ARDUINO_NANO_RP2040_CONNECT
7996  /*  _________________________________________________________SOUND_INTENSITY */ 
8097  service.addCharacteristic (sndIntensityCharacteristic);
8198  /*  _____________________________________________________________SOUND_PITCH */ 
8299  service.addCharacteristic (sndPitchCharacteristic);
100+   #endif 
101+ 
83102  /*  _________________________________________________________________INPUT_A */ 
84103  service.addCharacteristic (inputACharacteristic);
85104  /*  _________________________________________________________________INPUT_B */ 
@@ -101,38 +120,73 @@ void setup(){
101120
102121  BLE.addService (service);
103122  BLE.advertise ();
104- 
123+    
105124  science_kit.startAuxiliaryThreads (); //  start the BME688 and External Temperature Probe threads
106125
107-   thread_update_sensors.start (update); //  this thread updates sensors
126+   #ifdef  ARDUINO_NANO_RP2040_CONNECT
127+     thread_update_sensors.start (update); //  this thread updates sensors
128+   #endif 
129+   #ifdef  ARDUINO_NANO_ESP32
130+     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)
132+   #endif 
108133}
109134
110135
111136void  update (void ){
112137  while (1 ){
113138    science_kit.update (ROUND_ROBIN_ENABLED);
114-     rtos::ThisThread::sleep_for (25 );
139+     delay (25 );
115140  }
116141}
117142
118- void  loop (){
143+ #ifdef  ARDUINO_NANO_ESP32
144+ static  void  freeRTOSUpdate (void  * pvParameters){
145+   update ();
146+ }
147+ 
148+ static  void  freeRTOSble (void  * pvParameters){
149+   while (1 ){
150+     updateBle ();
151+     delay (1 );
152+   }
153+ }
154+ #endif 
155+ 
156+ void  updateBle (){
119157  BLEDevice central = BLE.central ();
120158  if  (central) {
121159    ble_is_connected = true ;
160+     #ifdef  ARDUINO_NANO_ESP32
161+       science_kit.setStatusLed (STATUS_LED_BLE);
162+     #endif   
122163    lastNotify=millis ();
123164    while  (central.connected ()) {
124165      if  (millis ()-lastNotify>10 ){
125166        updateSubscribedCharacteristics ();
126167        lastNotify=millis ();
168+         #ifdef  ARDUINO_NANO_ESP32
169+           delay (1 );
170+         #endif 
127171      }
128172    }
129173  }
130174  else  {
131175    delay (100 );
132176    ble_is_connected = false ;
177+     #ifdef  ARDUINO_NANO_ESP32
178+       science_kit.setStatusLed (STATUS_LED_PAIRING);
179+     #endif   
133180  }
134181}
135182
183+ 
184+ void  loop (){
185+   #ifdef  ARDUINO_NANO_RP2040_CONNECT
186+     updateBle ();
187+   #endif 
188+ }
189+ 
136190void  updateSubscribedCharacteristics (){
137191  /*  ________________________________________________________________CURRENT  */ 
138192  if (currentCharacteristic.subscribed ()){
@@ -199,7 +253,6 @@ void updateSubscribedCharacteristics(){
199253  /*  
200254   * BME688  
201255   */  
202- 
203256  /*  _____________________________________________________________TEMPERATURE */ 
204257  if (temperatureCharacteristic.subscribed ()){
205258    temperatureCharacteristic.writeValue (science_kit.getTemperature ());
@@ -219,10 +272,11 @@ void updateSubscribedCharacteristics(){
219272  if (airQualityCharacteristic.subscribed ()){
220273    airQualityCharacteristic.writeValue (science_kit.getAirQuality ());
221274  }
222-    
275+ 
223276  /* 
224277   * MICROPHONE 
225278   */  
279+   #ifdef  ARDUINO_NANO_RP2040_CONNECT
226280
227281  /*  _________________________________________________________SOUND_INTENSITY */ 
228282  /*  NOTE: raw value - value not in Db */   
@@ -232,8 +286,9 @@ void updateSubscribedCharacteristics(){
232286
233287  /*  _____________________________________________________________SOUND_PITCH */ 
234288  if (sndPitchCharacteristic.subscribed ()){
235-     sndPitchCharacteristic.writeValue (science_kit. getExternalTemperature () );
289+     sndPitchCharacteristic.writeValue (0.0 );
236290  }
291+   #endif 
237292
238293  /*  _________________________________________________________________INPUT_A */ 
239294  if  (inputACharacteristic.subscribed ()){
@@ -269,26 +324,16 @@ void updateSubscribedCharacteristics(){
269324
270325  /*  ________________________________________________________________DISTANCE */ 
271326  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-     }
327+     /*  NOTE: getDistance() calls getMeters() */ 
328+     /*        Requested value is in meters    */ 
329+     distanceCharacteristic.writeValue (science_kit.getDistance ());
280330  }
281331
282332  /*  ____________________________________________________________________PING */ 
283333  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-     } 
334+     /*  NOTE: getTravelTime() returns micro seconds               */ 
335+     /*        Converted to milliseconds (agreed with RF 20230719) */ 
336+     pingCharacteristic.writeValue (science_kit.getTravelTime () * 1000.0  );
292337  }  
293338}
294339
0 commit comments