|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Dan Halbert 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 | +#include "py/runtime.h" |
| 30 | + |
| 31 | +#include "shared-bindings/keypad/Event.h" |
| 32 | + |
| 33 | +//| class Event: |
| 34 | +//| """A key transition event.""" |
| 35 | +//| def __init__(self, key_num: int, pressed: bool) -> None: |
| 36 | +//| """Create a key transition event, which reports a key-pressed or key-released transition. |
| 37 | +//| |
| 38 | +//| :param int key_num: the key number |
| 39 | +//| :param bool pressed: ``True`` if the key was pressed; ``False`` if it was released. |
| 40 | +//| """ |
| 41 | +//| ... |
| 42 | +//| |
| 43 | + |
| 44 | +STATIC mp_obj_t keypad_event_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 45 | + keypad_event_obj_t *self = m_new_obj(keypad_event_obj_t); |
| 46 | + self->base.type = &keypad_event_type; |
| 47 | + enum { ARG_key_num, ARG_pressed }; |
| 48 | + static const mp_arg_t allowed_args[] = { |
| 49 | + { MP_QSTR_key_num, MP_ARG_REQUIRED | MP_ARG_INT }, |
| 50 | + { MP_QSTR_pressed, MP_ARG_REQUIRED | MP_ARG_BOOL }, |
| 51 | + }; |
| 52 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 53 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 54 | + |
| 55 | + const mp_int_t key_num = args[ARG_key_num].u_int; |
| 56 | + if (key_num < 0) { |
| 57 | + mp_raise_ValueError_varg(translate("%q must be > 0"), MP_QSTR_key_num); |
| 58 | + } |
| 59 | + |
| 60 | + common_hal_keypad_event_construct(self, (mp_uint_t)key_num, args[ARG_pressed].u_bool); |
| 61 | + return MP_OBJ_FROM_PTR(self); |
| 62 | +} |
| 63 | + |
| 64 | +//| key_num: int |
| 65 | +//| """The key number.""" |
| 66 | +//| |
| 67 | +STATIC mp_obj_t keypad_event_obj_get_key_num(mp_obj_t self_in) { |
| 68 | + keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 69 | + return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_event_get_key_num(self)); |
| 70 | +} |
| 71 | +MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_key_num_obj, keypad_event_obj_get_key_num); |
| 72 | + |
| 73 | +const mp_obj_property_t keypad_event_key_num_obj = { |
| 74 | + .base.type = &mp_type_property, |
| 75 | + .proxy = {(mp_obj_t)&keypad_event_get_key_num_obj, |
| 76 | + MP_ROM_NONE, |
| 77 | + MP_ROM_NONE}, |
| 78 | +}; |
| 79 | + |
| 80 | +//| pressed: bool |
| 81 | +//| """True if event represents a key down (pressed) transition.""" |
| 82 | +//| |
| 83 | +STATIC mp_obj_t keypad_event_obj_get_pressed(mp_obj_t self_in) { |
| 84 | + keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 85 | + return mp_obj_new_bool(common_hal_keypad_event_get_pressed(self)); |
| 86 | +} |
| 87 | +MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_pressed_obj, keypad_event_obj_get_pressed); |
| 88 | + |
| 89 | +const mp_obj_property_t keypad_event_pressed_obj = { |
| 90 | + .base.type = &mp_type_property, |
| 91 | + .proxy = {(mp_obj_t)&keypad_event_get_pressed_obj, |
| 92 | + MP_ROM_NONE, |
| 93 | + MP_ROM_NONE}, |
| 94 | +}; |
| 95 | + |
| 96 | +//| released: bool |
| 97 | +//| """True if event represents a key up (released) transition.""" |
| 98 | +//| |
| 99 | +STATIC mp_obj_t keypad_event_obj_get_released(mp_obj_t self_in) { |
| 100 | + keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 101 | + return mp_obj_new_bool(common_hal_keypad_event_get_released(self)); |
| 102 | +} |
| 103 | +MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_released_obj, keypad_event_obj_get_released); |
| 104 | + |
| 105 | +const mp_obj_property_t keypad_event_released_obj = { |
| 106 | + .base.type = &mp_type_property, |
| 107 | + .proxy = {(mp_obj_t)&keypad_event_get_released_obj, |
| 108 | + MP_ROM_NONE, |
| 109 | + MP_ROM_NONE}, |
| 110 | +}; |
| 111 | + |
| 112 | +STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = { |
| 113 | + // Properties |
| 114 | + { MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) }, |
| 115 | + { MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) }, |
| 116 | + { MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) }, |
| 117 | +}; |
| 118 | +STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table); |
| 119 | + |
| 120 | +const mp_obj_type_t keypad_event_type = { |
| 121 | + { &mp_type_type }, |
| 122 | + .name = MP_QSTR_UART, |
| 123 | + .make_new = keypad_event_make_new, |
| 124 | + .locals_dict = (mp_obj_dict_t *)&keypad_event_locals_dict, |
| 125 | +}; |
0 commit comments