Merge pull request #452 from adafruit/add-sound-service

Add more sensor services
This commit is contained in:
Limor "Ladyada" Fried authored and GitHub committed 2020-03-11 13:31:48 -04:00
commit 70ff084b16
25 files changed
+701 -79

No files matched your search

+2 -3
View File
@@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
arduino-platform: ['feather52832', 'feather52840', 'feather52840sense', 'cplaynrf52840', 'itsybitsy52840', 'cluenrf52840' ]
arduino-platform: ['cluenrf52840', 'cplaynrf52840', 'feather52832', 'feather52840', 'feather52840sense', 'itsybitsy52840']
runs-on: ubuntu-latest
@@ -52,8 +52,7 @@ jobs:
rm -r $HOME/$BSP_PATH/*
ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION
# Install library dependency
arduino-cli lib install "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit ST7735 and ST7789 Library" "Adafruit EPD"
arduino-cli lib install "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" "MIDI Library" "Firmata"
arduino-cli lib install "Adafruit AHRS" "Adafruit APDS9960 Library" "Adafruit BMP280 Library" "Adafruit Circuit Playground" "Adafruit EPD" "Adafruit GFX Library" "Adafruit HX8357 Library" "Adafruit ILI9341" "Adafruit LIS3MDL" "Adafruit LSM6DS" "Adafruit NeoPixel" "Adafruit NeoMatrix" "Adafruit Sensor Calibration" "Adafruit SHT31 Library" "Adafruit SSD1306" "Adafruit ST7735 and ST7789 Library" "Firmata" "MIDI Library" "SdFat - Adafruit Fork"
- name: Build examples
run: python3 tools/build_all.py ${{ matrix.arduino-platform }}
+10 -4
View File
@@ -44,13 +44,19 @@ static void loop_task(void* arg)
{
(void) arg;
#if CFG_DEBUG
// If Serial is not begin(), call it to avoid hard fault
Serial.begin(115200);
// Wait for Serial connection in debug mode
while ( !Serial ) yield();
dbgPrintVersion();
#endif
setup();
#if CFG_DEBUG
// If Serial is not begin(), call it to avoid hard fault
if ( !Serial ) Serial.begin(115200);
dbgPrintVersion();
// dbgMemInfo();
Bluefruit_printInfo();
#endif
@@ -34,11 +34,15 @@
#include "services/BLEAdafruitAddressablePixel.h"
#include "services/BLEAdafruitBaro.h"
#include "services/BLEAdafruitButton.h"
#include "services/BLEAdafruitColor.h"
#include "services/BLEAdafruitGesture.h"
#include "services/BLEAdafruitGyro.h"
#include "services/BLEAdafruitHumid.h"
#include "services/BLEAdafruitLightSensor.h"
#include "services/BLEAdafruitMagnetic.h"
#include "services/BLEAdafruitProximity.h"
#include "services/BLEAdafruitQuaternion.h"
#include "services/BLEAdafruitSound.h"
#include "services/BLEAdafruitTemperature.h"
#include "services/BLEAdafruitTone.h"
@@ -109,9 +109,9 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
// Add Characteristic
_data.setProperties(CHR_PROPS_WRITE);
_data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN);
// Change to use VLOC STACK to USER due to lack of memroy
// Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH));
_data.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX);
// start (2 byte) + flags (1 byte)
// TODO: for backward compatible with current CPB app, force to at least 10 pixels
_data.setMaxLen(3 + 30 /* bufsize */);
VERIFY_STATUS( _data.begin() );
// Add Characteristic
@@ -126,28 +126,34 @@ err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t buf
return ERROR_NONE;
}
//--------------------------------------------------------------------+
// Static callbacks
//--------------------------------------------------------------------+
void BLEAdafruitAddressablePixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len)
void BLEAdafruitAddressablePixel::_pixel_write_handler(uint16_t conn_hdl, uint8_t* data, uint16_t len)
{
(void) conn_hdl;
if (len < 3) return;
BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService();
uint16_t index;
memcpy(&index, data, 2);
uint8_t flag = data[2];
uint8_t* buffer = svc._neo->getPixels() + index;
// pixel staring buffer
uint8_t* pixbuf = _neo->getPixels() + index;
memcpy(buffer, data+3, len-3);
// limit copied bytes up to strip's boundary
uint16_t copied_count = max16(_bufsize.read16(), index) - index;
copied_count = min16(len-3, copied_count);
if (copied_count) memcpy(pixbuf, data+3, copied_count);
// show flag
if ( flag & 0x01 )
{
svc._neo->show();
}
if ( flag & 0x01 ) _neo->show();
}
//--------------------------------------------------------------------+
// Static callbacks
//--------------------------------------------------------------------+
void BLEAdafruitAddressablePixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len)
{
BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService();
svc._pixel_write_handler(conn_hdl, data, len);
}
@@ -49,6 +49,8 @@ class BLEAdafruitAddressablePixel : public BLEService
err_t begin(uint8_t pin, uint8_t type, uint16_t bufsize);
void _pixel_write_handler(uint16_t conn_hdl, uint8_t* data, uint16_t len);
static void pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len);
};
@@ -0,0 +1,64 @@
/*
* 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
*
* Color service 0A00
* - Color 0A01 | uint16_t[3] | Read + Notify | Red, Green, Blue each is 16-bit color
* - Measurement Period 0001
*/
const uint8_t BLEAdafruitColor::UUID128_SERVICE[16] =
{
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0A, 0xAF, 0xAD
};
const uint8_t BLEAdafruitColor::UUID128_CHR_DATA[16] =
{
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0A, 0xAF, 0xAD
};
// Constructor
BLEAdafruitColor::BLEAdafruitColor(void)
: BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA)
{
// Setup Measurement Characteristic
_measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
_measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
_measurement.setFixedLen(2*3);
}
@@ -0,0 +1,39 @@
/*
* 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_COLOR_H_
#define BLEADAFRUIT_COLOR_H_
class BLEAdafruitColor : public BLEAdafruitSensor
{
public:
static const uint8_t UUID128_SERVICE[16];
static const uint8_t UUID128_CHR_DATA[16];
BLEAdafruitColor(void);
using BLEAdafruitSensor::begin;
};
#endif /* BLEADAFRUIT_COLOR_H_ */
@@ -0,0 +1,64 @@
/*
* 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
*
* Gesture service 06F00
* - Button 0F01 | unt8_t | Read + Notify | e.g 0: no gesture, 1: Up, 2: Down, 3: Left, 4: Right
* - Measurement Period 0001
*/
const uint8_t BLEAdafruitGesture::UUID128_SERVICE[16] =
{
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0F, 0xAF, 0xAD
};
const uint8_t BLEAdafruitGesture::UUID128_CHR_DATA[16] =
{
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0F, 0xAF, 0xAD
};
// Constructor
BLEAdafruitGesture::BLEAdafruitGesture(void)
: BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA)
{
// Setup Measurement Characteristic
_measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
_measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
_measurement.setFixedLen(1);
}
@@ -0,0 +1,39 @@
/*
* 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_GESTURE_H_
#define BLEADAFRUIT_GESTURE_H_
class BLEAdafruitGesture : public BLEAdafruitSensor
{
public:
static const uint8_t UUID128_SERVICE[16];
static const uint8_t UUID128_CHR_DATA[16];
BLEAdafruitGesture(void);
using BLEAdafruitSensor::begin;
};
#endif /* BLEADAFRUIT_GESTURE_H_ */
@@ -0,0 +1,64 @@
/*
* 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
*
* Accelerometer service 0E00
* - Data 0E01 | uint16_t | Read + Notify | unit-less
* - Measurement Period 0001
*/
const uint8_t BLEAdafruitProximity::UUID128_SERVICE[16] =
{
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0E, 0xAF, 0xAD
};
const uint8_t BLEAdafruitProximity::UUID128_CHR_DATA[16] =
{
0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93,
0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0E, 0xAF, 0xAD
};
// Constructor
BLEAdafruitProximity::BLEAdafruitProximity(void)
: BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA)
{
// Setup Measurement Characteristic
_measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
_measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
_measurement.setFixedLen(2);
}
@@ -0,0 +1,39 @@
/*
* 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_PROXIMITY_H_
#define BLEADAFRUIT_PROXIMITY_H_
class BLEAdafruitProximity : public BLEAdafruitSensor
{
public:
static const uint8_t UUID128_SERVICE[16];
static const uint8_t UUID128_CHR_DATA[16];
BLEAdafruitProximity(void);
using BLEAdafruitSensor::begin;
};
#endif /* BLEADAFRUIT_PROXIMITY_H_ */
@@ -95,7 +95,7 @@ void BLEAdafruitQuaternion::setCalibration(Adafruit_Sensor_Calibration* calib)
_calib = calib;
}
void BLEAdafruitQuaternion::_notify_cb(uint16_t conn_hdl, uint16_t value)
void BLEAdafruitQuaternion::_notify_handler(uint16_t conn_hdl, uint16_t value)
{
// Start/Stop filter timer
if (value & BLE_GATT_HVX_NOTIFICATION)
@@ -107,7 +107,7 @@ void BLEAdafruitQuaternion::_notify_cb(uint16_t conn_hdl, uint16_t value)
}
// Call SuperClass function
BLEAdafruitSensor::_notify_cb(conn_hdl, value);
BLEAdafruitSensor::_notify_handler(conn_hdl, value);
}
void BLEAdafruitQuaternion::_update_timer(int32_t ms)
@@ -43,7 +43,7 @@ class BLEAdafruitQuaternion : public BLEAdafruitSensor
protected:
void _update_timer(int32_t ms);
void _measure_handler(void);
void _notify_cb(uint16_t conn_hdl, uint16_t value);
void _notify_handler(uint16_t conn_hdl, uint16_t value);
void _fitler_update(void);
@@ -31,8 +31,9 @@
BLEAdafruitSensor::BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid)
: BLEService(service_uuid), _measurement(data_uuid), _period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD)
{
_measure_cb = NULL;
_sensor = NULL;
_measure_cb = NULL;
_notify_cb = NULL;
}
err_t BLEAdafruitSensor::_begin(int32_t ms)
@@ -41,8 +42,8 @@ err_t BLEAdafruitSensor::_begin(int32_t ms)
VERIFY_STATUS( BLEService::begin() );
_measurement.setCccdWriteCallback(sensor_data_cccd_cb, true);
VERIFY_STATUS( _measurement.begin() );
_measurement.write32(0); // zero 4 bytes could help to init some services
_period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE);
_period.setPermission(SECMODE_OPEN, SECMODE_OPEN);
@@ -76,10 +77,15 @@ void BLEAdafruitSensor::setPeriod(int32_t period_ms)
_update_timer(period_ms);
}
void BLEAdafruitSensor::setNotifyCallback(notify_callback_t fp)
{
_notify_cb = fp;
}
//--------------------------------------------------------------------+
// Internal API
//--------------------------------------------------------------------+
void BLEAdafruitSensor::_notify_cb(uint16_t conn_hdl, uint16_t value)
void BLEAdafruitSensor::_notify_handler(uint16_t conn_hdl, uint16_t value)
{
// notify enabled
if (value & BLE_GATT_HVX_NOTIFICATION)
@@ -90,6 +96,9 @@ void BLEAdafruitSensor::_notify_cb(uint16_t conn_hdl, uint16_t value)
_timer.stop();
}
// invoke callback if any
if (_notify_cb) _notify_cb(conn_hdl, value);
// send initial notification if period = 0
// if ( 0 == svc._period.read32() )
// {
@@ -135,6 +144,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() )
{
@@ -175,6 +187,6 @@ void BLEAdafruitSensor::sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic
{
BLEAdafruitSensor* svc = (BLEAdafruitSensor*) &chr->parentService();
svc->_notify_cb(conn_hdl, value);
svc->_notify_handler(conn_hdl, value);
}
@@ -31,6 +31,8 @@ class BLEAdafruitSensor : public BLEService
{
public:
static const int32_t DEFAULT_PERIOD = 1000;
typedef void (*notify_callback_t)(uint16_t conn_hdl, bool enabled);
typedef uint16_t (*measure_callback_t )(uint8_t* buf, uint16_t bufsize);
BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid);
@@ -39,6 +41,7 @@ class BLEAdafruitSensor : public BLEService
virtual err_t begin(Adafruit_Sensor* sensor, int32_t ms = DEFAULT_PERIOD);
void setPeriod(int32_t period_ms);
void setNotifyCallback(notify_callback_t fp);
protected:
BLECharacteristic _period;
@@ -47,13 +50,15 @@ class BLEAdafruitSensor : public BLEService
Adafruit_Sensor* _sensor;
measure_callback_t _measure_cb;
notify_callback_t _notify_cb;
SoftwareTimer _timer;
err_t _begin(int32_t ms);
virtual void _update_timer(int32_t ms);
virtual void _measure_handler(void);
virtual void _notify_cb(uint16_t conn_hdl, uint16_t value);
virtual void _notify_handler(uint16_t conn_hdl, uint16_t value);
static void sensor_timer_cb(TimerHandle_t xTimer);
static void sensor_period_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len);
@@ -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;
}
@@ -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_ */
@@ -24,7 +24,7 @@ BLEBas blebas; // battery
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
// while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Bluefruit52 BLEUART Example");
Serial.println("---------------------------\n");
@@ -21,26 +21,12 @@
#include <SPI.h>
#include <SdFat.h>
#include <PDM.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
#include <bluefruit.h>
#include <BLEAdafruitService.h>
// 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)
@@ -80,9 +66,9 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize)
#elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE)
#include <Adafruit_APDS9960.h>
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_SHT31.h>
#include <Adafruit_SPIFlash.h>
@@ -97,18 +83,19 @@ uint16_t measure_button(uint8_t* buf, uint16_t bufsize)
#define NEOPIXEL_COUNT 1
BLEAdafruitHumid bleHumid;
BLEAdafruitBaro bleBaro;
BLEAdafruitColor bleColor;
BLEAdafruitGesture bleGesture;
BLEAdafruitHumid bleHumid;
BLEAdafruitProximity bleProximity;
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
@@ -122,19 +109,85 @@ Adafruit_FlashTransport_QSPI flashTransport;
Adafruit_SPIFlash flash(&flashTransport);
FatFileSystem fatfs;
void light_enable_callback(uint16_t conn_hdl, bool enabled)
{
(void) conn_hdl;
apds9960.enableColor(enabled);
}
uint16_t measure_light(uint8_t* buf, uint16_t bufsize)
{
float lux;
uint16_t r, g, b, c;
apds9960.getColorData(&r, &g, &b, &c);
lux = c;
memcpy(buf, &lux, 4);
return 4;
}
void color_enable_callback(uint16_t conn_hdl, bool enabled)
{
(void) conn_hdl;
apds9960.enableColor(enabled);
#ifdef ARDUINO_NRF52840_CLUE
digitalWrite(PIN_LED2, enabled);
#endif
}
uint16_t measure_color(uint8_t* buf, uint16_t bufsize)
{
uint16_t rgb[3];
uint16_t c;
(void) c;
apds9960.getColorData(rgb+0, rgb+1, rgb+2, &c);
memcpy(buf, rgb, sizeof(rgb));
return sizeof(rgb);
}
void gesture_enable_callback(uint16_t conn_hdl, bool enabled)
{
(void) conn_hdl;
apds9960.enableProximity(enabled);
apds9960.enableGesture(enabled);
}
uint16_t measure_gesture(uint8_t* buf, uint16_t bufsize)
{
uint8_t gesture = apds9960.readGesture();
if (gesture == 0) return 0; // skip no gesture value
// APDS9960 sensor position is rotated 90 degree left on CLUE
// We will need to correct that by rotating right for user convenience
uint8_t const clue_rotation[] = { 0, APDS9960_LEFT, APDS9960_RIGHT, APDS9960_DOWN, APDS9960_UP };
buf[0] = clue_rotation[gesture];
return 1;
}
void proximity_enable_callback(uint16_t conn_hdl, bool enabled)
{
(void) conn_hdl;
apds9960.enableProximity(enabled);
}
uint16_t measure_proximity(uint8_t* buf, uint16_t bufsize)
{
// APDS is only 8-bit, we better to map it to 16-bit value
uint8_t data8 = apds9960.readProximity();
uint16_t data16 = (uint16_t) map(data8, 0, UINT8_MAX, 0, UINT16_MAX);
memcpy(buf, &data16, 2);
return 2;
}
uint16_t measure_button(uint8_t* buf, uint16_t bufsize)
{
// Button is active LOW on most board except CPlay
@@ -142,9 +195,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;
}
@@ -158,11 +211,53 @@ uint16_t measure_humid(uint8_t* buf, uint16_t bufsize)
#else
#error "Board is not supported"
#endif
#endif // end of board
//--------------------------------------------------------------------+
// Common for all Boards
//--------------------------------------------------------------------+
// 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;
//------------- Setup -------------//
//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;
}
//--------------------------------------------------------------------+
// Codes
//--------------------------------------------------------------------+
void setup()
{
Adafruit_Sensor* accel_sensor;
@@ -178,13 +273,17 @@ 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
#ifdef ARDUINO_NRF52840_CLUE
// White LEDs for color sensing
pinMode(PIN_LED2, OUTPUT);
digitalWrite(PIN_LED2, LOW);
#endif
apds9960.begin();
apds9960.enableColor(true);
bmp280.begin();
sht30.begin(0x44);
lsm6ds33.begin_I2C();
@@ -214,6 +313,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");
@@ -249,32 +352,48 @@ void setup()
blebas.write(100);
//------------- Adafruit Service -------------//
bleLight.begin(measure_light);
bleAccel.begin(accel_sensor, 100); // TODO dropped in favor to Quaternion service for CLUE & Sense
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
strip.begin();
blePixel.begin(&strip);
bleAccel.begin(accel_sensor);
bleSound.begin(1, measure_sound, 100);
// CPB doesn't support these on-board sensor
#ifdef ARDUINO_NRF52840_CIRCUITPLAY
bleTemp.begin(measure_temperature);
bleLight.begin(measure_light, 100);
bleTemp.begin(measure_temperature, 100);
#else
bleTemp.begin(bmp280.getTemperatureSensor());
bleHumid.begin(measure_humid);
bleBaro.begin(bmp280.getPressureSensor());
bleBaro.begin(bmp280.getPressureSensor(), 100);
bleColor.begin(measure_color, 100);
bleColor.setNotifyCallback(color_enable_callback);
bleGesture.begin(measure_gesture, 10); // sampling is 10 ms
bleGesture.setPeriod(0); // notify on changes only
bleGesture.setNotifyCallback(gesture_enable_callback);
bleHumid.begin(measure_humid, 100);
bleLight.begin(measure_light, 100);;
bleLight.setNotifyCallback(light_enable_callback);
bleProximity.begin(measure_proximity, 100);
bleProximity.setNotifyCallback(proximity_enable_callback);
// Quaternion with sensor calibration
bleQuater.begin(&filter, accel_sensor, lsm6ds33.getGyroSensor(), &lis3mdl);
bleQuater.setCalibration(&cal);
bleTemp.begin(bmp280.getTemperatureSensor(), 100);
#endif
#if defined(PIN_BUZZER)
bleTone.begin(PIN_BUZZER);
#endif
// Set up and start advertising
@@ -351,6 +470,26 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
(void) conn_handle;
(void) reason;
#if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE)
apds9960.enableGesture(false);
apds9960.enableProximity(false);
apds9960.enableColor(false);
#endif
#ifdef ARDUINO_NRF52840_CLUE
digitalWrite(PIN_LED2, LOW);
#endif
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);
}
@@ -29,7 +29,15 @@
#include <bluefruit.h>
#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;
+2 -2
View File
@@ -13,7 +13,7 @@ skip_count = 0
build_format = '| {:20} | {:35} | {:9} '
build_separator = '-' * 83
default_boards = [ 'feather52832', 'feather52840', 'cplaynrf52840', 'itsybitsy52840', 'cluenrf52840' ]
default_boards = [ 'cluenrf52840', 'cplaynrf52840', 'feather52832', 'feather52840', 'feather52840sense', 'itsybitsy52840' ]
build_boards = []
@@ -52,7 +52,7 @@ def build_examples(variant):
sketchdir = os.path.dirname(sketch)
if os.path.exists(sketchdir + '/.all.test.skip') or os.path.exists(sketchdir + '/.' + variant + '.test.skip'):
success = "\033[33mskipped\033[0m "
elif glob.glob(sketchdir+"/.*.test.only") and not os.path.exists(sketchdir + '/.build.' + variant + '.test.only'):
elif glob.glob(sketchdir+"/.*.test.only") and not os.path.exists(sketchdir + '/.' + variant + '.test.only'):
success = "\033[33mskipped\033[0m "
else:
# TODO - preferably, would have STDERR show up in **both** STDOUT and STDERR.
+1
View File
@@ -42,6 +42,7 @@ extern "C"
// LEDs
#define PIN_LED1 (17)
#define PIN_LED2 (43) // dual white LEDs
#define PIN_NEOPIXEL (18)
#define LED_BUILTIN PIN_LED1
+2 -2
View File
@@ -29,7 +29,7 @@ const uint32_t g_ADigitalPinMap[] =
25, // D0 is P0.25 (UART TX)
24, // D1 is P0.24 (UART RX
10, // D2 is P0.10 (NFC2)
43, // D3 is P1.11 (LED1)
43, // D3 is P1.11
42, // D4 is P1.10 (LED2)
40, // D5 is P1.08
7, // D6 is P0.07
@@ -39,7 +39,7 @@ const uint32_t g_ADigitalPinMap[] =
27, // D10 is P0.27
6, // D11 is P0.06
8, // D12 is P0.08
41, // D13 is P1.09
41, // D13 is P1.09 (LED1)
// D14 .. D21 (aka A0 .. A7)
4, // D14 is P0.04 (A0)
+1 -1
View File
@@ -42,7 +42,7 @@ extern "C"
#define NUM_ANALOG_OUTPUTS (0)
// LEDs
#define PIN_LED1 (3)
#define PIN_LED1 (13)
#define PIN_LED2 (4)
#define PIN_NEOPIXEL (8)