Skip to content

Commit be4e8c7

Browse files
committed
update throughput example with rx speed test
1 parent b160987 commit be4e8c7

File tree

1 file changed

+64
-20
lines changed

1 file changed

+64
-20
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ char test_data[256] = { 0 };
3030
BLEDis bledis;
3131
BLEUart bleuart;
3232

33+
uint32_t rxCount = 0;
34+
uint32_t rxStartTime = 0;
35+
uint32_t rxLastTime = 0;
36+
3337
/**************************************************************************/
3438
/*!
3539
@brief Sets up the HW an the BLE module (this function is called
@@ -69,6 +73,9 @@ void setup(void)
6973
// Configure and Start BLE Uart Service
7074
bleuart.begin();
7175

76+
bleuart.setRxCallback(bleuart_rx_callback);
77+
bleuart.setNotifyCallback(bleuart_notify_callback);
78+
7279
// Set up and start advertising
7380
startAdv();
7481
}
@@ -122,7 +129,7 @@ void connect_callback(uint16_t conn_handle)
122129
//conn->requestConnectionParameter(6); // in unit of 1.25
123130

124131
// delay a bit for all the request to complete
125-
delay(5000);
132+
delay(1000);
126133
}
127134

128135
/**
@@ -139,11 +146,47 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
139146
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
140147
}
141148

149+
void bleuart_rx_callback(uint16_t conn_hdl)
150+
{
151+
(void) conn_hdl;
152+
153+
rxLastTime = millis();
154+
155+
// first packet
156+
if ( rxCount == 0 )
157+
{
158+
rxStartTime = millis();
159+
}
160+
161+
rxCount += bleuart.available();
162+
bleuart.flush(); // empty rx fifo
163+
}
164+
165+
void bleuart_notify_callback(uint16_t conn_hdl, bool enabled)
166+
{
167+
if ( enabled )
168+
{
169+
Serial.println("Send a key and press enter to start test");
170+
}
171+
}
172+
173+
void print_speed(const char* text, uint32_t count, uint32_t ms)
174+
{
175+
Serial.print(text);
176+
Serial.print(count);
177+
Serial.print(" bytes in ");
178+
Serial.print(ms / 1000.0F, 2);
179+
Serial.println(" seconds.");
180+
181+
Serial.print("Speed : ");
182+
Serial.print( (count / 1000.0F) / (ms / 1000.0F), 2);
183+
Serial.println(" KB/s.\r\n");
184+
}
142185

143186
void test_throughput(void)
144187
{
145-
uint32_t start, stop, sent;
146-
start = stop = sent = 0;
188+
uint32_t start, sent;
189+
start = sent = 0;
147190

148191
const uint16_t data_mtu = Bluefruit.Connection(0)->getMtu() - 3;
149192
memset(test_data, '1', data_mtu);
@@ -164,31 +207,32 @@ void test_throughput(void)
164207
sent += data_mtu;
165208
remaining -= data_mtu;
166209
}
167-
stop = millis() - start;
168210

169-
Serial.print("Sent ");
170-
Serial.print(sent);
171-
Serial.print(" bytes in ");
172-
Serial.print(stop / 1000.0F, 2);
173-
Serial.println(" seconds.");
174-
175-
Serial.println("Speed ");
176-
Serial.print( (sent / 1000.0F) / (stop / 1000.0F), 2);
177-
Serial.println(" KB/s.\r\n");
211+
print_speed("Sent ", sent, millis() - start);
178212
}
179213

180214
void loop(void)
181215
{
182216
if (Bluefruit.connected() && bleuart.notifyEnabled())
183-
{
217+
{
184218
// Wait for user input before trying again
185-
Serial.println("Send a key and press enter to start test");
186-
//getUserInput();
187-
188-
test_throughput();
219+
if ( Serial.available() )
220+
{
221+
getUserInput();
222+
test_throughput();
189223

190-
Bluefruit.disconnect(0);
191-
delay(2000);
224+
}
225+
226+
// 3 seconds has passed and there is no data received
227+
// then reset rx count
228+
if ( (rxCount > 0) && (rxLastTime + 3000 < millis()) )
229+
{
230+
print_speed("Received ", rxCount, rxLastTime-rxStartTime);
231+
rxCount = 0;
232+
}
233+
234+
// Bluefruit.disconnect(0);
235+
// delay(2000);
192236
}
193237
}
194238

0 commit comments

Comments
 (0)