Skip to content

Commit fb16a2a

Browse files
committed
pybricks.parameters.Image: Use display type instead of a bool.
This doesn't change anything for now, but let's us have displays of different types or states.
1 parent 128af28 commit fb16a2a

2 files changed

Lines changed: 41 additions & 35 deletions

File tree

pybricks/parameters/pb_type_image.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323

2424
#include <pbdrv/display.h>
2525

26+
typedef enum {
27+
/** Image is not a display. */
28+
PB_TYPE_IMAGE_DISPLAY_NONE,
29+
/** Image is a display but it is not being used or in a system state. */
30+
PB_TYPE_IMAGE_DISPLAY_UNUSED,
31+
/** Image is a display and ready for use. */
32+
PB_TYPE_IMAGE_DISPLAY_READY,
33+
} pb_type_Image_display_t;
34+
2635
#if PYBRICKS_PY_PARAMETERS_IMAGE_FILE
2736
#include "pbio_image_media.h"
2837
#endif // PYBRICKS_PY_PARAMETERS_IMAGE_FILE
@@ -36,7 +45,7 @@ typedef struct _pb_type_Image_obj_t {
3645
// that owns the memory.
3746
mp_obj_t owner;
3847
pbio_image_t image;
39-
bool is_display;
48+
pb_type_Image_display_t display_type;
4049
} pb_type_Image_obj_t;
4150

