Skip to content

Commit ca4bbc5

Browse files
committed
adding Accel service
1 parent bf4ac6b commit ca4bbc5

File tree

7 files changed

+315
-4
lines changed

7 files changed

+315
-4
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*********************************************************************
2+
This is an example for our nRF52 based Bluefruit LE modules
3+
4+
Pick one up today in the adafruit shop!
5+
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
All text above, and the splash screen below must be included in
12+
any redistribution
13+
*********************************************************************/
14+
#include <bluefruit.h>
15+
#include <Adafruit_LittleFS.h>
16+
#include <InternalFileSystem.h>
17+
#include <Adafruit_CircuitPlayground.h>
18+
19+
// BLE Service
20+
BLEDfu bledfu; // OTA DFU service
21+
BLEDis bledis; // device information
22+
BLEUart bleuart; // uart over ble
23+
BLEBas blebas; // battery
24+
25+
// Base UUID : ADAF0000-C332-42A8-93BD-25E905756CB8
26+
27+
/* CPB Temperature
28+
* - Service: ADAF-0001-C332-42A8-93BD-25E905756CB8
29+
* - Temperature Celsius : 0x2A6E
30+
* - Measurement Interval : 0x2A21
31+
*/
32+
BLEAdafruitTemperature bleTemp;
33+
34+
/* Adafruit NeoPixel Service
35+
* - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8
36+
* - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8
37+
* - Type : ADAF-0004-C332-42A8-93BD-25E905756CB8
38+
* - Data : ADAF-0005-C332-42A8-93BD-25E905756CB8
39+
*/
40+
BLEAdafruitNeopixel bleNeopixel;
41+
42+
/* Adafruit Accelerometer Service
43+
* using micro:bit Accelerometer Service definition
44+
* https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html
45+
*
46+
* - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8
47+
* - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8
48+
* - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8
49+
*/
50+
BLEAdafruitAccel bleAccel;
51+
52+
void setup()
53+
{
54+
Serial.begin(115200);
55+
while ( !Serial ) delay(10); // for nrf52840 with native usb
56+
57+
Serial.println("Bluefruit52 BLEUART Example");
58+
Serial.println("---------------------------\n");
59+
60+
CircuitPlayground.begin();
61+
62+
// Setup the BLE LED to be enabled on CONNECT
63+
// Note: This is actually the default behaviour, but provided
64+
// here in case you want to control this LED manually via PIN 19
65+
Bluefruit.autoConnLed(true);
66+
67+
// Config the peripheral connection with maximum bandwidth
68+
// more SRAM required by SoftDevice
69+
// Note: All config***() function must be called before begin()
70+
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
71+
72+
Bluefruit.begin();
73+
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
74+
Bluefruit.setName("Bluefruit52");
75+
//Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
76+
Bluefruit.Periph.setConnectCallback(connect_callback);
77+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
78+
79+
// To be consistent OTA DFU should be added first if it exists
80+
bledfu.begin();
81+
82+
// Configure and Start Device Information Service
83+
bledis.setManufacturer("Adafruit Industries");
84+
bledis.setModel("Bluefruit Feather52");
85+
bledis.begin();
86+
87+
// Configure and Start BLE Uart Service
88+
bleuart.begin();
89+
90+
// Start BLE Battery Service
91+
blebas.begin();
92+
blebas.write(100);
93+
94+
// Adafruit service
95+
bleTemp.begin();
96+
bleNeopixel.begin();
97+
bleAccel.begin();
98+
99+
100+
// Set up and start advertising
101+
startAdv();
102+
103+
Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
104+
Serial.println("Once connected, enter character(s) that you wish to send");
105+
}
106+
107+
void startAdv(void)
108+
{
109+
// Advertising packet
110+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
111+
Bluefruit.Advertising.addTxPower();
112+
113+
// Include bleuart 128-bit uuid
114+
Bluefruit.Advertising.addService(bleuart);
115+
116+
// Secondary Scan Response packet (optional)
117+
// Since there is no room for 'Name' in Advertising packet
118+
Bluefruit.ScanResponse.addName();
119+
120+
/* Start Advertising
121+
* - Enable auto advertising if disconnected
122+
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
123+
* - Timeout for fast mode is 30 seconds
124+
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
125+
*
126+
* For recommended advertising interval
127+
* https://developer.apple.com/library/content/qa/qa1931/_index.html
128+
*/
129+
Bluefruit.Advertising.restartOnDisconnect(true);
130+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
131+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
132+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
133+
}
134+
135+
void loop()
136+
{
137+
if ( !Bluefruit.connected() ) return;
138+
if ( !bleTemp.Temperature.notifyEnabled() ) return;
139+
140+
int16_t temp = (int16_t) (100*CircuitPlayground.temperature());
141+
bleTemp.Temperature.notify16((uint16_t) temp);
142+
143+
delay(1000*bleTemp.MeasurementInterval.read16());
144+
}
145+
146+
// callback invoked when central connects
147+
void connect_callback(uint16_t conn_handle)
148+
{
149+
// Get the reference to current connection
150+
BLEConnection* connection = Bluefruit.Connection(conn_handle);
151+
152+
char central_name[32] = { 0 };
153+
connection->getPeerName(central_name, sizeof(central_name));
154+
155+
Serial.print("Connected to ");
156+
Serial.println(central_name);
157+
}
158+
159+
/**
160+
* Callback invoked when a connection is dropped
161+
* @param conn_handle connection where this event happens
162+
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
163+
*/
164+
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
165+
{
166+
(void) conn_handle;
167+
(void) reason;
168+
169+
Serial.println();
170+
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
171+
}

