|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 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/objarray.h" |
| 28 | +#include "py/runtime.h" |
| 29 | + |
| 30 | +#include "shared-bindings/busio/SPI.h" |
| 31 | +#include "shared-bindings/microcontroller/Pin.h" |
| 32 | +#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h" |
| 33 | +#include "shared-module/displayio/__init__.h" |
| 34 | +#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" |
| 35 | + |
| 36 | +STATIC mp_obj_t sharpdisplay_framebuffer_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 37 | + enum { ARG_spi_bus, ARG_chip_select, ARG_width, ARG_height, ARG_baudrate, NUM_ARGS }; |
| 38 | + static const mp_arg_t allowed_args[] = { |
| 39 | + { MP_QSTR_spi_bus, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, |
| 40 | + { MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, |
| 41 | + { MP_QSTR_width, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} }, |
| 42 | + { MP_QSTR_height, MP_ARG_INT | MP_ARG_REQUIRED, {.u_int = 0} }, |
| 43 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 2000000} }, |
| 44 | + }; |
| 45 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 46 | + MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS ); |
| 47 | + |
| 48 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 49 | + |
| 50 | + mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj); |
| 51 | + busio_spi_obj_t *spi = validate_obj_is_spi_bus(args[ARG_spi_bus].u_obj); |
| 52 | + |
| 53 | + sharpdisplay_framebuffer_obj_t* self = &allocate_display_bus_or_raise()->sharpdisplay; |
| 54 | + self->base.type = &sharpdisplay_framebuffer_type; |
| 55 | + |
| 56 | + common_hal_sharpdisplay_framebuffer_construct(self, spi, chip_select, args[ARG_baudrate].u_int, args[ARG_width].u_int, args[ARG_height].u_int); |
| 57 | + |
| 58 | + return MP_OBJ_FROM_PTR(self); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +STATIC mp_int_t sharpdisplay_framebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { |
| 63 | + sharpdisplay_framebuffer_obj_t *self = (sharpdisplay_framebuffer_obj_t*)self_in; |
| 64 | + // a readonly framebuffer would be unusual but not impossible |
| 65 | + if ((flags & MP_BUFFER_WRITE) && !(self->bufinfo.typecode & MP_OBJ_ARRAY_TYPECODE_FLAG_RW)) { |
| 66 | + return 1; |
| 67 | + } |
| 68 | + *bufinfo = self->bufinfo; |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +STATIC mp_obj_t sharpdisplay_framebuffer_deinit(mp_obj_t self_in) { |
| 73 | + sharpdisplay_framebuffer_obj_t *self = (sharpdisplay_framebuffer_obj_t*)self_in; |
| 74 | + common_hal_sharpdisplay_framebuffer_deinit(self); |
| 75 | + |
| 76 | + return mp_const_none; |
| 77 | +} |
| 78 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(sharpdisplay_framebuffer_deinit_obj, sharpdisplay_framebuffer_deinit); |
| 79 | + |
| 80 | +STATIC const mp_rom_map_elem_t sharpdisplay_framebuffer_locals_dict_table[] = { |
| 81 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&sharpdisplay_framebuffer_deinit_obj) }, |
| 82 | +}; |
| 83 | +STATIC MP_DEFINE_CONST_DICT(sharpdisplay_framebuffer_locals_dict, sharpdisplay_framebuffer_locals_dict_table); |
| 84 | + |
| 85 | +const mp_obj_type_t sharpdisplay_framebuffer_type = { |
| 86 | + { &mp_type_type }, |
| 87 | + .name = MP_QSTR_SharpMemoryFramebuffer, |
| 88 | + .buffer_p = { .get_buffer = sharpdisplay_framebuffer_get_buffer, }, |
| 89 | + .make_new = sharpdisplay_framebuffer_make_new, |
| 90 | + .protocol = &sharpdisplay_framebuffer_proto, |
| 91 | + .locals_dict = (mp_obj_dict_t*)&sharpdisplay_framebuffer_locals_dict, |
| 92 | +}; |
0 commit comments