@@ -43,25 +43,25 @@ void pbdrv_usb_set_receive_handler(pbdrv_usb_receive_handler_t handler) {
4343/**
4444 * Buffer scheduled status.
4545 */
46- static uint8_t status_data [PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE ];
47- static bool status_data_pending ;
46+ static uint8_t pbdrv_usb_status_data [PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE ];
47+ static bool pbdrv_usb_status_data_pending ;
4848
4949void pbdrv_usb_schedule_status_update (const uint8_t * status_msg ) {
5050 // Ignore if message identical to last.
51- if (!memcmp (status_data , status_msg , sizeof (status_data ))) {
51+ if (!memcmp (pbdrv_usb_status_data , status_msg , sizeof (pbdrv_usb_status_data ))) {
5252 return ;
5353 }
5454
5555 // Schedule to send whenever the Bluetooth process gets round to it.
56- memcpy (status_data , status_msg , sizeof (status_data ));
57- status_data_pending = true;
56+ memcpy (pbdrv_usb_status_data , status_msg , sizeof (pbdrv_usb_status_data ));
57+ pbdrv_usb_status_data_pending = true;
5858 pbio_os_request_poll ();
5959}
6060
6161/**
6262 * Buffer for scheduled stdout.
6363 */
64- static lwrb_t stdout_ring_buf ;
64+ static lwrb_t pbdrv_usb_stdout_ring_buf ;
6565
6666pbio_error_t pbdrv_usb_stdout_tx (const uint8_t * data , uint32_t * size ) {
6767
@@ -71,7 +71,7 @@ pbio_error_t pbdrv_usb_stdout_tx(const uint8_t *data, uint32_t *size) {
7171
7272 // Buffer data to send it more efficiently even if the caller is only
7373 // writing one byte at a time.
74- if ((* size = lwrb_write (& stdout_ring_buf , data , * size )) == 0 ) {
74+ if ((* size = lwrb_write (& pbdrv_usb_stdout_ring_buf , data , * size )) == 0 ) {
7575 return PBIO_ERROR_AGAIN ;
7676 }
7777
@@ -86,31 +86,33 @@ uint32_t pbdrv_usb_stdout_tx_available(void) {
8686 if (!pbdrv_usb_connection_is_active ()) {
8787 return UINT32_MAX ;
8888 }
89- return lwrb_get_free (& stdout_ring_buf );
89+ return lwrb_get_free (& pbdrv_usb_stdout_ring_buf );
9090}
9191
9292bool pbdrv_usb_stdout_tx_is_idle (void ) {
9393 if (!pbdrv_usb_connection_is_active ()) {
9494 return true;
9595 }
96- return lwrb_get_full (& stdout_ring_buf ) == 0 ;
96+ return lwrb_get_full (& pbdrv_usb_stdout_ring_buf ) == 0 ;
9797}
9898
99- static bool respond_soon ;
100- static pbio_pybricks_error_t respond_result ;
99+ static bool pbdrv_usb_respond_soon ;
100+ static pbio_pybricks_error_t pbdrv_usb_respond_result ;
101101
102102/**
103103 * Non-blocking poll handler to process pending incoming messages.
104104 */
105105static void pbdrv_usb_handle_data_in (void ) {
106106
107107 // Ignore incoming data if we haven't sent our previous response yet.
108- if (respond_soon ) {
108+ if (pbdrv_usb_respond_soon ) {
109109 return ;
110110 }
111111
112- static uint8_t data_in [PBDRV_CONFIG_USB_MAX_PACKET_SIZE ];
113- uint32_t size = pbdrv_usb_get_data_in (data_in );
112+ // Data is copied here so the driver can immediately clear it and queue
113+ // the next receive.
114+ uint8_t data_in [PBDRV_CONFIG_USB_MAX_PACKET_SIZE ];
115+ uint32_t size = pbdrv_usb_get_data_and_start_receive (data_in );
114116
115117 // Expecting at least EP_MSG and payload.
116118 if (size < 2 ) {
@@ -120,26 +122,26 @@ static void pbdrv_usb_handle_data_in(void) {
120122 switch (data_in [0 ]) {
121123 case PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE :
122124 pbdrv_usb_events_subscribed = data_in [1 ];
123- respond_result = PBIO_PYBRICKS_ERROR_OK ;
124- respond_soon = true;
125+ pbdrv_usb_respond_result = PBIO_PYBRICKS_ERROR_OK ;
126+ pbdrv_usb_respond_soon = true;
125127
126128 // Schedule sending current status immediately after subscribing.
127- status_data_pending = true;
129+ pbdrv_usb_status_data_pending = true;
128130 break ;
129131 case PBIO_PYBRICKS_OUT_EP_MSG_COMMAND :
130132 if (pbdrv_usb_receive_handler ) {
131- respond_result = pbdrv_usb_receive_handler (data_in + 1 , size - 1 );
132- respond_soon = true;
133+ pbdrv_usb_respond_result = pbdrv_usb_receive_handler (data_in + 1 , size - 1 );
134+ pbdrv_usb_respond_soon = true;
133135 }
134136 break ;
135137 }
136138}
137139
138140static void pbdrv_usb_reset_state (void ) {
139141 pbdrv_usb_events_subscribed = false;
140- respond_soon = false;
141- status_data_pending = false;
142- lwrb_reset (& stdout_ring_buf );
142+ pbdrv_usb_respond_soon = false;
143+ pbdrv_usb_status_data_pending = false;
144+ lwrb_reset (& pbdrv_usb_stdout_ring_buf );
143145}
144146
145147static pbio_os_process_t pbdrv_usb_process ;
@@ -167,23 +169,23 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
167169
168170 // Find out what we should send, if anything, priotizing response, then
169171 // status, then stdout, then other events.
170- if (respond_soon ) {
172+ if (pbdrv_usb_respond_soon ) {
171173 // Pack the response to the most recent message.
172174 pbdrv_usb_tx_get_buf (PBIO_PYBRICKS_IN_EP_MSG_RESPONSE , & out_data );
173- pbio_set_uint32_le (& out_data [1 ], respond_result );
175+ pbio_set_uint32_le (& out_data [1 ], pbdrv_usb_respond_result );
174176 out_size = sizeof (uint32_t ) + 1 ;
175- respond_soon = false;
176- } else if (pbdrv_usb_connection_is_active () && status_data_pending ) {
177+ pbdrv_usb_respond_soon = false;
178+ } else if (pbdrv_usb_connection_is_active () && pbdrv_usb_status_data_pending ) {
177179 // Send out status if pending (already includes event code).
178180 pbdrv_usb_tx_get_buf (PBIO_PYBRICKS_IN_EP_MSG_EVENT , & out_data );
179- memcpy (& out_data [1 ], status_data , PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE );
181+ memcpy (& out_data [1 ], pbdrv_usb_status_data , PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE );
180182 out_size = PBIO_PYBRICKS_USB_MESSAGE_SIZE (PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE );
181- status_data_pending = false;
182- } else if (pbdrv_usb_connection_is_active () && lwrb_get_full (& stdout_ring_buf ) != 0 ) {
183+ pbdrv_usb_status_data_pending = false;
184+ } else if (pbdrv_usb_connection_is_active () && lwrb_get_full (& pbdrv_usb_stdout_ring_buf ) != 0 ) {
183185 // Send out stdout if anything is buffered.
184186 uint32_t max_size = pbdrv_usb_tx_get_buf (PBIO_PYBRICKS_IN_EP_MSG_EVENT , & out_data );
185187 out_data [1 ] = PBIO_PYBRICKS_EVENT_WRITE_STDOUT ;
186- out_size = lwrb_read (& stdout_ring_buf , & out_data [2 ], max_size - 2 ) + 2 ;
188+ out_size = lwrb_read (& pbdrv_usb_stdout_ring_buf , & out_data [2 ], max_size - 2 ) + 2 ;
187189 }
188190
189191 // If there was anything to send, send it.
@@ -214,7 +216,7 @@ void pbdrv_usb_init(void) {
214216 pbdrv_usb_init_device ();
215217
216218 static uint8_t stdout_buf [PBDRV_CONFIG_USB_MAX_PACKET_SIZE * 2 ];
217- lwrb_init (& stdout_ring_buf , stdout_buf , PBIO_ARRAY_SIZE (stdout_buf ));
219+ lwrb_init (& pbdrv_usb_stdout_ring_buf , stdout_buf , PBIO_ARRAY_SIZE (stdout_buf ));
218220
219221 pbio_os_process_start (& pbdrv_usb_process , pbdrv_usb_process_thread , NULL );
220222}
0 commit comments