23
23
String name;
24
24
unsigned long lastNotify = 0 ;
25
25
ScienceKitCarrier science_kit;
26
+
27
+ #ifdef ARDUINO_NANO_RP2040_CONNECT
26
28
rtos::Thread thread_update_sensors;
29
+ #endif
30
+
31
+ #ifdef ARDUINO_NANO_ESP32
32
+ TaskHandle_t update_base;
33
+ TaskHandle_t update_ble;
34
+ #endif
27
35
28
36
bool ble_is_connected = false ;
29
37
30
38
39
+
31
40
void setup (){
32
41
science_kit.begin (NO_AUXILIARY_THREADS); // Doesn't start the BME688 and external temperature threads for the moment
33
42
34
43
if (!BLE.begin ()){
35
44
while (1 );
36
45
}
37
46
47
+ // BLE.setConnectionInterval(6, 12);
48
+
38
49
String address = BLE.address ();
39
50
40
51
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
43
58
name += address[address.length () - 5 ];
44
59
name += address[address.length () - 4 ];
45
60
name += address[address.length () - 2 ];
@@ -76,10 +91,14 @@ void setup(){
76
91
service.addCharacteristic (humidityCharacteristic);
77
92
/* _____________________________________________________________AIR_QUALITY */
78
93
service.addCharacteristic (airQualityCharacteristic);
94
+
95
+ #ifdef ARDUINO_NANO_RP2040_CONNECT
79
96
/* _________________________________________________________SOUND_INTENSITY */
80
97
service.addCharacteristic (sndIntensityCharacteristic);
81
98
/* _____________________________________________________________SOUND_PITCH */
82
99
service.addCharacteristic (sndPitchCharacteristic);
100
+ #endif
101
+
83
102
/* _________________________________________________________________INPUT_A */
84
103
service.addCharacteristic (inputACharacteristic);
85
104
/* _________________________________________________________________INPUT_B */
@@ -101,38 +120,73 @@ void setup(){
101
120
102
121
BLE.addService (service);
103
122
BLE.advertise ();
104
-
123
+
105
124
science_kit.startAuxiliaryThreads (); // start the BME688 and External Temperature Probe threads
106
125
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
108
133
}
109
134
110
135
111
136
void update (void ){
112
137
while (1 ){
113
138
science_kit.update (ROUND_ROBIN_ENABLED);
114
- rtos::ThisThread::sleep_for (25 );
139
+ delay (25 );
115
140
}
116
141
}
117
142
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 (){
119
157
BLEDevice central = BLE.central ();
120
158
if (central) {
121
159
ble_is_connected = true ;
160
+ #ifdef ARDUINO_NANO_ESP32
161
+ science_kit.setStatusLed (STATUS_LED_BLE);
162
+ #endif
122
163
lastNotify=millis ();
123
164
while (central.connected ()) {
124
165
if (millis ()-lastNotify>10 ){
125
166
updateSubscribedCharacteristics ();
126
167
lastNotify=millis ();
168
+ #ifdef ARDUINO_NANO_ESP32
169
+ delay (1 );
170
+ #endif
127
171
}
128
172
}
129
173
}
130
174
else {
131
175
delay (100 );
132
176
ble_is_connected = false ;
177
+ #ifdef ARDUINO_NANO_ESP32
178
+ science_kit.setStatusLed (STATUS_LED_PAIRING);
179
+ #endif
133
180
}
134
181
}
135
182
183
+
184
+ void loop (){
185
+ #ifdef ARDUINO_NANO_RP2040_CONNECT
186
+ updateBle ();
187
+ #endif
188
+ }
189
+
136
190
void updateSubscribedCharacteristics (){
137
191
/* ________________________________________________________________CURRENT */
138
192
if (currentCharacteristic.subscribed ()){
@@ -199,7 +253,6 @@ void updateSubscribedCharacteristics(){
199
253
/*
200
254
* BME688
201
255
*/
202
-
203
256
/* _____________________________________________________________TEMPERATURE */
204
257
if (temperatureCharacteristic.subscribed ()){
205
258
temperatureCharacteristic.writeValue (science_kit.getTemperature ());
@@ -219,10 +272,11 @@ void updateSubscribedCharacteristics(){
219
272
if (airQualityCharacteristic.subscribed ()){
220
273
airQualityCharacteristic.writeValue (science_kit.getAirQuality ());
221
274
}
222
-
275
+
223
276
/*
224
277
* MICROPHONE
225
278
*/
279
+ #ifdef ARDUINO_NANO_RP2040_CONNECT
226
280
227
281
/* _________________________________________________________SOUND_INTENSITY */
228
282
/* NOTE: raw value - value not in Db */
@@ -232,8 +286,9 @@ void updateSubscribedCharacteristics(){
232
286
233
287
/* _____________________________________________________________SOUND_PITCH */
234
288
if (sndPitchCharacteristic.subscribed ()){
235
- sndPitchCharacteristic.writeValue (science_kit. getExternalTemperature () );
289
+ sndPitchCharacteristic.writeValue (0.0 );
236
290
}
291
+ #endif
237
292
238
293
/* _________________________________________________________________INPUT_A */
239
294
if (inputACharacteristic.subscribed ()){
@@ -269,26 +324,16 @@ void updateSubscribedCharacteristics(){
269
324
270
325
/* ________________________________________________________________DISTANCE */
271
326
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 ());
280
330
}
281
331
282
332
/* ____________________________________________________________________PING */
283
333
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 );
292
337
}
293
338
}
294
339
0 commit comments