Skip to content

Commit fee7546

Browse files
committed
pbio/debug: Add new module.
This will redirect debug output to usb and uart, when available.
1 parent 77695ef commit fee7546

8 files changed

Lines changed: 54 additions & 58 deletions

File tree

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
204204
src/color/util.c \
205205
src/control_settings.c \
206206
src/control.c \
207+
src/debug.c \
207208
src/dcmotor.c \
208209
src/differentiator.c \
209210
src/drivebase.c \

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#define DEBUG 0
2424

2525
#if DEBUG
26-
#include <pbdrv/../../drv/uart/uart_debug_first_port.h>
27-
#define DEBUG_PRINT pbdrv_uart_debug_printf
26+
#include <pbio/debug.h>
27+
#define DEBUG_PRINT pbio_debug
2828
#else
2929
#define DEBUG_PRINT(...)
3030
#endif

lib/pbio/drv/uart/uart_debug_first_port.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,18 @@ int pbdrv_uart_debug_next_char(void) {
3737
/**
3838
* Formats and stores a string in the UART debug ring buffer.
3939
*
40-
* This function works similarly to vprintf, but instead of printing to the
41-
* standard output. The formatted string will be written to the UART when the
40+
* The formatted string will be written to the UART when the
4241
* buffer is processed.
4342
*
4443
* @param format The format string, similar to printf.
4544
* @param va_list The variable arguments, as a va_list.
4645
*/
47-
void pbdrv_uart_debug_vprintf(const char *format, va_list args) {
46+
void pbdrv_uart_debug_print(const char *data, size_t len) {
4847
if (!lwrb_is_ready(&ring_buffer)) {
4948
lwrb_init(&ring_buffer, ring_buf_storage, sizeof(ring_buf_storage));
5049
}
5150

52-
char buf[256];
53-
size_t len = vsnprintf(buf, sizeof(buf), format, args);
54-
lwrb_write(&ring_buffer, (const uint8_t *)buf, len);
51+
lwrb_write(&ring_buffer, (const uint8_t *)data, len);
5552

5653
if (!debug_uart) {
5754
// Not initialized yet, so just buffer for now.
@@ -62,23 +59,6 @@ void pbdrv_uart_debug_vprintf(const char *format, va_list args) {
6259
pbio_os_request_poll();
6360
}
6461

65-
/**
66-
* Formats and stores a string in the UART debug ring buffer.
67-
*
68-
* This function works similarly to printf, but instead of printing to the
69-
* standard output. The formatted string will be written to the UART when the
70-
* buffer is processed.
71-
*
72-
* @param format The format string, similar to printf.
73-
* @param ... The variable arguments, similar to printf.
74-
*/
75-
void pbdrv_uart_debug_printf(const char *format, ...) {
76-
va_list args;
77-
va_start(args, format);
78-
pbdrv_uart_debug_vprintf(format, args);
79-
va_end(args);
80-
}
81-
8262
/**
8363
* Gets a character from the UART debug port.
8464
*

lib/pbio/drv/uart/uart_debug_first_port.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
#if PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
1414

15-
void pbdrv_uart_debug_printf(const char *format, ...);
16-
void pbdrv_uart_debug_vprintf(const char *format, va_list argptr);
15+
void pbdrv_uart_debug_print(const char *data, size_t len);
1716

1817
bool pbdrv_uart_debug_is_done(void);
1918

@@ -26,8 +25,7 @@ int pbdrv_uart_debug_next_char(void);
2625

2726
#else // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
2827

29-
#define pbdrv_uart_debug_printf(...)
30-
#define pbdrv_uart_debug_vprintf(format, argptr)
28+
#define pbdrv_uart_debug_print(data, len)
3129

3230
#define pbdrv_uart_debug_is_done() (true)
3331

lib/pbio/drv/usb/usb.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,23 @@ bool pbdrv_usb_stdout_tx_is_idle(void) {
108108
return lwrb_get_full(&pbdrv_usb_stdout_ring_buf) == 0 && !pbdrv_usb_noti_size[PBIO_PYBRICKS_EVENT_WRITE_STDOUT];
109109
}
110110

111-
void pbdrv_usb_debug_vprintf(const char *format, va_list args) {
111+
void pbdrv_usb_debug_print(const char *data, size_t len) {
112+
112113
if (!lwrb_is_ready(&pbdrv_usb_stdout_ring_buf)) {
113114
return;
114115
}
115116

116-
char buf[256];
117-
size_t len = vsnprintf(buf, sizeof(buf), format, args);
118-
119117
// Buffer result with \r injected before \n.
120118
for (size_t i = 0; i < len; i++) {
121-
if (buf[i] == '\n') {
119+
if (data[i] == '\n') {
122120
lwrb_write(&pbdrv_usb_stdout_ring_buf, (const uint8_t *)"\r", 1);
123121
}
124-
lwrb_write(&pbdrv_usb_stdout_ring_buf, (const uint8_t *)&buf[i], 1);
122+
lwrb_write(&pbdrv_usb_stdout_ring_buf, (const uint8_t *)&data[i], 1);
125123
}
126124

127125
pbio_os_request_poll();
128126
}
129127

130-
void pbdrv_usb_debug_printf(const char *format, ...) {
131-
va_list args;
132-
va_start(args, format);
133-
pbdrv_usb_debug_vprintf(format, args);
134-
va_end(args);
135-
}
136-
137128
pbio_error_t pbdrv_usb_send_event_notification(pbio_os_state_t *state, pbio_pybricks_event_t event_type, const uint8_t *data, size_t size) {
138129
PBIO_OS_ASYNC_BEGIN(state);
139130

lib/pbio/include/pbdrv/usb.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,12 @@ bool pbdrv_usb_connection_is_active(void);
115115
pbio_error_t pbdrv_usb_send_event_notification(pbio_os_state_t *state, pbio_pybricks_event_t event, const uint8_t *data, size_t size);
116116

117117
/**
118-
* Formats and stores a string in the USB stdout ring buffer using a va_list.
118+
* Stores a string in the USB stdout ring buffer.
119119
*
120-
* @param format The format string, similar to printf.
121-
* @param args The variable arguments, as a va_list.
120+
* @param data The string data.
121+
* @param len The length of the string data.
122122
*/
123-
void pbdrv_usb_debug_vprintf(const char *format, va_list args);
124-
125-
/**
126-
* Formats and stores a string in the USB stdout ring buffer.
127-
*
128-
* @param format The format string, similar to printf.
129-
* @param ... The variable arguments, similar to printf.
130-
*/
131-
void pbdrv_usb_debug_printf(const char *format, ...);
123+
void pbdrv_usb_debug_print(const char *data, size_t len);
132124

133125
#else // PBDRV_CONFIG_USB
134126

@@ -162,10 +154,7 @@ static inline pbio_error_t pbdrv_usb_send_event_notification(pbio_os_state_t *st
162154
return PBIO_ERROR_NOT_SUPPORTED;
163155
}
164156

165-
static inline void pbdrv_usb_debug_vprintf(const char *format, va_list args) {
166-
}
167-
168-
static inline void pbdrv_usb_debug_printf(const char *format, ...) {
157+
static inline void pbdrv_usb_debug_print(const char *data, size_t len) {
169158
}
170159

171160
#endif // PBDRV_CONFIG_USB

lib/pbio/include/pbio/debug.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
#ifndef _PBIO_DEBUG_H_
5+
#define _PBIO_DEBUG_H_
6+
7+
#include <stdio.h>
8+
#include <stdarg.h>
9+
#include <stdint.h>
10+
#include <string.h>
11+
12+
void pbio_debug_va(const char *format, va_list args);
13+
14+
void pbio_debug(const char *format, ...);
15+
16+
#endif // _PBIO_DEBUG_H_

lib/pbio/src/debug.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2021-2025 The Pybricks Authors
3+
4+
#include <pbdrv/usb.h>
5+
#include <pbdrv/../../drv/uart/uart_debug_first_port.h>
6+
7+
#include <pbio/debug.h>
8+
9+
void pbio_debug_va(const char *format, va_list args) {
10+
char buf[256];
11+
size_t len = vsnprintf(buf, sizeof(buf), format, args);
12+
pbdrv_usb_debug_print(buf, len);
13+
pbdrv_uart_debug_print(buf, len);
14+
}
15+
16+
void pbio_debug(const char *format, ...) {
17+
va_list args;
18+
va_start(args, format);
19+
pbio_debug_va(format, args);
20+
va_end(args);
21+
}

0 commit comments

Comments
 (0)