|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Paul Sokolovsky |
| 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/mpconfig.h" |
| 28 | +#if MICROPY_PY_MACHINE |
| 29 | + |
| 30 | +#include "py/obj.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "extmod/virtpin.h" |
| 33 | +#include "extmod/machine_signal.h" |
| 34 | + |
| 35 | +// Signal class |
| 36 | + |
| 37 | +typedef struct _machine_signal_t { |
| 38 | + mp_obj_base_t base; |
| 39 | + mp_obj_t pin; |
| 40 | + bool inverted; |
| 41 | +} machine_signal_t; |
| 42 | + |
| 43 | +STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 44 | + enum { ARG_pin, ARG_inverted }; |
| 45 | + static const mp_arg_t allowed_args[] = { |
| 46 | + { MP_QSTR_, MP_ARG_OBJ | MP_ARG_REQUIRED }, |
| 47 | + { MP_QSTR_inverted, MP_ARG_BOOL, {.u_bool = false} }, |
| 48 | + }; |
| 49 | + |
| 50 | + mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)]; |
| 51 | + |
| 52 | + mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args); |
| 53 | + |
| 54 | + machine_signal_t *o = m_new_obj(machine_signal_t); |
| 55 | + o->base.type = type; |
| 56 | + o->pin = parsed_args[ARG_pin].u_obj; |
| 57 | + o->inverted = parsed_args[ARG_inverted].u_bool; |
| 58 | + return MP_OBJ_FROM_PTR(o); |
| 59 | +} |
| 60 | + |
| 61 | +STATIC mp_uint_t signal_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { |
| 62 | + (void)errcode; |
| 63 | + machine_signal_t *self = MP_OBJ_TO_PTR(self_in); |
| 64 | + |
| 65 | + switch (request) { |
| 66 | + case MP_PIN_READ: { |
| 67 | + return mp_virtual_pin_read(self->pin) ^ self->inverted; |
| 68 | + } |
| 69 | + case MP_PIN_WRITE: { |
| 70 | + mp_virtual_pin_write(self->pin, arg ^ self->inverted); |
| 71 | + return 0; |
| 72 | + } |
| 73 | + } |
| 74 | + return -1; |
| 75 | +} |
| 76 | + |
| 77 | +// fast method for getting/setting signal value |
| 78 | +STATIC mp_obj_t signal_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 79 | + mp_arg_check_num(n_args, n_kw, 0, 1, false); |
| 80 | + if (n_args == 0) { |
| 81 | + // get pin |
| 82 | + return MP_OBJ_NEW_SMALL_INT(mp_virtual_pin_read(self_in)); |
| 83 | + } else { |
| 84 | + // set pin |
| 85 | + mp_virtual_pin_write(self_in, mp_obj_is_true(args[0])); |
| 86 | + return mp_const_none; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +STATIC mp_obj_t signal_value(size_t n_args, const mp_obj_t *args) { |
| 91 | + return signal_call(args[0], n_args - 1, 0, args + 1); |
| 92 | +} |
| 93 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(signal_value_obj, 1, 2, signal_value); |
| 94 | + |
| 95 | +STATIC const mp_rom_map_elem_t signal_locals_dict_table[] = { |
| 96 | + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&signal_value_obj) }, |
| 97 | +}; |
| 98 | + |
| 99 | +STATIC MP_DEFINE_CONST_DICT(signal_locals_dict, signal_locals_dict_table); |
| 100 | + |
| 101 | +STATIC const mp_pin_p_t signal_pin_p = { |
| 102 | + .ioctl = signal_ioctl, |
| 103 | +}; |
| 104 | + |
| 105 | +const mp_obj_type_t machine_signal_type = { |
| 106 | + { &mp_type_type }, |
| 107 | + .name = MP_QSTR_Signal, |
| 108 | + .make_new = signal_make_new, |
| 109 | + .call = signal_call, |
| 110 | + .protocol = &signal_pin_p, |
| 111 | + .locals_dict = (void*)&signal_locals_dict, |
| 112 | +}; |
| 113 | + |
| 114 | +#endif // MICROPY_PY_MACHINE |
0 commit comments