|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "bluefruit.h" |
| 26 | + |
| 27 | +//--------------------------------------------------------------------+ |
| 28 | +// MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 29 | +//--------------------------------------------------------------------+ |
| 30 | + |
| 31 | +const uint8_t BLEAdafruitTone::UUID128_SERVICE[16] = |
| 32 | +{ |
| 33 | + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, |
| 34 | + 0xA8, 0x42, 0x32, 0xC3, 0x00, 0x0C, 0xAF, 0xAD |
| 35 | +}; |
| 36 | + |
| 37 | +const uint8_t BLEAdafruitTone::UUID128_CHR_TONE[16] = |
| 38 | +{ |
| 39 | + 0xB8, 0x6c, 0x75, 0x05, 0xE9, 0x25, 0xBD, 0x93, |
| 40 | + 0xA8, 0x42, 0x32, 0xC3, 0x01, 0x0C, 0xAF, 0xAD |
| 41 | +}; |
| 42 | + |
| 43 | +BLEAdafruitTone::BLEAdafruitTone(void) |
| 44 | + : BLEService(UUID128_SERVICE), _tone(UUID128_CHR_TONE) |
| 45 | +{ |
| 46 | + _pin = -1; |
| 47 | +} |
| 48 | + |
| 49 | +err_t BLEAdafruitTone::begin(int pin) |
| 50 | +{ |
| 51 | + _pin = pin; |
| 52 | + |
| 53 | + // Invoke base class begin() |
| 54 | + VERIFY_STATUS( BLEService::begin() ); |
| 55 | + |
| 56 | + // Setup Characteristic |
| 57 | + _tone.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); |
| 58 | + _tone.setPermission(SECMODE_OPEN, SECMODE_OPEN); |
| 59 | + _tone.setFixedLen(6); // uint16_t freq, uint32_t duration |
| 60 | + _tone.setWriteCallback(tone_write_cb, true); |
| 61 | + VERIFY_STATUS( _tone.begin() ); |
| 62 | + |
| 63 | + return ERROR_NONE; |
| 64 | +} |
| 65 | + |
| 66 | +void BLEAdafruitTone::tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len) |
| 67 | +{ |
| 68 | + BLEAdafruitTone& svc = (BLEAdafruitTone&) chr->parentService(); |
| 69 | + |
| 70 | + struct ATTR_PACKED { |
| 71 | + uint16_t freq; |
| 72 | + uint32_t duration; |
| 73 | + } tone_data; |
| 74 | + |
| 75 | + PRINT_LOCATION(); |
| 76 | + // invalid length |
| 77 | + if (len != sizeof(tone_data)) return; |
| 78 | + memcpy(&tone_data, data, len); |
| 79 | + |
| 80 | + PRINT_LOCATION(); |
| 81 | + tone(svc._pin, tone_data.freq, tone_data.duration); |
| 82 | +} |
0 commit comments