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 fae1e0e commit 4009ce4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 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 @@ -283,6 +284,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 @@ -304,6 +308,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 @@ -339,11 +345,26 @@ 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)) {
pbio_uuid128_le_copy(usb_out_buf, pbio_pybricks_command_event_char_uuid);
usb_out_sz = UUID_SZ;
usb_out_sz += pbio_pybricks_event_status_report(&usb_out_buf[UUID_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 4009ce4

Please sign in to comment.