|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024 Jeff Epler 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 "py/obj.h" |
| 28 | +#include "py/objproperty.h" |
| 29 | + |
| 30 | +#include "shared-bindings/usb_video/__init__.h" |
| 31 | +#include "shared-bindings/usb_video/USBFramebuffer.h" |
| 32 | +#include "shared-module/displayio/__init__.h" |
| 33 | +#include "shared-module/framebufferio/FramebufferDisplay.h" |
| 34 | +#include "shared-module/framebufferio/__init__.h" |
| 35 | +#include "shared-module/usb_video/__init__.h" |
| 36 | +#include "shared-module/usb_video/USBFramebuffer.h" |
| 37 | +#include "shared-bindings/util.h" |
| 38 | + |
| 39 | +static void check_for_deinit(usb_video_uvcframebuffer_obj_t *self) { |
| 40 | + if (!shared_module_usb_video_uvcframebuffer_get_width(self)) { |
| 41 | + raise_deinited_error(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +//| class USBFramebuffer: |
| 46 | +//| """Displays to a USB connected computer using the UVC protocol |
| 47 | +//| |
| 48 | +//| The data in the framebuffer is in RGB565_SWAPPED format. |
| 49 | +//| |
| 50 | +//| This object is most often used with `framebufferio.FramebufferDisplay`. However, |
| 51 | +//| it also supports the `WritableBuffer` protocol and can be accessed |
| 52 | +//| as an array of `H` (unsigned 16-bit values).""" |
| 53 | +//| |
| 54 | +//| def __new__(self): |
| 55 | +//| """Returns the singleton framebuffer object, if USB video is enabled""" |
| 56 | +STATIC mp_obj_t usb_video_uvcframebuffer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 57 | + static const mp_arg_t allowed_args[] = {}; |
| 58 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 59 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 60 | + usb_video_uvcframebuffer_obj_t *self = &usb_video_uvcframebuffer_singleton_obj; |
| 61 | + return self; |
| 62 | +} |
| 63 | + |
| 64 | +//| def refresh(self) -> None: |
| 65 | +//| """Transmits the color data in the buffer to the pixels so that |
| 66 | +//| they are shown.""" |
| 67 | +//| ... |
| 68 | +STATIC mp_obj_t usb_video_uvcframebuffer_refresh(mp_obj_t self_in) { |
| 69 | + usb_video_uvcframebuffer_obj_t *self = (usb_video_uvcframebuffer_obj_t *)self_in; |
| 70 | + check_for_deinit(self); |
| 71 | + shared_module_usb_video_uvcframebuffer_refresh(self); |
| 72 | + return mp_const_none; |
| 73 | +} |
| 74 | +MP_DEFINE_CONST_FUN_OBJ_1(usb_video_uvcframebuffer_refresh_obj, usb_video_uvcframebuffer_refresh); |
| 75 | + |
| 76 | +//| width: int |
| 77 | +//| """The width of the display, in pixels""" |
| 78 | +STATIC mp_obj_t usb_video_uvcframebuffer_get_width(mp_obj_t self_in) { |
| 79 | + usb_video_uvcframebuffer_obj_t *self = (usb_video_uvcframebuffer_obj_t *)self_in; |
| 80 | + check_for_deinit(self); |
| 81 | + return MP_OBJ_NEW_SMALL_INT(shared_module_usb_video_uvcframebuffer_get_width(self)); |
| 82 | +} |
| 83 | +MP_DEFINE_CONST_FUN_OBJ_1(usb_video_uvcframebuffer_get_width_obj, usb_video_uvcframebuffer_get_width); |
| 84 | +MP_PROPERTY_GETTER(usb_video_uvcframebuffer_width_obj, |
| 85 | + (mp_obj_t)&usb_video_uvcframebuffer_get_width_obj); |
| 86 | + |
| 87 | +//| height: int |
| 88 | +//| """The height of the display, in pixels""" |
| 89 | +//| |
| 90 | +STATIC mp_obj_t usb_video_uvcframebuffer_get_height(mp_obj_t self_in) { |
| 91 | + usb_video_uvcframebuffer_obj_t *self = (usb_video_uvcframebuffer_obj_t *)self_in; |
| 92 | + check_for_deinit(self); |
| 93 | + return MP_OBJ_NEW_SMALL_INT(shared_module_usb_video_uvcframebuffer_get_height(self)); |
| 94 | +} |
| 95 | +MP_DEFINE_CONST_FUN_OBJ_1(usb_video_uvcframebuffer_get_height_obj, usb_video_uvcframebuffer_get_height); |
| 96 | + |
| 97 | +MP_PROPERTY_GETTER(usb_video_uvcframebuffer_height_obj, |
| 98 | + (mp_obj_t)&usb_video_uvcframebuffer_get_height_obj); |
| 99 | + |
| 100 | +STATIC const mp_rom_map_elem_t usb_video_uvcframebuffer_locals_dict_table[] = { |
| 101 | + { MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&usb_video_uvcframebuffer_refresh_obj) }, |
| 102 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&usb_video_uvcframebuffer_width_obj) }, |
| 103 | + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&usb_video_uvcframebuffer_height_obj) }, |
| 104 | +}; |
| 105 | +STATIC MP_DEFINE_CONST_DICT(usb_video_uvcframebuffer_locals_dict, usb_video_uvcframebuffer_locals_dict_table); |
| 106 | + |
| 107 | +STATIC void usb_video_uvcframebuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) { |
| 108 | + shared_module_usb_video_uvcframebuffer_get_bufinfo(self_in, bufinfo); |
| 109 | +} |
| 110 | + |
| 111 | +// These version exists so that the prototype matches the protocol, |
| 112 | +// avoiding a type cast that can hide errors |
| 113 | +STATIC void usb_video_uvcframebuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) { |
| 114 | + (void)dirty_row_bitmap; |
| 115 | + shared_module_usb_video_uvcframebuffer_refresh(self_in); |
| 116 | +} |
| 117 | + |
| 118 | +STATIC void usb_video_uvcframebuffer_deinit_proto(mp_obj_t self_in) { |
| 119 | + /* NOTHING */ |
| 120 | +} |
| 121 | + |
| 122 | +STATIC int usb_video_uvcframebuffer_get_width_proto(mp_obj_t self_in) { |
| 123 | + return shared_module_usb_video_uvcframebuffer_get_width(self_in); |
| 124 | +} |
| 125 | + |
| 126 | +STATIC int usb_video_uvcframebuffer_get_height_proto(mp_obj_t self_in) { |
| 127 | + return shared_module_usb_video_uvcframebuffer_get_height(self_in); |
| 128 | +} |
| 129 | + |
| 130 | +STATIC int usb_video_uvcframebuffer_get_native_frames_per_second_proto(mp_obj_t self_in) { |
| 131 | + return 10; |
| 132 | +} |
| 133 | + |
| 134 | +STATIC bool usb_video_uvcframebuffer_get_reverse_pixels_in_word_proto(mp_obj_t self_in) { |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +STATIC const framebuffer_p_t usb_video_uvcframebuffer_proto = { |
| 140 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer) |
| 141 | + .get_bufinfo = usb_video_uvcframebuffer_get_bufinfo, |
| 142 | + .get_width = usb_video_uvcframebuffer_get_width_proto, |
| 143 | + .get_height = usb_video_uvcframebuffer_get_height_proto, |
| 144 | + .get_native_frames_per_second = usb_video_uvcframebuffer_get_native_frames_per_second_proto, |
| 145 | + .swapbuffers = usb_video_uvcframebuffer_swapbuffers, |
| 146 | + .deinit = usb_video_uvcframebuffer_deinit_proto, |
| 147 | + .get_reverse_pixels_in_word = usb_video_uvcframebuffer_get_reverse_pixels_in_word_proto, |
| 148 | +}; |
| 149 | + |
| 150 | +STATIC mp_int_t usb_video_uvcframebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
| 151 | + shared_module_usb_video_uvcframebuffer_get_bufinfo(self_in, bufinfo); |
| 152 | + bufinfo->typecode = 'H'; |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
| 156 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 157 | + usb_video_USBFramebuffer_type, |
| 158 | + MP_QSTR_USBFramebuffer, |
| 159 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 160 | + locals_dict, &usb_video_uvcframebuffer_locals_dict, |
| 161 | + make_new, usb_video_uvcframebuffer_make_new, |
| 162 | + buffer, usb_video_uvcframebuffer_get_buffer, |
| 163 | + protocol, &usb_video_uvcframebuffer_proto |
| 164 | + ); |
0 commit comments