@@ -30,6 +30,10 @@ char test_data[256] = { 0 };
30
30
BLEDis bledis;
31
31
BLEUart bleuart;
32
32
33
+ uint32_t rxCount = 0 ;
34
+ uint32_t rxStartTime = 0 ;
35
+ uint32_t rxLastTime = 0 ;
36
+
33
37
/* *************************************************************************/
34
38
/* !
35
39
@brief Sets up the HW an the BLE module (this function is called
@@ -69,6 +73,9 @@ void setup(void)
69
73
// Configure and Start BLE Uart Service
70
74
bleuart.begin ();
71
75
76
+ bleuart.setRxCallback (bleuart_rx_callback);
77
+ bleuart.setNotifyCallback (bleuart_notify_callback);
78
+
72
79
// Set up and start advertising
73
80
startAdv ();
74
81
}
@@ -122,7 +129,7 @@ void connect_callback(uint16_t conn_handle)
122
129
// conn->requestConnectionParameter(6); // in unit of 1.25
123
130
124
131
// delay a bit for all the request to complete
125
- delay (5000 );
132
+ delay (1000 );
126
133
}
127
134
128
135
/* *
@@ -139,11 +146,47 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
139
146
Serial.print (" Disconnected, reason = 0x" ); Serial.println (reason, HEX);
140
147
}
141
148
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
+ }
142
185
143
186
void test_throughput (void )
144
187
{
145
- uint32_t start, stop, sent;
146
- start = stop = sent = 0 ;
188
+ uint32_t start, sent;
189
+ start = sent = 0 ;
147
190
148
191
const uint16_t data_mtu = Bluefruit.Connection (0 )->getMtu () - 3 ;
149
192
memset (test_data, ' 1' , data_mtu);
@@ -164,31 +207,32 @@ void test_throughput(void)
164
207
sent += data_mtu;
165
208
remaining -= data_mtu;
166
209
}
167
- stop = millis () - start;
168
210
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);
178
212
}
179
213
180
214
void loop (void )
181
215
{
182
216
if (Bluefruit.connected () && bleuart.notifyEnabled ())
183
- {
217
+ {
184
218
// 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 ();
189
223
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);
192
236
}
193
237
}
194
238
0 commit comments