Skip to content

Commit c189741

Browse files
committed
add sound service
1 parent c7d9e2f commit c189741

File tree

6 files changed

+209
-28
lines changed

6 files changed

+209
-28
lines changed

libraries/BLEAdafruitService/src/BLEAdafruitService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "services/BLEAdafruitLightSensor.h"
4040
#include "services/BLEAdafruitMagnetic.h"
4141
#include "services/BLEAdafruitQuaternion.h"
42+
#include "services/BLEAdafruitSound.h"
4243
#include "services/BLEAdafruitTemperature.h"
4344
#include "services/BLEAdafruitTone.h"
4445

libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ void BLEAdafruitSensor::_measure_handler(void)
135135
return; // nothing to measure
136136
}
137137

138+
// no data to notify
139+
if (!len) return;
140+
138141
// Period = 0, compare with old data, only update on changes
139142
if ( 0 == _period.read32() )
140143
{
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "BLEAdafruitService.h"
26+
27+
//--------------------------------------------------------------------+
28+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
29+
//--------------------------------------------------------------------+
30+
31+
32+
/* All Adafruit Service/Characteristic UUID128 share the same Base UUID:
33+
* ADAFxxx-C332-42A8-93BD-25E905756CB8
34+
*
35+
* Shared Characteristics
36+
* - Measurement Period 0001 | int32_t | Read + Write |
37+
* ms between measurements, -1: stop reading, 0: update when changes
38+
*
39+
* Sound service 0B00
40+
* - Sound Samples 0B01 | int16_t[] | Read + Notify | samples, if dual channel then Left, Right, Left, Right ...
41+
* - Number Channels 0B02 | uint8_t | Read | 1 for mono, 2 for dual
42+
* - Measurement Period 0001
43+
*/
44+
45+
const uint8_t BLEAdafruitSound::UUID128_SERVICE[16] =
46+
{
47+
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
48+
0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0B, 0xAF, 0xAD
49+
};
50+
51+
const uint8_t BLEAdafruitSound::UUID128_CHR_DATA[16] =
52+
{
53+
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
54+
0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0B, 0xAF, 0xAD
55+
};
56+
57+
const uint8_t BLEAdafruitSound::UUID128_CHR_CHANNEL_COUNT[16] =
58+
{
59+
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
60+
0xA8, 0x42, 0x32, 0xC3, 0x02, 0x0B, 0xAF, 0xAD
61+
};
62+
63+
// Constructor
64+
BLEAdafruitSound::BLEAdafruitSound(void)
65+
: BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA), _channel_count(UUID128_CHR_CHANNEL_COUNT)
66+
{
67+
// Setup Measurement Characteristic
68+
_measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
69+
_measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
70+
_measurement.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX);
71+
72+
// channel count Characteristic
73+
_channel_count.setProperties(CHR_PROPS_READ);
74+
_channel_count.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
75+
_channel_count.setFixedLen(1);
76+
}
77+
78+
err_t BLEAdafruitSound::begin(uint8_t channel_count, measure_callback_t fp, int32_t ms)
79+
{
80+
// Invoke base class begin(), this will add Service, Measurement and Period characteristics
81+
VERIFY_STATUS( BLEAdafruitSensor::begin(fp, ms) );
82+
83+
// Add channel count Characteristic
84+
VERIFY_STATUS( _channel_count.begin() );
85+
_channel_count.write8(channel_count);
86+
87+
return ERROR_NONE;
88+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef BLEADAFRUIT_SOUND_H_
26+
#define BLEADAFRUIT_SOUND_H_
27+
28+
class BLEAdafruitSound : public BLEAdafruitSensor
29+
{
30+
public:
31+
static const uint8_t UUID128_SERVICE[16];
32+
static const uint8_t UUID128_CHR_DATA[16];
33+
static const uint8_t UUID128_CHR_CHANNEL_COUNT[16];
34+
35+
BLEAdafruitSound(void);
36+
37+
err_t begin(uint8_t channel_count, measure_callback_t fp, int32_t ms = DEFAULT_PERIOD);
38+
39+
private:
40+
BLECharacteristic _channel_count;
41+
};
42+
43+
#endif /* BLEADAFRUIT_SOUND_H_ */

libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,12 @@
2121

2222
#include <SPI.h>
2323
#include <SdFat.h>
24+
#include <PDM.h>
2425
#include <Adafruit_LittleFS.h>
2526
#include <InternalFileSystem.h>
2627
#include <bluefruit.h>
2728
#include <BLEAdafruitService.h>
2829

29-
// BLE Service
30-
BLEDfu bledfu; // OTA DFU service
31-
BLEDis bledis; // device information
32-
BLEUart bleuart; // uart over ble
33-
BLEBas blebas; // battery
34-
35-
// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8
36-
BLEAdafruitTemperature bleTemp;
37-
BLEAdafruitAccel bleAccel;
38-
BLEAdafruitLightSensor bleLight;
39-
BLEAdafruitButton bleButton;
40-
BLEAdafruitTone bleTone;
41-
42-
BLEAdafruitAddressablePixel blePixel;
43-
4430
//------------- Circuit Playground Bluefruit -------------//
4531
#if defined(ARDUINO_NRF52840_CIRCUITPLAY)
4632

@@ -103,12 +89,10 @@ BLEAdafruitQuaternion bleQuater;
10389

10490
Adafruit_LSM6DS33 lsm6ds33; // Gyro and Accel
10591
Adafruit_LIS3MDL lis3mdl; // Magnetometer
106-
10792
Adafruit_APDS9960 apds9960; // Proximity, Light, Gesture, Color
10893
Adafruit_BMP280 bmp280; // Temperature, Barometric
10994
Adafruit_SHT31 sht30; // Humid
11095

111-
11296
// pick your filter! slower == better quality output
11397
//Adafruit_NXPSensorFusion filter; // slowest
11498
//Adafruit_Madgwick filter; // faster than NXP
@@ -142,9 +126,9 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize)
142126

