|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Radomir Dopieralski 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 | +#include "py/obj.h" |
| 27 | +#include "py/runtime.h" |
| 28 | +#include "py/mphal.h" |
| 29 | +#include "py/gc.h" |
| 30 | +#include "py/mpstate.h" |
| 31 | +#include "shared-bindings/digitalio/DigitalInOut.h" |
| 32 | +#include "shared-bindings/util.h" |
| 33 | +#include "PewPew.h" |
| 34 | +#include "shared-module/_pew/PewPew.h" |
| 35 | + |
| 36 | + |
| 37 | +//| .. currentmodule:: _pew |
| 38 | +//| |
| 39 | +//| :class:`PewPew` -- LED Matrix driver |
| 40 | +//| ==================================== |
| 41 | +//| |
| 42 | +//| Usage:: |
| 43 | +//| |
| 44 | +//| |
| 45 | + |
| 46 | +//| .. class:: PewPew(buffer, rows, cols) |
| 47 | +//| |
| 48 | +//| Initializes matrix scanning routines. |
| 49 | +//| |
| 50 | +//| |
| 51 | +STATIC mp_obj_t pewpew_make_new(const mp_obj_type_t *type, size_t n_args, |
| 52 | + size_t n_kw, const mp_obj_t *pos_args) { |
| 53 | + mp_arg_check_num(n_args, n_kw, 3, 3, true); |
| 54 | + mp_map_t kw_args; |
| 55 | + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); |
| 56 | + enum { ARG_buffer, ARG_rows, ARG_cols }; |
| 57 | + static const mp_arg_t allowed_args[] = { |
| 58 | + { MP_QSTR_buffer, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 59 | + { MP_QSTR_rows, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 60 | + { MP_QSTR_cols, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 61 | + }; |
| 62 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 63 | + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), |
| 64 | + allowed_args, args); |
| 65 | + |
| 66 | + mp_buffer_info_t bufinfo; |
| 67 | + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); |
| 68 | + |
| 69 | + size_t rows_size = 0; |
| 70 | + mp_obj_t *rows; |
| 71 | + mp_obj_get_array(args[ARG_rows].u_obj, &rows_size, &rows); |
| 72 | + |
| 73 | + size_t cols_size = 0; |
| 74 | + mp_obj_t *cols; |
| 75 | + mp_obj_get_array(args[ARG_cols].u_obj, &cols_size, &cols); |
| 76 | + |
| 77 | + if (bufinfo.len != rows_size * cols_size) { |
| 78 | + mp_raise_TypeError("wrong buffer size"); |
| 79 | + } |
| 80 | + |
| 81 | + for (size_t i = 0; i < rows_size; ++i) { |
| 82 | + if (!MP_OBJ_IS_TYPE(rows[i], &digitalio_digitalinout_type)) { |
| 83 | + mp_raise_TypeError("expected a DigitalInOut"); |
| 84 | + } |
| 85 | + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(rows[i]); |
| 86 | + raise_error_if_deinited( |
| 87 | + common_hal_digitalio_digitalinout_deinited(pin)); |
| 88 | + } |
| 89 | + |
| 90 | + for (size_t i = 0; i < cols_size; ++i) { |
| 91 | + if (!MP_OBJ_IS_TYPE(cols[i], &digitalio_digitalinout_type)) { |
| 92 | + mp_raise_TypeError("expected a DigitalInOut"); |
| 93 | + } |
| 94 | + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(cols[i]); |
| 95 | + raise_error_if_deinited( |
| 96 | + common_hal_digitalio_digitalinout_deinited(pin)); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + pew_obj_t *pew = MP_STATE_VM(pew_singleton); |
| 101 | + if (!pew) { |
| 102 | + pew = m_new_obj(pew_obj_t); |
| 103 | + pew->base.type = &pewpew_type; |
| 104 | + pew = gc_make_long_lived(pew); |
| 105 | + MP_STATE_VM(pew_singleton) = pew; |
| 106 | + } |
| 107 | + |
| 108 | + pew->buffer = bufinfo.buf; |
| 109 | + pew->rows = rows; |
| 110 | + pew->rows_size = rows_size; |
| 111 | + pew->cols = cols; |
| 112 | + pew->cols_size = cols_size; |
| 113 | + pew->col = 0; |
| 114 | + pew->turn = 0; |
| 115 | + pew_init(); |
| 116 | + |
| 117 | + return MP_OBJ_FROM_PTR(pew); |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +STATIC const mp_rom_map_elem_t pewpew_locals_dict_table[] = { |
| 122 | +}; |
| 123 | +STATIC MP_DEFINE_CONST_DICT(pewpew_locals_dict, pewpew_locals_dict_table); |
| 124 | +const mp_obj_type_t pewpew_type = { |
| 125 | + { &mp_type_type }, |
| 126 | + .name = MP_QSTR_PewPew, |
| 127 | + .make_new = pewpew_make_new, |
| 128 | + .locals_dict = (mp_obj_dict_t*)&pewpew_locals_dict, |
| 129 | +}; |
| 130 | + |
0 commit comments