@@ -924,16 +924,30 @@ uint32_t pbdrv_usb_tx_get_buf(pbio_pybricks_usb_in_ep_msg_t message_type, uint8_
924924 return sizeof (pbdrv_usb_tx_buf );
925925}
926926
927+ // As a stepping stone, we use RFCOMM to provide the REPL through this USB
928+ // module. This should be removed when USB is fully implemented.
929+ #include <nxos/drivers/bt.h>
930+
931+ extern bool nx_bt_is_ready (void );
932+
927933pbio_error_t pbdrv_usb_tx (pbio_os_state_t * state , const uint8_t * data , uint32_t size ) {
928934
929935 static pbio_os_timer_t timer ;
930936
931937 PBIO_OS_ASYNC_BEGIN (state );
932938
933- // TODO: Transmit and await completion
934- pbio_os_timer_set (& timer , PBDRV_USB_TRANSMIT_TIMEOUT );
939+ if (!nx_bt_is_ready ()) {
940+ return PBIO_SUCCESS ;
941+ }
942+
943+ // Only map USB stdout messages to this RFCOMM test.
944+ if (size < 2 || data [0 ] != PBIO_PYBRICKS_IN_EP_MSG_EVENT || data [1 ] != PBIO_PYBRICKS_EVENT_WRITE_STDOUT ) {
945+ return PBIO_SUCCESS ;
946+ }
935947
936- PBIO_OS_AWAIT_UNTIL (state , /*!transmitting ||*/ pbio_os_timer_is_expired (& timer ));
948+ pbio_os_timer_set (& timer , PBDRV_USB_TRANSMIT_TIMEOUT * 100 );
949+ nx_bt_stream_write ((uint8_t * )data + 2 , size - 2 );
950+ PBIO_OS_AWAIT_UNTIL (state , nx_bt_stream_data_written () || pbio_os_timer_is_expired (& timer ));
937951 if (pbio_os_timer_is_expired (& timer )) {
938952 return PBIO_ERROR_TIMEDOUT ;
939953 }
@@ -948,18 +962,34 @@ pbio_error_t pbdrv_usb_tx_reset(pbio_os_state_t *state) {
948962
949963uint32_t pbdrv_usb_get_data_in (uint8_t * data ) {
950964
965+ // Use RFCOMM as mock input.
966+ static uint8_t byte [1 ];
967+ static bool initialized ;
968+ if (!initialized && nx_bt_is_ready ()) {
969+ initialized = true;
970+
971+ // nxos API requires size in advance, so start reading one byte.
972+ nx_bt_stream_read (byte , sizeof (byte ));
973+
974+ // First USB message is the pretend-subscribe message.
975+ const uint8_t subscribe [] = { PBIO_PYBRICKS_OUT_EP_MSG_SUBSCRIBE , 1 };
976+ memcpy (data , subscribe , sizeof (subscribe ));
977+ return sizeof (subscribe );
978+ }
979+
951980 // Nothing received yet.
952- if (0 ) {
981+ if (! initialized || ! nx_bt_stream_data_read () ) {
953982 return 0 ;
954983 }
955984
956- uint32_t size = 0 ;
957-
958- // TODO: memcpy to data
985+ // Pretend to send a single-character stdin message.
986+ const uint8_t stdin [] = { PBIO_PYBRICKS_OUT_EP_MSG_COMMAND , PBIO_PYBRICKS_COMMAND_WRITE_STDIN , byte [ 0 ]};
987+ memcpy ( data , stdin , sizeof ( stdin ));
959988
960- // TODO: Reset to start waiting for new data.
989+ // Start waiting for another character.
990+ nx_bt_stream_read (byte , sizeof (byte ));
961991
962- return size ;
992+ return sizeof ( stdin ) ;
963993}
964994
965995#endif // PBDRV_CONFIG_USB_NXT
0 commit comments