143127
uint32_t button = 0;
144128
button |= ( digitalRead(PIN_BUTTON1) ? 0x00 : 0x02 );
145-
#if defined(PIN_BUTTON2)
146-
button |= ( digitalRead(PIN_BUTTON2) ? 0x00 : 0x04 );
147-
#endif
129+
#if defined(PIN_BUTTON2)
130+
button |= ( digitalRead(PIN_BUTTON2) ? 0x00 : 0x04 );
131+
#endif
148132
memcpy(buf, &button, 4);
149133
return 4;
150134
}
@@ -160,7 +144,46 @@ uint16_t measure_humid(uint8_t* buf, uint16_t bufsize)
160144
#error "Board is not supported"
161145
#endif
162146

147+
//--------------------------------------------------------------------+
148+
// Common Services
149+
//--------------------------------------------------------------------+
150+
151+
// BLE Service
152+
BLEDfu bledfu; // OTA DFU service
153+
BLEDis bledis; // device information
154+
BLEUart bleuart; // uart over ble
155+
BLEBas blebas; // battery
156+
157+
// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8
158+
BLEAdafruitTemperature bleTemp;
159+
BLEAdafruitAccel bleAccel;
160+
BLEAdafruitLightSensor bleLight;
161+
BLEAdafruitButton bleButton;
162+
BLEAdafruitTone bleTone;
163+
BLEAdafruitAddressablePixel blePixel;
164+
BLEAdafruitSound bleSound;
165+
163166
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXEL_COUNT, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
167+
int16_t pdmSample[256]; // sound samples
168+
uint16_t pdmByteCount = 0;
169+
170+
//void pdm_plotter(uint16_t count)
171+
//{
172+
// for (int i = 0; i < count/2; i++) Serial.println(pdmSample[i]);
173+
//}
174+
175+
uint16_t measure_sound(uint8_t* buf, uint16_t bufsize)
176+
{
177+
uint16_t const len = min(bufsize, pdmByteCount);
178+
179+
if ( len ) memcpy(buf, pdmSample, len);
180+
pdmByteCount = 0; // clear count
181+
182+
// if (len) ada_callback(NULL, 0, pdm_plotter, len);
183+
184+
return len;
185+
}
186+
164187

165188
//------------- Setup -------------//
166189
void setup()
@@ -178,9 +201,9 @@ void setup()
178201

179202
// Button
180203
pinMode(PIN_BUTTON1, INPUT_PULLUP);
181-
#if defined(PIN_BUTTON2)
182-
pinMode(PIN_BUTTON2, INPUT_PULLUP);
183-
#endif
204+
#if defined(PIN_BUTTON2)
205+
pinMode(PIN_BUTTON2, INPUT_PULLUP);
206+
#endif
184207

185208
apds9960.begin();
186209
apds9960.enableColor(true);
@@ -214,6 +237,10 @@ void setup()
214237
cal.loadCalibration();
215238
#endif
216239

240+
// 1 channel (mono mode) with 16 kHz sample rate
241+
PDM.onReceive(onPDMdata);
242+
PDM.begin(1, 16000);
243+
217244
Serial.println("Bluefruit Playground Example");
218245
Serial.println("---------------------------\n");
219246

@@ -254,14 +281,15 @@ void setup()
254281
bleButton.begin(measure_button, 100);
255282
bleButton.setPeriod(0); // only notify if there is changes with buttons
256283

257-
#if defined(PIN_BUZZER)
258-
bleTone.begin(PIN_BUZZER);
259-
#endif
284+
#if defined(PIN_BUZZER)
285+
bleTone.begin(PIN_BUZZER);
286+
#endif
260287

261288
strip.begin();
262289
blePixel.begin(&strip);
263290

264-
bleAccel.begin(accel_sensor);
291+
bleAccel.begin(accel_sensor); // TODO dropped in favor to Quaternion service for CLUE & Sense
292+
bleSound.begin(1, measure_sound, 100);
265293

266294
// CPB doesn't support these on-board sensor
267295
#ifdef ARDUINO_NRF52840_CIRCUITPLAY
@@ -354,3 +382,13 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
354382
Serial.println();
355383
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
356384
}
385+
386+
387+
void onPDMdata()
388+
{
389+
// query the number of bytes available
390+
pdmByteCount = PDM.available();
391+
392+
// read into the sample buffer
393+
PDM.read(pdmSample, pdmByteCount);
394+
}

libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
#include <bluefruit.h>
3030

3131
#define NEOPIXEL_VERSION_STRING "Neopixel v2.0"
32-
#define PIN 30 /* Pin used to drive the NeoPixels */
32+
33+
/* Pin used to drive the NeoPixels */
34+
#if defined ARDUINO_NRF52840_CIRCUITPLAY
35+
#define PIN PIN_NEOPIXEL
36+
#elif defined ARDUINO_NRF52832_FEATHER
37+
#define PIN 30
38+
#else
39+
#define PIN 6
40+
#endif
3341

3442
#define MAXCOMPONENTS 4
3543
uint8_t *pixelBuffer = NULL;

0 commit comments

Comments
 (0)