1+ /*
2+ This file is part of the Arduino_ScienceKitCarrier library.
3+ Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+ This library is free software; you can redistribute it and/or
6+ modify it under the terms of the GNU Lesser General Public
7+ License as published by the Free Software Foundation; either
8+ version 2.1 of the License, or (at your option) any later version.
9+
10+ This library is distributed in the hope that it will be useful,
11+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+ Lesser General Public License for more details.
14+
15+ You should have received a copy of the GNU Lesser General Public
16+ License along with this library; if not, write to the Free Software
17+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+ */
19+
20+ #include " ble_config.h"
21+ #include " Arduino_ScienceKitCarrier.h"
22+
23+ String name;
24+ unsigned long lastNotify = 0 ;
25+ ScienceKitCarrier science_kit;
26+
27+ #ifdef ARDUINO_NANO_RP2040_CONNECT
28+ rtos::Thread thread_update_sensors;
29+ #endif
30+
31+ bool ble_is_connected = false ;
32+
33+
34+ void setup (){
35+ science_kit.begin (NO_AUXILIARY_THREADS); // Doesn't start the BME688 and external temperature threads for the moment
36+
37+ if (!BLE.begin ()){
38+ while (1 );
39+ }
40+
41+ String address = BLE.address ();
42+
43+ address.toUpperCase ();
44+
45+ name = " ScienceKit R3 - " ;
46+ name += address[address.length () - 5 ];
47+ name += address[address.length () - 4 ];
48+ name += address[address.length () - 2 ];
49+ name += address[address.length () - 1 ];
50+
51+ BLE.setLocalName (name.c_str ());
52+ BLE.setDeviceName (name.c_str ());
53+
54+
55+ BLE.setAdvertisedService (service);
56+ /* ________________________________________________________________VERSION */
57+ service.addCharacteristic (versionCharacteristic);
58+ /* ________________________________________________________________CURRENT */
59+ service.addCharacteristic (currentCharacteristic);
60+ /* ________________________________________________________________VOLTAGE */
61+ service.addCharacteristic (voltageCharacteristic);
62+ /* ______________________________________________________________RESISTANCE */
63+ service.addCharacteristic (resistanceCharacteristic);
64+ /* _________________________________________________________________LIGHT */
65+ service.addCharacteristic (lightCharacteristic);
66+ /* _______________________________________________________________PROXIMITY */
67+ service.addCharacteristic (proximityCharacteristic);
68+ /* ____________________________________________________________ACCELERATION */
69+ service.addCharacteristic (accelerationCharacteristic);
70+ /* _______________________________________________________________GYROSCOPE */
71+ service.addCharacteristic (gyroscopeCharacteristic);
72+ /* ____________________________________________________________MAGNETOMETER */
73+ service.addCharacteristic (magnetometerCharacteristic);
74+ /* _____________________________________________________________TEMPERATURE */
75+ service.addCharacteristic (temperatureCharacteristic);
76+ /* ________________________________________________________________PRESSURE */
77+ service.addCharacteristic (pressureCharacteristic);
78+ /* ________________________________________________________________HUMIDITY */
79+ service.addCharacteristic (humidityCharacteristic);
80+ /* _____________________________________________________________AIR_QUALITY */
81+ service.addCharacteristic (airQualityCharacteristic);
82+
83+ #ifdef ARDUINO_NANO_RP2040_CONNECT
84+ /* _________________________________________________________SOUND_INTENSITY */
85+ service.addCharacteristic (sndIntensityCharacteristic);
86+ /* _____________________________________________________________SOUND_PITCH */
87+ service.addCharacteristic (sndPitchCharacteristic);
88+ #endif
89+
90+ /* _________________________________________________________________INPUT_A */
91+ service.addCharacteristic (inputACharacteristic);
92+ /* _________________________________________________________________INPUT_B */
93+ service.addCharacteristic (inputBCharacteristic);
94+ /* ____________________________________________________EXTERNAL_TEMPERATURE */
95+ service.addCharacteristic (extTempCharacteristic);
96+ /* ____________________________________________________FUNCTION_GENERATOR_1 */
97+ service.addCharacteristic (funcGenOneCharacteristic);
98+ /* ____________________________________________________FUNCTION_GENERATOR_2 */
99+ service.addCharacteristic (funcGenTwoCharacteristic);
100+ /* ________________________________________________________________DISTANCE */
101+ service.addCharacteristic (distanceCharacteristic);
102+ /* ____________________________________________________________________PING */
103+ service.addCharacteristic (pingCharacteristic);
104+
105+
106+ /* ________________________________________________________________VERSION */
107+ versionCharacteristic.setValue (VERSION);
108+
109+ BLE.addService (service);
110+ BLE.advertise ();
111+
112+ // science_kit.startAuxiliaryThreads(); // start the BME688 and External Temperature Probe threads
113+ #ifdef ARDUINO_NANO_RP2040_CONNECT
114+ thread_update_sensors.start (update); // this thread updates sensors
115+ #endif
116+ }
117+
118+
119+ void update (void ){
120+ while (1 ){
121+ science_kit.update (ROUND_ROBIN_ENABLED);
122+ // rtos::ThisThread::sleep_for(25);
123+ delay (25 );
124+ }
125+ }
126+
127+ void loop (){
128+ BLEDevice central = BLE.central ();
129+ if (central) {
130+ ble_is_connected = true ;
131+ lastNotify=millis ();
132+ while (central.connected ()) {
133+ if (millis ()-lastNotify>10 ){
134+ updateSubscribedCharacteristics ();
135+ lastNotify=millis ();
136+ }
137+ }
138+ }
139+ else {
140+ delay (100 );
141+ ble_is_connected = false ;
142+ }
143+ }
144+
145+ void updateSubscribedCharacteristics (){
146+ /* ________________________________________________________________CURRENT */
147+ if (currentCharacteristic.subscribed ()){
148+ currentCharacteristic.writeValue (science_kit.getCurrent ());
149+ }
150+
151+
152+ /* ________________________________________________________________VOLTAGE */
153+ if (voltageCharacteristic.subscribed ()){
154+ voltageCharacteristic.writeValue (science_kit.getVoltage ());
155+ }
156+
157+
158+ /* ______________________________________________________________RESISTANCE */
159+ if (resistanceCharacteristic.subscribed ()){
160+ resistanceCharacteristic.writeValue (science_kit.getResistance ());
161+ }
162+
163+
164+ /* _________________________________________________________________LIGHT */
165+ if (lightCharacteristic.subscribed ()){
166+ long light[4 ];
167+ light[0 ] = science_kit.getRed ();
168+ light[1 ] = science_kit.getGreen ();
169+ light[2 ] = science_kit.getBlue ();
170+ light[3 ] = science_kit.getClear ();
171+ lightCharacteristic.writeValue ((byte*)light, sizeof (light));
172+ }
173+
174+
175+ /* _______________________________________________________________PROXIMITY */
176+ if (proximityCharacteristic.subscribed ()){
177+ proximityCharacteristic.writeValue (science_kit.getProximity ());
178+ }
179+
180+
181+ /* ____________________________________________________________ACCELERATION */
182+ if (accelerationCharacteristic.subscribed ()){
183+ float acceleration[3 ];
184+ acceleration[0 ] = science_kit.getAccelerationX ();
185+ acceleration[1 ] = science_kit.getAccelerationY ();
186+ acceleration[2 ] = science_kit.getAccelerationZ ();
187+ accelerationCharacteristic.writeValue ((byte*)acceleration, sizeof (acceleration));
188+ }
189+
190+ /* _______________________________________________________________GYROSCOPE */
191+ if (gyroscopeCharacteristic.subscribed ()){
192+ float gyroscope[3 ];
193+ gyroscope[0 ] = science_kit.getAngularVelocityX ();
194+ gyroscope[1 ] = science_kit.getAngularVelocityY ();
195+ gyroscope[2 ] = science_kit.getAngularVelocityZ ();
196+ gyroscopeCharacteristic.writeValue ((byte*)gyroscope, sizeof (gyroscope));
197+ }
198+
199+ /* ____________________________________________________________MAGNETOMETER */
200+ if (magnetometerCharacteristic.subscribed ()){
201+ float magnetometer[3 ];
202+ magnetometer[0 ] = science_kit.getMagneticFieldX ();
203+ magnetometer[1 ] = science_kit.getMagneticFieldY ();
204+ magnetometer[2 ] = science_kit.getMagneticFieldZ ();
205+ magnetometerCharacteristic.writeValue ((byte*)magnetometer, sizeof (magnetometer));
206+ }
207+
208+ /*
209+ * BME688
210+ */
211+
212+ /* _____________________________________________________________TEMPERATURE */
213+ if (temperatureCharacteristic.subscribed ()){
214+ temperatureCharacteristic.writeValue (science_kit.getTemperature ());
215+ }
216+
217+ /* ________________________________________________________________PRESSURE */
218+ if (pressureCharacteristic.subscribed ()){
219+ pressureCharacteristic.writeValue (science_kit.getPressure ());
220+ }
221+
222+ /* ________________________________________________________________HUMIDITY */
223+ if (humidityCharacteristic.subscribed ()){
224+ humidityCharacteristic.writeValue (science_kit.getHumidity ());
225+ }
226+
227+ /* _____________________________________________________________AIR_QUALITY */
228+ if (airQualityCharacteristic.subscribed ()){
229+ airQualityCharacteristic.writeValue (science_kit.getAirQuality ());
230+ }
231+
232+ /*
233+ * MICROPHONE
234+ */
235+ #ifdef ARDUINO_NANO_RP2040_CONNECT
236+
237+ /* _________________________________________________________SOUND_INTENSITY */
238+ /* NOTE: raw value - value not in Db */
239+ if (sndIntensityCharacteristic.subscribed ()){
240+ sndIntensityCharacteristic.writeValue (science_kit.getMicrophoneRMS ());
241+ }
242+
243+ /* _____________________________________________________________SOUND_PITCH */
244+ if (sndPitchCharacteristic.subscribed ()){
245+ sndPitchCharacteristic.writeValue (science_kit.getExternalTemperature ());
246+ }
247+ #endif
248+
249+ /* _________________________________________________________________INPUT_A */
250+ if (inputACharacteristic.subscribed ()){
251+ inputACharacteristic.writeValue (science_kit.getInputA ());
252+ }
253+
254+ /* _________________________________________________________________INPUT_B */
255+ if (inputBCharacteristic.subscribed ()){
256+ inputBCharacteristic.writeValue (science_kit.getInputB ());
257+ }
258+
259+ /* _____________________________________________________EXTERNAL_TEMPERATURE */
260+ if (extTempCharacteristic.subscribed ()){
261+ // extTempCharacteristic.writeValue(science_kit.getExternalTemperature());
262+ extTempCharacteristic.writeValue (123.0 );
263+ }
264+
265+
266+ /* ____________________________________________________FUNCTION_GENERATOR_1 */
267+ if (funcGenOneCharacteristic.subscribed ()){
268+ long f1[2 ];
269+ f1[0 ] = (science_kit.getFrequency1 () * science_kit.getRange1 ());
270+ f1[1 ] = science_kit.getPhase1 ();
271+ funcGenOneCharacteristic.writeValue ((byte*)f1, sizeof (f1));
272+ }
273+
274+ /* ____________________________________________________FUNCTION_GENERATOR_2 */
275+ if (funcGenTwoCharacteristic.subscribed ()){
276+ long f2[2 ];
277+ f2[0 ] = (science_kit.getFrequency2 () * science_kit.getRange2 ());
278+ f2[1 ] = science_kit.getPhase2 ();
279+ funcGenTwoCharacteristic.writeValue ((byte*)f2, sizeof (f2));
280+ }
281+
282+ /* ________________________________________________________________DISTANCE */
283+ if (distanceCharacteristic.subscribed ()){
284+ if (science_kit.getUltrasonicIsConnected ()){
285+ /* NOTE: getDistance() calls getMeters()
286+ Requested value is in meters */
287+ distanceCharacteristic.writeValue (science_kit.getDistance ());
288+ }
289+ else {
290+ distanceCharacteristic.writeValue (-1.0 );
291+ }
292+ }
293+
294+ /* ____________________________________________________________________PING */
295+ if (pingCharacteristic.subscribed ()){
296+ if (science_kit.getUltrasonicIsConnected ()){
297+ /* NOTE: getTravelTime() returns micro seconds */
298+ /* Converted to milliseconds (agreed with RF 20230719) */
299+ pingCharacteristic.writeValue (science_kit.getTravelTime () * 1000.0 );
300+ }
301+ else {
302+ pingCharacteristic.writeValue (-1.0 );
303+ }
304+ }
305+ }
306+
307+
308+
309+
310+
311+ /* **
312+ * _ _
313+ * /\ | | (_)
314+ * / \ _ __ __| |_ _ _ _ __ ___
315+ * / /\ \ | '__/ _` | | | | | '_ \ / _ \
316+ * / ____ \| | | (_| | |_| | | | | | (_) |
317+ * /_/____\_\_| _\__,_|\__,_|_|_| |_|\___/ ___ _ _____ ____
318+ * / ____| (_) | |/ (_) | | __ \|___ \
319+ * | (___ ___ _ ___ _ __ ___ ___ | ' / _| |_ | |__) | __) |
320+ * \___ \ / __| |/ _ \ '_ \ / __/ _ \ | < | | __| | _ / |__ <
321+ * ____) | (__| | __/ | | | (_| __/ | . \| | |_ | | \ \ ___) |
322+ * |_____/ \___|_|\___|_| |_|\___\___| |_|\_\_|\__| |_| \_\____/
323+ *
324+ *
325+ */
0 commit comments