From ca4bbc5bfea19961c9aab17a24dafa2345d0ae06 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 7 Nov 2019 01:30:18 +0700 Subject: [PATCH] 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() );