libraries/Bluefruit52Lib/src/BLEGatt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
232232
*------------------------------------------------------------------*/
233233
bool BLEGatt::_addCharacteristic(BLECharacteristic* chr)
234234
{
235-
if ( _server.chr_count == CFG_GATT_MAX_SERVER_CHARS ) return false;
235+
VERIFY( _server.chr_count < CFG_GATT_MAX_SERVER_CHARS );
236236
_server.chr_list[ _server.chr_count++ ] = chr;
237237

238238
return true;

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ static void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info)
122122
LOG_LV1("SD Err", "assert at %s : %d", assert_info->p_file_name, assert_info->line_num);
123123
}
124124

125-
while(1) { }
125+
while(1)
126+
{
127+
yield();
128+
}
126129
#endif
127130
}
128131

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
#include "services/BLEAdafruitTemperature.h"
6464
#include "services/BLEAdafruitNeopixel.h"
65+
#include "services/BLEAdafruitAccel.h"
6566

6667
#include "clients/BLEAncs.h"
6768
#include "clients/BLEClientUart.h"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
32+
/* Adafruit Accelerometer Service
33+
* using micro:bit Accelerometer Service definition
34+
* https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html
35+
*
36+
* - Service: E95D-0753-251D-470A-A062-FA1922DFA9A8
37+
* - Data : E95D-CA4B-251D-470A-A062-FA1922DFA9A8
38+
* - Period : E95D-FB24-251D-470A-A062-FA1922DFA9A8
39+
*/
40+
const uint8_t BLEAdafruitAccel::UUID128_SERVICE[16] =
41+
{
42+
0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0,
43+
0x0A, 0x47, 0x1D, 0x25, 0x53, 0x07, 0x5D, 0xE9
44+
};
45+
46+
const uint8_t BLEAdafruitAccel::UUID128_CHR_DATA[16] =
47+
{
48+
0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0,
49+
0x0A, 0x47, 0x1D, 0x25, 0x4B, 0xCA, 0x5D, 0xE9
50+
};
51+
52+
const uint8_t BLEAdafruitAccel::UUID128_CHR_PERIOD[16] =
53+
{
54+
0xA8, 0xA9, 0xDF, 0x22, 0x19, 0xFA, 0x62, 0xA0,
55+
0x0A, 0x47, 0x1D, 0x25, 0x24, 0xFB, 0x5D, 0xE9
56+
};
57+
58+
// Constructor
59+
BLEAdafruitAccel::BLEAdafruitAccel(void)
60+
: BLEService(UUID128_SERVICE), Data(UUID128_CHR_DATA), Period(UUID128_CHR_PERIOD)
61+
{
62+
63+
}
64+
65+
err_t BLEAdafruitAccel::begin (void)
66+
{
67+
// Invoke base class begin()
68+
VERIFY_STATUS( BLEService::begin() );
69+
70+
// Add Characteristic
71+
Data.setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
72+
Data.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
73+
Data.setFixedLen(6);
74+
Data.setUserDescriptor("Data");
75+
VERIFY_STATUS( Data.begin() );
76+
77+
// Add Characteristic
78+
Period.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE);
79+
Period.setPermission(SECMODE_OPEN, SECMODE_OPEN);
80+
Period.setFixedLen(2);
81+
Period.setUserDescriptor("Period");
82+
VERIFY_STATUS( Period.begin() );
83+
Period.write16(10);
84+
85+
return ERROR_NONE;
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 BLEADAFRUITACCEL_H_
26+
#define BLEADAFRUITACCEL_H_
27+
28+
#include "bluefruit_common.h"
29+
30+
#include "BLECharacteristic.h"
31+
#include "BLEService.h"
32+
33+
class BLEAdafruitAccel : public BLEService
34+
{
35+
public:
36+
static const uint8_t UUID128_SERVICE[16];
37+
static const uint8_t UUID128_CHR_DATA[16];
38+
static const uint8_t UUID128_CHR_PERIOD[16];
39+
40+
BLECharacteristic Data;
41+
BLECharacteristic Period;
42+
43+
BLEAdafruitAccel(void);
44+
virtual err_t begin(void);
45+
};
46+
47+
#endif /* BLEADAFRUITACCEL_H_ */

libraries/Bluefruit52Lib/src/services/BLEAdafruitNeopixel.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//--------------------------------------------------------------------+
3030

3131

32-
//------------- IMPLEMENTATION -------------//
32+
3333
/* Adafruit NeoPixel Service
3434
* - Service: ADAF-0002-C332-42A8-93BD-25E905756CB8
3535
* - Count : ADAF-0003-C332-42A8-93BD-25E905756CB8
@@ -92,7 +92,10 @@ err_t BLEAdafruitNeopixel::begin (void)
9292
// Add Characteristic
9393
Data.setProperties(CHR_PROPS_WRITE);
9494
Data.setPermission(SECMODE_NO_ACCESS, SECMODE_OPEN);
95-
Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH));
95+
// Change to use VLOC STACK to USER due to lack of memroy
96+
// Data.setMaxLen(Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH));
97+
Data.setMaxLen(20);
98+
9699
Data.setUserDescriptor("Data");
97100
VERIFY_STATUS( Data.begin() );
98101

0 commit comments

Comments
 (0)