@@ -19,6 +19,8 @@ let esp_pin_left_b = 16; // m1b
1919let esp_pin_right_a = 23 ; // m2a
2020let esp_pin_right_b = 22 ; // m2b
2121
22+ let queued_ble_write = null ;
23+
2224//===========================================
2325// get handles to elements
2426//===========================================
@@ -162,10 +164,31 @@ function generate_cmd(pin, state) {
162164 return `:w${ pin } ${ state } ` ;
163165}
164166
165- function send_cmd ( cmd ) {
167+ function queue_cmd ( cmd ) {
168+ queued_ble_write = cmd ;
169+ }
170+
171+ function send_cmd ( ) {
172+
173+ // if no command is queued then return
174+ if ( queued_ble_write == null ) return
175+
176+ console . log ( "sending queued command: " + queued_ble_write ) ;
177+
178+ // otherwise, send queued command
166179 const utf8encoder = new TextEncoder ( ) ;
167- let cmd_bytes = utf8encoder . encode ( cmd ) ;
168- return ble_char_nrf_uart_rx . writeValueWithoutResponse ( cmd_bytes ) ;
180+ let cmd_bytes = utf8encoder . encode ( queued_ble_write ) ;
181+
182+ // command is about to be sent, clear queue
183+ queued_ble_write = null ;
184+
185+ // send command
186+ ble_char_nrf_uart_rx . writeValueWithoutResponse ( cmd_bytes ) . then ( _ => {
187+
188+ // once command has written, write next command
189+ send_cmd ( ) ;
190+
191+ } )
169192}
170193
171194function write_pin ( pin , state ) {
@@ -181,50 +204,55 @@ function write_pin(pin, state) {
181204
182205function drive_stop ( ) {
183206 console . log ( "stopping" ) ;
184- send_cmd ( ( [
207+ queue_cmd ( ( [
185208 generate_cmd ( esp_pin_left_a , 0 ) ,
186209 generate_cmd ( esp_pin_left_b , 0 ) ,
187210 generate_cmd ( esp_pin_right_a , 0 ) ,
188211 generate_cmd ( esp_pin_right_b , 0 ) ,
189212 ] ) . join ( ";" ) )
213+ send_cmd ( ) ;
190214}
191215
192216function drive_forward ( ) {
193217 console . log ( "driving forward" ) ;
194- send_cmd ( ( [
218+ queue_cmd ( ( [
195219 generate_cmd ( esp_pin_left_a , 1 ) ,
196220 generate_cmd ( esp_pin_left_b , 0 ) ,
197221 generate_cmd ( esp_pin_right_a , 1 ) ,
198222 generate_cmd ( esp_pin_right_b , 0 ) ,
199223 ] ) . join ( ";" ) )
224+ send_cmd ( ) ;
200225}
201226
202227function drive_backward ( ) {
203228 console . log ( "driving backward" ) ;
204- send_cmd ( ( [
229+ queue_cmd ( ( [
205230 generate_cmd ( esp_pin_left_a , 0 ) ,
206231 generate_cmd ( esp_pin_left_b , 1 ) ,
207232 generate_cmd ( esp_pin_right_a , 0 ) ,
208233 generate_cmd ( esp_pin_right_b , 1 ) ,
209234 ] ) . join ( ";" ) )
235+ send_cmd ( ) ;
210236}
211237
212238function drive_rot_right ( ) {
213239 console . log ( "driving right" ) ;
214- send_cmd ( ( [
240+ queue_cmd ( ( [
215241 generate_cmd ( esp_pin_left_a , 0 ) ,
216242 generate_cmd ( esp_pin_left_b , 1 ) ,
217243 generate_cmd ( esp_pin_right_a , 1 ) ,
218244 generate_cmd ( esp_pin_right_b , 0 ) ,
219245 ] ) . join ( ";" ) )
246+ send_cmd ( ) ;
220247}
221248
222249function drive_rot_left ( ) {
223250 console . log ( "driving left" ) ;
224- send_cmd ( ( [
251+ queue_cmd ( ( [
225252 generate_cmd ( esp_pin_left_a , 1 ) ,
226253 generate_cmd ( esp_pin_left_b , 0 ) ,
227254 generate_cmd ( esp_pin_right_a , 0 ) ,
228255 generate_cmd ( esp_pin_right_b , 1 ) ,
229256 ] ) . join ( ";" ) )
257+ send_cmd ( ) ;
230258}
0 commit comments