|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/displayio/OnDiskGif.h" |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "py/objproperty.h" |
| 33 | +#include "supervisor/shared/translate/translate.h" |
| 34 | +#include "shared-bindings/displayio/OnDiskGif.h" |
| 35 | + |
| 36 | +//| class OnDiskBitmap: |
| 37 | +//| """Loads values straight from disk. This minimizes memory use but can lead to |
| 38 | +//| much slower pixel load times. These load times may result in frame tearing where only part of |
| 39 | +//| the image is visible. |
| 40 | +//| |
| 41 | +//| It's easiest to use on a board with a built in display such as the `Hallowing M0 Express |
| 42 | +//| <https://www.adafruit.com/product/3900>`_. |
| 43 | +//| |
| 44 | +//| .. code-block:: Python |
| 45 | +//| |
| 46 | +//| import board |
| 47 | +//| import displayio |
| 48 | +//| import time |
| 49 | +//| import pulseio |
| 50 | +//| |
| 51 | +//| board.DISPLAY.brightness = 0 |
| 52 | +//| splash = displayio.Group() |
| 53 | +//| board.DISPLAY.show(splash) |
| 54 | +//| |
| 55 | +//| odb = displayio.OnDiskBitmap('/sample.bmp') |
| 56 | +//| face = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader) |
| 57 | +//| splash.append(face) |
| 58 | +//| # Wait for the image to load. |
| 59 | +//| board.DISPLAY.refresh(target_frames_per_second=60) |
| 60 | +//| |
| 61 | +//| # Fade up the backlight |
| 62 | +//| for i in range(100): |
| 63 | +//| board.DISPLAY.brightness = 0.01 * i |
| 64 | +//| time.sleep(0.05) |
| 65 | +//| |
| 66 | +//| # Wait forever |
| 67 | +//| while True: |
| 68 | +//| pass""" |
| 69 | +//| |
| 70 | +//| def __init__(self, file: Union[str, typing.BinaryIO]) -> None: |
| 71 | +//| """Create an OnDiskBitmap object with the given file. |
| 72 | +//| |
| 73 | +//| :param file file: The name of the bitmap file. For backwards compatibility, a file opened in binary mode may also be passed. |
| 74 | +//| |
| 75 | +//| Older versions of CircuitPython required a file opened in binary |
| 76 | +//| mode. CircuitPython 7.0 modified OnDiskBitmap so that it takes a |
| 77 | +//| filename instead, and opens the file internally. A future version |
| 78 | +//| of CircuitPython will remove the ability to pass in an opened file. |
| 79 | +//| """ |
| 80 | +//| ... |
| 81 | +STATIC mp_obj_t displayio_ondiskgif_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 82 | + mp_arg_check_num(n_args, n_kw, 1, 1, false); |
| 83 | + mp_obj_t arg = all_args[0]; |
| 84 | + |
| 85 | + if (mp_obj_is_str(arg)) { |
| 86 | + arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb)); |
| 87 | + } |
| 88 | + |
| 89 | + if (!mp_obj_is_type(arg, &mp_type_fileio)) { |
| 90 | + mp_raise_TypeError(translate("file must be a file opened in byte mode")); |
| 91 | + } |
| 92 | + |
| 93 | + displayio_ondiskgif_t *self = m_new_obj(displayio_ondiskgif_t); |
| 94 | + self->base.type = &displayio_ondiskgif_type; |
| 95 | + common_hal_displayio_ondiskgif_construct(self, MP_OBJ_TO_PTR(arg)); |
| 96 | + |
| 97 | + return MP_OBJ_FROM_PTR(self); |
| 98 | +} |
| 99 | + |
| 100 | +//| width: int |
| 101 | +//| """Width of the bitmap. (read only)""" |
| 102 | +STATIC mp_obj_t displayio_ondiskgif_obj_get_width(mp_obj_t self_in) { |
| 103 | + displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in); |
| 104 | + |
| 105 | + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_width(self)); |
| 106 | +} |
| 107 | + |
| 108 | +MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_width_obj, displayio_ondiskgif_obj_get_width); |
| 109 | + |
| 110 | +MP_PROPERTY_GETTER(displayio_ondiskgif_width_obj, |
| 111 | + (mp_obj_t)&displayio_ondiskgif_get_width_obj); |
| 112 | + |
| 113 | +//| height: int |
| 114 | +//| """Height of the bitmap. (read only)""" |
| 115 | +STATIC mp_obj_t displayio_ondiskgif_obj_get_height(mp_obj_t self_in) { |
| 116 | + displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in); |
| 117 | + |
| 118 | + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_get_height(self)); |
| 119 | +} |
| 120 | + |
| 121 | +MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_height_obj, displayio_ondiskgif_obj_get_height); |
| 122 | + |
| 123 | +MP_PROPERTY_GETTER(displayio_ondiskgif_height_obj, |
| 124 | + (mp_obj_t)&displayio_ondiskgif_get_height_obj); |
| 125 | + |
| 126 | +//| pixel_shader: Union[ColorConverter, Palette] |
| 127 | +//| """The image's pixel_shader. The type depends on the underlying |
| 128 | +//| bitmap's structure. The pixel shader can be modified (e.g., to set the |
| 129 | +//| transparent pixel or, for palette shaded images, to update the palette.)""" |
| 130 | +//| |
| 131 | +STATIC mp_obj_t displayio_ondiskgif_obj_get_pixel_shader(mp_obj_t self_in) { |
| 132 | + displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in); |
| 133 | + return common_hal_displayio_ondiskgif_get_pixel_shader(self); |
| 134 | +} |
| 135 | + |
| 136 | +MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_pixel_shader_obj, displayio_ondiskgif_obj_get_pixel_shader); |
| 137 | + |
| 138 | +const mp_obj_property_t displayio_ondiskgif_pixel_shader_obj = { |
| 139 | + .base.type = &mp_type_property, |
| 140 | + .proxy = {(mp_obj_t)&displayio_ondiskgif_get_pixel_shader_obj, |
| 141 | + (mp_obj_t)MP_ROM_NONE, |
| 142 | + (mp_obj_t)MP_ROM_NONE}, |
| 143 | +}; |
| 144 | + |
| 145 | +//| bitmap: Bitmap |
| 146 | +//| """The image's bitmap. The type depends on the underlying |
| 147 | +//| bitmap's structure. The pixel shader can be modified (e.g., to set the |
| 148 | +//| transparent pixel or, for palette shaded images, to update the palette.)""" |
| 149 | +//| |
| 150 | +STATIC mp_obj_t displayio_ondiskgif_obj_get_bitmap(mp_obj_t self_in) { |
| 151 | + displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in); |
| 152 | + return common_hal_displayio_ondiskgif_get_bitmap(self); |
| 153 | +} |
| 154 | + |
| 155 | +MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_get_bitmap_obj, displayio_ondiskgif_obj_get_bitmap); |
| 156 | + |
| 157 | +const mp_obj_property_t displayio_ondiskgif_bitmap_obj = { |
| 158 | + .base.type = &mp_type_property, |
| 159 | + .proxy = {(mp_obj_t)&displayio_ondiskgif_get_bitmap_obj, |
| 160 | + (mp_obj_t)MP_ROM_NONE, |
| 161 | + (mp_obj_t)MP_ROM_NONE}, |
| 162 | +}; |
| 163 | + |
| 164 | + |
| 165 | +//| play_frame: None |
| 166 | +//| """Play next frame. (read only)""" |
| 167 | +//| |
| 168 | +STATIC mp_obj_t displayio_ondiskgif_obj_play_frame(mp_obj_t self_in) { |
| 169 | + displayio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in); |
| 170 | + |
| 171 | + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskgif_play_frame(self)); |
| 172 | +} |
| 173 | + |
| 174 | +MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskgif_play_frame_obj, displayio_ondiskgif_obj_play_frame); |
| 175 | + |
| 176 | + |
| 177 | +STATIC const mp_rom_map_elem_t displayio_ondiskgif_locals_dict_table[] = { |
| 178 | + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_ondiskgif_height_obj) }, |
| 179 | + { MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_ondiskgif_pixel_shader_obj) }, |
| 180 | + { MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&displayio_ondiskgif_bitmap_obj) }, |
| 181 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_ondiskgif_width_obj) }, |
| 182 | + { MP_ROM_QSTR(MP_QSTR_play_frame), MP_ROM_PTR(&displayio_ondiskgif_play_frame_obj) }, |
| 183 | +}; |
| 184 | +STATIC MP_DEFINE_CONST_DICT(displayio_ondiskgif_locals_dict, displayio_ondiskgif_locals_dict_table); |
| 185 | + |
| 186 | +const mp_obj_type_t displayio_ondiskgif_type = { |
| 187 | + { &mp_type_type }, |
| 188 | + .name = MP_QSTR_OnDiskGif, |
| 189 | + .make_new = displayio_ondiskgif_make_new, |
| 190 | + .locals_dict = (mp_obj_dict_t *)&displayio_ondiskgif_locals_dict, |
| 191 | +}; |
0 commit comments