|
| 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/uvc/__init__.h" |
| 31 | +#include "shared-bindings/uvc/UVCFramebuffer.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/uvc/__init__.h" |
| 36 | +#include "shared-module/uvc/UVCFramebuffer.h" |
| 37 | +#include "shared-bindings/util.h" |
| 38 | + |
| 39 | +static void check_for_deinit(uvc_uvcframebuffer_obj_t *self) { |
| 40 | + if (!shared_module_uvc_uvcframebuffer_get_width(self)) { |
| 41 | + raise_deinited_error(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +//| class UVCFramebuffer: |
| 46 | +//| """Displays to a USB connected computer using the UVC protocol""" |
| 47 | +//| |
| 48 | +//| def __new__(): |
| 49 | +//| """Returns the singleton UVC framebuffer object, if UVC is enabled""" |
| 50 | +// |
| 51 | +//| def refresh(self) -> None: |
| 52 | +//| """Transmits the color data in the buffer to the pixels so that |
| 53 | +//| they are shown.""" |
| 54 | +//| ... |
| 55 | +STATIC mp_obj_t uvc_uvcframebuffer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 56 | + static const mp_arg_t allowed_args[] = {}; |
| 57 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 58 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 59 | + uvc_uvcframebuffer_obj_t *self = &uvc_uvcframebuffer_singleton_obj; |
| 60 | + return self; |
| 61 | +} |
| 62 | + |
| 63 | +STATIC mp_obj_t uvc_uvcframebuffer_refresh(mp_obj_t self_in) { |
| 64 | + uvc_uvcframebuffer_obj_t *self = (uvc_uvcframebuffer_obj_t *)self_in; |
| 65 | + check_for_deinit(self); |
| 66 | + shared_module_uvc_uvcframebuffer_refresh(self); |
| 67 | + return mp_const_none; |
| 68 | +} |
| 69 | +MP_DEFINE_CONST_FUN_OBJ_1(uvc_uvcframebuffer_refresh_obj, uvc_uvcframebuffer_refresh); |
| 70 | + |
| 71 | +//| width: int |
| 72 | +//| """The width of the display, in pixels""" |
| 73 | +STATIC mp_obj_t uvc_uvcframebuffer_get_width(mp_obj_t self_in) { |
| 74 | + uvc_uvcframebuffer_obj_t *self = (uvc_uvcframebuffer_obj_t *)self_in; |
| 75 | + check_for_deinit(self); |
| 76 | + return MP_OBJ_NEW_SMALL_INT(shared_module_uvc_uvcframebuffer_get_width(self)); |
| 77 | +} |
| 78 | +MP_DEFINE_CONST_FUN_OBJ_1(uvc_uvcframebuffer_get_width_obj, uvc_uvcframebuffer_get_width); |
| 79 | +MP_PROPERTY_GETTER(uvc_uvcframebuffer_width_obj, |
| 80 | + (mp_obj_t)&uvc_uvcframebuffer_get_width_obj); |
| 81 | + |
| 82 | +//| height: int |
| 83 | +//| """The height of the display, in pixels""" |
| 84 | +//| |
| 85 | +STATIC mp_obj_t uvc_uvcframebuffer_get_height(mp_obj_t self_in) { |
| 86 | + uvc_uvcframebuffer_obj_t *self = (uvc_uvcframebuffer_obj_t *)self_in; |
| 87 | + check_for_deinit(self); |
| 88 | + return MP_OBJ_NEW_SMALL_INT(shared_module_uvc_uvcframebuffer_get_height(self)); |
| 89 | +} |
| 90 | +MP_DEFINE_CONST_FUN_OBJ_1(uvc_uvcframebuffer_get_height_obj, uvc_uvcframebuffer_get_height); |
| 91 | + |
| 92 | +MP_PROPERTY_GETTER(uvc_uvcframebuffer_height_obj, |
| 93 | + (mp_obj_t)&uvc_uvcframebuffer_get_height_obj); |
| 94 | + |
| 95 | +STATIC const mp_rom_map_elem_t uvc_uvcframebuffer_locals_dict_table[] = { |
| 96 | + { MP_ROM_QSTR(MP_QSTR_refresh), MP_ROM_PTR(&uvc_uvcframebuffer_refresh_obj) }, |
| 97 | + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&uvc_uvcframebuffer_width_obj) }, |
| 98 | + { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&uvc_uvcframebuffer_height_obj) }, |
| 99 | +}; |
| 100 | +STATIC MP_DEFINE_CONST_DICT(uvc_uvcframebuffer_locals_dict, uvc_uvcframebuffer_locals_dict_table); |
| 101 | + |
| 102 | +STATIC void uvc_uvcframebuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) { |
| 103 | + shared_module_uvc_uvcframebuffer_get_bufinfo(self_in, bufinfo); |
| 104 | +} |
| 105 | + |
| 106 | +// These version exists so that the prototype matches the protocol, |
| 107 | +// avoiding a type cast that can hide errors |
| 108 | +STATIC void uvc_uvcframebuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) { |
| 109 | + (void)dirty_row_bitmap; |
| 110 | + shared_module_uvc_uvcframebuffer_refresh(self_in); |
| 111 | +} |
| 112 | + |
| 113 | +STATIC void uvc_uvcframebuffer_deinit_proto(mp_obj_t self_in) { |
| 114 | + /* NOTHING */ |
| 115 | +} |
| 116 | + |
| 117 | +STATIC int uvc_uvcframebuffer_get_width_proto(mp_obj_t self_in) { |
| 118 | + return shared_module_uvc_uvcframebuffer_get_width(self_in); |
| 119 | +} |
| 120 | + |
| 121 | +STATIC int uvc_uvcframebuffer_get_height_proto(mp_obj_t self_in) { |
| 122 | + return shared_module_uvc_uvcframebuffer_get_height(self_in); |
| 123 | +} |
| 124 | + |
| 125 | +STATIC int uvc_uvcframebuffer_get_native_frames_per_second_proto(mp_obj_t self_in) { |
| 126 | + return 10; |
| 127 | +} |
| 128 | + |
| 129 | +STATIC bool uvc_uvcframebuffer_get_reverse_pixels_in_word_proto(mp_obj_t self_in) { |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +STATIC const framebuffer_p_t uvc_uvcframebuffer_proto = { |
| 135 | + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer) |
| 136 | + .get_bufinfo = uvc_uvcframebuffer_get_bufinfo, |
| 137 | + .get_width = uvc_uvcframebuffer_get_width_proto, |
| 138 | + .get_height = uvc_uvcframebuffer_get_height_proto, |
| 139 | + .get_native_frames_per_second = uvc_uvcframebuffer_get_native_frames_per_second_proto, |
| 140 | + .swapbuffers = uvc_uvcframebuffer_swapbuffers, |
| 141 | + .deinit = uvc_uvcframebuffer_deinit_proto, |
| 142 | + .get_reverse_pixels_in_word = uvc_uvcframebuffer_get_reverse_pixels_in_word_proto, |
| 143 | +}; |
| 144 | + |
| 145 | +STATIC mp_int_t uvc_uvcframebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
| 146 | + shared_module_uvc_uvcframebuffer_get_bufinfo(self_in, bufinfo); |
| 147 | + bufinfo->typecode = 'H'; |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 152 | + uvc_UVCFramebuffer_type, |
| 153 | + MP_QSTR_UVCFramebuffer, |
| 154 | + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, |
| 155 | + locals_dict, &uvc_uvcframebuffer_locals_dict, |
| 156 | + make_new, uvc_uvcframebuffer_make_new, |
| 157 | + buffer, uvc_uvcframebuffer_get_buffer, |
| 158 | + protocol, &uvc_uvcframebuffer_proto |
| 159 | + ); |
0 commit comments