1212#include <unistd.h>
1313
1414#include "usb.h"
15+
16+ #include <pbdrv/config.h>
1517#include <pbdrv/usb.h>
1618
1719#include <pbio/error.h>
1820#include <pbio/os.h>
21+ #include <pbio/protocol.h>
1922
2023#include <lwrb/lwrb.h>
2124
2528static bool pbdrv_usb_events_subscribed ;
2629
2730bool pbdrv_usb_connection_is_active (void ) {
28- return pbdrv_usb_events_subscribed ;
31+ return pbdrv_usb_events_subscribed && pbdrv_usb_is_ready () ;
2932}
3033
3134/**
@@ -106,7 +109,7 @@ static void pbdrv_usb_handle_data_in(void) {
106109 return ;
107110 }
108111
109- static uint8_t data_in [PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE ];
112+ static uint8_t data_in [PBDRV_CONFIG_USB_MAX_PACKET_SIZE ];
110113 uint32_t size = pbdrv_usb_get_data_in (data_in );
111114
112115 // Expecting at least EP_MSG and payload.
@@ -145,7 +148,7 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
145148
146149 static pbio_os_state_t sub ;
147150
148- static uint8_t out_data [ PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE ] ;
151+ static uint8_t * out_data ;
149152 static uint32_t out_size ;
150153
151154 pbio_error_t err ;
@@ -166,21 +169,21 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
166169 // status, then stdout, then other events.
167170 if (respond_soon ) {
168171 // Pack the response to the most recent message.
169- out_data [ 0 ] = PBIO_PYBRICKS_IN_EP_MSG_RESPONSE ;
172+ pbdrv_usb_tx_get_buf ( PBIO_PYBRICKS_IN_EP_MSG_RESPONSE , & out_data ) ;
170173 pbio_set_uint32_le (& out_data [1 ], respond_result );
171174 out_size = sizeof (uint32_t ) + 1 ;
172175 respond_soon = false;
173176 } else if (pbdrv_usb_connection_is_active () && status_data_pending ) {
174177 // Send out status if pending (already includes event code).
175- out_data [ 0 ] = PBIO_PYBRICKS_IN_EP_MSG_EVENT ;
178+ pbdrv_usb_tx_get_buf ( PBIO_PYBRICKS_IN_EP_MSG_EVENT , & out_data ) ;
176179 memcpy (& out_data [1 ], status_data , PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE );
177180 out_size = PBIO_PYBRICKS_USB_MESSAGE_SIZE (PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE );
178181 status_data_pending = false;
179182 } else if (pbdrv_usb_connection_is_active () && lwrb_get_full (& stdout_ring_buf ) != 0 ) {
180183 // Send out stdout if anything is buffered.
181- out_data [ 0 ] = PBIO_PYBRICKS_IN_EP_MSG_EVENT ;
184+ uint32_t max_size = pbdrv_usb_tx_get_buf ( PBIO_PYBRICKS_IN_EP_MSG_EVENT , & out_data ) ;
182185 out_data [1 ] = PBIO_PYBRICKS_EVENT_WRITE_STDOUT ;
183- out_size = lwrb_read (& stdout_ring_buf , & out_data [2 ], PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE - 2 ) + 2 ;
186+ out_size = lwrb_read (& stdout_ring_buf , & out_data [2 ], max_size - 2 ) + 2 ;
184187 }
185188
186189 // If there was anything to send, send it.
@@ -210,7 +213,7 @@ static pbio_error_t pbdrv_usb_process_thread(pbio_os_state_t *state, void *conte
210213void pbdrv_usb_init (void ) {
211214 pbdrv_usb_init_device ();
212215
213- static uint8_t stdout_buf [PBDRV_USB_PYBRICKS_MAX_PACKET_SIZE * 2 ];
216+ static uint8_t stdout_buf [PBDRV_CONFIG_USB_MAX_PACKET_SIZE * 2 ];
214217 lwrb_init (& stdout_ring_buf , stdout_buf , PBIO_ARRAY_SIZE (stdout_buf ));
215218
216219 pbio_os_process_start (& pbdrv_usb_process , pbdrv_usb_process_thread , NULL );
0 commit comments