From 8189353d320aebd595085b0fe29058ca4db824d0 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Nov 2019 22:52:40 +0700 Subject: [PATCH] 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_ */