diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h index 5ece5c2e..8d086f98 100644 --- a/libraries/BLEAdafruitService/src/BLEAdafruitService.h +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -39,6 +39,7 @@ #include "services/BLEAdafruitLightSensor.h" #include "services/BLEAdafruitMagnetic.h" #include "services/BLEAdafruitQuaternion.h" +#include "services/BLEAdafruitSound.h" #include "services/BLEAdafruitTemperature.h" #include "services/BLEAdafruitTone.h" diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp index fdcab020..d5bc931e 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp @@ -135,6 +135,9 @@ void BLEAdafruitSensor::_measure_handler(void) return; // nothing to measure } + // no data to notify + if (!len) return; + // Period = 0, compare with old data, only update on changes if ( 0 == _period.read32() ) { diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSound.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitSound.cpp new file mode 100644 index 00000000..aec85fc5 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSound.cpp @@ -0,0 +1,88 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "BLEAdafruitService.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + + +/* All Adafruit Service/Characteristic UUID128 share the same Base UUID: + * ADAFxxx-C332-42A8-93BD-25E905756CB8 + * + * Shared Characteristics + * - Measurement Period 0001 | int32_t | Read + Write | + * ms between measurements, -1: stop reading, 0: update when changes + * + * Sound service 0B00 + * - Sound Samples 0B01 | int16_t[] | Read + Notify | samples, if dual channel then Left, Right, Left, Right ... + * - Number Channels 0B02 | uint8_t | Read | 1 for mono, 2 for dual + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitSound::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0B, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitSound::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0B, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitSound::UUID128_CHR_CHANNEL_COUNT[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x0B, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitSound::BLEAdafruitSound(void) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA), _channel_count(UUID128_CHR_CHANNEL_COUNT) +{ + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX); + + // channel count Characteristic + _channel_count.setProperties(CHR_PROPS_READ); + _channel_count.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _channel_count.setFixedLen(1); +} + +err_t BLEAdafruitSound::begin(uint8_t channel_count, measure_callback_t fp, int32_t ms) +{ + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(fp, ms) ); + + // Add channel count Characteristic + VERIFY_STATUS( _channel_count.begin() ); + _channel_count.write8(channel_count); + + return ERROR_NONE; +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSound.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitSound.h new file mode 100644 index 00000000..e8972ba6 --- /dev/null +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSound.h @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef BLEADAFRUIT_SOUND_H_ +#define BLEADAFRUIT_SOUND_H_ + +class BLEAdafruitSound : public BLEAdafruitSensor +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + static const uint8_t UUID128_CHR_CHANNEL_COUNT[16]; + + BLEAdafruitSound(void); + + err_t begin(uint8_t channel_count, measure_callback_t fp, int32_t ms = DEFAULT_PERIOD); + + private: + BLECharacteristic _channel_count; +}; + +#endif /* BLEADAFRUIT_SOUND_H_ */ diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino index d0258696..70425b98 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/bluefruit_playground/bluefruit_playground.ino @@ -21,26 +21,12 @@ #include #include +#include #include #include #include #include -// BLE Service -BLEDfu bledfu; // OTA DFU service -BLEDis bledis; // device information -BLEUart bleuart; // uart over ble -BLEBas blebas; // battery - -// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8 -BLEAdafruitTemperature bleTemp; -BLEAdafruitAccel bleAccel; -BLEAdafruitLightSensor bleLight; -BLEAdafruitButton bleButton; -BLEAdafruitTone bleTone; - -BLEAdafruitAddressablePixel blePixel; - //------------- Circuit Playground Bluefruit -------------// #if defined(ARDUINO_NRF52840_CIRCUITPLAY) @@ -103,12 +89,10 @@ BLEAdafruitQuaternion bleQuater; Adafruit_LSM6DS33 lsm6ds33; // Gyro and Accel Adafruit_LIS3MDL lis3mdl; // Magnetometer - Adafruit_APDS9960 apds9960; // Proximity, Light, Gesture, Color Adafruit_BMP280 bmp280; // Temperature, Barometric Adafruit_SHT31 sht30; // Humid - // pick your filter! slower == better quality output //Adafruit_NXPSensorFusion filter; // slowest //Adafruit_Madgwick filter; // faster than NXP @@ -142,9 +126,9 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize) uint32_t button = 0; button |= ( digitalRead(PIN_BUTTON1) ? 0x00 : 0x02 ); - #if defined(PIN_BUTTON2) - button |= ( digitalRead(PIN_BUTTON2) ? 0x00 : 0x04 ); - #endif +#if defined(PIN_BUTTON2) + button |= ( digitalRead(PIN_BUTTON2) ? 0x00 : 0x04 ); +#endif memcpy(buf, &button, 4); return 4; } @@ -160,7 +144,46 @@ uint16_t measure_humid(uint8_t* buf, uint16_t bufsize) #error "Board is not supported" #endif +//--------------------------------------------------------------------+ +// Common Services +//--------------------------------------------------------------------+ + +// BLE Service +BLEDfu bledfu; // OTA DFU service +BLEDis bledis; // device information +BLEUart bleuart; // uart over ble +BLEBas blebas; // battery + +// Adafruit Service: ADAFxx-C332-42A8-93BD-25E905756CB8 +BLEAdafruitTemperature bleTemp; +BLEAdafruitAccel bleAccel; +BLEAdafruitLightSensor bleLight; +BLEAdafruitButton bleButton; +BLEAdafruitTone bleTone; +BLEAdafruitAddressablePixel blePixel; +BLEAdafruitSound bleSound; + Adafruit_NeoPixel strip = Adafruit_NeoPixel(NEOPIXEL_COUNT, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); +int16_t pdmSample[256]; // sound samples +uint16_t pdmByteCount = 0; + +//void pdm_plotter(uint16_t count) +//{ +// for (int i = 0; i < count/2; i++) Serial.println(pdmSample[i]); +//} + +uint16_t measure_sound(uint8_t* buf, uint16_t bufsize) +{ + uint16_t const len = min(bufsize, pdmByteCount); + + if ( len ) memcpy(buf, pdmSample, len); + pdmByteCount = 0; // clear count + +// if (len) ada_callback(NULL, 0, pdm_plotter, len); + + return len; +} + //------------- Setup -------------// void setup() @@ -178,9 +201,9 @@ void setup() // Button pinMode(PIN_BUTTON1, INPUT_PULLUP); - #if defined(PIN_BUTTON2) - pinMode(PIN_BUTTON2, INPUT_PULLUP); - #endif +#if defined(PIN_BUTTON2) + pinMode(PIN_BUTTON2, INPUT_PULLUP); +#endif apds9960.begin(); apds9960.enableColor(true); @@ -214,6 +237,10 @@ void setup() cal.loadCalibration(); #endif + // 1 channel (mono mode) with 16 kHz sample rate + PDM.onReceive(onPDMdata); + PDM.begin(1, 16000); + Serial.println("Bluefruit Playground Example"); Serial.println("---------------------------\n"); @@ -254,14 +281,15 @@ void setup() bleButton.begin(measure_button, 100); bleButton.setPeriod(0); // only notify if there is changes with buttons - #if defined(PIN_BUZZER) - bleTone.begin(PIN_BUZZER); - #endif +#if defined(PIN_BUZZER) + bleTone.begin(PIN_BUZZER); +#endif strip.begin(); blePixel.begin(&strip); - bleAccel.begin(accel_sensor); + bleAccel.begin(accel_sensor); // TODO dropped in favor to Quaternion service for CLUE & Sense + bleSound.begin(1, measure_sound, 100); // CPB doesn't support these on-board sensor #ifdef ARDUINO_NRF52840_CIRCUITPLAY @@ -354,3 +382,13 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) Serial.println(); Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); } + + +void onPDMdata() +{ + // query the number of bytes available + pdmByteCount = PDM.available(); + + // read into the sample buffer + PDM.read(pdmSample, pdmByteCount); +} diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino b/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino index 0da9a287..99449c30 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/neopixel/neopixel.ino @@ -29,7 +29,15 @@ #include #define NEOPIXEL_VERSION_STRING "Neopixel v2.0" -#define PIN 30 /* Pin used to drive the NeoPixels */ + +/* Pin used to drive the NeoPixels */ +#if defined ARDUINO_NRF52840_CIRCUITPLAY + #define PIN PIN_NEOPIXEL +#elif defined ARDUINO_NRF52832_FEATHER + #define PIN 30 +#else + #define PIN 6 +#endif #define MAXCOMPONENTS 4 uint8_t *pixelBuffer = NULL;