1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/*********************************************************************
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
*********************************************************************/
#include <bluefruit.h>
// String to send in the throughput test
#define TEST_STRING "01234567899876543210"
const int TEST_STRLEN = strlen(TEST_STRING);
// Number of total data sent ( 1024 times the test string)
#define TOTAL_BYTES (1024 * strlen(TEST_STRING))
BLEDis bledis;
BLEUart bleuart;
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Bluefruit52 Throughput Example");
Serial.println("------------------------------\n");
// Setup the BLE LED to be enabled on CONNECT
// Note: This is actually the default behaviour, but provided
// here in case you want to control this manually via PIN 19
Bluefruit.autoConnLed(true);
Bluefruit.begin();
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
Bluefruit.setName("Bluefruit52");
Bluefruit.setConnectCallback(connect_callback);
Bluefruit.setDisconnectCallback(disconnect_callback);
// Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather52");
bledis.begin();
// Configure and Start BLE Uart Service
bleuart.begin();
// Set up and start advertising
startAdv();
}
void startAdv(void)
{
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);
// There is no room for Name in Advertising packet
// Use Scan response for Name
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void connect_callback(uint16_t conn_handle)
{
(void) conn_handle;
Serial.println("Connected");
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle connection where this event happens
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
* https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.println();
Serial.println("Disconnected");
}
/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
uint32_t start, stop, sent;
uint32_t remaining = TOTAL_BYTES;
start = stop = sent = 0;
if (Bluefruit.connected() && bleuart.notifyEnabled())
{
// Wait for user input before trying again
Serial.println("Connected. Send a key and press enter to start test");
getUserInput();
Serial.print("Sending ");
Serial.print(remaining);
Serial.println(" bytes ...");
start = millis();
while ( (remaining > 0) && Bluefruit.connected() && bleuart.notifyEnabled() )
{
if ( !bleuart.print(TEST_STRING) ) break;
sent += TEST_STRLEN;
remaining -= TEST_STRLEN;
// Only print every 100th time
// if ( (sent % (100*TEST_STRLEN) ) == 0 )
// {
// Serial.print("Sent: "); Serial.print(sent);
// Serial.print(" Remaining: "); Serial.println(remaining);
// }
}
stop = millis() - start;
Serial.print("Sent ");
Serial.print(sent);
Serial.print(" bytes in ");
Serial.print(stop / 1000.0F, 2);
Serial.println(" seconds.");
Serial.println("Speed ");
Serial.print( (sent / 1000.0F) / (stop / 1000.0F), 2);
Serial.println(" KB/s.\r\n");
}
}
/**************************************************************************/
/*!
@brief Get user input from Serial
*/
/**************************************************************************/
char* getUserInput(void)
{
static char inputs[64+1];
memset(inputs, 0, sizeof(inputs));
// wait until data is available
while( Serial.available() == 0 ) { delay(1); }
uint8_t count=0;
do
{
count += Serial.readBytes(inputs+count, 64);
} while( (count < 64) && Serial.available() );
return inputs;
}
|