From 6f0e361b8fa888344ac6f75fc36204b6c874b481 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Mar 2017 13:38:22 +0700 Subject: [PATCH] adding BLECentralCharacteritic, add hid_camerashutter sketch --- .../hid_camerashutter/hid_camerashutter.ino | 117 ++++++++++++++++++ .../Peripheral/hid_keyboard/hid_keyboard.ino | 23 ++-- .../Peripheral/hid_mouse/hid_mouse.ino | 6 +- libraries/Bluefruit52Lib/keywords.txt | 10 ++ libraries/Bluefruit52Lib/library.properties | 2 +- libraries/Bluefruit52Lib/src/BLECentral.h | 1 + .../src/BLECentralCharacteristic.cpp | 38 ++++++ .../src/BLECentralCharacteristic.h | 52 ++++++++ .../Bluefruit52Lib/src/BLECharacteristic.cpp | 2 +- .../Bluefruit52Lib/src/BLECharacteristic.h | 21 ++-- libraries/Bluefruit52Lib/src/bluefruit.h | 5 +- platform.txt | 2 +- 12 files changed, 242 insertions(+), 37 deletions(-) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino create mode 100644 libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp create mode 100644 libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino new file mode 100644 index 00000000..0438e9e7 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino @@ -0,0 +1,117 @@ +/********************************************************************* + 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 +*********************************************************************/ + +/* + * Sketch use HID Consumer Key API to send Volume Down key when + * PIN_SHUTTER is grounded. Which cause mobile to capture photo + * when you are in Camera App + */ +#include + +BLEDis bledis; +BLEHidAdafruit blehid; + +#define PIN_SHUTTER 27 + +void setup() +{ + pinMode(PIN_SHUTTER, INPUT_PULLUP); + + Serial.begin(115200); + + Serial.println("Bluefruit52 HID Camera Shutter Example"); + Serial.println("--------------------------------------"); + + Serial.println(); + Serial.println("Go to your phone's Bluetooth settings to pair your device"); + Serial.println("then open an Camera application"); + + Serial.println(); + Serial.printf("Wire your Pin %d to GND to capture picture\n", PIN_SHUTTER); + Serial.println(); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + + // Configure and Start Device Information Service + bledis.setManufacturer("Adafruit Industries"); + bledis.setModel("Bluefruit Feather 52"); + bledis.begin(); + + /* Start BLE HID + * Note: Apple requires BLE device must have min connection interval >= 20m + * ( The smaller the connection interval the faster we could send data). + * However for HID and MIDI device, Apple could accept min connection interval + * up to 11.25 ms. Therefore BLEHidAdafruit::begin() will try to set the min and max + * connection interval to 11.25 ms and 15 ms respectively for best performance. + */ + blehid.begin(); + + /* Set connection interval (min, max) to your perferred value. + * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms + * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms + */ + /* Bluefruit.setConnInterval(9, 12); */ + + // Setup Advertising Packet + setupAdv(); + + // Start Advertising + Bluefruit.Advertising.start(); +} + +void setupAdv(void) +{ + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD); + + // Include BLE HID service + Bluefruit.Advertising.addService(blehid); + + // There is enough room for the dev name in the advertising packet + Bluefruit.Advertising.addName(); +} + +void loop() +{ + // Connected and Bonded/Paired + if ( Bluefruit.connected() && Bluefruit.connBonded() ) + { + // Pin is wired to GND + if ( digitalRead(PIN_SHUTTER) == 0 ) + { + // Turn on LED Red when start sending + digitalWrite(LED_RED, 1); + + // Send Volumn Down key press + // Check BLEHidGerneric.h for list of defined consumer usage code + blehid.consumerKeyPress(HID_USAGE_CONSUMER_VOLUME_DECREMENT); + + // Delay a bit between repots + delay(10); + + // Send key release + blehid.consumerKeyRelease(); + + // Turn off LED Red + digitalWrite(LED_RED, 0); + + // Delay to avoid constant capturing + delay(500); + } + } +} + diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino index 383a74d7..29be53e4 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino @@ -14,7 +14,6 @@ #include BLEDis bledis; -BLEUart bleuart; BLEHidAdafruit blehid; bool hasKeyPressed = false; @@ -24,18 +23,24 @@ void setup() Serial.begin(115200); Serial.println("Bluefruit52 HID Keyboard Example"); + Serial.println("--------------------------------"); + + Serial.println(); + Serial.println("Go to your phone's Bluetooth settings to pair your device"); + Serial.println("then open an application that accepts keyboard input"); + + Serial.println(); + Serial.println("Enter the character(s) to send:"); + Serial.println(); Bluefruit.begin(); Bluefruit.setName("Bluefruit52"); // Configure and Start Device Information Service bledis.setManufacturer("Adafruit Industries"); - bledis.setModel("Bluefruit Feather52"); + bledis.setModel("Bluefruit Feather 52"); bledis.begin(); - // Configure and Start BLE Uart Service - bleuart.begin(); - /* Start BLE HID * Note: Apple requires BLE device must have min connection interval >= 20m * ( The smaller the connection interval the faster we could send data). @@ -56,14 +61,6 @@ void setup() // Start Advertising Bluefruit.Advertising.start(); - - Serial.println(); - Serial.println("Go to your phone's Bluetooth settings to pair your device"); - Serial.println("then open an application that accepts keyboard input"); - - Serial.println(); - Serial.println("Enter the character(s) to send:"); - Serial.println(); } void setupAdv(void) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino index faf8be01..157118f5 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino @@ -14,7 +14,6 @@ #include BLEDis bledis; -BLEUart bleuart; BLEHidAdafruit blehid; #define MOVE_STEP 10 @@ -41,12 +40,9 @@ void setup() // Configure and Start Device Information Service bledis.setManufacturer("Adafruit Industries"); - bledis.setModel("Bluefruit Feather52"); + bledis.setModel("Bluefruit Feather 52"); bledis.begin(); - // Configure and Start BLE Uart Service - bleuart.begin(); - // BLE HID blehid.begin(); diff --git a/libraries/Bluefruit52Lib/keywords.txt b/libraries/Bluefruit52Lib/keywords.txt index d6c32fc4..b403975c 100644 --- a/libraries/Bluefruit52Lib/keywords.txt +++ b/libraries/Bluefruit52Lib/keywords.txt @@ -186,6 +186,16 @@ notifyEnabled KEYWORD2 setRxCallback KEYWORD2 notifyEnabled KEYWORD2 +####################################### +# Constants (LITERAL1) +####################################### +LED_RED LITERAL1 +LED_BLUE LITERAL1 + +LED_BUILTIN LITERAL1 +LED_CONN LITERAL1 + + ####################################### # Constants (LITERAL1) ####################################### diff --git a/libraries/Bluefruit52Lib/library.properties b/libraries/Bluefruit52Lib/library.properties index 2b40ca42..fb62df6b 100644 --- a/libraries/Bluefruit52Lib/library.properties +++ b/libraries/Bluefruit52Lib/library.properties @@ -1,5 +1,5 @@ name=Adafruit BLE Libraries for Bluefruit52 -version=0.2.1 +version=0.5.5 author=Adafruit maintainer=Adafruit sentence=Arduino library for nRF52-based Adafruit Bluefruit LE modules diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index d63eb096..fff76995 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -41,6 +41,7 @@ #include "BLEUuid.h" #include "BLECharacteristic.h" +#include "BLECentralCharacteristic.h" #include "BLEService.h" class AdafruitBluefruit; diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp new file mode 100644 index 00000000..13f0612c --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp @@ -0,0 +1,38 @@ +/**************************************************************************/ +/*! + @file BLECentralCharacteristic.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "bluefruit.h" + diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h new file mode 100644 index 00000000..43ccc150 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h @@ -0,0 +1,52 @@ +/**************************************************************************/ +/*! + @file BLECentralCharacteristic.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef BLECHARACTERISTICCENTRAL_H_ +#define BLECHARACTERISTICCENTRAL_H_ + +#include "bluefruit_common.h" +#include "BLEUuid.h" +#include "BLECharacteristic.h" + +class BLECharacteristicCentral +{ + public: + + private: + ble_gattc_char_t _chr; + +}; + +#endif /* BLECHARACTERISTICCENTRAL_H_ */ diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index 46104893..5dbaef60 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -269,7 +269,7 @@ err_t BLECharacteristic::begin(void) } // Currently Only register to Bluefruit when having callback support - // And The Characteristic must not be temporary memory aka local variable + // And The Characteristic must not be temporary memory i.e local variable if ( !_is_temp && (_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb) ) { diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index f9ecc714..5b411e53 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -115,15 +115,12 @@ class BLECharacteristic BLECharacteristic(void); BLECharacteristic(BLEUuid bleuuid); - BLEService& parentService() - { - return *_service; - } + BLEService& parentService() { return *_service; } - void setUuid(BLEUuid bleuuid); void setTempMemory(void); - // Configure + /*------------- Configure -------------*/ + void setUuid(BLEUuid bleuuid); void setProperties(uint8_t prop); void setPermission(BleSecurityMode read_perm, BleSecurityMode write_perm); void setMaxLen(uint16_t max_len); @@ -131,10 +128,9 @@ class BLECharacteristic void setStringDescriptor(const char* descriptor); // aka user descriptor void setReportRefDescriptor(uint8_t id, uint8_t type); - // Callback + /*------------- Callback -------------*/ void setWriteCallback(write_cb_t fp); void setCccdWriteCallback(write_cccd_cb_t fp); - void setReadAuthorizeCallback(read_authorize_cb_t fp); void setWriteAuthorizeCallbak(write_authorize_cb_t fp); @@ -142,7 +138,7 @@ class BLECharacteristic ble_gatts_char_handles_t handles(void); - // Write + /*------------- Write -------------*/ err_t write(const void* data, int len, uint16_t offset = 0); err_t write(const char * str); @@ -151,26 +147,23 @@ class BLECharacteristic err_t write(uint16_t num); err_t write(uint8_t num); - // Read + /*------------- Read -------------*/ uint16_t read(void* buffer, int bufsize, uint16_t offset = 0); uint16_t read(uint32_t* num); uint16_t read(uint16_t* num); uint16_t read(uint8_t* num); - // Notify + /*------------- Notify -------------*/ bool notifyEnabled(void); - err_t notify(const void* data, int len, uint16_t offset); err_t notify(const void* data, int len); err_t notify(const char * str); - err_t notify(int num); err_t notify(uint32_t num); err_t notify(uint16_t num); err_t notify(uint8_t num); - friend class AdafruitBluefruit; }; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 960f7080..df5e15ae 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -41,7 +41,9 @@ #include "BLEAdvertising.h" #include "BLECharacteristic.h" #include "BLEService.h" + #include "BLECentral.h" +#include "BLECentralCharacteristic.h" // Services #include "services/BLEDis.h" @@ -146,9 +148,8 @@ class AdafruitBluefruit uint16_t _conn_hdl; bool _bonded; - // TODO move to bonding place -public: // temporary +public: // TODO temporary for bledfu to load bonding data struct { // Keys diff --git a/platform.txt b/platform.txt index bddd8d31..5e3094e5 100644 --- a/platform.txt +++ b/platform.txt @@ -17,7 +17,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA name=Adafruit Boards -version=0.5.1 +version=0.5.5 # Compile variables # -----------------