From 56c0a7885a9689a1377935564f3414dfa97da1cb Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 23 Oct 2019 22:07:20 +0700 Subject: [PATCH 01/38] image upload: add support for cplay bluefruit + tft gizmo --- .../Peripheral/image_upload/image_upload.ino | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 2bb3854a..6d8a217a 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -40,19 +40,25 @@ #define TFT_CS 9 #endif +// Circuit Playground Bluefruit for use with TFT 1.5" GIZMO #ifdef ARDUINO_NRF52840_CIRCUITPLAY - #define TFT_DC A7 - #define TFT_CS A6 + #define TFT_DC 1 + #define TFT_CS 0 + #define TFT_BACKLIGHT A3 #endif #if TFT_IN_USE == TFT_35_FEATHERWING #include "Adafruit_HX8357.h" Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC); + #elif TFT_IN_USE == TFT_24_FEATHERWING #include Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); + #elif TFT_IN_USE == TFT_15_GIZMO - #error "Not supported yet" + #include "Adafruit_ST7789.h" + Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, TFT_CS, TFT_DC, -1); + #else #error "TFT display is not supported" #endif @@ -99,7 +105,17 @@ void setup() { Serial.begin(115200); +#if TFT_IN_USE == TFT_15_GIZMO + tft.init(240, 240); + tft.setRotation(2); + pinMode(TFT_BACKLIGHT, OUTPUT); + digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on + +#else tft.begin(); + +#endif + tft.fillScreen(COLOR_BLACK); tft.setTextColor(COLOR_WHITE); tft.setTextSize(1); From bbdc756444812ab6e02b18bcf58a7a7743fad0fb Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 24 Oct 2019 00:07:05 +0700 Subject: [PATCH 02/38] correct sram size for feather nrf52832 --- boards.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boards.txt b/boards.txt index f8e12abd..0f04e014 100644 --- a/boards.txt +++ b/boards.txt @@ -32,7 +32,7 @@ feather52832.upload.use_1200bps_touch=false feather52832.upload.wait_for_upload_port=false feather52832.upload.native_usb=false feather52832.upload.maximum_size=290816 -feather52832.upload.maximum_data_size=51072 +feather52832.upload.maximum_data_size=52224 # Build feather52832.build.mcu=cortex-m4 From 205587c30a4e176742a2143d10c468435badc5be Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 6 Nov 2019 10:00:59 +0700 Subject: [PATCH 03/38] ading Adafruit Temperature Service --- libraries/Bluefruit52Lib/src/BLEUuid.h | 44 +++++++---- libraries/Bluefruit52Lib/src/bluefruit.h | 2 + .../src/services/BLEAdafruitTemperature.cpp | 77 +++++++++++++++++++ .../src/services/BLEAdafruitTemperature.h | 45 +++++++++++ 4 files changed, 155 insertions(+), 13 deletions(-) create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index 9b0b7529..0d136a22 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -101,13 +101,20 @@ class BLEUuid #define UUID16_SVC_BMS 0x181E #define UUID16_SVC_CGM 0x181F #define UUID16_SVC_PLX 0x1822 +#define UUID16_SVC_OTS 0x1825 #define UUID16_SVC_EDDYSTONE 0xFEAA +// +#define UUID16_EXTERNAL_REPORT_REF_DESCR 0x2907 +#define UUID16_REPORT_REF_DESCR 0x2908 + /*------------------------------------------------------------------*/ /* Characteristic UUID * https://www.bluetooth.com/specifications/gatt/characteristics *------------------------------------------------------------------*/ +#define UUID16_CHR_TEMPERATURE_CELSIUS 0x2A1F +#define UUID16_CHR_TEMPERATURE_FAHRENHEIT 0x2A20 #define UUID16_CHR_REMOVABLE 0x2A3A #define UUID16_CHR_SERVICE_REQUIRED 0x2A3B #define UUID16_CHR_ALERT_CATEGORY_ID 0x2A43 @@ -178,25 +185,36 @@ class BLEUuid #define UUID16_CHR_SC_CTRLPT 0x2A55 #define UUID16_CHR_RSC_MEASUREMENT 0x2A53 #define UUID16_CHR_SENSOR_LOCATION 0x2A5D -#define UUID16_EXTERNAL_REPORT_REF_DESCR 0x2907 -#define UUID16_REPORT_REF_DESCR 0x2908 #define UUID16_CHR_LN_FEATURE 0x2A6A #define UUID16_CHR_LN_POSITION_QUALITY 0x2A69 #define UUID16_CHR_LN_LOCATION_AND_SPEED 0x2A67 #define UUID16_CHR_LN_NAVIGATION 0x2A68 #define UUID16_CHR_LN_CONTROL_POINT 0x2A6B -#define UUID16_BMS_CTRLPT 0x2AA4 -#define UUID16_BMS_FEATURE 0x2AA5 -#define UUID16_CGM_MEASUREMENT 0x2AA7 -#define UUID16_CGM_FEATURE 0x2AA8 -#define UUID16_CGM_STATUS 0x2AA9 -#define UUID16_CGM_SESSION_START_TIME 0x2AAA -#define UUID16_CGM_SESSION_RUN_TIME 0x2AAB -#define UUID16_CGM_SPECIFIC_OPS_CTRLPT 0x2AAC -#define UUID16_PLX_SPOT_CHECK_MEAS 0x2A5E -#define UUID16_PLX_CONTINUOUS_MEAS 0x2A5F -#define UUID16_PLX_FEATURES 0x2A60 +#define UUID16_CHR_BMS_CTRLPT 0x2AA4 +#define UUID16_CHR_BMS_FEATURE 0x2AA5 +#define UUID16_CHR_CGM_MEASUREMENT 0x2AA7 +#define UUID16_CHR_CGM_FEATURE 0x2AA8 +#define UUID16_CHR_CGM_STATUS 0x2AA9 +#define UUID16_CHR_CGM_SESSION_START_TIME 0x2AAA +#define UUID16_CHR_CGM_SESSION_RUN_TIME 0x2AAB +#define UUID16_CHR_CGM_SPECIFIC_OPS_CTRLPT 0x2AAC +#define UUID16_CHR_PLX_SPOT_CHECK_MEAS 0x2A5E +#define UUID16_CHR_PLX_CONTINUOUS_MEAS 0x2A5F +#define UUID16_CHR_PLX_FEATURES 0x2A60 +#define UUID16_CHR_TEMPERATURE 0x2A6E #define UUID16_CHR_UV_INDEX 0x2A76 +#define UUID16_CHR_OTS_FEATURES 0x2ABD +#define UUID16_CHR_OTS_OBJECT_NAME 0x2ABE +#define UUID16_CHR_OTS_OBJECT_TYPE 0x2ABF +#define UUID16_CHR_OTS_OBJECT_SIZE 0x2AC0 +#define UUID16_CHR_OTS_OBJECT_FIRST_CREATED 0x2AC1 +#define UUID16_CHR_OTS_OBJECT_LAST_MODIFIED 0x2AC2 +#define UUID16_CHR_OTS_OBJECT_ID 0x2AC3 +#define UUID16_CHR_OTS_OBJECT_PROPERTIES 0x2AC4 +#define UUID16_CHR_OTS_OACP 0x2AC5 +#define UUID16_CHR_OTS_OLCP 0x2AC6 +#define UUID16_CHR_OTS_LF 0x2AC7 +#define UUID16_CHR_OTS_OBJECT_CHANGED 0x2AC8 /*------------------------------------------------------------------*/ /* Company UUID diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index ffe5280d..3eedc753 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -60,6 +60,8 @@ #include "services/BLEMidi.h" #include "services/EddyStone.h" +#include "services/BLEAdafruitTemperature.h" + #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" #include "clients/BLEClientDis.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp new file mode 100644 index 00000000..7465d5df --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp @@ -0,0 +1,77 @@ +/* + * 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 "bluefruit.h" + +#include "BLEAdafruitTemperature.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +/* Adafruit Temperature Service + * - Service: 20E6-0001-C332-42A8-93BD-25E905756CB8 + * - Temperature Celsius : 0x2A6E + * - Measurement Interval : 0x2A21 + * + * https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.temperature.xml + * https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.measurement_interval.xml + */ + +const uint8_t BLEAdafruitTemperature::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xE6, 0x20 +}; + +// Constructor +BLEAdafruitTemperature::BLEAdafruitTemperature(void) + : BLEService(UUID128_SERVICE), Temperature(UUID16_CHR_TEMPERATURE), MeasurementInterval(UUID16_CHR_MEASUREMENT_INTERVAL) +{ + +} + +err_t BLEAdafruitTemperature::begin (void) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Add Temperature Characteristic + Temperature.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + Temperature.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + Temperature.setFixedLen(2); + Temperature.setUserDescriptor("Temperature"); + VERIFY_STATUS( Temperature.begin() ); + + // Add Measurement Interval Characteristic + MeasurementInterval.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + MeasurementInterval.setPermission(SECMODE_OPEN, SECMODE_OPEN); + MeasurementInterval.setFixedLen(2); + MeasurementInterval.setUserDescriptor("Measurement Interval"); + VERIFY_STATUS( MeasurementInterval.begin() ); + + MeasurementInterval.write16(30); // measure every 30 seconds + + return ERROR_NONE; +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h new file mode 100644 index 00000000..e80e634c --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h @@ -0,0 +1,45 @@ +/* + * 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 BLEADAFRUITTEMPERATURE_H_ +#define BLEADAFRUITTEMPERATURE_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitTemperature : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + + BLECharacteristic Temperature; + BLECharacteristic MeasurementInterval; + + BLEAdafruitTemperature(); + virtual err_t begin(void); +}; + +#endif /* BLEADAFRUITTEMPERATURE_H_ */ From bf4ac6ba718a29eae0b798e51e8df824d7c9b3d2 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 6 Nov 2019 23:10:38 +0700 Subject: [PATCH 04/38] adding Adafruit Neo Pixel service --- libraries/Bluefruit52Lib/src/bluefruit.h | 1 + .../src/services/BLEAdafruitNeopixel.cpp | 100 ++++++++++++++++++ .../src/services/BLEAdafruitNeopixel.h | 51 +++++++++ .../src/services/BLEAdafruitTemperature.cpp | 10 +- 4 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 3eedc753..53393952 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -61,6 +61,7 @@ #include "services/EddyStone.h" #include "services/BLEAdafruitTemperature.h" +#include "services/BLEAdafruitNeopixel.h" #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp new file mode 100644 index 00000000..4fce213e --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp @@ -0,0 +1,100 @@ +/* + * 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 "bluefruit.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + + +//------------- IMPLEMENTATION -------------// +/* Adafruit NeoPixel Service + * - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8 + * - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8 + * - Type : ADAF-0004-C332-42A8-93BD-25E905756CB8 + * - Data : ADAF-0005-C332-42A8-93BD-25E905756CB8 + */ + +const uint8_t BLEAdafruitNeopixel::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x00, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitNeopixel::UUID128_CHR_COUNT[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x03, 0x00, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitNeopixel::UUID128_CHR_TYPE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x04, 0x00, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitNeopixel::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x05, 0x00, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitNeopixel::BLEAdafruitNeopixel(void) + : BLEService(UUID128_SERVICE), Count(UUID128_CHR_COUNT), Type(UUID128_CHR_TYPE), Data(UUID128_CHR_DATA) +{ + +} + +err_t BLEAdafruitNeopixel::begin (void) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Add Characteristic + Count.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Count.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Count.setFixedLen(2); + Count.setUserDescriptor("Count"); + VERIFY_STATUS( Count.begin() ); + Count.write16(0); + + // Add Characteristic + Type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Type.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Type.setFixedLen(2); + Type.setUserDescriptor("Type"); + VERIFY_STATUS( Type.begin() ); + Type.write16(0); + + // Add Characteristic + Data.setProperties(CHR_PROPS_WRITE); + Data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); + Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH)); + Data.setUserDescriptor("Data"); + VERIFY_STATUS( Data.begin() ); + + return ERROR_NONE; +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h new file mode 100644 index 00000000..7e2ddce4 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h @@ -0,0 +1,51 @@ +/* + * 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 BLEADAFRUITNEOPIXEL_H_ +#define BLEADAFRUITNEOPIXEL_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitNeopixel : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_COUNT[16]; + static const uint8_t UUID128_CHR_TYPE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLECharacteristic Count; + BLECharacteristic Type; + BLECharacteristic Data; + + BLEAdafruitNeopixel(void); + virtual err_t begin(void); +}; + + + +#endif /* BLEADAFRUITNEOPIXEL_H_ */ diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp index 7465d5df..a8e423a8 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp @@ -24,16 +24,14 @@ #include "bluefruit.h" -#include "BLEAdafruitTemperature.h" - //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ /* Adafruit Temperature Service - * - Service: 20E6-0001-C332-42A8-93BD-25E905756CB8 - * - Temperature Celsius : 0x2A6E - * - Measurement Interval : 0x2A21 + * - Service: ADAF-0001-C332-42A8-93BD-25E905756CB8 + * - Temperature Celsius : 0x2A6E + * - Measurement Interval : 0x2A21 * * https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.temperature.xml * https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.measurement_interval.xml @@ -42,7 +40,7 @@ const uint8_t BLEAdafruitTemperature::UUID128_SERVICE[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, - 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xE6, 0x20 + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xAF, 0xAD }; // Constructor From ca4bbc5bfea19961c9aab17a24dafa2345d0ae06 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 7 Nov 2019 01:30:18 +0700 Subject: [PATCH 05/38] adding Accel service --- .../cplay_bluefruit/cplay_bluefruit.ino | 171 ++++++++++++++++++ libraries/Bluefruit52Lib/src/BLEGatt.cpp | 2 +- libraries/Bluefruit52Lib/src/bluefruit.cpp | 5 +- libraries/Bluefruit52Lib/src/bluefruit.h | 1 + .../src/services/BLEAdafruitAccel.cpp | 86 +++++++++ .../src/services/BLEAdafruitAccel.h | 47 +++++ .../src/services/BLEAdafruitNeopixel.cpp | 7 +- 7 files changed, 315 insertions(+), 4 deletions(-) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino new file mode 100644 index 00000000..d071207c --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino @@ -0,0 +1,171 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#include +#include +#include +#include + +// BLE Service +BLEDfu bledfu; // OTA DFU service +BLEDis bledis; // device information +BLEUart bleuart; // uart over ble +BLEBas blebas; // battery + +// Base UUID : ADAF0000-C332-42A8-93BD-25E905756CB8 + +/* CPB Temperature + * - Service: ADAF-0001-C332-42A8-93BD-25E905756CB8 + * - Temperature Celsius : 0x2A6E + * - Measurement Interval : 0x2A21 + */ +BLEAdafruitTemperature bleTemp; + +/* Adafruit NeoPixel Service + * - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8 + * - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8 + * - Type : ADAF-0004-C332-42A8-93BD-25E905756CB8 + * - Data : ADAF-0005-C332-42A8-93BD-25E905756CB8 + */ +BLEAdafruitNeopixel bleNeopixel; + +/* Adafruit Accelerometer Service + * using micro:bit Accelerometer Service definition + * https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html + * + * - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8 + * - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8 + * - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8 + */ +BLEAdafruitAccel bleAccel; + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLEUART Example"); + Serial.println("---------------------------\n"); + + CircuitPlayground.begin(); + + // Setup the BLE LED to be enabled on CONNECT + // Note: This is actually the default behaviour, but provided + // here in case you want to control this LED manually via PIN 19 + Bluefruit.autoConnLed(true); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.setName("Bluefruit52"); + //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + + // To be consistent OTA DFU should be added first if it exists + bledfu.begin(); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and Start BLE Uart Service + bleuart.begin(); + + // Start BLE Battery Service + blebas.begin(); + blebas.write(100); + + // Adafruit service + bleTemp.begin(); + bleNeopixel.begin(); + bleAccel.begin(); + + + // Set up and start advertising + startAdv(); + + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); + Serial.println("Once connected, enter character(s) that you wish to send"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + if ( !Bluefruit.connected() ) return; + if ( !bleTemp.Temperature.notifyEnabled() ) return; + + int16_t temp = (int16_t) (100*CircuitPlayground.temperature()); + bleTemp.Temperature.notify16((uint16_t) temp); + + delay(1000*bleTemp.MeasurementInterval.read16()); +} + +// callback invoked when central connects +void connect_callback(uint16_t conn_handle) +{ + // Get the reference to current connection + BLEConnection* connection = Bluefruit.Connection(conn_handle); + + char central_name[32] = { 0 }; + connection->getPeerName(central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println(); + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); +} diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index fa7223d9..55101f3b 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -232,7 +232,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) *------------------------------------------------------------------*/ bool BLEGatt::_addCharacteristic(BLECharacteristic* chr) { - if ( _server.chr_count == CFG_GATT_MAX_SERVER_CHARS ) return false; + VERIFY( _server.chr_count < CFG_GATT_MAX_SERVER_CHARS ); _server.chr_list[ _server.chr_count++ ] = chr; return true; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index c3c6c603..9bb65d8e 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -122,7 +122,10 @@ static void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info) LOG_LV1("SD Err", "assert at %s : %d", assert_info->p_file_name, assert_info->line_num); } - while(1) { } + while(1) + { + yield(); + } #endif } diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 53393952..c54a04dd 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -62,6 +62,7 @@ #include "services/BLEAdafruitTemperature.h" #include "services/BLEAdafruitNeopixel.h" +#include "services/BLEAdafruitAccel.h" #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp new file mode 100644 index 00000000..3165c1d1 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp @@ -0,0 +1,86 @@ +/* + * 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 "bluefruit.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + + +/* Adafruit Accelerometer Service + * using micro:bit Accelerometer Service definition + * https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html + * + * - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8 + * - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8 + * - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8 + */ +const uint8_t BLEAdafruitAccel::UUID128_SERVICE[16] = +{ + 0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0, + 0x0A, 0x47, 0x1D, 0x25, 0x53, 0x07, 0x5D, 0xE9 +}; + +const uint8_t BLEAdafruitAccel::UUID128_CHR_DATA[16] = +{ + 0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0, + 0x0A, 0x47, 0x1D, 0x25, 0x4B, 0xCA, 0x5D, 0xE9 +}; + +const uint8_t BLEAdafruitAccel::UUID128_CHR_PERIOD[16] = +{ + 0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0, + 0x0A, 0x47, 0x1D, 0x25, 0x24, 0xFB, 0x5D, 0xE9 +}; + +// Constructor +BLEAdafruitAccel::BLEAdafruitAccel(void) + : BLEService(UUID128_SERVICE), Data(UUID128_CHR_DATA), Period(UUID128_CHR_PERIOD) +{ + +} + +err_t BLEAdafruitAccel::begin (void) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Add Characteristic + Data.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + Data.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + Data.setFixedLen(6); + Data.setUserDescriptor("Data"); + VERIFY_STATUS( Data.begin() ); + + // Add Characteristic + Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Period.setFixedLen(2); + Period.setUserDescriptor("Period"); + VERIFY_STATUS( Period.begin() ); + Period.write16(10); + + return ERROR_NONE; +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h new file mode 100644 index 00000000..8418af89 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h @@ -0,0 +1,47 @@ +/* + * 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 BLEADAFRUITACCEL_H_ +#define BLEADAFRUITACCEL_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitAccel : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + static const uint8_t UUID128_CHR_PERIOD[16]; + + BLECharacteristic Data; + BLECharacteristic Period; + + BLEAdafruitAccel(void); + virtual err_t begin(void); +}; + +#endif /* BLEADAFRUITACCEL_H_ */ diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp index 4fce213e..239bd0e9 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp @@ -29,7 +29,7 @@ //--------------------------------------------------------------------+ -//------------- IMPLEMENTATION -------------// + /* Adafruit NeoPixel Service * - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8 * - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8 @@ -92,7 +92,10 @@ err_t BLEAdafruitNeopixel::begin (void) // Add Characteristic Data.setProperties(CHR_PROPS_WRITE); Data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); - Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH)); + // Change to use VLOC STACK to USER due to lack of memroy + // Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH)); + Data.setMaxLen(20); + Data.setUserDescriptor("Data"); VERIFY_STATUS( Data.begin() ); From f0ca568d36cffbcfd8c7e0d7ba9e73ed4735406c Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 8 Nov 2019 00:18:41 +0700 Subject: [PATCH 06/38] update service specs --- .../cplay_bluefruit/cplay_bluefruit.ino | 67 +++++++++++++++---- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino index d071207c..412f6cdd 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino @@ -22,22 +22,60 @@ BLEDis bledis; // device information BLEUart bleuart; // uart over ble BLEBas blebas; // battery -// Base UUID : ADAF0000-C332-42A8-93BD-25E905756CB8 - -/* CPB Temperature - * - Service: ADAF-0001-C332-42A8-93BD-25E905756CB8 - * - Temperature Celsius : 0x2A6E - * - Measurement Interval : 0x2A21 +/* All Adafruit Service/Characteristic UUID128 share the same + * Base UUID : ADAF0000-C332-42A8-93BD-25E905756CB8 + * + * Temperature service 0100 + * - Temperature 0101 | float | Read + Notify | degree in Celsius + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Accelerometer service 0200 + * - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Light sensor service 0300 + * - Light Data 0301 | uint16_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Sound sensor service 0400 + * - Sound Data 0401 | int16_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Captouch service 0500 + * - Sensitivity 0501 | uint32_t | difference that Capn should report immediately + * - Cap0 0510 | int32_t | Read + Notify + * - ......................... + * - Cap7 0517 | int32_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Button service 0600 + * - Button Data 0601 | uint16_t | Read + Notify | e.g (Slide sw, Left, Right) + * - Measurement Period 0001 | uint32_t | Read + Write + * + * PIN I/O service 0700 + * - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out + * - Pin Data 0702 | uint64_t | Read + Write | + * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit value is 0-1024 for duty cycle + * + * PIN Analog 0800 + * - A0 0810 | uint16_t | Read + Notify + * ......................... + * - A7 0817 | uint16_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Neopixel Service 0900 + * - Pixel Pin 0901 | uint8_t | Read + Write + * - Pixel Count 0902 | uint16_t | Read + Write + * - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc .. + * - Pixel Data 0904 | RGB array| Write */ -BLEAdafruitTemperature bleTemp; /* Adafruit NeoPixel Service - * - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8 - * - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8 - * - Type : ADAF-0004-C332-42A8-93BD-25E905756CB8 - * - Data : ADAF-0005-C332-42A8-93BD-25E905756CB8 + * - Service: ADAF0002-C332-42A8-93BD-25E905756CB8 + * - Count : ADAF0003-C332-42A8-93BD-25E905756CB8 + * - Type : ADAF0004-C332-42A8-93BD-25E905756CB8 + * - Data : ADAF0005-C332-42A8-93BD-25E905756CB8 */ -BLEAdafruitNeopixel bleNeopixel; /* Adafruit Accelerometer Service * using micro:bit Accelerometer Service definition @@ -47,7 +85,12 @@ BLEAdafruitNeopixel bleNeopixel; * - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8 * - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8 */ + +BLEAdafruitTemperature bleTemp; +BLEAdafruitNeopixel bleNeopixel; BLEAdafruitAccel bleAccel; + + void setup() { From 5afcadb3681e10297a52b6e2347249c30d0b45f5 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 12 Nov 2019 22:49:59 +0700 Subject: [PATCH 07/38] update adafruit services --- .../cplay_bluefruit/cplay_bluefruit.ino | 28 ++++++++--- libraries/Bluefruit52Lib/src/BLEUuid.cpp | 12 ++--- libraries/Bluefruit52Lib/src/BLEUuid.h | 5 ++ libraries/Bluefruit52Lib/src/bluefruit.h | 1 + .../src/services/BLEAdafruitAccel.cpp | 46 +++++++++---------- .../src/services/BLEAdafruitAccel.h | 3 +- .../src/services/BLEAdafruitNeopixel.cpp | 4 -- .../src/services/BLEAdafruitTemperature.cpp | 37 +++++++++------ .../src/services/BLEAdafruitTemperature.h | 3 +- 9 files changed, 78 insertions(+), 61 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino index 412f6cdd..4f59f280 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino @@ -23,23 +23,26 @@ BLEUart bleuart; // uart over ble BLEBas blebas; // battery /* All Adafruit Service/Characteristic UUID128 share the same - * Base UUID : ADAF0000-C332-42A8-93BD-25E905756CB8 + * Base UUID : ADAFxxx-C332-42A8-93BD-25E905756CB8 + * + * Share Characteristics + * - Measurement Period 0001 | int32_t | Read + Write | ms between measurements, -1: stop reading, 0: update when changes * * Temperature service 0100 * - Temperature 0101 | float | Read + Notify | degree in Celsius - * - Measurement Period 0001 | uint32_t | Read + Write + * - Measurement Period 0001 * * Accelerometer service 0200 * - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z - * - Measurement Period 0001 | uint32_t | Read + Write + * - Measurement Period 0001 * * Light sensor service 0300 - * - Light Data 0301 | uint16_t | Read + Notify - * - Measurement Period 0001 | uint32_t | Read + Write + * - Lux Data 0301 | uint16_t | Read + Notify + * - Measurement Period 0001 * * Sound sensor service 0400 * - Sound Data 0401 | int16_t | Read + Notify - * - Measurement Period 0001 | uint32_t | Read + Write + * - Measurement Period 0001 * * Captouch service 0500 * - Sensitivity 0501 | uint32_t | difference that Capn should report immediately @@ -55,7 +58,7 @@ BLEBas blebas; // battery * PIN I/O service 0700 * - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out * - Pin Data 0702 | uint64_t | Read + Write | - * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit value is 0-1024 for duty cycle + * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit, value is 0-1024 for duty cycle * * PIN Analog 0800 * - A0 0810 | uint16_t | Read + Notify @@ -68,6 +71,17 @@ BLEBas blebas; // battery * - Pixel Count 0902 | uint16_t | Read + Write * - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc .. * - Pixel Data 0904 | RGB array| Write + * + * Gamepad Service 0A00 + * - Gamepad Button 0A01 | uint16_t | Write + * - Left XY 0A02 | int8_t[2] | Write + * - Right XY 0A03 | int8_t[2] | Write + * + * Image Service 0B00 + * - Format 0B01 | uint16_t[3] | Write | width + height + type, type is enum value e.g RGB_655, RGB_888, JPG, GIF etc ... + * - Data 0B02 | pixel[] | Write | will save to flash as `default_image`, and auto load on reset ? + * - Fill 0B03 | uint8_t[3] | Write | Fill with solid color + * - Load File 0B04 | utf8_t | Write | Load image from qspi flash ? */ /* Adafruit NeoPixel Service diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.cpp b/libraries/Bluefruit52Lib/src/BLEUuid.cpp index 6922afce..b7f360a1 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.cpp +++ b/libraries/Bluefruit52Lib/src/BLEUuid.cpp @@ -36,13 +36,11 @@ #include "BLEUuid.h" -//void reverse_uuid128(uint8_t const in[16], uint8_t out[16]) -//{ -// for(uint8_t i=0; i<16; i++) -// { -// out[i] = in[15-i]; -// } -//} +const uint8_t UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xAF, 0xAD +}; void BLEUuid::set(uint16_t uuid16) { diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index 0d136a22..3c903098 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -72,6 +72,11 @@ class BLEUuid BLEUuid& operator=(ble_uuid_t uuid); }; +//--------------------------------------------------------------------+ +// Custom UUID Service +//--------------------------------------------------------------------+ +extern const uint8_t UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD[16]; + /*------------------------------------------------------------------*/ /* Service UUID * https://www.bluetooth.com/specifications/gatt/services diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index c54a04dd..ed3a5244 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -63,6 +63,7 @@ #include "services/BLEAdafruitTemperature.h" #include "services/BLEAdafruitNeopixel.h" #include "services/BLEAdafruitAccel.h" +#include "services/BLEAdafruitLightSensor.h" #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp index 3165c1d1..d8427c31 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp @@ -29,35 +29,33 @@ //--------------------------------------------------------------------+ -/* Adafruit Accelerometer Service - * using micro:bit Accelerometer Service definition - * https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html +/* All Adafruit Service/Characteristic UUID128 share the same Base UUID: + * ADAFxxx-C332-42A8-93BD-25E905756CB8 * - * - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8 - * - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8 - * - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8 + * Shared Characteristics + * - Measurement Period 0001 | int32_t | Read + Write | + * ms between measurements, -1: stop reading, 0: update when changes + * + * Accelerometer service 0200 + * - Accel 0201 | float[3] | Read + Notify | x, y, z + * - Measurement Period 0001 */ + const uint8_t BLEAdafruitAccel::UUID128_SERVICE[16] = { - 0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0, - 0x0A, 0x47, 0x1D, 0x25, 0x53, 0x07, 0x5D, 0xE9 + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x02, 0xAF, 0xAD }; const uint8_t BLEAdafruitAccel::UUID128_CHR_DATA[16] = { - 0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0, - 0x0A, 0x47, 0x1D, 0x25, 0x4B, 0xCA, 0x5D, 0xE9 -}; - -const uint8_t BLEAdafruitAccel::UUID128_CHR_PERIOD[16] = -{ - 0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0, - 0x0A, 0x47, 0x1D, 0x25, 0x24, 0xFB, 0x5D, 0xE9 + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x02, 0xAF, 0xAD }; // Constructor BLEAdafruitAccel::BLEAdafruitAccel(void) - : BLEService(UUID128_SERVICE), Data(UUID128_CHR_DATA), Period(UUID128_CHR_PERIOD) + : BLEService(UUID128_SERVICE), Accel(UUID128_CHR_DATA), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) { } @@ -68,19 +66,17 @@ err_t BLEAdafruitAccel::begin (void) VERIFY_STATUS( BLEService::begin() ); // Add Characteristic - Data.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); - Data.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - Data.setFixedLen(6); - Data.setUserDescriptor("Data"); - VERIFY_STATUS( Data.begin() ); + Accel.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + Accel.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + Accel.setFixedLen(4*3); + VERIFY_STATUS( Accel.begin() ); // Add Characteristic Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Period.setFixedLen(2); - Period.setUserDescriptor("Period"); + Period.setFixedLen(4); VERIFY_STATUS( Period.begin() ); - Period.write16(10); + Period.write32(10); return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h index 8418af89..496e16ee 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h @@ -35,9 +35,8 @@ class BLEAdafruitAccel : public BLEService public: static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; - static const uint8_t UUID128_CHR_PERIOD[16]; - BLECharacteristic Data; + BLECharacteristic Accel; BLECharacteristic Period; BLEAdafruitAccel(void); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp index 239bd0e9..53b05899 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp @@ -77,7 +77,6 @@ err_t BLEAdafruitNeopixel::begin (void) Count.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); Count.setPermission(SECMODE_OPEN, SECMODE_OPEN); Count.setFixedLen(2); - Count.setUserDescriptor("Count"); VERIFY_STATUS( Count.begin() ); Count.write16(0); @@ -85,7 +84,6 @@ err_t BLEAdafruitNeopixel::begin (void) Type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); Type.setPermission(SECMODE_OPEN, SECMODE_OPEN); Type.setFixedLen(2); - Type.setUserDescriptor("Type"); VERIFY_STATUS( Type.begin() ); Type.write16(0); @@ -95,8 +93,6 @@ err_t BLEAdafruitNeopixel::begin (void) // Change to use VLOC STACK to USER due to lack of memroy // Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH)); Data.setMaxLen(20); - - Data.setUserDescriptor("Data"); VERIFY_STATUS( Data.begin() ); return ERROR_NONE; diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp index a8e423a8..812dfd67 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp @@ -28,24 +28,33 @@ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ -/* Adafruit Temperature Service - * - Service: ADAF-0001-C332-42A8-93BD-25E905756CB8 - * - Temperature Celsius : 0x2A6E - * - Measurement Interval : 0x2A21 +/* All Adafruit Service/Characteristic UUID128 share the same Base UUID: + * ADAFxxx-C332-42A8-93BD-25E905756CB8 * - * https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.temperature.xml - * https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.measurement_interval.xml + * Shared Characteristics + * - Measurement Period 0001 | int32_t | Read + Write | + * ms between measurements, -1: stop reading, 0: update when changes + * + * Temperature service 0100 + * - Temperature 0101 | float | Read + Notify | degree in Celsius + * - Measurement Period 0001 */ const uint8_t BLEAdafruitTemperature::UUID128_SERVICE[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, - 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xAF, 0xAD + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x01, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitTemperature::UUID128_CHR_TEMPERATURE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x01, 0xAF, 0xAD }; // Constructor BLEAdafruitTemperature::BLEAdafruitTemperature(void) - : BLEService(UUID128_SERVICE), Temperature(UUID16_CHR_TEMPERATURE), MeasurementInterval(UUID16_CHR_MEASUREMENT_INTERVAL) + : BLEService(UUID128_SERVICE), Temperature(UUID128_CHR_TEMPERATURE), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) { } @@ -59,17 +68,15 @@ err_t BLEAdafruitTemperature::begin (void) Temperature.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); Temperature.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); Temperature.setFixedLen(2); - Temperature.setUserDescriptor("Temperature"); VERIFY_STATUS( Temperature.begin() ); // Add Measurement Interval Characteristic - MeasurementInterval.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - MeasurementInterval.setPermission(SECMODE_OPEN, SECMODE_OPEN); - MeasurementInterval.setFixedLen(2); - MeasurementInterval.setUserDescriptor("Measurement Interval"); - VERIFY_STATUS( MeasurementInterval.begin() ); + Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Period.setFixedLen(4); + VERIFY_STATUS( Period.begin() ); - MeasurementInterval.write16(30); // measure every 30 seconds + Period.write32(30); // measure every 30 seconds return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h index e80e634c..85323785 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h @@ -34,9 +34,10 @@ class BLEAdafruitTemperature : public BLEService { public: static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_TEMPERATURE[16]; BLECharacteristic Temperature; - BLECharacteristic MeasurementInterval; + BLECharacteristic Period; BLEAdafruitTemperature(); virtual err_t begin(void); From 335cde068e9aa422e7c1fb8be032614d6abafcf9 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 12 Nov 2019 23:06:52 +0700 Subject: [PATCH 08/38] add button service --- libraries/Bluefruit52Lib/src/bluefruit.h | 1 + .../src/services/BLEAdafruitButton.cpp | 82 +++++++++++++++++++ .../src/services/BLEAdafruitButton.h | 46 +++++++++++ .../src/services/BLEAdafruitLightSensor.cpp | 82 +++++++++++++++++++ .../src/services/BLEAdafruitLightSensor.h | 46 +++++++++++ .../src/services/BLEAdafruitTemperature.cpp | 2 +- 6 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index ed3a5244..61778bd7 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -64,6 +64,7 @@ #include "services/BLEAdafruitNeopixel.h" #include "services/BLEAdafruitAccel.h" #include "services/BLEAdafruitLightSensor.h" +#include "services/BLEAdafruitButton.h" #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp new file mode 100644 index 00000000..41702f9c --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp @@ -0,0 +1,82 @@ +/* + * 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 "bluefruit.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 + * + * Board Button service 0600 + * - Button 0601 | uint32_t | Read + Notify | e.g slide (b0), button A (b1), button B (b2) + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitButton::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x06, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitButton::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x06, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitButton::BLEAdafruitButton(void) + : BLEService(UUID128_SERVICE), Button(UUID128_CHR_DATA), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) +{ + +} + +err_t BLEAdafruitButton::begin (void) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Add Characteristic + Button.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + Button.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + Button.setFixedLen(4); + VERIFY_STATUS( Button.begin() ); + + // Add Characteristic + Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Period.setFixedLen(4); + VERIFY_STATUS( Period.begin() ); + Period.write32(10); + + return ERROR_NONE; +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h new file mode 100644 index 00000000..b6e9f742 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h @@ -0,0 +1,46 @@ +/* + * 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 BLE_ADAFRUIT_BUTTON_H_ +#define BLE_ADAFRUIT_BUTTON_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitButton : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLECharacteristic Button; + BLECharacteristic Period; + + BLEAdafruitButton(void); + virtual err_t begin(void); +}; + +#endif /* BLE_ADAFRUIT_BUTTON_H_ */ diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp new file mode 100644 index 00000000..1d21d7ba --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp @@ -0,0 +1,82 @@ +/* + * 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 "bluefruit.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 + * + * Light Sensor service 0300 + * - Lux 0301 | float | Read + Notify | + * - Measurement Period 0001 + */ + +const uint8_t BLEAdafruitLightSensor::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x03, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitLightSensor::UUID128_CHR_DATA[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x03, 0xAF, 0xAD +}; + +// Constructor +BLEAdafruitLightSensor::BLEAdafruitLightSensor(void) + : BLEService(UUID128_SERVICE), Lux(UUID128_CHR_DATA), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) +{ + +} + +err_t BLEAdafruitLightSensor::begin (void) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Add Characteristic + Lux.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + Lux.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + Lux.setFixedLen(4); + VERIFY_STATUS( Lux.begin() ); + + // Add Characteristic + Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Period.setFixedLen(4); + VERIFY_STATUS( Period.begin() ); + Period.write32(10); + + return ERROR_NONE; +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h new file mode 100644 index 00000000..c184113b --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h @@ -0,0 +1,46 @@ +/* + * 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 BLE_ADAFRUIT_LIGHT_SENSOR_H_ +#define BLE_ADAFRUIT_LIGHT_SENSOR_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitLightSensor : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_DATA[16]; + + BLECharacteristic Lux; + BLECharacteristic Period; + + BLEAdafruitLightSensor(void); + virtual err_t begin(void); +}; + +#endif /* BLE_ADAFRUIT_LIGHT_SENSOR_H_ */ diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp index 812dfd67..a3f73c43 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp @@ -67,7 +67,7 @@ err_t BLEAdafruitTemperature::begin (void) // Add Temperature Characteristic Temperature.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); Temperature.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - Temperature.setFixedLen(2); + Temperature.setFixedLen(4); VERIFY_STATUS( Temperature.begin() ); // Add Measurement Interval Characteristic From 9e75e65c5af53f0c1b8d11dd43a63d4f12520978 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Nov 2019 01:00:35 +0700 Subject: [PATCH 09/38] increase timer callback task for more usage --- cores/nRF5/freertos/config/FreeRTOSConfig.h | 2 +- libraries/Bluefruit52Lib/src/BLEUuid.cpp | 6 ++++++ libraries/Bluefruit52Lib/src/BLEUuid.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cores/nRF5/freertos/config/FreeRTOSConfig.h b/cores/nRF5/freertos/config/FreeRTOSConfig.h index c080ec2d..7b03755a 100644 --- a/cores/nRF5/freertos/config/FreeRTOSConfig.h +++ b/cores/nRF5/freertos/config/FreeRTOSConfig.h @@ -91,7 +91,7 @@ #define configUSE_TIMERS 1 #define configTIMER_TASK_PRIORITY ( 2 ) // Normal #define configTIMER_QUEUE_LENGTH 32 -#define configTIMER_TASK_STACK_DEPTH ( 100 ) +#define configTIMER_TASK_STACK_DEPTH ( 256 ) /* Tickless Idle configuration. */ #define configEXPECTED_IDLE_TIME_BEFORE_SLEEP 2 diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.cpp b/libraries/Bluefruit52Lib/src/BLEUuid.cpp index b7f360a1..018bdd6e 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.cpp +++ b/libraries/Bluefruit52Lib/src/BLEUuid.cpp @@ -42,6 +42,12 @@ const uint8_t UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD[16] = 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x00, 0xAF, 0xAD }; +const uint8_t UUID128_CHR_ADAFRUIT_VERSION[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x00, 0xAF, 0xAD +}; + void BLEUuid::set(uint16_t uuid16) { _uuid.type = BLE_UUID_TYPE_BLE; diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index 3c903098..e7138b22 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -76,6 +76,7 @@ class BLEUuid // Custom UUID Service //--------------------------------------------------------------------+ extern const uint8_t UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD[16]; +extern const uint8_t UUID128_CHR_ADAFRUIT_VERSION[16]; /*------------------------------------------------------------------*/ /* Service UUID From ef11cdb391b857ef6a7ee48dc55027bc3aa26d06 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Nov 2019 01:01:24 +0700 Subject: [PATCH 10/38] remove cplay_bluefruit, move it to CPlay example repo --- .../cplay_bluefruit/cplay_bluefruit.ino | 228 ------------------ 1 file changed, 228 deletions(-) delete mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino deleted file mode 100644 index 4f59f280..00000000 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_bluefruit/cplay_bluefruit.ino +++ /dev/null @@ -1,228 +0,0 @@ -/********************************************************************* - This is an example for our nRF52 based Bluefruit LE modules - - Pick one up today in the adafruit shop! - - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing - products from Adafruit! - - MIT license, check LICENSE for more information - All text above, and the splash screen below must be included in - any redistribution -*********************************************************************/ -#include -#include -#include -#include - -// BLE Service -BLEDfu bledfu; // OTA DFU service -BLEDis bledis; // device information -BLEUart bleuart; // uart over ble -BLEBas blebas; // battery - -/* All Adafruit Service/Characteristic UUID128 share the same - * Base UUID : ADAFxxx-C332-42A8-93BD-25E905756CB8 - * - * Share Characteristics - * - Measurement Period 0001 | int32_t | Read + Write | ms between measurements, -1: stop reading, 0: update when changes - * - * Temperature service 0100 - * - Temperature 0101 | float | Read + Notify | degree in Celsius - * - Measurement Period 0001 - * - * Accelerometer service 0200 - * - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z - * - Measurement Period 0001 - * - * Light sensor service 0300 - * - Lux Data 0301 | uint16_t | Read + Notify - * - Measurement Period 0001 - * - * Sound sensor service 0400 - * - Sound Data 0401 | int16_t | Read + Notify - * - Measurement Period 0001 - * - * Captouch service 0500 - * - Sensitivity 0501 | uint32_t | difference that Capn should report immediately - * - Cap0 0510 | int32_t | Read + Notify - * - ......................... - * - Cap7 0517 | int32_t | Read + Notify - * - Measurement Period 0001 | uint32_t | Read + Write - * - * Button service 0600 - * - Button Data 0601 | uint16_t | Read + Notify | e.g (Slide sw, Left, Right) - * - Measurement Period 0001 | uint32_t | Read + Write - * - * PIN I/O service 0700 - * - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out - * - Pin Data 0702 | uint64_t | Read + Write | - * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit, value is 0-1024 for duty cycle - * - * PIN Analog 0800 - * - A0 0810 | uint16_t | Read + Notify - * ......................... - * - A7 0817 | uint16_t | Read + Notify - * - Measurement Period 0001 | uint32_t | Read + Write - * - * Neopixel Service 0900 - * - Pixel Pin 0901 | uint8_t | Read + Write - * - Pixel Count 0902 | uint16_t | Read + Write - * - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc .. - * - Pixel Data 0904 | RGB array| Write - * - * Gamepad Service 0A00 - * - Gamepad Button 0A01 | uint16_t | Write - * - Left XY 0A02 | int8_t[2] | Write - * - Right XY 0A03 | int8_t[2] | Write - * - * Image Service 0B00 - * - Format 0B01 | uint16_t[3] | Write | width + height + type, type is enum value e.g RGB_655, RGB_888, JPG, GIF etc ... - * - Data 0B02 | pixel[] | Write | will save to flash as `default_image`, and auto load on reset ? - * - Fill 0B03 | uint8_t[3] | Write | Fill with solid color - * - Load File 0B04 | utf8_t | Write | Load image from qspi flash ? - */ - -/* Adafruit NeoPixel Service - * - Service: ADAF0002-C332-42A8-93BD-25E905756CB8 - * - Count : ADAF0003-C332-42A8-93BD-25E905756CB8 - * - Type : ADAF0004-C332-42A8-93BD-25E905756CB8 - * - Data : ADAF0005-C332-42A8-93BD-25E905756CB8 - */ - -/* Adafruit Accelerometer Service - * using micro:bit Accelerometer Service definition - * https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html - * - * - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8 - * - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8 - * - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8 - */ - -BLEAdafruitTemperature bleTemp; -BLEAdafruitNeopixel bleNeopixel; -BLEAdafruitAccel bleAccel; - - - -void setup() -{ - Serial.begin(115200); - while ( !Serial ) delay(10); // for nrf52840 with native usb - - Serial.println("Bluefruit52 BLEUART Example"); - Serial.println("---------------------------\n"); - - CircuitPlayground.begin(); - - // Setup the BLE LED to be enabled on CONNECT - // Note: This is actually the default behaviour, but provided - // here in case you want to control this LED manually via PIN 19 - Bluefruit.autoConnLed(true); - - // Config the peripheral connection with maximum bandwidth - // more SRAM required by SoftDevice - // Note: All config***() function must be called before begin() - Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); - - Bluefruit.begin(); - Bluefruit.setTxPower(4); // Check bluefruit.h for supported values - Bluefruit.setName("Bluefruit52"); - //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections - Bluefruit.Periph.setConnectCallback(connect_callback); - Bluefruit.Periph.setDisconnectCallback(disconnect_callback); - - // To be consistent OTA DFU should be added first if it exists - bledfu.begin(); - - // Configure and Start Device Information Service - bledis.setManufacturer("Adafruit Industries"); - bledis.setModel("Bluefruit Feather52"); - bledis.begin(); - - // Configure and Start BLE Uart Service - bleuart.begin(); - - // Start BLE Battery Service - blebas.begin(); - blebas.write(100); - - // Adafruit service - bleTemp.begin(); - bleNeopixel.begin(); - bleAccel.begin(); - - - // Set up and start advertising - startAdv(); - - Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); - Serial.println("Once connected, enter character(s) that you wish to send"); -} - -void startAdv(void) -{ - // Advertising packet - Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.Advertising.addTxPower(); - - // Include bleuart 128-bit uuid - Bluefruit.Advertising.addService(bleuart); - - // Secondary Scan Response packet (optional) - // Since there is no room for 'Name' in Advertising packet - Bluefruit.ScanResponse.addName(); - - /* Start Advertising - * - Enable auto advertising if disconnected - * - Interval: fast mode = 20 ms, slow mode = 152.5 ms - * - Timeout for fast mode is 30 seconds - * - Start(timeout) with timeout = 0 will advertise forever (until connected) - * - * For recommended advertising interval - * https://developer.apple.com/library/content/qa/qa1931/_index.html - */ - Bluefruit.Advertising.restartOnDisconnect(true); - Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms - Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode - Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds -} - -void loop() -{ - if ( !Bluefruit.connected() ) return; - if ( !bleTemp.Temperature.notifyEnabled() ) return; - - int16_t temp = (int16_t) (100*CircuitPlayground.temperature()); - bleTemp.Temperature.notify16((uint16_t) temp); - - delay(1000*bleTemp.MeasurementInterval.read16()); -} - -// callback invoked when central connects -void connect_callback(uint16_t conn_handle) -{ - // Get the reference to current connection - BLEConnection* connection = Bluefruit.Connection(conn_handle); - - char central_name[32] = { 0 }; - connection->getPeerName(central_name, sizeof(central_name)); - - Serial.print("Connected to "); - Serial.println(central_name); -} - -/** - * Callback invoked when a connection is dropped - * @param conn_handle connection where this event happens - * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h - */ -void disconnect_callback(uint16_t conn_handle, uint8_t reason) -{ - (void) conn_handle; - (void) reason; - - Serial.println(); - Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); -} From 54c87e8f29153b3f5c0719c575ff26cb69040c1c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Nov 2019 16:29:58 +0700 Subject: [PATCH 11/38] refactor Software Timer, auto detect if in ISR mode --- cores/nRF5/utility/AdaCallback.c | 10 +-- cores/nRF5/utility/SoftwareTimer.cpp | 111 +++++++++++++++++++++++++++ cores/nRF5/utility/SoftwareTimer.h | 52 ++----------- cores/nRF5/wiring.h | 6 ++ 4 files changed, 127 insertions(+), 52 deletions(-) create mode 100644 cores/nRF5/utility/SoftwareTimer.cpp diff --git a/cores/nRF5/utility/AdaCallback.c b/cores/nRF5/utility/AdaCallback.c index 64e51b70..3fb9aa0d 100644 --- a/cores/nRF5/utility/AdaCallback.c +++ b/cores/nRF5/utility/AdaCallback.c @@ -73,15 +73,9 @@ void adafruit_callback_task(void* arg) } } -//! Test if in interrupt mode -static inline bool is_isr(void) -{ - return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ; -} - void ada_callback_queue(ada_callback_t* cb_item) { - BaseType_t ret = is_isr() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT); + BaseType_t ret = isInISR() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT); if ( ret != pdTRUE ) { @@ -91,7 +85,7 @@ void ada_callback_queue(ada_callback_t* cb_item) _cb_qdepth = 2*_cb_qdepth; // try again - if ( is_isr() ) + if ( isInISR() ) { xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL); }else diff --git a/cores/nRF5/utility/SoftwareTimer.cpp b/cores/nRF5/utility/SoftwareTimer.cpp new file mode 100644 index 00000000..d86a0490 --- /dev/null +++ b/cores/nRF5/utility/SoftwareTimer.cpp @@ -0,0 +1,111 @@ +/* + * 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 "SoftwareTimer.h" + +SoftwareTimer::SoftwareTimer() +{ + _handle = NULL; +} + +SoftwareTimer::~SoftwareTimer () +{ + if ( _handle != NULL ) xTimerDelete(_handle, 0); +} + +void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating) +{ + _handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback); +} + +bool SoftwareTimer::start(void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + }else + { + xTimerStart(_handle, 0); + } + return ret; +} + +bool SoftwareTimer::stop (void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerStop (_handle, 0); + } + + return ret; +} + +bool SoftwareTimer::reset (void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerReset(_handle, 0); + } + + return ret; +} + +bool SoftwareTimer::setPeriod(uint32_t ms) +{ + BaseType_t active = xTimerIsTimerActive(_handle); + bool ret = true; + + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerChangePeriodFromISR(_handle, ms2tick(ms), &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerChangePeriod(_handle, ms2tick(ms), 0); + } + + // Change period of inactive timer will also start it !! + if ( !active ) stop(); + + return ret; +} diff --git a/cores/nRF5/utility/SoftwareTimer.h b/cores/nRF5/utility/SoftwareTimer.h index e39b7301..7649187c 100644 --- a/cores/nRF5/utility/SoftwareTimer.h +++ b/cores/nRF5/utility/SoftwareTimer.h @@ -44,52 +44,16 @@ class SoftwareTimer TimerHandle_t _handle; public: - SoftwareTimer() { _handle = NULL; } - virtual ~SoftwareTimer() { if(_handle != NULL) xTimerDelete(_handle, 0); } + SoftwareTimer(); + virtual ~SoftwareTimer(); - void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true) - { - _handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback); - } + void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true); + TimerHandle_t getHandle(void) { return _handle; } - TimerHandle_t getHandle(void) - { - return _handle; - } - - void start(void) { xTimerStart(_handle, 0); } - void stop (void) { xTimerStop (_handle, 0); } - void reset(void) { xTimerReset(_handle, 0); } - - bool startFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - bool stopFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - bool resetFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - void setPeriod(uint32_t ms) - { - BaseType_t active = xTimerIsTimerActive(_handle); - xTimerChangePeriod(_handle, ms2tick(ms), 0); - - // Change period of inactive timer will also start it !! - if ( !active ) xTimerStop(_handle, 0); - } + bool start(void); + bool stop (void); + bool reset (void); + bool setPeriod(uint32_t ms); }; #endif /* SOFTWARETIMER_H_ */ diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index 40bbd734..a557f547 100644 --- a/cores/nRF5/wiring.h +++ b/cores/nRF5/wiring.h @@ -36,6 +36,12 @@ void enterUf2Dfu(void); void waitForEvent(void); void systemOff(uint32_t pin, uint8_t wake_logic); +// Test if in interrupt mode +static inline bool isInISR(void) +{ + return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ; +} + #ifdef __cplusplus } #endif From 33857e9ed3180e9dd3c9b69a3cbd958b3008520c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Nov 2019 16:56:44 +0700 Subject: [PATCH 12/38] add timer ID support to software timer --- cores/nRF5/utility/SoftwareTimer.cpp | 4 ++-- cores/nRF5/utility/SoftwareTimer.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cores/nRF5/utility/SoftwareTimer.cpp b/cores/nRF5/utility/SoftwareTimer.cpp index d86a0490..84c64c0e 100644 --- a/cores/nRF5/utility/SoftwareTimer.cpp +++ b/cores/nRF5/utility/SoftwareTimer.cpp @@ -34,9 +34,9 @@ SoftwareTimer::~SoftwareTimer () if ( _handle != NULL ) xTimerDelete(_handle, 0); } -void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating) +void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, void* timerID, bool repeating) { - _handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback); + _handle = xTimerCreate(NULL, ms2tick(ms), repeating, timerID, callback); } bool SoftwareTimer::start(void) diff --git a/cores/nRF5/utility/SoftwareTimer.h b/cores/nRF5/utility/SoftwareTimer.h index 7649187c..24453c49 100644 --- a/cores/nRF5/utility/SoftwareTimer.h +++ b/cores/nRF5/utility/SoftwareTimer.h @@ -47,7 +47,7 @@ class SoftwareTimer SoftwareTimer(); virtual ~SoftwareTimer(); - void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true); + void begin(uint32_t ms, TimerCallbackFunction_t callback, void* timerID = NULL, bool repeating = true); TimerHandle_t getHandle(void) { return _handle; } bool start(void); From 603447a0d63941750ba21a40931564c0663e06c9 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Nov 2019 17:19:56 +0700 Subject: [PATCH 13/38] adding abstract class BLEAdafruitSensor --- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 5 ++ .../Bluefruit52Lib/src/BLECharacteristic.h | 1 + .../src/services/BLEAdafruitSensor.cpp | 88 +++++++++++++++++++ .../src/services/BLEAdafruitSensor.h | 53 +++++++++++ 4 files changed, 147 insertions(+) create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index a06fefcf..d9dc16aa 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -545,6 +545,11 @@ uint16_t BLECharacteristic::write32(int num) return write32( (uint32_t) num ); } +uint16_t BLECharacteristic::write32(int32_t num) +{ + return write32( (uint32_t) num ); +} + /*------------------------------------------------------------------*/ /* READ *------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 9135d572..370f8750 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -125,6 +125,7 @@ class BLECharacteristic uint16_t write16 (uint16_t num); uint16_t write32 (uint32_t num); uint16_t write32 (int num); + uint16_t write32 (int32_t num); /*------------- Read -------------*/ uint16_t read (void* buffer, uint16_t bufsize, uint16_t offset = 0); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp new file mode 100644 index 00000000..38587481 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.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 "bluefruit.h" +#include "BLEAdafruitSensor.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +BLEAdafruitSensor::BLEAdafruitSensor(void) : + Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) +{ + _measure_cb = NULL; +} + +void BLEAdafruitSensor::setMeasureCallback(measure_callback_t fp) +{ + _measure_cb = fp; +} + +bool BLEAdafruitSensor::begin(int32_t ms) +{ + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Period.setFixedLen(4); + VERIFY_STATUS( Period.begin() ); + Period.write32(ms); + + Period.setWriteCallback(sensor_period_write_cb, true); + + // setup timer + _timer.begin(ms, sensor_timer_cb, this, true); + + return true; +} + + +void BLEAdafruitSensor::sensor_timer_cb(TimerHandle_t xTimer) +{ + BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID(xTimer); + + // invoke measure callback + if (svc->_measure_cb) svc->_measure_cb(); +} + +// Client update period, adjust timer accordingly +void BLEAdafruitSensor::sensor_period_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +{ + BLEAdafruitSensor& svc = (BLEAdafruitSensor&) chr->parentService(); + + int32_t ms = 0; + memcpy(&ms, data, len); + + // TODO handle period = 0 which notify on changes ASAP + if ( ms < 0 ) + { + svc._timer.stop(); + }else + { + svc._timer.setPeriod(ms); + } +} + diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h new file mode 100644 index 00000000..92d6e3ee --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h @@ -0,0 +1,53 @@ +/* + * 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 BLE_ADAFRUIT_SENSOR_H_ +#define BLE_ADAFRUIT_SENSOR_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitSensor : public BLEService +{ + public: + typedef void (*measure_callback_t )(void); + + BLEAdafruitSensor(void); + virtual bool begin(int32_t ms); + + void setMeasureCallback(measure_callback_t fp); + + protected: + measure_callback_t _measure_cb; + SoftwareTimer _timer; + + BLECharacteristic Period; + + 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); +}; + +#endif /* BLE_ADAFRUIT_SENSOR_H_ */ From 8db3f2e372663cfcf7ffbea41d57cb09c42ba7db Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 00:36:12 +0700 Subject: [PATCH 14/38] use BLEAdafruitSensor as base class for other services --- .../src/services/BLEAdafruitAccel.cpp | 13 +++---------- .../src/services/BLEAdafruitAccel.h | 5 +++-- .../src/services/BLEAdafruitButton.cpp | 13 +++---------- .../src/services/BLEAdafruitButton.h | 4 ++-- .../src/services/BLEAdafruitLightSensor.cpp | 13 +++---------- .../src/services/BLEAdafruitLightSensor.h | 5 +++-- .../src/services/BLEAdafruitSensor.cpp | 17 +++++++++++++---- .../src/services/BLEAdafruitSensor.h | 9 ++++++--- .../src/services/BLEAdafruitTemperature.cpp | 14 +++----------- .../src/services/BLEAdafruitTemperature.h | 5 +++-- 10 files changed, 42 insertions(+), 56 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp index d8427c31..a069879e 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp @@ -55,15 +55,15 @@ const uint8_t BLEAdafruitAccel::UUID128_CHR_DATA[16] = // Constructor BLEAdafruitAccel::BLEAdafruitAccel(void) - : BLEService(UUID128_SERVICE), Accel(UUID128_CHR_DATA), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) + : BLEAdafruitSensor(UUID128_SERVICE), Accel(UUID128_CHR_DATA) { } err_t BLEAdafruitAccel::begin (void) { - // Invoke base class begin() - VERIFY_STATUS( BLEService::begin() ); + // Invoke base class begin(), this will add service and Period chr + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); // Add Characteristic Accel.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); @@ -71,12 +71,5 @@ err_t BLEAdafruitAccel::begin (void) Accel.setFixedLen(4*3); VERIFY_STATUS( Accel.begin() ); - // Add Characteristic - Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Period.setFixedLen(4); - VERIFY_STATUS( Period.begin() ); - Period.write32(10); - return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h index 496e16ee..69539db2 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h @@ -30,14 +30,15 @@ #include "BLECharacteristic.h" #include "BLEService.h" -class BLEAdafruitAccel : public BLEService +#include "BLEAdafruitSensor.h" + +class BLEAdafruitAccel : public BLEAdafruitSensor { public: static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; BLECharacteristic Accel; - BLECharacteristic Period; BLEAdafruitAccel(void); virtual err_t begin(void); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp index 41702f9c..e25d44c2 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp @@ -55,15 +55,15 @@ const uint8_t BLEAdafruitButton::UUID128_CHR_DATA[16] = // Constructor BLEAdafruitButton::BLEAdafruitButton(void) - : BLEService(UUID128_SERVICE), Button(UUID128_CHR_DATA), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) + : BLEAdafruitSensor(UUID128_SERVICE), Button(UUID128_CHR_DATA) { } err_t BLEAdafruitButton::begin (void) { - // Invoke base class begin() - VERIFY_STATUS( BLEService::begin() ); + // Invoke base class begin(), this will add service and Period chr + VERIFY_STATUS( BLEAdafruitSensor::begin(100) ); // Add Characteristic Button.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); @@ -71,12 +71,5 @@ err_t BLEAdafruitButton::begin (void) Button.setFixedLen(4); VERIFY_STATUS( Button.begin() ); - // Add Characteristic - Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Period.setFixedLen(4); - VERIFY_STATUS( Period.begin() ); - Period.write32(10); - return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h index b6e9f742..0e325332 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h @@ -29,15 +29,15 @@ #include "BLECharacteristic.h" #include "BLEService.h" +#include "BLEAdafruitSensor.h" -class BLEAdafruitButton : public BLEService +class BLEAdafruitButton : public BLEAdafruitSensor { public: static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; BLECharacteristic Button; - BLECharacteristic Period; BLEAdafruitButton(void); virtual err_t begin(void); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp index 1d21d7ba..5464a013 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp @@ -55,15 +55,15 @@ const uint8_t BLEAdafruitLightSensor::UUID128_CHR_DATA[16] = // Constructor BLEAdafruitLightSensor::BLEAdafruitLightSensor(void) - : BLEService(UUID128_SERVICE), Lux(UUID128_CHR_DATA), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) + : BLEAdafruitSensor(UUID128_SERVICE), Lux(UUID128_CHR_DATA) { } err_t BLEAdafruitLightSensor::begin (void) { - // Invoke base class begin() - VERIFY_STATUS( BLEService::begin() ); + // Invoke base class begin(), this will add service and Period chr + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); // Add Characteristic Lux.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); @@ -71,12 +71,5 @@ err_t BLEAdafruitLightSensor::begin (void) Lux.setFixedLen(4); VERIFY_STATUS( Lux.begin() ); - // Add Characteristic - Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Period.setFixedLen(4); - VERIFY_STATUS( Period.begin() ); - Period.write32(10); - return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h index c184113b..a5d46c66 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h @@ -30,14 +30,15 @@ #include "BLECharacteristic.h" #include "BLEService.h" -class BLEAdafruitLightSensor : public BLEService +#include "BLEAdafruitSensor.h" + +class BLEAdafruitLightSensor : public BLEAdafruitSensor { public: static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; BLECharacteristic Lux; - BLECharacteristic Period; BLEAdafruitLightSensor(void); virtual err_t begin(void); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp index 38587481..0ca14c0f 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp @@ -29,8 +29,8 @@ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ -BLEAdafruitSensor::BLEAdafruitSensor(void) : - Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) +BLEAdafruitSensor::BLEAdafruitSensor(BLEUuid bleuuid) + : BLEService(bleuuid), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) { _measure_cb = NULL; } @@ -40,7 +40,7 @@ void BLEAdafruitSensor::setMeasureCallback(measure_callback_t fp) _measure_cb = fp; } -bool BLEAdafruitSensor::begin(int32_t ms) +err_t BLEAdafruitSensor::begin(int32_t ms) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); @@ -56,9 +56,18 @@ bool BLEAdafruitSensor::begin(int32_t ms) // setup timer _timer.begin(ms, sensor_timer_cb, this, true); - return true; + return ERROR_NONE; } +void BLEAdafruitSensor::startMeasuring(void) +{ + _timer.start(); +} + +void BLEAdafruitSensor::stopMeasuring(void) +{ + _timer.stop(); +} void BLEAdafruitSensor::sensor_timer_cb(TimerHandle_t xTimer) { diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h index 92d6e3ee..f8988554 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h @@ -35,16 +35,19 @@ class BLEAdafruitSensor : public BLEService public: typedef void (*measure_callback_t )(void); - BLEAdafruitSensor(void); - virtual bool begin(int32_t ms); + BLEAdafruitSensor(BLEUuid bleuuid); + virtual err_t begin(int32_t ms); void setMeasureCallback(measure_callback_t fp); + void startMeasuring(void); + void stopMeasuring(void); + + BLECharacteristic Period; protected: measure_callback_t _measure_cb; SoftwareTimer _timer; - BLECharacteristic Period; 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); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp index a3f73c43..1135031b 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp @@ -54,15 +54,15 @@ const uint8_t BLEAdafruitTemperature::UUID128_CHR_TEMPERATURE[16] = // Constructor BLEAdafruitTemperature::BLEAdafruitTemperature(void) - : BLEService(UUID128_SERVICE), Temperature(UUID128_CHR_TEMPERATURE), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) + : BLEAdafruitSensor(UUID128_SERVICE), Temperature(UUID128_CHR_TEMPERATURE) { } err_t BLEAdafruitTemperature::begin (void) { - // Invoke base class begin() - VERIFY_STATUS( BLEService::begin() ); + // Invoke base class begin(), this will add service and Period chr + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); // Add Temperature Characteristic Temperature.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); @@ -70,13 +70,5 @@ err_t BLEAdafruitTemperature::begin (void) Temperature.setFixedLen(4); VERIFY_STATUS( Temperature.begin() ); - // Add Measurement Interval Characteristic - Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Period.setFixedLen(4); - VERIFY_STATUS( Period.begin() ); - - Period.write32(30); // measure every 30 seconds - return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h index 85323785..05dc0583 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h @@ -30,14 +30,15 @@ #include "BLECharacteristic.h" #include "BLEService.h" -class BLEAdafruitTemperature : public BLEService +#include "BLEAdafruitSensor.h" + +class BLEAdafruitTemperature : public BLEAdafruitSensor { public: static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_TEMPERATURE[16]; BLECharacteristic Temperature; - BLECharacteristic Period; BLEAdafruitTemperature(); virtual err_t begin(void); From 0ab39ca9dfd52b24dc2f88a640a085a49458f88c Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 12:35:06 +0700 Subject: [PATCH 15/38] change BLEUuid begin() return type to bool --- libraries/Bluefruit52Lib/src/BLEUuid.cpp | 6 +++--- libraries/Bluefruit52Lib/src/BLEUuid.h | 2 +- libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.cpp b/libraries/Bluefruit52Lib/src/BLEUuid.cpp index 018bdd6e..882184b3 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.cpp +++ b/libraries/Bluefruit52Lib/src/BLEUuid.cpp @@ -102,7 +102,7 @@ size_t BLEUuid::size (void) const return 0; } -err_t BLEUuid::begin(void) +bool BLEUuid::begin(void) { /* Add base uuid and decode to get uuid16 * This should cover the already added base uuid128 previously @@ -110,10 +110,10 @@ err_t BLEUuid::begin(void) if (_uuid.type == BLE_UUID_TYPE_UNKNOWN && _uuid128 != NULL ) { (void) sd_ble_uuid_vs_add( (ble_uuid128_t const*) _uuid128, &_uuid.type ); - VERIFY_STATUS( sd_ble_uuid_decode(16, _uuid128, &_uuid) ); + VERIFY_STATUS( sd_ble_uuid_decode(16, _uuid128, &_uuid), false ); } - return ERROR_NONE; + return true; } bool BLEUuid::operator== (const BLEUuid& uuid) const diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index e7138b22..f2ea1a5b 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -59,7 +59,7 @@ class BLEUuid size_t size (void) const; // Add UUID128 if needed, in case of UUID16, no actions is required - err_t begin(void); + bool begin(void); bool operator==(const BLEUuid& uuid) const; bool operator!=(const BLEUuid& uuid) const; diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h index f8988554..a826c00d 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h @@ -42,13 +42,12 @@ class BLEAdafruitSensor : public BLEService void startMeasuring(void); void stopMeasuring(void); + protected: BLECharacteristic Period; - protected: measure_callback_t _measure_cb; SoftwareTimer _timer; - 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); }; From d898f17f44dde6a472b3aa4317b498a72cc17ffa Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 13:14:03 +0700 Subject: [PATCH 16/38] BLECharacteristic allow user set buffer --- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 11 +++++++- .../Bluefruit52Lib/src/BLECharacteristic.h | 25 +++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index d9dc16aa..347f103c 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -53,6 +53,7 @@ void BLECharacteristic::_init(void) _attr_meta.read_perm = _attr_meta.write_perm = BLE_SECMODE_OPEN; _attr_meta.vlen = 1; _attr_meta.vloc = BLE_GATTS_VLOC_STACK; + _userbuf = NULL; _handles.value_handle = BLE_GATT_HANDLE_INVALID; _handles.user_desc_handle = BLE_GATT_HANDLE_INVALID; @@ -133,6 +134,14 @@ void BLECharacteristic::setFixedLen(uint16_t fixed_len) } } +// Use application buffer instead of SD stack buffer +void BLECharacteristic::setBuffer(void* buf, uint16_t bufsize) +{ + _max_len = bufsize; + _userbuf = buf; + _attr_meta.vloc = buf ? BLE_GATTS_VLOC_USER : BLE_GATTS_VLOC_STACK; +} + void BLECharacteristic::setPermission(BleSecurityMode read_perm, BleSecurityMode write_perm) { memcpy(&_attr_meta.read_perm , &read_perm, 1); @@ -270,7 +279,7 @@ err_t BLECharacteristic::begin(void) .init_len = (_attr_meta.vlen == 1) ? (uint16_t) 0 : _max_len, .init_offs = 0, .max_len = _max_len, - .p_value = NULL + .p_value = _userbuf }; VERIFY_STATUS( sd_ble_gatts_characteristic_add(BLE_GATT_HANDLE_INVALID, &char_md, &attr_char_value, &_handles) ); diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 370f8750..97c1cf76 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -98,6 +98,7 @@ class BLECharacteristic void setPermission(BleSecurityMode read_perm, BleSecurityMode write_perm); void setMaxLen(uint16_t max_len); void setFixedLen(uint16_t fixed_len); + void setBuffer(void* buf, uint16_t bufsize); /*------------- Descriptors -------------*/ void setUserDescriptor(const char* descriptor); // aka user descriptor @@ -182,17 +183,12 @@ class BLECharacteristic virtual void _eventHandler(ble_evt_t* event); protected: - struct ATTR_PACKED { - uint8_t write : 1; - uint8_t cccd_write : 1; - uint8_t read_authorize : 1; - uint8_t write_authorize : 1; - } _use_ada_cb; - bool _is_temp; uint16_t _max_len; BLEService* _service; // pointer to parent's service + void* _userbuf; + /*------------- Descriptors -------------*/ const char* _usr_descriptor; struct ATTR_PACKED { @@ -212,11 +208,18 @@ class BLECharacteristic }_long_wr; /*------------- Callback pointers -------------*/ - read_authorize_cb_t _rd_authorize_cb; - write_authorize_cb_t _wr_authorize_cb; + struct ATTR_PACKED { + uint8_t write : 1; + uint8_t cccd_write : 1; + uint8_t read_authorize : 1; + uint8_t write_authorize : 1; + } _use_ada_cb; - write_cb_t _wr_cb; - write_cccd_cb_t _cccd_wr_cb; + read_authorize_cb_t _rd_authorize_cb; + write_authorize_cb_t _wr_authorize_cb; + + write_cb_t _wr_cb; + write_cccd_cb_t _cccd_wr_cb; /*------------- Internal Functions -------------*/ void _init(void); From 65eb11829b6b9111c6a62eba4af64783b7f3f572 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 14:55:20 +0700 Subject: [PATCH 17/38] update BLEAdafruitSensor service --- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 7 +++- .../Bluefruit52Lib/src/BLECharacteristic.h | 4 +- .../src/services/BLEAdafruitAccel.cpp | 15 ++++---- .../src/services/BLEAdafruitButton.cpp | 16 ++++---- .../src/services/BLEAdafruitButton.h | 2 - .../src/services/BLEAdafruitLightSensor.cpp | 15 ++++---- .../src/services/BLEAdafruitSensor.cpp | 37 +++++++++++++++++-- .../src/services/BLEAdafruitSensor.h | 6 ++- .../src/services/BLEAdafruitTemperature.cpp | 17 ++++----- .../src/services/BLEAdafruitTemperature.h | 4 +- 10 files changed, 76 insertions(+), 47 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index 347f103c..5ab0084a 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -122,6 +122,11 @@ void BLECharacteristic::setMaxLen(uint16_t max_len) _max_len = max_len; } +uint16_t BLECharacteristic::getMaxLen(void) +{ + return _max_len; +} + void BLECharacteristic::setFixedLen(uint16_t fixed_len) { if ( fixed_len ) @@ -138,7 +143,7 @@ void BLECharacteristic::setFixedLen(uint16_t fixed_len) void BLECharacteristic::setBuffer(void* buf, uint16_t bufsize) { _max_len = bufsize; - _userbuf = buf; + _userbuf = (uint8_t*) buf; _attr_meta.vloc = buf ? BLE_GATTS_VLOC_USER : BLE_GATTS_VLOC_STACK; } diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 97c1cf76..15e19d4e 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -100,6 +100,8 @@ class BLECharacteristic void setFixedLen(uint16_t fixed_len); void setBuffer(void* buf, uint16_t bufsize); + uint16_t getMaxLen(void); + /*------------- Descriptors -------------*/ void setUserDescriptor(const char* descriptor); // aka user descriptor void setReportRefDescriptor(uint8_t id, uint8_t type); // TODO refactor to use addDescriptor() @@ -187,7 +189,7 @@ class BLECharacteristic uint16_t _max_len; BLEService* _service; // pointer to parent's service - void* _userbuf; + uint8_t* _userbuf; /*------------- Descriptors -------------*/ const char* _usr_descriptor; diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp index a069879e..a866ced1 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp @@ -55,21 +55,20 @@ const uint8_t BLEAdafruitAccel::UUID128_CHR_DATA[16] = // Constructor BLEAdafruitAccel::BLEAdafruitAccel(void) - : BLEAdafruitSensor(UUID128_SERVICE), Accel(UUID128_CHR_DATA) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) { } err_t BLEAdafruitAccel::begin (void) { - // Invoke base class begin(), this will add service and Period chr - VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4*3); - // Add Characteristic - Accel.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); - Accel.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - Accel.setFixedLen(4*3); - VERIFY_STATUS( Accel.begin() ); + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp index e25d44c2..5f855c65 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp @@ -55,21 +55,19 @@ const uint8_t BLEAdafruitButton::UUID128_CHR_DATA[16] = // Constructor BLEAdafruitButton::BLEAdafruitButton(void) - : BLEAdafruitSensor(UUID128_SERVICE), Button(UUID128_CHR_DATA) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) { - } err_t BLEAdafruitButton::begin (void) { - // Invoke base class begin(), this will add service and Period chr - VERIFY_STATUS( BLEAdafruitSensor::begin(100) ); + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4); - // Add Characteristic - Button.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); - Button.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - Button.setFixedLen(4); - VERIFY_STATUS( Button.begin() ); + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(100) ); return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h index 0e325332..49459fe4 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h @@ -37,8 +37,6 @@ class BLEAdafruitButton : public BLEAdafruitSensor static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_DATA[16]; - BLECharacteristic Button; - BLEAdafruitButton(void); virtual err_t begin(void); }; diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp index 5464a013..db75c60f 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp @@ -55,21 +55,20 @@ const uint8_t BLEAdafruitLightSensor::UUID128_CHR_DATA[16] = // Constructor BLEAdafruitLightSensor::BLEAdafruitLightSensor(void) - : BLEAdafruitSensor(UUID128_SERVICE), Lux(UUID128_CHR_DATA) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) { } err_t BLEAdafruitLightSensor::begin (void) { - // Invoke base class begin(), this will add service and Period chr - VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4); - // Add Characteristic - Lux.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); - Lux.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - Lux.setFixedLen(4); - VERIFY_STATUS( Lux.begin() ); + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp index 0ca14c0f..103f6bdd 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp @@ -29,8 +29,8 @@ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ -BLEAdafruitSensor::BLEAdafruitSensor(BLEUuid bleuuid) - : BLEService(bleuuid), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) +BLEAdafruitSensor::BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid) + : BLEService(service_uuid), _measurement(data_uuid), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) { _measure_cb = NULL; } @@ -45,6 +45,10 @@ err_t BLEAdafruitSensor::begin(int32_t ms) // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); + _measurement.setCccdWriteCallback(sensor_data_cccd_cb, true); + + VERIFY_STATUS( _measurement.begin() ); + Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); Period.setFixedLen(4); @@ -69,12 +73,23 @@ void BLEAdafruitSensor::stopMeasuring(void) _timer.stop(); } +//--------------------------------------------------------------------+ +// Static Callbacks +//--------------------------------------------------------------------+ + void BLEAdafruitSensor::sensor_timer_cb(TimerHandle_t xTimer) { BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID(xTimer); - // invoke measure callback - if (svc->_measure_cb) svc->_measure_cb(); + if (svc->_measure_cb) + { + uint8_t buf[svc->_measurement.getMaxLen()]; + uint16_t len = svc->_measure_cb(buf, sizeof(buf)); + len = min(len, sizeof(buf)); + + // notify + svc->_measurement.notify(buf, len); + } } // Client update period, adjust timer accordingly @@ -95,3 +110,17 @@ void BLEAdafruitSensor::sensor_period_write_cb(uint16_t conn_hdl, BLECharacteris } } +void BLEAdafruitSensor::sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value) +{ + BLEAdafruitSensor& svc = (BLEAdafruitSensor&) chr->parentService(); + + // notify enabled + if (value & BLE_GATT_HVX_NOTIFICATION) + { + svc._timer.start(); + }else + { + svc._timer.stop(); + } +} + diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h index a826c00d..2ef427bb 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h @@ -33,9 +33,9 @@ class BLEAdafruitSensor : public BLEService { public: - typedef void (*measure_callback_t )(void); + typedef uint16_t (*measure_callback_t )(uint8_t* buf, uint16_t bufsize); - BLEAdafruitSensor(BLEUuid bleuuid); + BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid); virtual err_t begin(int32_t ms); void setMeasureCallback(measure_callback_t fp); @@ -44,12 +44,14 @@ class BLEAdafruitSensor : public BLEService protected: BLECharacteristic Period; + BLECharacteristic _measurement; measure_callback_t _measure_cb; SoftwareTimer _timer; 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); + static void sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value); }; #endif /* BLE_ADAFRUIT_SENSOR_H_ */ diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp index 1135031b..d44975ab 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp @@ -46,7 +46,7 @@ const uint8_t BLEAdafruitTemperature::UUID128_SERVICE[16] = 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x01, 0xAF, 0xAD }; -const uint8_t BLEAdafruitTemperature::UUID128_CHR_TEMPERATURE[16] = +const uint8_t BLEAdafruitTemperature::UUID128_CHR_DATA[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x01, 0xAF, 0xAD @@ -54,21 +54,20 @@ const uint8_t BLEAdafruitTemperature::UUID128_CHR_TEMPERATURE[16] = // Constructor BLEAdafruitTemperature::BLEAdafruitTemperature(void) - : BLEAdafruitSensor(UUID128_SERVICE), Temperature(UUID128_CHR_TEMPERATURE) + : BLEAdafruitSensor(UUID128_SERVICE, UUID128_CHR_DATA) { } err_t BLEAdafruitTemperature::begin (void) { - // Invoke base class begin(), this will add service and Period chr - VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); + // Setup Measurement Characteristic + _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); + _measurement.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _measurement.setFixedLen(4); - // Add Temperature Characteristic - Temperature.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); - Temperature.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - Temperature.setFixedLen(4); - VERIFY_STATUS( Temperature.begin() ); + // Invoke base class begin(), this will add Service, Measurement and Period characteristics + VERIFY_STATUS( BLEAdafruitSensor::begin(1000) ); return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h index 05dc0583..338bbab0 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h @@ -36,9 +36,7 @@ class BLEAdafruitTemperature : public BLEAdafruitSensor { public: static const uint8_t UUID128_SERVICE[16]; - static const uint8_t UUID128_CHR_TEMPERATURE[16]; - - BLECharacteristic Temperature; + static const uint8_t UUID128_CHR_DATA[16]; BLEAdafruitTemperature(); virtual err_t begin(void); From 6ac348ff01364557bb0ba72a057be69c25380ac7 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 17:16:45 +0700 Subject: [PATCH 18/38] implement period = 0, notify on changes --- .../src/services/BLEAdafruitButton.cpp | 2 +- .../src/services/BLEAdafruitSensor.cpp | 79 +++++++++++++------ .../src/services/BLEAdafruitSensor.h | 7 +- 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp index 5f855c65..0eafdfd7 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp @@ -59,7 +59,7 @@ BLEAdafruitButton::BLEAdafruitButton(void) { } -err_t BLEAdafruitButton::begin (void) +err_t BLEAdafruitButton::begin(void) { // Setup Measurement Characteristic _measurement.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp index 103f6bdd..3fee5466 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp @@ -30,7 +30,7 @@ //--------------------------------------------------------------------+ BLEAdafruitSensor::BLEAdafruitSensor(BLEUuid service_uuid, BLEUuid data_uuid) - : BLEService(service_uuid), _measurement(data_uuid), Period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) + : BLEService(service_uuid), _measurement(data_uuid), _period(UUID128_CHR_ADAFRUIT_MEASUREMENT_PERIOD) { _measure_cb = NULL; } @@ -49,13 +49,13 @@ err_t BLEAdafruitSensor::begin(int32_t ms) VERIFY_STATUS( _measurement.begin() ); - Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Period.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Period.setFixedLen(4); - VERIFY_STATUS( Period.begin() ); - Period.write32(ms); + _period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _period.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _period.setFixedLen(4); + VERIFY_STATUS( _period.begin() ); + _period.write32(ms); - Period.setWriteCallback(sensor_period_write_cb, true); + _period.setWriteCallback(sensor_period_write_cb, true); // setup timer _timer.begin(ms, sensor_timer_cb, this, true); @@ -63,6 +63,13 @@ err_t BLEAdafruitSensor::begin(int32_t ms) return ERROR_NONE; } +void BLEAdafruitSensor::setPeriod(int32_t period_ms) +{ + _period.write32(period_ms); + _update_timer(period_ms); +} + + void BLEAdafruitSensor::startMeasuring(void) { _timer.start(); @@ -73,6 +80,44 @@ void BLEAdafruitSensor::stopMeasuring(void) _timer.stop(); } + +void BLEAdafruitSensor::_update_timer(int32_t ms) +{ + // TODO handle period = 0 which notify on changes ASAP + if ( ms < 0 ) + { + _timer.stop(); + }else if ( ms > 0) + { + _timer.setPeriod(ms); + }else + { + // Period = 0: keeping the current interval, but report on changes only + } +} + +void BLEAdafruitSensor::_timer_callback(void) +{ + if (_measure_cb) + { + uint8_t buf[_measurement.getMaxLen()]; + uint16_t len = _measure_cb(buf, sizeof(buf)); + len = min(len, sizeof(buf)); + + // Period = 0, compare with old data, only update on changes + if ( 0 == _period.read32() ) + { + uint8_t prev_buf[_measurement.getMaxLen()]; + _measurement.read(prev_buf, sizeof(prev_buf)); + + // skip notify if there is no changes + if ( 0 == memcmp(prev_buf, buf, len) ) return; + } + + _measurement.notify(buf, len); + } +} + //--------------------------------------------------------------------+ // Static Callbacks //--------------------------------------------------------------------+ @@ -80,16 +125,7 @@ void BLEAdafruitSensor::stopMeasuring(void) void BLEAdafruitSensor::sensor_timer_cb(TimerHandle_t xTimer) { BLEAdafruitSensor* svc = (BLEAdafruitSensor*) pvTimerGetTimerID(xTimer); - - if (svc->_measure_cb) - { - uint8_t buf[svc->_measurement.getMaxLen()]; - uint16_t len = svc->_measure_cb(buf, sizeof(buf)); - len = min(len, sizeof(buf)); - - // notify - svc->_measurement.notify(buf, len); - } + svc->_timer_callback(); } // Client update period, adjust timer accordingly @@ -100,14 +136,7 @@ void BLEAdafruitSensor::sensor_period_write_cb(uint16_t conn_hdl, BLECharacteris int32_t ms = 0; memcpy(&ms, data, len); - // TODO handle period = 0 which notify on changes ASAP - if ( ms < 0 ) - { - svc._timer.stop(); - }else - { - svc._timer.setPeriod(ms); - } + svc._update_timer(ms); } void BLEAdafruitSensor::sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value) diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h index 2ef427bb..408a2104 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h @@ -39,16 +39,21 @@ class BLEAdafruitSensor : public BLEService virtual err_t begin(int32_t ms); void setMeasureCallback(measure_callback_t fp); + void setPeriod(int32_t period_ms); + void startMeasuring(void); void stopMeasuring(void); protected: - BLECharacteristic Period; + BLECharacteristic _period; BLECharacteristic _measurement; measure_callback_t _measure_cb; SoftwareTimer _timer; + void _update_timer(int32_t ms); + void _timer_callback(void); + 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); static void sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value); From b80abcb62983259cf05fc31516a84d643a76b6cd Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 17:18:07 +0700 Subject: [PATCH 19/38] added cplay_ble.ino --- .../Peripheral/cplay_ble/cplay_ble.ino | 189 ++++++++++++++++++ .../Peripheral/cplay_ble/services.txt | 129 ++++++++++++ 2 files changed, 318 insertions(+) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino new file mode 100644 index 00000000..0f4579a0 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -0,0 +1,189 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ +#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; + +uint16_t measure_button(uint8_t* buf, uint16_t bufsize) +{ + uint32_t button = 0; + + button |= ( CircuitPlayground.slideSwitch() ? 0x01 : 0x00 ); + button |= ( CircuitPlayground.leftButton() ? 0x02 : 0x00 ); + button |= ( CircuitPlayground.rightButton() ? 0x04 : 0x00 ); + + memcpy(buf, &button, 4); + return 4; +} + +uint16_t measure_temperature(uint8_t* buf, uint16_t bufsize) +{ + float temp = CircuitPlayground.temperature(); + memcpy(buf, &temp, 4); + return 4; +} + +uint16_t measure_light_sensor(uint8_t* buf, uint16_t bufsize) +{ + float lux = CircuitPlayground.lightSensor(); + memcpy(buf, &lux, 4); + return 4; +} + +uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) +{ + float accel[3]; + accel[0] = CircuitPlayground.motionX(); + accel[1] = CircuitPlayground.motionY(); + accel[2] = CircuitPlayground.motionZ(); + + memcpy(buf, accel, sizeof(accel)); + return sizeof(accel); +} + +void setup() +{ + Serial.begin(115200); + //while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 BLEUART Example"); + Serial.println("---------------------------\n"); + + CircuitPlayground.begin(); + + // Setup the BLE LED to be enabled on CONNECT + // Note: This is actually the default behaviour, but provided + // here in case you want to control this LED manually via PIN 19 + Bluefruit.autoConnLed(false); + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.setName("Bluefruit52"); + //Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + + // To be consistent OTA DFU should be added first if it exists + bledfu.begin(); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather52"); + bledis.begin(); + + // Configure and Start BLE Uart Service + bleuart.begin(); + + // Start BLE Battery Service + blebas.begin(); + blebas.write(100); + + // Adafruit Service + bleTemp.begin(); + bleTemp.setMeasureCallback(measure_temperature); + + bleAccel.begin(); + bleAccel.setMeasureCallback(measure_accel); + + bleLight.begin(); + bleLight.setMeasureCallback(measure_light_sensor); + + bleButton.begin(); + bleButton.setMeasureCallback(measure_button); + bleButton.setPeriod(0); // only notify if there is changes with buttons + + // Set up and start advertising + startAdv(); + + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); + Serial.println("Once connected, enter character(s) that you wish to send"); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + +} + +// callback invoked when central connects +void connect_callback(uint16_t conn_handle) +{ + // Get the reference to current connection + BLEConnection* connection = Bluefruit.Connection(conn_handle); + + char central_name[32] = { 0 }; + connection->getPeerName(central_name, sizeof(central_name)); + + Serial.print("Connected to "); + Serial.println(central_name); +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println(); + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); +} diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt new file mode 100644 index 00000000..5764c599 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt @@ -0,0 +1,129 @@ +/* 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 + * + * Temperature service 0100 + * - Temperature 0101 | float | Read + Notify | degree in Celsius + * - Measurement Period 0001 + * + * Accelerometer service 0200 + * - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z + * - Measurement Period 0001 + * + * Light sensor service 0300 + * - Lux Data 0301 | uint16_t | Read + Notify + * - Measurement Period 0001 + * + * Button service 0600 + * - Pressed 0601 | uint32_t | Read + Notify | slide sw (b2), button B (b1), button A (b0) + * - Measurement Period 0001 + * + * Tone Service 0C00 + * - Frequency 0C01 | uint16_t | frequency in Hz + * - Duration 0C01 | + * + * Sound sensor service 0400 + * - Sound Data 0401 | int16_t | Read + Notify + * - Measurement Period 0001 + * + * Captouch service 0500 + * - Sensitivity 0501 | uint32_t | difference that Capn should report immediately + * - Cap0 0510 | int32_t | Read + Notify + * - ......................... + * - Cap7 0517 | int32_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * + * PIN I/O service 0700 + * - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out + * - Pin Data 0702 | uint64_t | Read + Write | + * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit, value is 0-1024 for duty cycle + * + * PIN Analog 0800 + * - A0 0810 | uint16_t | Read + Notify + * ......................... + * - A7 0817 | uint16_t | Read + Notify + * - Measurement Period 0001 | uint32_t | Read + Write + * + * Neopixel Service 0900 + * - Pixel Pin 0901 | uint8_t | Read + Write + * - Pixel Count 0902 | uint16_t | Read + Write + * - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc .. + * - Pixel Data 0904 | RGB array| Write + * + * Gamepad Service 0A00 + * - Gamepad Button 0A01 | uint16_t | Write + * - Left XY 0A02 | int8_t[2] | Write + * - Right XY 0A03 | int8_t[2] | Write + * + * Image Service 0B00 + * - Format 0B01 | uint16_t[3] | Write | width + height + type, type is enum value e.g RGB_655, RGB_888, JPG, GIF etc ... + * - Data 0B02 | pixel[] | Write | will save to flash as `default_image`, and auto load on reset ? + * - Fill 0B03 | uint8_t[3] | Write | Fill with solid color + * - Load File 0B04 | utf8_t | Write | Load image from qspi flash ? + */ + + + + + { + "_comment": { + "comment": [ + "Add UUIDs for your services, characteristics, and descriptors in this file.", + "The format is the UUID of the item, followed by an object containing the data.", + "'name' is a mandatory handle, while 'format' is optional.", + "Currently the only supported format is 'TEXT', default is 'NO_FORMAT'.", + "The application must be reloaded to access changes in this file." + ] + }, + "uuid16bitServiceDefinitions": { + "180D": { + "name": "Heart Rate" + } + }, + "uuid16bitCharacteristicDefinitions": { + "2A37": { + "name": "Heart Rate Measurement" + }, + "2A29": { + "name": "Manufacturer Name String", + "format": "TEXT" + } + }, + "uuid16bitDescriptorDefinitions": {}, + "uuid128bitServiceDefinitions": { + "6E400001B5A3F393E0A9E50E24DCCA9E": { + "name": "UART over BLE" + }, + + "ADAF0100C33242A893BD25E905756CB8": { "name": "Adafruit Temperature" }, + "ADAF0200C33242A893BD25E905756CB8": { "name": "Adafruit Accelerometer" }, + "ADAF0300C33242A893BD25E905756CB8": { "name": "Adafruit Light" }, + + "ADAF0400C33242A893BD25E905756CB8": { "name": "Adafruit Sound" }, + "ADAF0500C33242A893BD25E905756CB8": { "name": "Adafruit Capacitive Touch" }, + "ADAF0600C33242A893BD25E905756CB8": { "name": "Adafruit Buttons" } + + "ADAF0700C33242A893BD25E905756CB8": { "name": "Adafruit Pin DIO" }, + "ADAF0800C33242A893BD25E905756CB8": { "name": "Adafruit Pin Analog" }, + "ADAF0900C33242A893BD25E905756CB8": { "name": "Adafruit RGB Pixel" } + + "ADAF0A00C33242A893BD25E905756CB8": { "name": "Adafruit Gamepad" }, + "ADAF0B00C33242A893BD25E905756CB8": { "name": "Adafruit Image" }, + "ADAF0C00C33242A893BD25E905756CB8": { "name": "Adafruit Tone" } + + }, + "uuid128bitCharacteristicDefinitions": { + "6E400002B5A3F393E0A9E50E24DCCA9E": { "name": "UART RX" }, + "6E400003B5A3F393E0A9E50E24DCCA9E": { "name": "UART TX" }, + + "ADAF0001C33242A893BD25E905756CB8": { "name": "Measurement Period" }, + "ADAF0101C33242A893BD25E905756CB8": { "name": "Temperature" }, + "ADAF0201C33242A893BD25E905756CB8": { "name": "Accelerometer" }, + "ADAF0301C33242A893BD25E905756CB8": { "name": "Lux" } + }, + "uuid128bitDescriptorDefinitions": {} +} From 8189353d320aebd595085b0fe29058ca4db824d0 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 22:52:40 +0700 Subject: [PATCH 20/38] added tone service --- .../Peripheral/cplay_ble/cplay_ble.ino | 3 + .../Peripheral/cplay_ble/services.txt | 129 ------------------ libraries/Bluefruit52Lib/src/bluefruit.h | 1 + .../src/services/BLEAdafruitTone.cpp | 82 +++++++++++ .../src/services/BLEAdafruitTone.h | 49 +++++++ 5 files changed, 135 insertions(+), 129 deletions(-) delete mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.cpp create mode 100644 libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.h diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index 0f4579a0..101e5692 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -27,6 +27,7 @@ BLEAdafruitTemperature bleTemp; BLEAdafruitAccel bleAccel; BLEAdafruitLightSensor bleLight; BLEAdafruitButton bleButton; +BLEAdafruitTone bleTone; uint16_t measure_button(uint8_t* buf, uint16_t bufsize) { @@ -121,6 +122,8 @@ void setup() bleButton.setMeasureCallback(measure_button); bleButton.setPeriod(0); // only notify if there is changes with buttons + bleTone.begin(CPLAY_BUZZER); + // Set up and start advertising startAdv(); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt deleted file mode 100644 index 5764c599..00000000 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt +++ /dev/null @@ -1,129 +0,0 @@ -/* 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 - * - * Temperature service 0100 - * - Temperature 0101 | float | Read + Notify | degree in Celsius - * - Measurement Period 0001 - * - * Accelerometer service 0200 - * - Accel Data 0201 | float (x, y, z) | Read + Notify | accel x, y, z - * - Measurement Period 0001 - * - * Light sensor service 0300 - * - Lux Data 0301 | uint16_t | Read + Notify - * - Measurement Period 0001 - * - * Button service 0600 - * - Pressed 0601 | uint32_t | Read + Notify | slide sw (b2), button B (b1), button A (b0) - * - Measurement Period 0001 - * - * Tone Service 0C00 - * - Frequency 0C01 | uint16_t | frequency in Hz - * - Duration 0C01 | - * - * Sound sensor service 0400 - * - Sound Data 0401 | int16_t | Read + Notify - * - Measurement Period 0001 - * - * Captouch service 0500 - * - Sensitivity 0501 | uint32_t | difference that Capn should report immediately - * - Cap0 0510 | int32_t | Read + Notify - * - ......................... - * - Cap7 0517 | int32_t | Read + Notify - * - Measurement Period 0001 | uint32_t | Read + Write - * - * - * PIN I/O service 0700 - * - Pin Dir 0701 | uint64_t | Read + Write | bit 1 is In, 0 is Out - * - Pin Data 0702 | uint64_t | Read + Write | - * - PWM Control 0710 | struct { uint8_t pin, uint16_t value, uint32_t period_ms} | as microbit, value is 0-1024 for duty cycle - * - * PIN Analog 0800 - * - A0 0810 | uint16_t | Read + Notify - * ......................... - * - A7 0817 | uint16_t | Read + Notify - * - Measurement Period 0001 | uint32_t | Read + Write - * - * Neopixel Service 0900 - * - Pixel Pin 0901 | uint8_t | Read + Write - * - Pixel Count 0902 | uint16_t | Read + Write - * - Pixel Type 0903 | uint16_t | Read + Write | NEO_RGB, NEO_GRB etc .. - * - Pixel Data 0904 | RGB array| Write - * - * Gamepad Service 0A00 - * - Gamepad Button 0A01 | uint16_t | Write - * - Left XY 0A02 | int8_t[2] | Write - * - Right XY 0A03 | int8_t[2] | Write - * - * Image Service 0B00 - * - Format 0B01 | uint16_t[3] | Write | width + height + type, type is enum value e.g RGB_655, RGB_888, JPG, GIF etc ... - * - Data 0B02 | pixel[] | Write | will save to flash as `default_image`, and auto load on reset ? - * - Fill 0B03 | uint8_t[3] | Write | Fill with solid color - * - Load File 0B04 | utf8_t | Write | Load image from qspi flash ? - */ - - - - - { - "_comment": { - "comment": [ - "Add UUIDs for your services, characteristics, and descriptors in this file.", - "The format is the UUID of the item, followed by an object containing the data.", - "'name' is a mandatory handle, while 'format' is optional.", - "Currently the only supported format is 'TEXT', default is 'NO_FORMAT'.", - "The application must be reloaded to access changes in this file." - ] - }, - "uuid16bitServiceDefinitions": { - "180D": { - "name": "Heart Rate" - } - }, - "uuid16bitCharacteristicDefinitions": { - "2A37": { - "name": "Heart Rate Measurement" - }, - "2A29": { - "name": "Manufacturer Name String", - "format": "TEXT" - } - }, - "uuid16bitDescriptorDefinitions": {}, - "uuid128bitServiceDefinitions": { - "6E400001B5A3F393E0A9E50E24DCCA9E": { - "name": "UART over BLE" - }, - - "ADAF0100C33242A893BD25E905756CB8": { "name": "Adafruit Temperature" }, - "ADAF0200C33242A893BD25E905756CB8": { "name": "Adafruit Accelerometer" }, - "ADAF0300C33242A893BD25E905756CB8": { "name": "Adafruit Light" }, - - "ADAF0400C33242A893BD25E905756CB8": { "name": "Adafruit Sound" }, - "ADAF0500C33242A893BD25E905756CB8": { "name": "Adafruit Capacitive Touch" }, - "ADAF0600C33242A893BD25E905756CB8": { "name": "Adafruit Buttons" } - - "ADAF0700C33242A893BD25E905756CB8": { "name": "Adafruit Pin DIO" }, - "ADAF0800C33242A893BD25E905756CB8": { "name": "Adafruit Pin Analog" }, - "ADAF0900C33242A893BD25E905756CB8": { "name": "Adafruit RGB Pixel" } - - "ADAF0A00C33242A893BD25E905756CB8": { "name": "Adafruit Gamepad" }, - "ADAF0B00C33242A893BD25E905756CB8": { "name": "Adafruit Image" }, - "ADAF0C00C33242A893BD25E905756CB8": { "name": "Adafruit Tone" } - - }, - "uuid128bitCharacteristicDefinitions": { - "6E400002B5A3F393E0A9E50E24DCCA9E": { "name": "UART RX" }, - "6E400003B5A3F393E0A9E50E24DCCA9E": { "name": "UART TX" }, - - "ADAF0001C33242A893BD25E905756CB8": { "name": "Measurement Period" }, - "ADAF0101C33242A893BD25E905756CB8": { "name": "Temperature" }, - "ADAF0201C33242A893BD25E905756CB8": { "name": "Accelerometer" }, - "ADAF0301C33242A893BD25E905756CB8": { "name": "Lux" } - }, - "uuid128bitDescriptorDefinitions": {} -} diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 61778bd7..62d80d07 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -65,6 +65,7 @@ #include "services/BLEAdafruitAccel.h" #include "services/BLEAdafruitLightSensor.h" #include "services/BLEAdafruitButton.h" +#include "services/BLEAdafruitTone.h" #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.cpp new file mode 100644 index 00000000..8cab1e48 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.cpp @@ -0,0 +1,82 @@ +/* + * 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 "bluefruit.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +const uint8_t BLEAdafruitTone::UUID128_SERVICE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0C, 0xAF, 0xAD +}; + +const uint8_t BLEAdafruitTone::UUID128_CHR_TONE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0C, 0xAF, 0xAD +}; + +BLEAdafruitTone::BLEAdafruitTone(void) + : BLEService(UUID128_SERVICE), _tone(UUID128_CHR_TONE) +{ + _pin = -1; +} + +err_t BLEAdafruitTone::begin(int pin) +{ + _pin = pin; + + // Invoke base class begin() + VERIFY_STATUS( BLEService::begin() ); + + // Setup Characteristic + _tone.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _tone.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _tone.setFixedLen(6); // uint16_t freq, uint32_t duration + _tone.setWriteCallback(tone_write_cb, true); + VERIFY_STATUS( _tone.begin() ); + + return ERROR_NONE; +} + +void BLEAdafruitTone::tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +{ + BLEAdafruitTone& svc = (BLEAdafruitTone&) chr->parentService(); + + struct ATTR_PACKED { + uint16_t freq; + uint32_t duration; + } tone_data; + + PRINT_LOCATION(); + // invalid length + if (len != sizeof(tone_data)) return; + memcpy(&tone_data, data, len); + + PRINT_LOCATION(); + tone(svc._pin, tone_data.freq, tone_data.duration); +} diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.h new file mode 100644 index 00000000..97258db9 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.h @@ -0,0 +1,49 @@ +/* + * 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 BLEADAFRUITTONE_H_ +#define BLEADAFRUITTONE_H_ + +#include "bluefruit_common.h" + +#include "BLECharacteristic.h" +#include "BLEService.h" + +class BLEAdafruitTone : public BLEService +{ + public: + static const uint8_t UUID128_SERVICE[16]; + static const uint8_t UUID128_CHR_TONE[16]; + + BLEAdafruitTone(void); + virtual err_t begin(int pin); + + private: + int _pin; + BLECharacteristic _tone; + + static void tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); +}; + +#endif /* BLEADAFRUITTONE_H_ */ From 24ca5738e8aab7d2b007c0ebbb862887d63ea459 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 23:19:58 +0700 Subject: [PATCH 21/38] try to fix travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 529a4e46..363dd568 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,7 @@ install: - BSP_VERSION=`eval ls $BSP_PATH` - rm -r $BSP_PATH/* - ln -s $TRAVIS_BUILD_DIR $BSP_PATH/$BSP_VERSION - - arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library" + - arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library", "Adafruit Circuit Playground" before_script: From 9860a6e757128f48174e15b6cda3c7ea09483f60 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Nov 2019 17:16:01 +0700 Subject: [PATCH 22/38] renaming neopixel to Rgb Pixel --- .../Peripheral/cplay_ble/cplay_ble.ino | 6 ++- libraries/Bluefruit52Lib/src/bluefruit.h | 2 +- ...itNeopixel.cpp => BLEAdafruitRgbPixel.cpp} | 49 ++++++++++--------- ...afruitNeopixel.h => BLEAdafruitRgbPixel.h} | 16 +++--- 4 files changed, 40 insertions(+), 33 deletions(-) rename libraries/Bluefruit52Lib/src/services/{BLEAdafruitNeopixel.cpp => BLEAdafruitRgbPixel.cpp} (66%) rename libraries/Bluefruit52Lib/src/services/{BLEAdafruitNeopixel.h => BLEAdafruitRgbPixel.h} (82%) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index 101e5692..02f3a5f2 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -29,6 +29,8 @@ BLEAdafruitLightSensor bleLight; BLEAdafruitButton bleButton; BLEAdafruitTone bleTone; +BLEAdafruitRgbPixel blePixel; + uint16_t measure_button(uint8_t* buf, uint16_t bufsize) { uint32_t button = 0; @@ -69,7 +71,7 @@ uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) 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"); @@ -124,6 +126,8 @@ void setup() bleTone.begin(CPLAY_BUZZER); + blePixel.begin(); + // Set up and start advertising startAdv(); diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 62d80d07..0eb653a3 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -61,7 +61,7 @@ #include "services/EddyStone.h" #include "services/BLEAdafruitTemperature.h" -#include "services/BLEAdafruitNeopixel.h" +#include "services/BLEAdafruitRgbPixel.h" #include "services/BLEAdafruitAccel.h" #include "services/BLEAdafruitLightSensor.h" #include "services/BLEAdafruitButton.h" diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp b/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.cpp similarity index 66% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp rename to libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.cpp index 53b05899..9e223660 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.cpp @@ -28,62 +28,63 @@ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ - - -/* Adafruit NeoPixel Service - * - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8 - * - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8 - * - Type : ADAF-0004-C332-42A8-93BD-25E905756CB8 - * - Data : ADAF-0005-C332-42A8-93BD-25E905756CB8 +/* All Adafruit Service/Characteristic UUID128 share the same Base UUID: + * ADAFxxx-C332-42A8-93BD-25E905756CB8 + * + * Adafruit NeoPixel Service 0900 + * - Pin 0901 | uint8_t | Read + Write | + * - Type 0902 | uint8_t | Read + Write | 0: neopixel, 1: dotstar + * - Data 0903 | { uint16_t start, uint8_t flags, data } + * - Count */ -const uint8_t BLEAdafruitNeopixel::UUID128_SERVICE[16] = +const uint8_t BLEAdafruitRgbPixel::UUID128_SERVICE[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, - 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x00, 0xAF, 0xAD + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x09, 0xAF, 0xAD }; -const uint8_t BLEAdafruitNeopixel::UUID128_CHR_COUNT[16] = +const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_PIN[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, - 0xA8, 0x42, 0x32, 0xC3, 0x03, 0x00, 0xAF, 0xAD + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x09, 0xAF, 0xAD }; -const uint8_t BLEAdafruitNeopixel::UUID128_CHR_TYPE[16] = +const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_TYPE[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, - 0xA8, 0x42, 0x32, 0xC3, 0x04, 0x00, 0xAF, 0xAD + 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x09, 0xAF, 0xAD }; -const uint8_t BLEAdafruitNeopixel::UUID128_CHR_DATA[16] = +const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_DATA[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, - 0xA8, 0x42, 0x32, 0xC3, 0x05, 0x00, 0xAF, 0xAD + 0xA8, 0x42, 0x32, 0xC3, 0x03, 0x09, 0xAF, 0xAD }; // Constructor -BLEAdafruitNeopixel::BLEAdafruitNeopixel(void) - : BLEService(UUID128_SERVICE), Count(UUID128_CHR_COUNT), Type(UUID128_CHR_TYPE), Data(UUID128_CHR_DATA) +BLEAdafruitRgbPixel::BLEAdafruitRgbPixel(void) + : BLEService(UUID128_SERVICE), Pin(UUID128_CHR_PIN), Type(UUID128_CHR_TYPE), Data(UUID128_CHR_DATA) { } -err_t BLEAdafruitNeopixel::begin (void) +err_t BLEAdafruitRgbPixel::begin (void) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); // Add Characteristic - Count.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Count.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Count.setFixedLen(2); - VERIFY_STATUS( Count.begin() ); - Count.write16(0); + Pin.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + Pin.setPermission(SECMODE_OPEN, SECMODE_OPEN); + Pin.setFixedLen(1); + VERIFY_STATUS( Pin.begin() ); + Pin.write8(0); // Add Characteristic Type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); Type.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Type.setFixedLen(2); + Type.setFixedLen(1); VERIFY_STATUS( Type.begin() ); Type.write16(0); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h b/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.h similarity index 82% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h rename to libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.h index 7e2ddce4..d9c28a30 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.h +++ b/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.h @@ -22,30 +22,32 @@ * THE SOFTWARE. */ -#ifndef BLEADAFRUITNEOPIXEL_H_ -#define BLEADAFRUITNEOPIXEL_H_ +#ifndef BLEADAFRUITRGBPIXEL_H_ +#define BLEADAFRUITRGBPIXEL_H_ #include "bluefruit_common.h" #include "BLECharacteristic.h" #include "BLEService.h" -class BLEAdafruitNeopixel : public BLEService +class BLEAdafruitRgbPixel : public BLEService { public: static const uint8_t UUID128_SERVICE[16]; - static const uint8_t UUID128_CHR_COUNT[16]; + static const uint8_t UUID128_CHR_PIN[16]; +// static const uint8_t UUID128_CHR_COUNT[16]; static const uint8_t UUID128_CHR_TYPE[16]; static const uint8_t UUID128_CHR_DATA[16]; - BLECharacteristic Count; +// BLECharacteristic Count; + BLECharacteristic Pin; BLECharacteristic Type; BLECharacteristic Data; - BLEAdafruitNeopixel(void); + BLEAdafruitRgbPixel(void); virtual err_t begin(void); }; -#endif /* BLEADAFRUITNEOPIXEL_H_ */ +#endif /* BLEADAFRUITRGBPIXEL_H_ */ From 583ad02a7d268922aac862bf6f9368773dcdedc6 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Nov 2019 20:12:34 +0700 Subject: [PATCH 23/38] increase sd attribute table size from 0x800 to 0xC00, increase linker memory for SD 840 from 3400 to 6000 --- boards.txt | 10 +++++----- cores/nRF5/linker/nrf52840_s140_v6.ld | 4 ++-- libraries/Bluefruit52Lib/src/bluefruit.cpp | 4 ++-- libraries/Bluefruit52Lib/src/bluefruit.h | 2 ++ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/boards.txt b/boards.txt index 0f04e014..893b2efc 100644 --- a/boards.txt +++ b/boards.txt @@ -81,7 +81,7 @@ feather52840.upload.protocol=nrfutil feather52840.upload.use_1200bps_touch=true feather52840.upload.wait_for_upload_port=true feather52840.upload.maximum_size=815104 -feather52840.upload.maximum_data_size=248832 +feather52840.upload.maximum_data_size=237568 # Build feather52840.build.mcu=cortex-m4 @@ -135,7 +135,7 @@ itsybitsy52840.upload.protocol=nrfutil itsybitsy52840.upload.use_1200bps_touch=true itsybitsy52840.upload.wait_for_upload_port=true itsybitsy52840.upload.maximum_size=815104 -itsybitsy52840.upload.maximum_data_size=248832 +itsybitsy52840.upload.maximum_data_size=237568 # Build itsybitsy52840.build.mcu=cortex-m4 @@ -188,7 +188,7 @@ cplaynrf52840.upload.protocol=nrfutil cplaynrf52840.upload.use_1200bps_touch=true cplaynrf52840.upload.wait_for_upload_port=true cplaynrf52840.upload.maximum_size=815104 -cplaynrf52840.upload.maximum_data_size=248832 +cplaynrf52840.upload.maximum_data_size=237568 # Build cplaynrf52840.build.mcu=cortex-m4 @@ -241,7 +241,7 @@ metro52840.upload.protocol=nrfutil metro52840.upload.use_1200bps_touch=true metro52840.upload.wait_for_upload_port=true metro52840.upload.maximum_size=815104 -metro52840.upload.maximum_data_size=248832 +metro52840.upload.maximum_data_size=237568 # Build metro52840.build.mcu=cortex-m4 @@ -284,7 +284,7 @@ pca10056.upload.protocol=nrfutil pca10056.upload.use_1200bps_touch=true pca10056.upload.wait_for_upload_port=true pca10056.upload.maximum_size=815104 -pca10056.upload.maximum_data_size=248832 +pca10056.upload.maximum_data_size=237568 # Build pca10056.build.mcu=cortex-m4 diff --git a/cores/nRF5/linker/nrf52840_s140_v6.ld b/cores/nRF5/linker/nrf52840_s140_v6.ld index dc718462..d676b605 100755 --- a/cores/nRF5/linker/nrf52840_s140_v6.ld +++ b/cores/nRF5/linker/nrf52840_s140_v6.ld @@ -8,13 +8,13 @@ MEMORY FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xED000 - 0x26000 /* SRAM required by S132 depend on - * - Attribute Table Size + * - Attribute Table Size (Number of Services and Characteristics) * - Vendor UUID count * - Max ATT MTU * - Concurrent connection peripheral + central + secure links * - Event Len, HVN queue, Write CMD queue */ - RAM (rwx) : ORIGIN = 0x20003400, LENGTH = 0x20040000 - 0x20003400 + RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000 } SECTIONS diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 9bb65d8e..d17f66e4 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -144,7 +144,7 @@ AdafruitBluefruit::AdafruitBluefruit(void) /*------------------------------------------------------------------*/ varclr(&_sd_cfg); - _sd_cfg.attr_table_size = 0x800; + _sd_cfg.attr_table_size = CFG_SD_ATTR_TABLE_SIZE; _sd_cfg.uuid128_max = BLE_UUID_VS_COUNT_DEFAULT; _sd_cfg.service_changed = 1; @@ -329,7 +329,7 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count) * prph and central connections for optimal SRAM usage. * * - If Peripheral mode is enabled - * - ATTR Table Size = 0x800. + * - ATTR Table Size = CFG_SD_ATTR_TABLE_SIZE. * - HVN TX Queue Size = 3 * * - If Central mode is enabled diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 0eb653a3..deb9ed8f 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -36,6 +36,8 @@ #define BLE_GATT_ATT_MTU_MAX 247 #define BLE_MAX_CONNECTION 20 // SD support up to 20 connections +#define CFG_SD_ATTR_TABLE_SIZE 0xC00 + #include "BLEUuid.h" #include "BLEAdvertising.h" #include "BLECharacteristic.h" From 8b60180b45b4e62c3a6ce178ddd21f6c2b1bfb82 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Nov 2019 20:43:17 +0700 Subject: [PATCH 24/38] move BLEAdafruit service to its own library due to neopixel/dotstar dependency --- .../src/services/BLEAdafruitAccel.cpp | 0 .../src/services/BLEAdafruitAccel.h | 0 .../src/services/BLEAdafruitButton.cpp | 0 .../src/services/BLEAdafruitButton.h | 0 .../src/services/BLEAdafruitLightSensor.cpp | 0 .../src/services/BLEAdafruitLightSensor.h | 0 .../src/services/BLEAdafruitRgbPixel.cpp | 2 +- .../src/services/BLEAdafruitRgbPixel.h | 0 .../src/services/BLEAdafruitSensor.cpp | 6 ++++++ .../src/services/BLEAdafruitSensor.h | 0 .../src/services/BLEAdafruitTemperature.cpp | 0 .../src/services/BLEAdafruitTemperature.h | 0 .../src/services/BLEAdafruitTone.cpp | 0 .../src/services/BLEAdafruitTone.h | 0 14 files changed, 7 insertions(+), 1 deletion(-) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitAccel.cpp (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitAccel.h (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitButton.cpp (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitButton.h (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitLightSensor.cpp (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitLightSensor.h (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitRgbPixel.cpp (98%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitRgbPixel.h (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitSensor.cpp (97%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitSensor.h (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitTemperature.cpp (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitTemperature.h (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitTone.cpp (100%) rename libraries/{Bluefruit52Lib => BLEAdafruitService}/src/services/BLEAdafruitTone.h (100%) diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitAccel.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitButton.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitLightSensor.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp similarity index 98% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp index 9e223660..8bfdf103 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp @@ -69,7 +69,7 @@ BLEAdafruitRgbPixel::BLEAdafruitRgbPixel(void) } -err_t BLEAdafruitRgbPixel::begin (void) +err_t BLEAdafruitRgbPixel::begin (Adafruit_NeoPixel* neo_pixel) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitRgbPixel.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp similarity index 97% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp index 3fee5466..15434ff6 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp @@ -151,5 +151,11 @@ void BLEAdafruitSensor::sensor_data_cccd_cb(uint16_t conn_hdl, BLECharacteristic { svc._timer.stop(); } + + // send initial notification if period = 0 +// if ( 0 == svc._period.read32() ) +// { +// svc._measurement.notify(); +// } } diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitSensor.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitTemperature.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp diff --git a/libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h similarity index 100% rename from libraries/Bluefruit52Lib/src/services/BLEAdafruitTone.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h From 986a0f8b900b475e548968fec3ec01707bc4e133 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Nov 2019 20:57:56 +0700 Subject: [PATCH 25/38] complete file moving --- .../src/BLEAdafruitService.h | 42 +++++++++++++++++++ .../src/services/BLEAdafruitAccel.cpp | 2 +- .../src/services/BLEAdafruitAccel.h | 7 ---- .../src/services/BLEAdafruitButton.cpp | 2 +- .../src/services/BLEAdafruitButton.h | 6 --- .../src/services/BLEAdafruitLightSensor.cpp | 2 +- .../src/services/BLEAdafruitLightSensor.h | 7 ---- .../src/services/BLEAdafruitRgbPixel.cpp | 4 +- .../src/services/BLEAdafruitRgbPixel.h | 5 --- .../src/services/BLEAdafruitSensor.cpp | 3 +- .../src/services/BLEAdafruitSensor.h | 5 --- .../src/services/BLEAdafruitTemperature.cpp | 2 +- .../src/services/BLEAdafruitTemperature.h | 7 ---- .../src/services/BLEAdafruitTone.cpp | 2 +- .../src/services/BLEAdafruitTone.h | 5 --- .../Peripheral/cplay_ble/cplay_ble.ino | 3 +- .../Bluefruit52Lib/src/BLECharacteristic.h | 2 + libraries/Bluefruit52Lib/src/bluefruit.h | 7 ---- 18 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 libraries/BLEAdafruitService/src/BLEAdafruitService.h diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h new file mode 100644 index 00000000..6041a3d6 --- /dev/null +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -0,0 +1,42 @@ +/* + * 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 BLEADAFRUITSERVICE_H_ +#define BLEADAFRUITSERVICE_H_ + +#include "bluefruit_common.h" +#include "BLECharacteristic.h" +#include "BLEService.h" + +#include "services/BLEAdafruitSensor.h" +#include "services/BLEAdafruitTemperature.h" +#include "services/BLEAdafruitTemperature.h" +#include "services/BLEAdafruitRgbPixel.h" +#include "services/BLEAdafruitAccel.h" +#include "services/BLEAdafruitLightSensor.h" +#include "services/BLEAdafruitButton.h" +#include "services/BLEAdafruitTone.h" + + +#endif /* BLEADAFRUITSERVICE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp index a866ced1..044163b8 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.cpp @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h index 69539db2..45ed51cc 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAccel.h @@ -25,13 +25,6 @@ #ifndef BLEADAFRUITACCEL_H_ #define BLEADAFRUITACCEL_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" - -#include "BLEAdafruitSensor.h" - class BLEAdafruitAccel : public BLEAdafruitSensor { public: diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp index 0eafdfd7..11ff0c19 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.cpp @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h index 49459fe4..2bdc40e3 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitButton.h @@ -25,12 +25,6 @@ #ifndef BLE_ADAFRUIT_BUTTON_H_ #define BLE_ADAFRUIT_BUTTON_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" -#include "BLEAdafruitSensor.h" - class BLEAdafruitButton : public BLEAdafruitSensor { public: diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp index db75c60f..1238e194 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.cpp @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h index a5d46c66..bcf4200d 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitLightSensor.h @@ -25,13 +25,6 @@ #ifndef BLE_ADAFRUIT_LIGHT_SENSOR_H_ #define BLE_ADAFRUIT_LIGHT_SENSOR_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" - -#include "BLEAdafruitSensor.h" - class BLEAdafruitLightSensor : public BLEAdafruitSensor { public: diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp index 8bfdf103..64b7d794 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION @@ -69,7 +69,7 @@ BLEAdafruitRgbPixel::BLEAdafruitRgbPixel(void) } -err_t BLEAdafruitRgbPixel::begin (Adafruit_NeoPixel* neo_pixel) +err_t BLEAdafruitRgbPixel::begin (/*Adafruit_NeoPixel* neo_pixel*/) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h index d9c28a30..fbad3336 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h @@ -25,11 +25,6 @@ #ifndef BLEADAFRUITRGBPIXEL_H_ #define BLEADAFRUITRGBPIXEL_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" - class BLEAdafruitRgbPixel : public BLEService { public: diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp index 15434ff6..bb54e242 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.cpp @@ -22,8 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" -#include "BLEAdafruitSensor.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h index 408a2104..fe5c5e4f 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitSensor.h @@ -25,11 +25,6 @@ #ifndef BLE_ADAFRUIT_SENSOR_H_ #define BLE_ADAFRUIT_SENSOR_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" - class BLEAdafruitSensor : public BLEService { public: diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp index d44975ab..f9667152 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.cpp @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h index 338bbab0..7ab8b837 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTemperature.h @@ -25,13 +25,6 @@ #ifndef BLEADAFRUITTEMPERATURE_H_ #define BLEADAFRUITTEMPERATURE_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" - -#include "BLEAdafruitSensor.h" - class BLEAdafruitTemperature : public BLEAdafruitSensor { public: diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp index 8cab1e48..97268c0e 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -#include "bluefruit.h" +#include "BLEAdafruitService.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h index 97258db9..4ea3b2a4 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.h @@ -25,11 +25,6 @@ #ifndef BLEADAFRUITTONE_H_ #define BLEADAFRUITTONE_H_ -#include "bluefruit_common.h" - -#include "BLECharacteristic.h" -#include "BLEService.h" - class BLEAdafruitTone : public BLEService { public: diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index 02f3a5f2..915cb610 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -14,6 +14,7 @@ #include #include #include +#include #include // BLE Service @@ -71,7 +72,7 @@ uint16_t measure_accel(uint8_t* buf, uint16_t bufsize) 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"); diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 15e19d4e..b1ac6d98 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -143,6 +143,7 @@ class BLECharacteristic bool notifyEnabled(uint16_t conn_hdl); /*------------- Notify -------------*/ +// bool notify (void); bool notify (const void* data, uint16_t len); bool notify (const char* str); @@ -152,6 +153,7 @@ class BLECharacteristic bool notify32 (int num); /*------------- Notify multiple connections -------------*/ +// bool notify (uint16_t conn_hdl); bool notify (uint16_t conn_hdl, const void* data, uint16_t len); bool notify (uint16_t conn_hdl, const char* str); diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index deb9ed8f..94d18dcc 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -62,13 +62,6 @@ #include "services/BLEMidi.h" #include "services/EddyStone.h" -#include "services/BLEAdafruitTemperature.h" -#include "services/BLEAdafruitRgbPixel.h" -#include "services/BLEAdafruitAccel.h" -#include "services/BLEAdafruitLightSensor.h" -#include "services/BLEAdafruitButton.h" -#include "services/BLEAdafruitTone.h" - #include "clients/BLEAncs.h" #include "clients/BLEClientUart.h" #include "clients/BLEClientDis.h" From 55d78cfb9bb8f32af450c29e2c1f6045e6c77a60 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Nov 2019 23:25:10 +0700 Subject: [PATCH 26/38] correct BLEAdafruitRgbPixel --- .../src/BLEAdafruitService.h | 3 +-- .../src/services/BLEAdafruitRgbPixel.cpp | 20 ++++++++++++++----- .../src/services/BLEAdafruitRgbPixel.h | 18 ++++++++++++++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h index 6041a3d6..e4dd10a4 100644 --- a/libraries/BLEAdafruitService/src/BLEAdafruitService.h +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -31,12 +31,11 @@ #include "services/BLEAdafruitSensor.h" #include "services/BLEAdafruitTemperature.h" -#include "services/BLEAdafruitTemperature.h" -#include "services/BLEAdafruitRgbPixel.h" #include "services/BLEAdafruitAccel.h" #include "services/BLEAdafruitLightSensor.h" #include "services/BLEAdafruitButton.h" #include "services/BLEAdafruitTone.h" +#include "services/BLEAdafruitRgbPixel.h" #endif /* BLEADAFRUITSERVICE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp index 64b7d794..ba103c8c 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp @@ -22,7 +22,11 @@ * THE SOFTWARE. */ -#include "BLEAdafruitService.h" +#include "bluefruit_common.h" +#include "BLECharacteristic.h" +#include "BLEService.h" + +#include "BLEAdafruitRgbPixel.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION @@ -66,10 +70,16 @@ const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_DATA[16] = BLEAdafruitRgbPixel::BLEAdafruitRgbPixel(void) : BLEService(UUID128_SERVICE), Pin(UUID128_CHR_PIN), Type(UUID128_CHR_TYPE), Data(UUID128_CHR_DATA) { - + _neo = NULL; } -err_t BLEAdafruitRgbPixel::begin (/*Adafruit_NeoPixel* neo_pixel*/) +err_t BLEAdafruitRgbPixel::begin (Adafruit_NeoPixel_Type* neo_pixel) +{ + _neo = neo_pixel; + return begin((uint8_t) _neo->getPin(), 0); +} + +err_t BLEAdafruitRgbPixel::begin(uint8_t pin, uint8_t type) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); @@ -79,14 +89,14 @@ err_t BLEAdafruitRgbPixel::begin (/*Adafruit_NeoPixel* neo_pixel*/) Pin.setPermission(SECMODE_OPEN, SECMODE_OPEN); Pin.setFixedLen(1); VERIFY_STATUS( Pin.begin() ); - Pin.write8(0); + Pin.write8(pin); // Add Characteristic Type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); Type.setPermission(SECMODE_OPEN, SECMODE_OPEN); Type.setFixedLen(1); VERIFY_STATUS( Type.begin() ); - Type.write16(0); + Type.write8(type); // Add Characteristic Data.setProperties(CHR_PROPS_WRITE); diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h index fbad3336..9306080f 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h @@ -25,6 +25,17 @@ #ifndef BLEADAFRUITRGBPIXEL_H_ #define BLEADAFRUITRGBPIXEL_H_ +// use Adafruit_CPlay_NeoPixel instead of Adafruit_NeoPixel +#define USE_CPLAY_NEOPIXEL 0 + +#if USE_CPLAY_NEOPIXEL + #include "Adafruit_CPlay_NeoPixel.h" + #define Adafruit_NeoPixel_Type Adafruit_CPlay_NeoPixel +#else + #include "Adafruit_NeoPixel.h" + #define Adafruit_NeoPixel_Type Adafruit_NeoPixel +#endif + class BLEAdafruitRgbPixel : public BLEService { public: @@ -40,7 +51,12 @@ class BLEAdafruitRgbPixel : public BLEService BLECharacteristic Data; BLEAdafruitRgbPixel(void); - virtual err_t begin(void); + virtual err_t begin(Adafruit_NeoPixel_Type* neo); + + private: + Adafruit_NeoPixel_Type* _neo; + + err_t begin(uint8_t pin, uint8_t type); }; From 7770836fbebedebb77e010dbdfe6cce1bd061a58 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 19 Nov 2019 00:44:39 +0700 Subject: [PATCH 27/38] RGB Pixel seems to work --- .../src/services/BLEAdafruitRgbPixel.cpp | 30 ++++++++++++++++++- .../src/services/BLEAdafruitRgbPixel.h | 2 ++ .../Peripheral/cplay_ble/cplay_ble.ino | 9 ++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp index ba103c8c..e4781d47 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp @@ -103,8 +103,36 @@ err_t BLEAdafruitRgbPixel::begin(uint8_t pin, uint8_t type) 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(20); + Data.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX); VERIFY_STATUS( Data.begin() ); + Data.setWriteCallback(pixel_data_write_cb, true); + return ERROR_NONE; } + +//--------------------------------------------------------------------+ +// Static callbacks +//--------------------------------------------------------------------+ +void BLEAdafruitRgbPixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +{ + (void) conn_hdl; + + if (len < 3) return; + + BLEAdafruitRgbPixel& svc = (BLEAdafruitRgbPixel&) chr->parentService(); + + uint16_t index; + memcpy(&index, data, 2); + uint8_t flag = data[2]; + + uint8_t* buffer = svc._neo->getPixels(); + + memcpy(buffer, data+3, len-3); + + // show flag + if ( flag & 0x01 ) + { + svc._neo->show(); + } +} diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h index 9306080f..7a9ad961 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h @@ -57,6 +57,8 @@ class BLEAdafruitRgbPixel : public BLEService Adafruit_NeoPixel_Type* _neo; err_t begin(uint8_t pin, uint8_t type); + + static void pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); }; diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index 915cb610..0dc7e804 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -11,11 +11,11 @@ All text above, and the splash screen below must be included in any redistribution *********************************************************************/ -#include +#include #include #include +#include #include -#include // BLE Service BLEDfu bledfu; // OTA DFU service @@ -32,6 +32,8 @@ BLEAdafruitTone bleTone; BLEAdafruitRgbPixel blePixel; +Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, CPLAY_NEOPIXELPIN, NEO_GRB + NEO_KHZ800); + uint16_t measure_button(uint8_t* buf, uint16_t bufsize) { uint32_t button = 0; @@ -127,7 +129,8 @@ void setup() bleTone.begin(CPLAY_BUZZER); - blePixel.begin(); + strip.begin(); + blePixel.begin(&strip); // Set up and start advertising startAdv(); From c8398e1d6e177ea16e1859ee02580becbbeb0ea7 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 19 Nov 2019 09:01:33 +0700 Subject: [PATCH 28/38] fix rgb pixel service index --- .../BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp index e4781d47..3ef86b8a 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp @@ -126,7 +126,7 @@ void BLEAdafruitRgbPixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacterist memcpy(&index, data, 2); uint8_t flag = data[2]; - uint8_t* buffer = svc._neo->getPixels(); + uint8_t* buffer = svc._neo->getPixels() + index; memcpy(buffer, data+3, len-3); From fe4e24edeac5ff5fa4c165932964e31d94bfa23a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 19 Nov 2019 10:32:57 +0700 Subject: [PATCH 29/38] adding mising library properites file --- libraries/BLEAdafruitService/library.properties | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 libraries/BLEAdafruitService/library.properties diff --git a/libraries/BLEAdafruitService/library.properties b/libraries/BLEAdafruitService/library.properties new file mode 100644 index 00000000..9fb87d11 --- /dev/null +++ b/libraries/BLEAdafruitService/library.properties @@ -0,0 +1,10 @@ +name=Adafruit BLE Services +version=1.0 +author=Adafruit +maintainer=Adafruit +sentence=Arduino library for nRF52-based Adafruit Bluefruit LE modules +paragraph=Arduino library for nRF52-based Adafruit Bluefruit LE modules +category=Communication +url=https://github.com/adafruit/Adafruit_nRF52_Arduino +architectures=* +includes=BLEAdafruitService.h From 64f94ca22f5ddb2d063510a21b280262e72abd80 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Nov 2019 00:15:07 +0700 Subject: [PATCH 30/38] noTone() if frequency = 0 --- .../src/services/BLEAdafruitTone.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp index 97268c0e..781602b5 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp @@ -72,11 +72,18 @@ void BLEAdafruitTone::tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, u uint32_t duration; } tone_data; - PRINT_LOCATION(); // invalid length if (len != sizeof(tone_data)) return; + + // extra data memcpy(&tone_data, data, len); - PRINT_LOCATION(); - tone(svc._pin, tone_data.freq, tone_data.duration); + // frequency = 0 means no tone + if (tone_data.freq == 0) + { + noTone(svc._pin); + }else + { + tone(svc._pin, tone_data.freq, tone_data.duration); + } } From 74f58f5e2c0f30a4e3820cd8d3397a5e3fb72884 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Nov 2019 00:50:17 +0700 Subject: [PATCH 31/38] change tone chars to write only --- libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp index 781602b5..45407d01 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitTone.cpp @@ -54,8 +54,8 @@ err_t BLEAdafruitTone::begin(int pin) VERIFY_STATUS( BLEService::begin() ); // Setup Characteristic - _tone.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - _tone.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _tone.setProperties(CHR_PROPS_WRITE); + _tone.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); _tone.setFixedLen(6); // uint16_t freq, uint32_t duration _tone.setWriteCallback(tone_write_cb, true); VERIFY_STATUS( _tone.begin() ); From a1e45995cf4d6e6a58331bb2252d6cf6cf19eb4a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Nov 2019 11:33:16 +0700 Subject: [PATCH 32/38] update advertising with USB PID as board ID --- .../Peripheral/cplay_ble/cplay_ble.ino | 18 +++++++++++++++--- libraries/Bluefruit52Lib/src/BLEUuid.h | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index 0dc7e804..ccc3097e 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -143,10 +143,22 @@ void startAdv(void) { // Advertising packet Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.Advertising.addTxPower(); - // Include bleuart 128-bit uuid - Bluefruit.Advertising.addService(bleuart); + // Advertising with only board ID + struct ATTR_PACKED { + uint16_t mfr_id; + + uint8_t field_len; + uint16_t field_key; + uint16_t field_value; + } mfr_adv; + + mfr_adv.mfr_id = UUID16_COMPANY_ID_ADAFRUIT; + mfr_adv.field_len = 4; + mfr_adv.field_key = 1; // board id + mfr_adv.field_value = USB_PID; + + Bluefruit.Advertising.addManufacturerData(&mfr_adv, sizeof(mfr_adv)); // Secondary Scan Response packet (optional) // Since there is no room for 'Name' in Advertising packet diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index f2ea1a5b..1193cdad 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -227,7 +227,7 @@ extern const uint8_t UUID128_CHR_ADAFRUIT_VERSION[16]; * https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers *------------------------------------------------------------------*/ #define UUID16_COMPANY_ID_APPLE 0x004C - +#define UUID16_COMPANY_ID_ADAFRUIT 0x0822 /*------------------------------------------------------------------*/ /* Unit values ( used in Characteristic Presentation Format ) From b555dd3e47e6b9b25481a2526edaf8d6b8d3ff37 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Nov 2019 11:35:42 +0700 Subject: [PATCH 33/38] rename to addressable pixel --- libraries/BLEAdafruitService/src/BLEAdafruitService.h | 3 +-- ...AdafruitRgbPixel.cpp => BLEAdafruitAddressablePixel.cpp} | 2 +- ...{BLEAdafruitRgbPixel.h => BLEAdafruitAddressablePixel.h} | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) rename libraries/BLEAdafruitService/src/services/{BLEAdafruitRgbPixel.cpp => BLEAdafruitAddressablePixel.cpp} (99%) rename libraries/BLEAdafruitService/src/services/{BLEAdafruitRgbPixel.h => BLEAdafruitAddressablePixel.h} (94%) diff --git a/libraries/BLEAdafruitService/src/BLEAdafruitService.h b/libraries/BLEAdafruitService/src/BLEAdafruitService.h index e4dd10a4..0fb56bbe 100644 --- a/libraries/BLEAdafruitService/src/BLEAdafruitService.h +++ b/libraries/BLEAdafruitService/src/BLEAdafruitService.h @@ -35,7 +35,6 @@ #include "services/BLEAdafruitLightSensor.h" #include "services/BLEAdafruitButton.h" #include "services/BLEAdafruitTone.h" -#include "services/BLEAdafruitRgbPixel.h" - +#include "services/BLEAdafruitAddressablePixel.h" #endif /* BLEADAFRUITSERVICE_H_ */ diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp similarity index 99% rename from libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp rename to libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp index 3ef86b8a..1cb8d4e8 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp @@ -26,7 +26,7 @@ #include "BLECharacteristic.h" #include "BLEService.h" -#include "BLEAdafruitRgbPixel.h" +#include "BLEAdafruitAddressablePixel.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h similarity index 94% rename from libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h rename to libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h index 7a9ad961..d9a52033 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitRgbPixel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h @@ -22,8 +22,8 @@ * THE SOFTWARE. */ -#ifndef BLEADAFRUITRGBPIXEL_H_ -#define BLEADAFRUITRGBPIXEL_H_ +#ifndef BLEADAFRUITADDRESSABLEPIXEL_H_ +#define BLEADAFRUITADDRESSABLEPIXEL_H_ // use Adafruit_CPlay_NeoPixel instead of Adafruit_NeoPixel #define USE_CPLAY_NEOPIXEL 0 @@ -63,4 +63,4 @@ class BLEAdafruitRgbPixel : public BLEService -#endif /* BLEADAFRUITRGBPIXEL_H_ */ +#endif /* BLEADAFRUITADDRESSABLEPIXEL_H_ */ From 5ec05e9d84cf228f3780c3338b05dcc0752c7ad8 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Nov 2019 11:37:12 +0700 Subject: [PATCH 34/38] more rename --- .../services/BLEAdafruitAddressablePixel.cpp | 18 +++++++++--------- .../src/services/BLEAdafruitAddressablePixel.h | 4 ++-- .../Peripheral/cplay_ble/cplay_ble.ino | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp index 1cb8d4e8..0ecf0e98 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp @@ -42,44 +42,44 @@ * - Count */ -const uint8_t BLEAdafruitRgbPixel::UUID128_SERVICE[16] = +const uint8_t BLEAdafruitAddressablePixel::UUID128_SERVICE[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x09, 0xAF, 0xAD }; -const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_PIN[16] = +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_PIN[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x09, 0xAF, 0xAD }; -const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_TYPE[16] = +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_TYPE[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, 0xA8, 0x42, 0x32, 0xC3, 0x02, 0x09, 0xAF, 0xAD }; -const uint8_t BLEAdafruitRgbPixel::UUID128_CHR_DATA[16] = +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_DATA[16] = { 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, 0xA8, 0x42, 0x32, 0xC3, 0x03, 0x09, 0xAF, 0xAD }; // Constructor -BLEAdafruitRgbPixel::BLEAdafruitRgbPixel(void) +BLEAdafruitAddressablePixel::BLEAdafruitAddressablePixel(void) : BLEService(UUID128_SERVICE), Pin(UUID128_CHR_PIN), Type(UUID128_CHR_TYPE), Data(UUID128_CHR_DATA) { _neo = NULL; } -err_t BLEAdafruitRgbPixel::begin (Adafruit_NeoPixel_Type* neo_pixel) +err_t BLEAdafruitAddressablePixel::begin (Adafruit_NeoPixel_Type* neo_pixel) { _neo = neo_pixel; return begin((uint8_t) _neo->getPin(), 0); } -err_t BLEAdafruitRgbPixel::begin(uint8_t pin, uint8_t type) +err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); @@ -114,13 +114,13 @@ err_t BLEAdafruitRgbPixel::begin(uint8_t pin, uint8_t type) //--------------------------------------------------------------------+ // Static callbacks //--------------------------------------------------------------------+ -void BLEAdafruitRgbPixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) +void BLEAdafruitAddressablePixel::pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) { (void) conn_hdl; if (len < 3) return; - BLEAdafruitRgbPixel& svc = (BLEAdafruitRgbPixel&) chr->parentService(); + BLEAdafruitAddressablePixel& svc = (BLEAdafruitAddressablePixel&) chr->parentService(); uint16_t index; memcpy(&index, data, 2); diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h index d9a52033..a9f22dd3 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h @@ -36,7 +36,7 @@ #define Adafruit_NeoPixel_Type Adafruit_NeoPixel #endif -class BLEAdafruitRgbPixel : public BLEService +class BLEAdafruitAddressablePixel : public BLEService { public: static const uint8_t UUID128_SERVICE[16]; @@ -50,7 +50,7 @@ class BLEAdafruitRgbPixel : public BLEService BLECharacteristic Type; BLECharacteristic Data; - BLEAdafruitRgbPixel(void); + BLEAdafruitAddressablePixel(void); virtual err_t begin(Adafruit_NeoPixel_Type* neo); private: diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index ccc3097e..e552c10d 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -30,7 +30,7 @@ BLEAdafruitLightSensor bleLight; BLEAdafruitButton bleButton; BLEAdafruitTone bleTone; -BLEAdafruitRgbPixel blePixel; +BLEAdafruitAddressablePixel blePixel; Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, CPLAY_NEOPIXELPIN, NEO_GRB + NEO_KHZ800); From 5171954d4fa708c4e9d4fc1e1dba12d1aa72ab44 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Nov 2019 12:00:29 +0700 Subject: [PATCH 35/38] added Pixel Buffer Size --- .../services/BLEAdafruitAddressablePixel.cpp | 53 ++++++++++++------- .../services/BLEAdafruitAddressablePixel.h | 29 ++++------ 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp index 0ecf0e98..4501f546 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.cpp @@ -66,47 +66,62 @@ const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_DATA[16] = 0xA8, 0x42, 0x32, 0xC3, 0x03, 0x09, 0xAF, 0xAD }; +const uint8_t BLEAdafruitAddressablePixel::UUID128_CHR_BUFSIZE[16] = +{ + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, + 0xA8, 0x42, 0x32, 0xC3, 0x04, 0x09, 0xAF, 0xAD +}; + // Constructor BLEAdafruitAddressablePixel::BLEAdafruitAddressablePixel(void) - : BLEService(UUID128_SERVICE), Pin(UUID128_CHR_PIN), Type(UUID128_CHR_TYPE), Data(UUID128_CHR_DATA) + : BLEService(UUID128_SERVICE), _pin(UUID128_CHR_PIN), _type(UUID128_CHR_TYPE), + _data(UUID128_CHR_DATA), _bufsize(UUID128_CHR_BUFSIZE) { _neo = NULL; } -err_t BLEAdafruitAddressablePixel::begin (Adafruit_NeoPixel_Type* neo_pixel) +err_t BLEAdafruitAddressablePixel::begin(Adafruit_NeoPixel* neo_pixel) { _neo = neo_pixel; - return begin((uint8_t) _neo->getPin(), 0); + uint16_t bufsize = _neo->numPixels()*3; // TODO actual bufsize for 3 RGB or 4 RGBW later + return begin((uint8_t) _neo->getPin(), 0, bufsize); // TODO dotstart support } -err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type) +err_t BLEAdafruitAddressablePixel::begin(uint8_t pin, uint8_t type, uint16_t bufsize) { // Invoke base class begin() VERIFY_STATUS( BLEService::begin() ); // Add Characteristic - Pin.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Pin.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Pin.setFixedLen(1); - VERIFY_STATUS( Pin.begin() ); - Pin.write8(pin); + _pin.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _pin.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _pin.setFixedLen(1); + VERIFY_STATUS( _pin.begin() ); + _pin.write8(pin); // Add Characteristic - Type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - Type.setPermission(SECMODE_OPEN, SECMODE_OPEN); - Type.setFixedLen(1); - VERIFY_STATUS( Type.begin() ); - Type.write8(type); + _type.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _type.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _type.setFixedLen(1); + VERIFY_STATUS( _type.begin() ); + _type.write8(type); // Add Characteristic - Data.setProperties(CHR_PROPS_WRITE); - Data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN); + _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); - VERIFY_STATUS( Data.begin() ); + _data.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX); + VERIFY_STATUS( _data.begin() ); - Data.setWriteCallback(pixel_data_write_cb, true); + // Add Characteristic + _bufsize.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _bufsize.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _bufsize.setFixedLen(2); + VERIFY_STATUS( _bufsize.begin() ); + _bufsize.write16(bufsize); + + _data.setWriteCallback(pixel_data_write_cb, true); return ERROR_NONE; } diff --git a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h index a9f22dd3..ec666933 100644 --- a/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h +++ b/libraries/BLEAdafruitService/src/services/BLEAdafruitAddressablePixel.h @@ -25,38 +25,29 @@ #ifndef BLEADAFRUITADDRESSABLEPIXEL_H_ #define BLEADAFRUITADDRESSABLEPIXEL_H_ -// use Adafruit_CPlay_NeoPixel instead of Adafruit_NeoPixel -#define USE_CPLAY_NEOPIXEL 0 - -#if USE_CPLAY_NEOPIXEL - #include "Adafruit_CPlay_NeoPixel.h" - #define Adafruit_NeoPixel_Type Adafruit_CPlay_NeoPixel -#else - #include "Adafruit_NeoPixel.h" - #define Adafruit_NeoPixel_Type Adafruit_NeoPixel -#endif +#include "Adafruit_NeoPixel.h" class BLEAdafruitAddressablePixel : public BLEService { public: static const uint8_t UUID128_SERVICE[16]; static const uint8_t UUID128_CHR_PIN[16]; -// static const uint8_t UUID128_CHR_COUNT[16]; static const uint8_t UUID128_CHR_TYPE[16]; static const uint8_t UUID128_CHR_DATA[16]; - -// BLECharacteristic Count; - BLECharacteristic Pin; - BLECharacteristic Type; - BLECharacteristic Data; + static const uint8_t UUID128_CHR_BUFSIZE[16]; BLEAdafruitAddressablePixel(void); - virtual err_t begin(Adafruit_NeoPixel_Type* neo); + virtual err_t begin(Adafruit_NeoPixel* neo); private: - Adafruit_NeoPixel_Type* _neo; + BLECharacteristic _pin; + BLECharacteristic _type; + BLECharacteristic _data; + BLECharacteristic _bufsize; - err_t begin(uint8_t pin, uint8_t type); + Adafruit_NeoPixel* _neo; + + err_t begin(uint8_t pin, uint8_t type, uint16_t bufsize); static void pixel_data_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); }; From 94b9a37d76ec0eb45278a1838d458408096c683c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 25 Dec 2019 13:52:56 +0700 Subject: [PATCH 36/38] build cplay_ble example with cplay bluefruit only --- .../examples/Peripheral/cplay_ble/.build.cplaynrf52840 | 0 .../Bluefruit52Lib/examples/Peripheral/cplay_ble/.skip | 0 .../examples/Peripheral/cplay_ble/cplay_ble.ino | 2 +- tools/build_all.py | 6 +++++- 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.build.cplaynrf52840 create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.skip diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.build.cplaynrf52840 b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.build.cplaynrf52840 new file mode 100644 index 00000000..e69de29b diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.skip b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/.skip new file mode 100644 index 00000000..e69de29b diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino index e552c10d..268704b3 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino @@ -11,11 +11,11 @@ All text above, and the splash screen below must be included in any redistribution *********************************************************************/ -#include #include #include #include #include +#include // BLE Service BLEDfu bledfu; // OTA DFU service diff --git a/tools/build_all.py b/tools/build_all.py index 38f1a293..4f6178a4 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -67,7 +67,11 @@ def build_examples(variant): for sketch in glob.iglob('libraries/**/*.ino', recursive=True): start_time = time.monotonic() - if os.path.exists(os.path.dirname(sketch) + '/.skip') or os.path.exists(os.path.dirname(sketch) + '/.skip.' + variant): + # skip if example contains: ".skip" or ".skip.variant" + # however ".build.variant" file can overwrite ".skip", used to build a specific variant only + sketchdir = os.path.dirname(sketch) + if ( (os.path.exists(sketchdir + '/.skip') or os.path.exists(sketchdir + '/.skip.' + variant)) and + not os.path.exists(sketchdir + '/.build.' + variant)): success = "skipped" else: # TODO - preferably, would have STDERR show up in **both** STDOUT and STDERR. From 38bace90e82871115d03b24a346d62e34a28d017 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 27 Dec 2019 11:49:17 +0700 Subject: [PATCH 37/38] try to fix travis remove IDE-included Adafruit_Circuit_Playground --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index eb00e4a9..3ddb12bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,9 +46,10 @@ install: # Filter only mDNS / Bonjour traffic - sudo iptables --insert INPUT --jump DROP --protocol udp --dport 5353 -m comment --comment "silently drop all 5353/udp input" - sudo iptables --insert INPUT --jump DROP --destination 224.0.0.251 -m comment --comment "silently drop all mDNS ipv4 broadcast" - # Install the nRF52 support files for arduino + # Install the nRF52 support files for arduino, remove IDE-included Adafruit_Circuit_Playground - pip3 install --user adafruit-nrfutil - umake electronics arduino $HOME/arduino_ide + - rm -rf $HOME/arduino_ide/libraries/Adafruit_Circuit_Playground - export PATH=$HOME/arduino_ide:$PATH - arduino --pref "boardsmanager.additional.urls=https://adafruit.github.io/arduino-board-index/package_adafruit_index.json" --save-prefs # Install BSP for tools, then remove and create symlink the git code From 8d7a2b1e65dd460788de068c3fe73e334d220687 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 27 Dec 2019 12:52:31 +0700 Subject: [PATCH 38/38] travis build --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3ddb12bb..2a3fcb26 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,10 +46,9 @@ install: # Filter only mDNS / Bonjour traffic - sudo iptables --insert INPUT --jump DROP --protocol udp --dport 5353 -m comment --comment "silently drop all 5353/udp input" - sudo iptables --insert INPUT --jump DROP --destination 224.0.0.251 -m comment --comment "silently drop all mDNS ipv4 broadcast" - # Install the nRF52 support files for arduino, remove IDE-included Adafruit_Circuit_Playground + # Install the nRF52 support files for arduino - pip3 install --user adafruit-nrfutil - umake electronics arduino $HOME/arduino_ide - - rm -rf $HOME/arduino_ide/libraries/Adafruit_Circuit_Playground - export PATH=$HOME/arduino_ide:$PATH - arduino --pref "boardsmanager.additional.urls=https://adafruit.github.io/arduino-board-index/package_adafruit_index.json" --save-prefs # Install BSP for tools, then remove and create symlink the git code @@ -57,7 +56,7 @@ install: - BSP_VERSION=`eval ls $BSP_PATH` - rm -r $BSP_PATH/* - ln -s $TRAVIS_BUILD_DIR $BSP_PATH/$BSP_VERSION - - arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library", "Adafruit Circuit Playground" + - arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library","Adafruit Circuit Playground" before_script: