Skip to content

Commit 8189353

Browse files
committed
added tone service
1 parent b80abcb commit 8189353

File tree

5 files changed

+135
-129
lines changed

5 files changed

+135
-129
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/cplay_ble.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ BLEAdafruitTemperature bleTemp;
2727
BLEAdafruitAccel bleAccel;
2828
BLEAdafruitLightSensor bleLight;
2929
BLEAdafruitButton bleButton;
30+
BLEAdafruitTone bleTone;
3031

3132
uint16_t measure_button(uint8_t* buf, uint16_t bufsize)
3233
{
@@ -121,6 +122,8 @@ void setup()
121122
bleButton.setMeasureCallback(measure_button);
122123
bleButton.setPeriod(0); // only notify if there is changes with buttons
123124

125+
bleTone.begin(CPLAY_BUZZER);
126+
124127
// Set up and start advertising
125128
startAdv();
126129

libraries/Bluefruit52Lib/examples/Peripheral/cplay_ble/services.txt

Lines changed: 0 additions & 129 deletions
This file was deleted.

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "services/BLEAdafruitAccel.h"
6666
#include "services/BLEAdafruitLightSensor.h"
6767
#include "services/BLEAdafruitButton.h"
68+
#include "services/BLEAdafruitTone.h"
6869

6970
#include "clients/BLEAncs.h"
7071
#include "clients/BLEClientUart.h"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#ifndef BLEADAFRUITTONE_H_
26+
#define BLEADAFRUITTONE_H_
27+
28+
#include "bluefruit_common.h"
29+
30+
#include "BLECharacteristic.h"
31+
#include "BLEService.h"
32+
33+
class BLEAdafruitTone : public BLEService
34+
{
35+
public:
36+
static const uint8_t UUID128_SERVICE[16];
37+
static const uint8_t UUID128_CHR_TONE[16];
38+
39+
BLEAdafruitTone(void);
40+
virtual err_t begin(int pin);
41+
42+
private:
43+
int _pin;
44+
BLECharacteristic _tone;
45+
46+
static void tone_write_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len);
47+
};
48+
49+
#endif /* BLEADAFRUITTONE_H_ */

0 commit comments

Comments
 (0)