Skip to content

Commit 86e774a

Browse files
committed
pbio/drv/display: Add driver for Virtual Hub.
1 parent 8c2bf4d commit 86e774a

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
148148
drv/counter/counter_nxt.c \
149149
drv/counter/counter_stm32f0_gpio_quad_enc.c \
150150
drv/display/display_ev3.c \
151+
drv/display/display_virtual.c \
151152
drv/gpio/gpio_ev3.c \
152153
drv/gpio/gpio_stm32f0.c \
153154
drv/gpio/gpio_stm32f4.c \
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
//
4+
// Display driver for a Virtual Hub
5+
6+
#include <pbdrv/config.h>
7+
8+
#if PBDRV_CONFIG_DISPLAY_VIRTUAL
9+
10+
#include <stdio.h>
11+
#include <string.h>
12+
13+
#include <pbdrv/display.h>
14+
15+
#include <pbio/error.h>
16+
#include <pbio/image.h>
17+
#include <pbio/os.h>
18+
19+
/**
20+
* User frame buffer. Each value is one pixel with value:
21+
*
22+
* 0: Empty / White
23+
* 1: Light Grey
24+
* 2: Dark Grey
25+
* 3: Black
26+
*
27+
* Non-atomic updated by the application are allowed.
28+
*/
29+
static uint8_t pbdrv_display_user_frame[PBDRV_CONFIG_DISPLAY_NUM_ROWS][PBDRV_CONFIG_DISPLAY_NUM_COLS] __attribute__((section(".noinit"), used));
30+
31+
/**
32+
* "Hardware" buffer of the virtual display.
33+
*/
34+
static uint8_t pbdrv_display_hardware_frame[PBDRV_CONFIG_DISPLAY_NUM_ROWS][PBDRV_CONFIG_DISPLAY_NUM_COLS] __attribute__((section(".noinit"), used));
35+
36+
/**
37+
* Flag to indicate that the user frame has been updated and needs to be
38+
* encoded and sent to the display driver.
39+
*/
40+
static bool pbdrv_display_user_frame_update_requested;
41+
42+
static pbio_os_process_t pbdrv_display_virtual_process;
43+
44+
/**
45+
* Display driver process. Initializes the display and updates the display
46+
* with the user frame buffer if the user data was updated.
47+
*/
48+
static pbio_error_t pbdrv_display_virtual_process_thread(pbio_os_state_t *state, void *context) {
49+
50+
PBIO_OS_ASYNC_BEGIN(state);
51+
52+
// Clear display to start with.
53+
memset(&pbdrv_display_user_frame, 0, sizeof(pbdrv_display_user_frame));
54+
pbdrv_display_user_frame_update_requested = true;
55+
56+
// Update the display with the user frame buffer, if changed.
57+
for (;;) {
58+
PBIO_OS_AWAIT_UNTIL(state, pbdrv_display_user_frame_update_requested);
59+
pbdrv_display_user_frame_update_requested = false;
60+
memcpy(pbdrv_display_hardware_frame, pbdrv_display_user_frame, sizeof(pbdrv_display_hardware_frame));
61+
}
62+
63+
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
64+
}
65+
66+
/**
67+
* Image corresponding to the display.
68+
*/
69+
static pbio_image_t display_image;
70+
71+
/**
72+
* Initialize the display driver.
73+
*/
74+
void pbdrv_display_init(void) {
75+
76+
// Initialize image.
77+
pbio_image_init(&display_image, (uint8_t *)pbdrv_display_user_frame,
78+
PBDRV_CONFIG_DISPLAY_NUM_COLS, PBDRV_CONFIG_DISPLAY_NUM_ROWS,
79+
PBDRV_CONFIG_DISPLAY_NUM_COLS);
80+
81+
pbio_os_process_start(&pbdrv_display_virtual_process, pbdrv_display_virtual_process_thread, NULL);
82+
}
83+
84+
pbio_image_t *pbdrv_display_get_image(void) {
85+
return &display_image;
86+
}
87+
88+
uint8_t pbdrv_display_get_max_value(void) {
89+
return 3;
90+
}
91+
92+
void pbdrv_display_update(void) {
93+
pbdrv_display_user_frame_update_requested = true;
94+
pbio_os_request_poll();
95+
}
96+
97+
#endif // PBDRV_CONFIG_DISPLAY_VIRTUAL

lib/pbio/platform/virtual_hub/pbdrvconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
#define PBDRV_CONFIG_COUNTER (1)
2525

26+
#define PBDRV_CONFIG_DISPLAY (1)
27+
#define PBDRV_CONFIG_DISPLAY_VIRTUAL (1)
28+
#define PBDRV_CONFIG_DISPLAY_NUM_COLS (178)
29+
#define PBDRV_CONFIG_DISPLAY_NUM_ROWS (128)
30+
2631
#define PBDRV_CONFIG_GPIO (1)
2732
#define PBDRV_CONFIG_GPIO_VIRTUAL (1)
2833

lib/pbio/platform/virtual_hub/pbioconfig.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2022 The Pybricks Authors
2+
// Copyright (c) 2022-2025 The Pybricks Authors
33

44
#define PBIO_CONFIG_BATTERY (1)
55
#define PBIO_CONFIG_DCMOTOR (6)
66
#define PBIO_CONFIG_DCMOTOR_NUM_DEV (6)
77
#define PBIO_CONFIG_DRIVEBASE_SPIKE (1)
8+
#define PBIO_CONFIG_IMAGE (1)
89
#define PBIO_CONFIG_LIGHT (0)
910
#define PBIO_CONFIG_LOGGER (1)
1011
#define PBIO_CONFIG_LIGHT_MATRIX (0)

0 commit comments

Comments
 (0)