wip
This commit is contained in:
parent
b815164bdf
commit
627c426259
@ -95,13 +95,18 @@ msgstr ""
|
||||
msgid "%q must be 1-255"
|
||||
msgstr ""
|
||||
|
||||
#: shared-bindings/keypad/Event.c
|
||||
msgid "%q must be > 0"
|
||||
msgstr ""
|
||||
|
||||
#: shared-bindings/memorymonitor/AllocationAlarm.c
|
||||
msgid "%q must be >= 0"
|
||||
msgstr ""
|
||||
|
||||
#: shared-bindings/_bleio/CharacteristicBuffer.c
|
||||
#: shared-bindings/_bleio/PacketBuffer.c shared-bindings/displayio/Group.c
|
||||
#: shared-bindings/displayio/Shape.c
|
||||
#: shared-bindings/displayio/Shape.c shared-bindings/keypad/KeyMatrix.c
|
||||
#: shared-bindings/keypad/Keys.c
|
||||
#: shared-bindings/memorymonitor/AllocationAlarm.c
|
||||
#: shared-bindings/vectorio/Circle.c shared-bindings/vectorio/Rectangle.c
|
||||
msgid "%q must be >= 1"
|
||||
@ -116,7 +121,8 @@ msgid "%q must be a tuple of length 2"
|
||||
msgstr ""
|
||||
|
||||
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
|
||||
#: shared-bindings/canio/Match.c shared-bindings/keypad/Keys.c
|
||||
#: shared-bindings/canio/Match.c shared-bindings/keypad/KeyMatrix.c
|
||||
#: shared-bindings/keypad/Keys.c
|
||||
msgid "%q out of range"
|
||||
msgstr ""
|
||||
|
||||
@ -905,8 +911,7 @@ msgstr ""
|
||||
|
||||
#: ports/raspberrypi/bindings/rp2pio/StateMachine.c py/enum.c
|
||||
#: shared-bindings/_bleio/__init__.c shared-bindings/aesio/aes.c
|
||||
#: shared-bindings/busio/SPI.c shared-bindings/keypad/Keys.c
|
||||
#: shared-bindings/microcontroller/Pin.c
|
||||
#: shared-bindings/busio/SPI.c shared-bindings/microcontroller/Pin.c
|
||||
#: shared-bindings/neopixel_write/__init__.c
|
||||
#: shared-bindings/terminalio/Terminal.c
|
||||
msgid "Expected a %q"
|
||||
|
@ -516,8 +516,9 @@ SRC_SHARED_MODULE_ALL = \
|
||||
ipaddress/IPv4Address.c \
|
||||
ipaddress/__init__.c \
|
||||
keypad/__init__.c \
|
||||
keypad/Event.c \
|
||||
keypad/KeyMatrix.c \
|
||||
keypad/Keys.c \
|
||||
keypad/State.c \
|
||||
sdcardio/SDCard.c \
|
||||
sdcardio/__init__.c \
|
||||
gamepad/GamePad.c \
|
||||
|
125
shared-bindings/keypad/Event.c
Normal file
125
shared-bindings/keypad/Event.c
Normal file
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
|
||||
//| class Event:
|
||||
//| """A key transition event."""
|
||||
//| def __init__(self, key_num: int, pressed: bool) -> None:
|
||||
//| """Create a key transition event, which reports a key-pressed or key-released transition.
|
||||
//|
|
||||
//| :param int key_num: the key number
|
||||
//| :param bool pressed: ``True`` if the key was pressed; ``False`` if it was released.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
|
||||
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) {
|
||||
keypad_event_obj_t *self = m_new_obj(keypad_event_obj_t);
|
||||
self->base.type = &keypad_event_type;
|
||||
enum { ARG_key_num, ARG_pressed };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_key_num, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_pressed, MP_ARG_REQUIRED | MP_ARG_BOOL },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
const mp_int_t key_num = args[ARG_key_num].u_int;
|
||||
if (key_num < 0) {
|
||||
mp_raise_ValueError_varg(translate("%q must be > 0"), MP_QSTR_key_num);
|
||||
}
|
||||
|
||||
common_hal_keypad_event_construct(self, (mp_uint_t)key_num, args[ARG_pressed].u_bool);
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
//| key_num: int
|
||||
//| """The key number."""
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_obj_get_key_num(mp_obj_t self_in) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_event_get_key_num(self));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_key_num_obj, keypad_event_obj_get_key_num);
|
||||
|
||||
const mp_obj_property_t keypad_event_key_num_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&keypad_event_get_key_num_obj,
|
||||
MP_ROM_NONE,
|
||||
MP_ROM_NONE},
|
||||
};
|
||||
|
||||
//| pressed: bool
|
||||
//| """True if event represents a key down (pressed) transition."""
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_obj_get_pressed(mp_obj_t self_in) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return mp_obj_new_bool(common_hal_keypad_event_get_pressed(self));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_pressed_obj, keypad_event_obj_get_pressed);
|
||||
|
||||
const mp_obj_property_t keypad_event_pressed_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&keypad_event_get_pressed_obj,
|
||||
MP_ROM_NONE,
|
||||
MP_ROM_NONE},
|
||||
};
|
||||
|
||||
//| released: bool
|
||||
//| """True if event represents a key up (released) transition."""
|
||||
//|
|
||||
STATIC mp_obj_t keypad_event_obj_get_released(mp_obj_t self_in) {
|
||||
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return mp_obj_new_bool(common_hal_keypad_event_get_released(self));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_released_obj, keypad_event_obj_get_released);
|
||||
|
||||
const mp_obj_property_t keypad_event_released_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&keypad_event_get_released_obj,
|
||||
MP_ROM_NONE,
|
||||
MP_ROM_NONE},
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t keypad_event_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_UART,
|
||||
.make_new = keypad_event_make_new,
|
||||
.locals_dict = (mp_obj_dict_t *)&keypad_event_locals_dict,
|
||||
};
|
41
shared-bindings/keypad/Event.h
Normal file
41
shared-bindings/keypad/Event.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENT__H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENT__H
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "shared-module/keypad/Event.h"
|
||||
|
||||
extern const mp_obj_type_t keypad_event_type;
|
||||
|
||||
void common_hal_keypad_event_construct(keypad_event_obj_t *self, uint16_t key_num, bool pressed);
|
||||
mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self);
|
||||
bool common_hal_keypad_event_get_pressed(keypad_event_obj_t *self);
|
||||
bool common_hal_keypad_event_get_released(keypad_event_obj_t *self);
|
||||
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENT__H
|
@ -24,17 +24,15 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "py/enum.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
#include "shared-bindings/keypad/Keys.h"
|
||||
#include "shared-bindings/keypad/State.h"
|
||||
#include "shared-bindings/microcontroller/Pin.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
//| class Keys:
|
||||
//| """Manage a set of independent keys."""
|
||||
//|
|
||||
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True) -> None:
|
||||
//| def __init__(self, pins: Sequence[microcontroller.Pin], *, level_when_pressed: bool, pull: bool = True, max_events: int = 16) -> None:
|
||||
//| """
|
||||
//| Create a `Keys` object that will scan keys attached to the given sequence of pins.
|
||||
//| Each key is independent and attached to its own pin.
|
||||
@ -45,24 +43,27 @@
|
||||
//| ``False`` if the pin reads low (is grounded) when the key is pressed.
|
||||
//| All the pins must be connected in the same way.
|
||||
//| :param bool pull: ``True`` if an internal pull-up or pull-down should be
|
||||
//| enabled on each pin. A pull-up will be used if ``value_when_pressed`` is ``False``;
|
||||
//| a pull-down will be used if it is ``True``.
|
||||
//| If an external pull is already provided for all the pins, you can set ``pull`` to ``False``.
|
||||
//| However, enabling an internal pull when an external one is already present is not a problem;
|
||||
//| it simply uses slightly more current.
|
||||
//|
|
||||
//| Calls `scan()` once before returning, to initialize internal state.
|
||||
//| enabled on each pin. A pull-up will be used if ``value_when_pressed`` is ``False``;
|
||||
//| a pull-down will be used if it is ``True``.
|
||||
//| If an external pull is already provided for all the pins, you can set ``pull`` to ``False``.
|
||||
//| However, enabling an internal pull when an external one is already present is not a problem;
|
||||
//| it simply uses slightly more current.
|
||||
//| :param int max_events: Size of key event queue:
|
||||
//| maximum number of key transition events that are saved.
|
||||
//| Must be >= 1.
|
||||
//| If a new event arrives when the queue is full, the oldest event is discarded.
|
||||
//| """
|
||||
//| ...
|
||||
|
||||
STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
keypad_keys_obj_t *self = m_new_obj(keypad_keys_obj_t);
|
||||
self->base.type = &keypad_keys_type;
|
||||
enum { ARG_pins, ARG_value_when_pressed, ARG_pull };
|
||||
enum { ARG_pins, ARG_value_when_pressed, ARG_pull, ARG_max_events };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_pins, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_value_when_pressed, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
|
||||
{ MP_QSTR_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
||||
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 16} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
@ -70,8 +71,14 @@ STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, c
|
||||
mp_obj_t pins = args[ARG_pins].u_obj;
|
||||
// mp_obj_len() will be >= 0.
|
||||
const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(pins));
|
||||
|
||||
const bool value_when_pressed = args[ARG_value_when_pressed].u_bool;
|
||||
|
||||
if (args[ARG_max_events].u_int < 1) {
|
||||
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_max_events);
|
||||
}
|
||||
const size_t max_events = (size_t)args[ARG_max_events].u_int;
|
||||
|
||||
mcu_pin_obj_t *pins_array[num_pins];
|
||||
|
||||
for (mp_uint_t i = 0; i < num_pins; i++) {
|
||||
@ -80,95 +87,61 @@ STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, c
|
||||
pins_array[i] = pin;
|
||||
}
|
||||
|
||||
common_hal_keypad_keys_construct(self, num_pins, pins_array, value_when_pressed, args[ARG_pull].u_bool);
|
||||
common_hal_keypad_keys_scan(self);
|
||||
common_hal_keypad_keys_construct(self, num_pins, pins_array, value_when_pressed, args[ARG_pull].u_bool, max_events);
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
//| def scan(self) -> bool:
|
||||
//| """Scan the keys and record which are newly pressed, still pressed,
|
||||
//| newly released, and still released. If not enough time has elapsed since
|
||||
//| the last scan for debouncing, do nothing and return ``False``.
|
||||
//| def next_event(self) -> Optional[Event]:
|
||||
//| """Return the next key transition event. Return ``None` if no events are pending.
|
||||
//|
|
||||
//| :return: ``True`` if sufficient time has elapsed for debouncing (about 20 msecs),
|
||||
//| otherwise ``False``.
|
||||
//| :rtype: bool
|
||||
//| Note that the queue size is limited; see ``max_events`` in the constructor.
|
||||
//| If a new event arrives when the queue is full, the oldest event is discarded.
|
||||
//|
|
||||
//| :return: the next queued key transition `Event`
|
||||
//| :rtype: Optional[Event]
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_keys_scan(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t keypad_keys_next_event(mp_obj_t self_in, mp_obj_t event_in) {
|
||||
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
return mp_obj_new_bool(common_hal_keypad_keys_scan(self));
|
||||
return common_hal_keypad_keys_next_event(self);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_scan_obj, keypad_keys_scan);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keys_next_event_obj, keypad_keys_next_event);
|
||||
|
||||
//| def state(self, key_num: int) -> keypad.State:
|
||||
//| """Return the state for the given ``key_num``, based
|
||||
//| on the results of the most recent `scan()`.
|
||||
//|
|
||||
//| :param int key_num: Key number: corresponds to the sequence of pins
|
||||
//| :return: state of key number ``key_num``
|
||||
//| :rtype: keypad.State: One of `State.JUST_PRESSED`, `State.STILL_PRESSED`,
|
||||
//| `State.JUST_RELEASED`, or `State.STILL_RELEASED`.
|
||||
//| The inclusive states `State.PRESSED` and `State.RELEASED` will *not* be returned.
|
||||
//| def clear_events(self) -> None:
|
||||
//| """Clear any queued key transition events.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_keys_state(mp_obj_t self_in, mp_obj_t key_num_obj) {
|
||||
STATIC mp_obj_t keypad_keys_clear_events(mp_obj_t self_in) {
|
||||
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_int_t key_num = mp_obj_int_get_checked(key_num_obj);
|
||||
if (key_num < 0 || (mp_uint_t)key_num >= common_hal_keypad_keys_length(self)) {
|
||||
|
||||
common_hal_keypad_keys_clear_events(self);
|
||||
return MP_ROM_NONE;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_clear_events_obj, keypad_keys_clear_events);
|
||||
|
||||
//| def pressed(self, key_num: int) -> None:
|
||||
//| """Return ``True`` if the given key is pressed. This is a debounced read
|
||||
//| of the key state which bypasses the event queue.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_keys_pressed(mp_obj_t self_in, mp_obj_t key_num_in) {
|
||||
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_int_t key_num = mp_obj_get_int(key_num_in);
|
||||
if (key_num < 0 || key_num >= common_hal_keypad_keys_num_keys(self)) {
|
||||
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num);
|
||||
}
|
||||
|
||||
return cp_enum_find(&keypad_state_type, common_hal_keypad_keys_state(self, (mp_uint_t)key_num));
|
||||
return mp_obj_new_bool(common_hal_keypad_keys_pressed(self, (mp_uint_t)key_num));
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keys_state_obj, keypad_keys_state);
|
||||
|
||||
//| def keys_with_state(self, state: State, into_list: List[Optional[int]]) -> None:
|
||||
//| """Store key numbers of keys with state ``state`` in ``into_list``.
|
||||
//| The states checked are based on the results of the most recent `scan()`.
|
||||
//|
|
||||
//| You can use the inclusive states `State.PRESSED` and `State.RELEASED`.
|
||||
//| `State.PRESSED` includes states `State.JUST_PRESSED` and `State.STILL_PRESSED`.
|
||||
//| `State.RELEASED` includes `State.JUST_RELEASED` and `State.STILL_RELEASED`.
|
||||
//|
|
||||
//| The key numbers are stored in ``into_list`` consecutively, up to ``len(into_list)``.
|
||||
//| The ``into_list`` is not extended if there are more keys with the given
|
||||
//| state than list slots. Instead, leftover key numbers are discarded.
|
||||
//| If there are fewer keys with the given state, the rest of ``into_list``
|
||||
//| is padded with ``None``. For example,
|
||||
//| if four keys are being monitored, and only key numbers 0 and 2 have the given state,
|
||||
//| ``into_list`` will be set to ``[0, 2, None, None]``. You can iterate over
|
||||
//| ``into_list`` and stop when you find the first ``None``.
|
||||
//| """
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t keypad_keys_keys_with_state(mp_obj_t self_in, mp_obj_t state_in, mp_obj_t into_list_in) {
|
||||
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
if (!mp_obj_is_type(state_in, &keypad_state_type)) {
|
||||
mp_raise_ValueError_varg(translate("Expected a %q"), keypad_state_type.name);
|
||||
}
|
||||
|
||||
if (!mp_obj_is_type(into_list_in, &mp_type_list)) {
|
||||
mp_raise_ValueError_varg(translate("Expected a %q"), mp_type_list.name);
|
||||
}
|
||||
|
||||
int state = cp_enum_value(&keypad_state_type, state_in);
|
||||
mp_obj_list_t *into_list = MP_OBJ_TO_PTR(into_list_in);
|
||||
|
||||
common_hal_keypad_keys_keys_with_state(self, state, into_list);
|
||||
|
||||
return MP_ROM_NONE;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_3(keypad_keys_keys_with_state_obj, keypad_keys_keys_with_state);
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(keypad_keys_pressed_obj, keypad_keys_pressed);
|
||||
|
||||
STATIC const mp_rom_map_elem_t keypad_keys_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_keys_with_state), MP_ROM_PTR(&keypad_keys_keys_with_state_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&keypad_keys_scan_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_state), MP_ROM_PTR(&keypad_keys_state_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_clear_events), MP_ROM_PTR(&keypad_keys_clear_events_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_next_event), MP_ROM_PTR(&keypad_keys_next_event_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_keys_pressed_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table);
|
||||
|
@ -32,10 +32,14 @@
|
||||
|
||||
extern const mp_obj_type_t keypad_keys_type;
|
||||
|
||||
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull);
|
||||
void common_hal_keypad_keys_keys_with_state(keypad_keys_obj_t *self, mp_int_t state, mp_obj_list_t *into);
|
||||
size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self);
|
||||
bool common_hal_keypad_keys_scan(keypad_keys_obj_t *self);
|
||||
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num);
|
||||
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, size_t max_events);
|
||||
|
||||
mp_uint_t common_hal_keypad_keys_num_keys(keypad_keys_obj_t *self);
|
||||
bool common_hal_keypad_keys_pressed(keypad_keys_obj_t *self, mp_uint_t key_num);
|
||||
|
||||
mp_obj_t common_hal_keypad_keys_next_event(keypad_keys_obj_t *self);
|
||||
void common_hal_keypad_keys_clear_events(keypad_keys_obj_t *self);
|
||||
|
||||
void keypad_keys_scan(keypad_keys_obj_t *self);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H
|
||||
|
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/enum.h"
|
||||
|
||||
#include "shared-bindings/keypad/State.h"
|
||||
|
||||
// Defines enum values like
|
||||
// const cp_enum_obj_t state_JUST_PRESSED = ...
|
||||
MAKE_ENUM_VALUE(keypad_state_type, state, JUST_PRESSED, STATE_JUST_PRESSED);
|
||||
MAKE_ENUM_VALUE(keypad_state_type, state, STILL_PRESSED, STATE_STILL_PRESSED);
|
||||
MAKE_ENUM_VALUE(keypad_state_type, state, PRESSED, STATE_PRESSED);
|
||||
MAKE_ENUM_VALUE(keypad_state_type, state, JUST_RELEASED, STATE_JUST_RELEASED);
|
||||
MAKE_ENUM_VALUE(keypad_state_type, state, STILL_RELEASED, STATE_STILL_RELEASED);
|
||||
MAKE_ENUM_VALUE(keypad_state_type, state, RELEASED, STATE_RELEASED);
|
||||
|
||||
//| class State:
|
||||
//| """The state of a key, based on the last call to ``scan()``."""
|
||||
//|
|
||||
//| JUST_PRESSED: State
|
||||
//| """The key transitioned from released to pressed."""
|
||||
//|
|
||||
//| STILL_PRESSED: State
|
||||
//| """The key was already pressed, and continues to be pressed."""
|
||||
//|
|
||||
//| PRESSED: State
|
||||
//| """The key is now pressed. Used to indicate states `JUST_PRESSED` and `STILL_PRESSED` inclusively."""
|
||||
//|
|
||||
//| JUST_RELEASED: State
|
||||
//| """The key transitioned from pressed to released."""
|
||||
//|
|
||||
//| STILL_RELEASED: State
|
||||
//| """The key was already released, and continues to be released."""
|
||||
//|
|
||||
//| RELEASED: State
|
||||
//| """The key is now released. Used to indicate states `JUST_RELEASED` and `STILL_RELEASED` inclusively."""
|
||||
//|
|
||||
MAKE_ENUM_MAP(keypad_state) {
|
||||
MAKE_ENUM_MAP_ENTRY(state, JUST_PRESSED),
|
||||
MAKE_ENUM_MAP_ENTRY(state, STILL_PRESSED),
|
||||
MAKE_ENUM_MAP_ENTRY(state, PRESSED),
|
||||
MAKE_ENUM_MAP_ENTRY(state, JUST_RELEASED),
|
||||
MAKE_ENUM_MAP_ENTRY(state, STILL_RELEASED),
|
||||
MAKE_ENUM_MAP_ENTRY(state, RELEASED),
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(keypad_state_locals_dict, keypad_state_locals_table);
|
||||
|
||||
MAKE_PRINTER(keypad, keypad_state);
|
||||
|
||||
// Defines keypad_state_type.
|
||||
MAKE_ENUM_TYPE(keypad, State, keypad_state);
|
@ -27,9 +27,9 @@
|
||||
#include "py/obj.h"
|
||||
|
||||
#include "shared-bindings/keypad/__init__.h"
|
||||
// #include "shared-bindings/keypad/KeyMatrix.h"
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
#include "shared-bindings/keypad/KeyMatrix.h"
|
||||
#include "shared-bindings/keypad/Keys.h"
|
||||
#include "shared-bindings/keypad/State.h"
|
||||
|
||||
//| """Support for scanning keys and key matrices
|
||||
//|
|
||||
@ -40,9 +40,9 @@
|
||||
|
||||
STATIC mp_map_elem_t keypad_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_keypad) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_KeyMatrix), MP_OBJ_FROM_PTR(&keypad_key_matrix_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Event), MP_OBJ_FROM_PTR(&keypad_event_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_KeyMatrix), MP_OBJ_FROM_PTR(&keypad_keymatrix_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Keys), MP_OBJ_FROM_PTR(&keypad_keys_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_State), MP_OBJ_FROM_PTR(&keypad_state_type) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_MUTABLE_DICT(keypad_module_globals, keypad_module_globals_table);
|
||||
|
44
shared-module/keypad/Event.c
Normal file
44
shared-module/keypad/Event.c
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "shared-module/keypad/Event.h"
|
||||
|
||||
void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_num, bool pressed) {
|
||||
self->key_num = key_num;
|
||||
self->pressed = true;
|
||||
}
|
||||
|
||||
mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self) {
|
||||
return self->key_num;
|
||||
}
|
||||
|
||||
bool common_hal_keypad_event_get_pressed(keypad_event_obj_t *self) {
|
||||
return self->pressed;
|
||||
}
|
||||
|
||||
bool common_hal_keypad_event_get_released(keypad_event_obj_t *self) {
|
||||
return !self->pressed;
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
@ -24,21 +24,16 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_STATE__H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_STATE__H
|
||||
#ifndef MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_EVENT_H
|
||||
#define MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_EVENT_H
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/enum.h"
|
||||
|
||||
typedef enum {
|
||||
STATE_JUST_PRESSED,
|
||||
STATE_STILL_PRESSED,
|
||||
STATE_PRESSED,
|
||||
STATE_JUST_RELEASED,
|
||||
STATE_STILL_RELEASED,
|
||||
STATE_RELEASED,
|
||||
} keypad_state_t;
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
uint16_t key_num;
|
||||
bool pressed;
|
||||
} keypad_event_obj_t;
|
||||
|
||||
extern const mp_obj_type_t keypad_state_type;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_STATE__H
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_EVENT_H
|
@ -25,16 +25,20 @@
|
||||
*/
|
||||
|
||||
#include "py/gc.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "shared-bindings/keypad/Event.h"
|
||||
#include "shared-bindings/keypad/Keys.h"
|
||||
#include "shared-bindings/keypad/State.h"
|
||||
#include "shared-bindings/digitalio/DigitalInOut.h"
|
||||
#include "py/runtime.h"
|
||||
#include "supervisor/port.h"
|
||||
|
||||
#define DEBOUNCE_TICKS (20)
|
||||
|
||||
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull) {
|
||||
// Top bit of 16-bit event indicates pressed or released. Rest is key_num.
|
||||
#define EVENT_PRESSED (1 << 15)
|
||||
#define EVENT_RELEASED (0)
|
||||
#define EVENT_KEY_NUM_MASK (~EVENT_PRESSED)
|
||||
|
||||
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull, size_t max_events) {
|
||||
mp_obj_t dios[num_pins];
|
||||
|
||||
for (size_t i = 0; i < num_pins; i++) {
|
||||
@ -52,88 +56,60 @@ void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pin
|
||||
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_pins, false, false);
|
||||
self->value_when_pressed = value_when_pressed;
|
||||
|
||||
// Event queue is 16-bit values.
|
||||
ringbuf_alloc(self->encoded_events, max_events * 2, false);
|
||||
}
|
||||
|
||||
void common_hal_keypad_keys_keys_with_state(keypad_keys_obj_t *self, int state, mp_obj_list_t *list_into) {
|
||||
const size_t list_length = list_into->len;
|
||||
|
||||
size_t next_list_slot = 0;
|
||||
|
||||
for (mp_uint_t key_num = 0; key_num < common_hal_keypad_keys_length(self); key_num++) {
|
||||
if (next_list_slot >= list_length) {
|
||||
// List is full.
|
||||
break;
|
||||
}
|
||||
|
||||
bool store_key = false;
|
||||
switch (state) {
|
||||
case STATE_JUST_PRESSED:
|
||||
store_key = !self->previously_pressed[key_num] && self->currently_pressed[key_num];
|
||||
break;
|
||||
case STATE_STILL_PRESSED:
|
||||
store_key = self->previously_pressed[key_num] && self->currently_pressed[key_num];
|
||||
break;
|
||||
case STATE_PRESSED:
|
||||
store_key = self->currently_pressed[key_num];
|
||||
break;
|
||||
case STATE_JUST_RELEASED:
|
||||
store_key = self->previously_pressed[key_num] && !self->currently_pressed[key_num];
|
||||
break;
|
||||
case STATE_STILL_RELEASED:
|
||||
store_key = !self->previously_pressed[key_num] && !self->currently_pressed[key_num];
|
||||
break;
|
||||
case STATE_RELEASED:
|
||||
store_key = !self->currently_pressed[key_num];
|
||||
break;
|
||||
}
|
||||
|
||||
if (store_key) {
|
||||
mp_obj_list_store(list_into, MP_OBJ_NEW_SMALL_INT(next_list_slot),
|
||||
MP_OBJ_NEW_SMALL_INT(key_num));
|
||||
next_list_slot++;
|
||||
}
|
||||
|
||||
for (size_t unused_slot = next_list_slot; unused_slot < list_length; unused_slot++) {
|
||||
mp_obj_list_store(list_into, MP_OBJ_NEW_SMALL_INT(unused_slot),
|
||||
MP_ROM_NONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self) {
|
||||
size_t common_hal_keypad_keys_num_keys(keypad_keys_obj_t *self) {
|
||||
return self->digitalinouts->len;
|
||||
}
|
||||
|
||||
bool common_hal_keypad_keys_scan(keypad_keys_obj_t *self) {
|
||||
void keypad_keys_scan(keypad_keys_obj_t *self) {
|
||||
uint64_t now = port_get_raw_ticks(NULL);
|
||||
if (now - self->last_scan_ticks < DEBOUNCE_TICKS) {
|
||||
// Too soon.
|
||||
return false;
|
||||
// Too soon. Wait longer to debounce.
|
||||
return;
|
||||
}
|
||||
|
||||
self->last_scan_ticks = now;
|
||||
|
||||
for (mp_uint_t key_num = 0; key_num < common_hal_keypad_keys_length(self); key_num++) {
|
||||
self->previously_pressed[key_num] = self->currently_pressed[key_num];
|
||||
self->currently_pressed[key_num] =
|
||||
common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) ==
|
||||
for (mp_uint_t key_num = 0; key_num < common_hal_keypad_keys_num_keys(self); key_num++) {
|
||||
// Remember the previous up/down state.
|
||||
const bool previous = self->currently_pressed[key_num];
|
||||
self->previously_pressed[key_num] = previous;
|
||||
|
||||
// Get the current state.
|
||||
const bool current = common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) ==
|
||||
self->value_when_pressed;
|
||||
self->currently_pressed[key_num] = current;
|
||||
|
||||
// Record any transitions.
|
||||
if (previous != current) {
|
||||
if (ringbuf_num_empty(self->encoded_events) == 0) {
|
||||
// Discard oldest if full.
|
||||
ringbuf_get16(self->encoded_events);
|
||||
}
|
||||
ringbuf_put16(self->encoded_events, key_num | (current ? EVENT_PRESSED : EVENT_RELEASED));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num) {
|
||||
if (self->currently_pressed[key_num]) {
|
||||
if (self->previously_pressed[key_num]) {
|
||||
return STATE_STILL_PRESSED;
|
||||
} else {
|
||||
return STATE_JUST_PRESSED;
|
||||
}
|
||||
} else {
|
||||
if (self->previously_pressed[key_num]) {
|
||||
return STATE_JUST_RELEASED;
|
||||
} else {
|
||||
return STATE_STILL_RELEASED;
|
||||
}
|
||||
}
|
||||
bool common_hal_keypad_keys_pressed(keypad_keys_obj_t *self, mp_uint_t key_num) {
|
||||
return self->currently_pressed[key_num];
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_keypad_keys_next_event(keypad_keys_obj_t *self) {
|
||||
int encoded_event = ringbuf_get16(self->encoded_events);
|
||||
if (encoded_event == -1) {
|
||||
return MP_ROM_NONE;
|
||||
}
|
||||
|
||||
keypad_event_obj_t *event = m_new_obj(keypad_event_obj_t);
|
||||
self->base.type = &keypad_event_type;
|
||||
common_hal_keypad_event_construct(event, encoded_event & EVENT_KEY_NUM_MASK, encoded_event & EVENT_PRESSED);
|
||||
return MP_OBJ_FROM_PTR(event);
|
||||
}
|
||||
|
||||
void common_hal_keypad_keys_clear_events(keypad_keys_obj_t *self) {
|
||||
ringbuf_clear(self->encoded_events);
|
||||
}
|
||||
|
@ -31,6 +31,7 @@
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/objtuple.h"
|
||||
#include "py/ringbuf.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
@ -39,6 +40,7 @@ typedef struct {
|
||||
bool value_when_pressed;
|
||||
bool *previously_pressed;
|
||||
bool *currently_pressed;
|
||||
ringbuf_t *encoded_events;
|
||||
} keypad_keys_obj_t;
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user