Skip to content

Commit b228891

Browse files
authored
Merge pull request #5017 from jepler/ondiskbitmap
OnDiskBitmap improvements
2 parents 9a373c4 + 7896bf7 commit b228891

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

shared-bindings/displayio/OnDiskBitmap.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,47 @@
5353
//| splash = displayio.Group()
5454
//| board.DISPLAY.show(splash)
5555
//|
56-
//| with open("/sample.bmp", "rb") as f:
57-
//| odb = displayio.OnDiskBitmap(f)
58-
//| face = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
59-
//| splash.append(face)
60-
//| # Wait for the image to load.
61-
//| board.DISPLAY.refresh(target_frames_per_second=60)
56+
//| odb = displayio.OnDiskBitmap('/sample.bmp')
57+
//| face = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
58+
//| splash.append(face)
59+
//| # Wait for the image to load.
60+
//| board.DISPLAY.refresh(target_frames_per_second=60)
6261
//|
63-
//| # Fade up the backlight
64-
//| for i in range(100):
65-
//| board.DISPLAY.brightness = 0.01 * i
66-
//| time.sleep(0.05)
62+
//| # Fade up the backlight
63+
//| for i in range(100):
64+
//| board.DISPLAY.brightness = 0.01 * i
65+
//| time.sleep(0.05)
6766
//|
68-
//| # Wait forever
69-
//| while True:
70-
//| pass"""
67+
//| # Wait forever
68+
//| while True:
69+
//| pass"""
7170
//|
72-
//| def __init__(self, file: typing.BinaryIO) -> None:
71+
//| def __init__(self, file: Union[str,typing.BinaryIO]) -> None:
7372
//| """Create an OnDiskBitmap object with the given file.
7473
//|
75-
//| :param file file: The open bitmap file"""
74+
//| :param file file: The name of the bitmap file. For backwards compatibility, a file opened in binary mode may also be passed.
75+
//|
76+
//| Older versions of CircuitPython required a file opened in binary
77+
//| mode. CircuitPython 7.0 modified OnDiskBitmap so that it takes a
78+
//| filename instead, and opens the file internally. A future version
79+
//| of CircuitPython will remove the ability to pass in an opened file.
80+
//| """
7681
//| ...
7782
//|
7883
STATIC mp_obj_t displayio_ondiskbitmap_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
7984
mp_arg_check_num(n_args, kw_args, 1, 1, false);
85+
mp_obj_t arg = pos_args[0];
8086

81-
if (!mp_obj_is_type(pos_args[0], &mp_type_fileio)) {
87+
if (mp_obj_is_str(arg)) {
88+
arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb));
89+
}
90+
if (!mp_obj_is_type(arg, &mp_type_fileio)) {
8291
mp_raise_TypeError(translate("file must be a file opened in byte mode"));
8392
}
8493

8594
displayio_ondiskbitmap_t *self = m_new_obj(displayio_ondiskbitmap_t);
8695
self->base.type = &displayio_ondiskbitmap_type;
87-
common_hal_displayio_ondiskbitmap_construct(self, MP_OBJ_TO_PTR(pos_args[0]));
96+
common_hal_displayio_ondiskbitmap_construct(self, MP_OBJ_TO_PTR(arg));
8897

8998
return MP_OBJ_FROM_PTR(self);
9099
}

shared-module/displayio/OnDiskBitmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
108108
if (palette_bytes_read != palette_size) {
109109
mp_raise_ValueError(translate("Unable to read color palette data"));
110110
}
111-
for (uint16_t i = 0; i < palette_size; i++) {
111+
for (uint16_t i = 0; i < number_of_colors; i++) {
112112
common_hal_displayio_palette_set_color(palette, i, palette_data[i]);
113113
}
114114
m_free(palette_data);

0 commit comments

Comments
 (0)