Skip to content

Commit

Permalink
Periodically send status
Browse files Browse the repository at this point in the history
Periodically sends status flags over the USB connection.

Signed-off-by: Nate Karstens <[email protected]>
  • Loading branch information
nkarstens committed Feb 10, 2024
1 parent f3115c7 commit 5770350
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/pbio/drv/usb/usb_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <usbd_pybricks.h>

#include <pbdrv/usb.h>
#include <pbio/protocol.h>
#include <pbio/util.h>
#include <pbsys/command.h>
#include <pbsys/status.h>
Expand Down Expand Up @@ -281,6 +282,9 @@ PROCESS_THREAD(pbdrv_usb_process, ev, data) {
static PBIO_ONESHOT(bcd_oneshot);
static PBIO_ONESHOT(pwrdn_oneshot);
static bool bcd_busy;
static struct etimer timer;
static uint32_t prev_status_flags = ~0;
static uint32_t new_status_flags;

PROCESS_POLLHANDLER({
if (!bcd_busy && pbio_oneshot(!vbus_active, &no_vbus_oneshot)) {
Expand All @@ -302,6 +306,8 @@ PROCESS_THREAD(pbdrv_usb_process, ev, data) {
// Prepare to receive the first packet
USBD_Pybricks_ReceivePacket(&husbd);

etimer_set(&timer, 500);

for (;;) {
PROCESS_WAIT_EVENT();

Expand Down Expand Up @@ -336,11 +342,27 @@ PROCESS_THREAD(pbdrv_usb_process, ev, data) {
USBD_Pybricks_ReceivePacket(&husbd);
}

if (!usb_out_sz && usb_stdout_sz) {
if (usb_out_sz) {
continue;
}

new_status_flags = pbsys_status_get_flags();

if (usb_stdout_sz) {
memcpy(usb_out_buf, usb_stdout_buf, usb_stdout_sz);
usb_out_sz = usb_stdout_sz;
usb_stdout_sz = 0;
} else if ((new_status_flags != prev_status_flags) || etimer_expired(&timer)) {
usb_out_buf[0] = USBD_PYBRICKS_MSG_EVENT;
usb_out_sz = 1;

usb_out_sz += pbio_pybricks_event_status_report(&usb_out_buf[usb_out_sz], new_status_flags);

etimer_restart(&timer);
prev_status_flags = new_status_flags;
}

if (usb_out_sz) {
USBD_Pybricks_TransmitPacket(&husbd, usb_out_buf, usb_out_sz);
}
}
Expand Down

0 comments on commit 5770350

Please sign in to comment.