4251
static int get_color(mp_obj_t obj) {
@@ -54,7 +63,7 @@ static int get_color(mp_obj_t obj) {
5463
mp_obj_t pb_type_Image_display_obj_new(void) {
5564
pb_type_Image_obj_t *self = mp_obj_malloc(pb_type_Image_obj_t, &pb_type_Image);
5665
self->owner = MP_OBJ_NULL;
57-
self->is_display = true;
66+
self->display_type = PB_TYPE_IMAGE_DISPLAY_READY;
5867
self->image = *pbdrv_display_get_image();
5968

6069
return MP_OBJ_FROM_PTR(self);
@@ -86,7 +95,7 @@ static mp_obj_t pb_type_Image_make_new(const mp_obj_type_t *type,
8695

8796
self = mp_obj_malloc_with_finaliser(pb_type_Image_obj_t, &pb_type_Image);
8897
self->owner = MP_OBJ_NULL;
89-
self->is_display = false;
98+
self->display_type = PB_TYPE_IMAGE_DISPLAY_NONE;
9099
pbio_image_init(&self->image, buf, width, height, width);
91100
self->image.print_font = source->image.print_font;
92101
self->image.print_value = source->image.print_value;
@@ -99,7 +108,7 @@ static mp_obj_t pb_type_Image_make_new(const mp_obj_type_t *type,
99108
mp_int_t y2 = y2_in == mp_const_none ? source->image.height - 1 : pb_obj_get_int(y2_in);
100109
self = mp_obj_malloc(pb_type_Image_obj_t, &pb_type_Image);
101110
self->owner = source_in;
102-
self->is_display = false;
111+
self->display_type = PB_TYPE_IMAGE_DISPLAY_NONE;
103112
int width = x2 - x1 + 1;
104113
int height = y2 - y1 + 1;
105114
pbio_image_init_sub(&self->image, &source->image, x1, y1, width, height);
@@ -111,7 +120,7 @@ static mp_obj_t pb_type_Image_make_new(const mp_obj_type_t *type,
111120
static mp_obj_t pb_type_Image_close(mp_obj_t self_in) {
112121
pb_type_Image_obj_t *self = MP_OBJ_TO_PTR(self_in);
113122
// If we own the memory, free it.
114-
if (self->owner == MP_OBJ_NULL && !self->is_display && self->image.pixels) {
123+
if (self->owner == MP_OBJ_NULL && self->display_type == PB_TYPE_IMAGE_DISPLAY_NONE && self->image.pixels) {
115124
umm_free(self->image.pixels);
116125
self->image.pixels = NULL;
117126
}
@@ -140,7 +149,7 @@ static mp_obj_t pb_type_Image_empty(size_t n_args, const mp_obj_t *pos_args, mp_
140149

141150
pb_type_Image_obj_t *self = mp_obj_malloc_with_finaliser(pb_type_Image_obj_t, &pb_type_Image);
142151
self->owner = MP_OBJ_NULL;
143-
self->is_display = false;
152+
self->display_type = PB_TYPE_IMAGE_DISPLAY_NONE;
144153
pbio_image_init(&self->image, buf, width, height, width);
145154
self->image.print_font = display->print_font;
146155
self->image.print_value = display->print_value;
@@ -168,14 +177,21 @@ static void pb_type_Image_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
168177
dest[1] = MP_OBJ_SENTINEL;
169178
}
170179

180+
/**
181+
* If this image is the display, request a frame update.
182+
*/
183+
static void pb_type_Image_handle_display_update(pb_type_Image_obj_t *self) {
184+
if (self->display_type == PB_TYPE_IMAGE_DISPLAY_READY) {
185+
pbdrv_display_update();
186+
}
187+
}
188+
171189
static mp_obj_t pb_type_Image_clear(mp_obj_t self_in) {
172190
pb_type_Image_obj_t *self = MP_OBJ_TO_PTR(self_in);
173191

174192
pbio_image_fill(&self->image, 0);
175193

176-
if (self->is_display) {
177-
pbdrv_display_update();
178-
}
194+
pb_type_Image_handle_display_update(self);
179195

180196
return mp_const_none;
181197
}
@@ -195,9 +211,7 @@ static mp_obj_t pb_type_Image_load_image(size_t n_args, const mp_obj_t *pos_args
195211
pbio_image_fill(&self->image, 0);
196212
pbio_image_draw_image(&self->image, &source->image, x, y);
197213

198-
if (self->is_display) {
199-
pbdrv_display_update();
200-
}
214+
pb_type_Image_handle_display_update(self);
201215

202216
return mp_const_none;
203217
}
@@ -224,9 +238,7 @@ static mp_obj_t pb_type_Image_draw_image(size_t n_args, const mp_obj_t *pos_args
224238
pbio_image_draw_image_transparent(&self->image, &source->image, x, y, transparent_value);
225239
}
226240

227-
if (self->is_display) {
228-
pbdrv_display_update();
229-
}
241+
pb_type_Image_handle_display_update(self);
230242

231243
return mp_const_none;
232244
}
@@ -245,9 +257,7 @@ static mp_obj_t pb_type_Image_draw_pixel(size_t n_args, const mp_obj_t *pos_args
245257

246258
pbio_image_draw_pixel(&self->image, x, y, color);
247259

248-
if (self->is_display) {
249-
pbdrv_display_update();
250-
}
260+
pb_type_Image_handle_display_update(self);
251261

252262
return mp_const_none;
253263
}
@@ -272,9 +282,7 @@ static mp_obj_t pb_type_Image_draw_line(size_t n_args, const mp_obj_t *pos_args,
272282

273283
pbio_image_draw_thick_line(&self->image, x1, y1, x2, y2, width, color);
274284

275-
if (self->is_display) {
276-
pbdrv_display_update();
277-
}
285+
pb_type_Image_handle_display_update(self);
278286

279287
return mp_const_none;
280288
}
@@ -307,9 +315,7 @@ static mp_obj_t pb_type_Image_draw_box(size_t n_args, const mp_obj_t *pos_args,
307315
pbio_image_draw_rounded_rect(&self->image, x1, y1, width, height, r, color);
308316
}
309317

310-
if (self->is_display) {
311-
pbdrv_display_update();
312-
}
318+
pb_type_Image_handle_display_update(self);
313319

314320
return mp_const_none;
315321
}
@@ -336,9 +342,7 @@ static mp_obj_t pb_type_Image_draw_circle(size_t n_args, const mp_obj_t *pos_arg
336342
pbio_image_draw_circle(&self->image, x, y, r, color);
337343
}
338344

339-
if (self->is_display) {
340-
pbdrv_display_update();
341-
}
345+
pb_type_Image_handle_display_update(self);
342346

343347
return mp_const_none;
344348
}
@@ -372,9 +376,7 @@ static mp_obj_t pb_type_Image_draw_text(size_t n_args, const mp_obj_t *pos_args,
372376

373377
pbio_image_draw_text(&self->image, font, x, y + font->top_max, text, text_len, text_color);
374378

375-
if (self->is_display) {
376-
pbdrv_display_update();
377-
}
379+
pb_type_Image_handle_display_update(self);
378380

379381
return mp_const_none;
380382
}
@@ -411,9 +413,7 @@ static mp_obj_t pb_type_Image_print(size_t n_args, const mp_obj_t *pos_args, mp_
411413

412414
vstr_clear(&vstr);
413415

414-
if (self->is_display) {
415-
pbdrv_display_update();
416-
}
416+
pb_type_Image_handle_display_update(self);
417417

418418
return mp_const_none;
419419
}
@@ -474,7 +474,7 @@ static void pb_type_image_file_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest)
474474

475475
// This is a standalone image.
476476
self->owner = MP_OBJ_NULL;
477-
self->is_display = false;
477+
self->display_type = PB_TYPE_IMAGE_DISPLAY_NONE;
478478

479479
// Default to same colors as display.
480480
pbio_image_t *display = pbdrv_display_get_image();

tests/virtualhub/basics/hello.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from pybricks.tools import wait
22

3+
from pybricks.hubs import VirtualHub
4+
5+
hub = VirtualHub()
6+
hub.screen.print("HI")
7+
hub.screen.print("HI")
8+
39
print("\nHello")
4-
wait(1000)
10+
wait(10000)
511
print("World\n")

0 commit comments

Comments
 (0)