Added keypad.ShiftRegisterKeys

This commit is contained in:
Dan Halbert 2021-06-17 20:51:30 -04:00
parent 04b69cde9b
commit af66931f71
7 changed files with 510 additions and 7 deletions

View File

@ -519,6 +519,7 @@ SRC_SHARED_MODULE_ALL = \
keypad/Event.c \
keypad/EventQueue.c \
keypad/KeyMatrix.c \
keypad/ShiftRegisterKeys.c \
keypad/Keys.c \
sdcardio/SDCard.c \
sdcardio/__init__.c \

View File

@ -0,0 +1,239 @@
/*
* 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 "lib/utils/context_manager_helpers.h"
#include "py/binary.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/keypad/Event.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
//| class ShiftRegisterKeys:
//| """Manage a set of keys attached to an incoming shift register."""
//|
//| def __init__(self, clock: microcontroller.Pin, data: microcontroller.Pin, latch: microcontroller.Pin, level_when_pressed: bool, max_events: int = 64) -> None:
//| """
//| Create a `Keys` object that will scan keys attached to a parallel-in serial-out shift register
//| like the 74HC165 or equivalent.
//| Note that you may chain shift registers to load in as many values as you need.
//|
//| Key number 0 is the first (or more properly, the zero-th) bit read. In the
//| 74HC165, this bit is labeled ``Q7``. Key number 1 will be the value of ``Q6``, etc.
//|
//| An `EventQueue` is created when this object is created and is available in the `events` attribute.
//|
//| The keys are debounced by waiting about 20 msecs before reporting a transition.
//|
//| :param microcontroller.Pin clock: The shift register clock pin.
//| The shift register should clock on a low-to-high transition.
//| :param microcontroller.Pin data: the incoming shift register data pin
//| :param microcontroller.Pin latch:
//| Pin used to trigger loading parallel data pins into the shift register.
//| Active low: pull low to load the data.
//| :param int num_keys: number of data lines to clock in
//| :param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
//| ``False`` if the pin reads low (is grounded) when the key is pressed.
//| :param int max_events: maximum size of `events` `EventQueue`:
//| 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_shiftregisterkeys_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
keypad_shiftregisterkeys_obj_t *self = m_new_obj(keypad_shiftregisterkeys_obj_t);
self->base.type = &keypad_shiftregisterkeys_type;
enum { ARG_clock, ARG_data, ARG_latch, ARG_num_keys, ARG_value_when_pressed, ARG_max_events };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_clock, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_data, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_latch, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_num_keys, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_value_when_pressed, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
{ MP_QSTR_max_events, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
};
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);
mcu_pin_obj_t *clock = validate_obj_is_free_pin(args[ARG_clock].u_obj);
mcu_pin_obj_t *data = validate_obj_is_free_pin(args[ARG_data].u_obj);
mcu_pin_obj_t *latch = validate_obj_is_free_pin(args[ARG_latch].u_obj);
if (args[ARG_num_keys].u_int < 1) {
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_num_keys);
}
const size_t num_keys = (size_t)args[ARG_num_keys].u_int;
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;
common_hal_keypad_shiftregisterkeys_construct(
self, clock, data, latch, num_keys, value_when_pressed, max_events);
return MP_OBJ_FROM_PTR(self);
}
//| def deinit(self) -> None:
//| """Stop scanning and release the pins."""
//| ...
//|
STATIC mp_obj_t keypad_shiftregisterkeys_deinit(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_keypad_shiftregisterkeys_deinit(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_deinit_obj, keypad_shiftregisterkeys_deinit);
//| def __enter__(self) -> Keys:
//| """No-op used by Context Managers."""
//| ...
//|
// Provided by context manager helper.
//| def __exit__(self) -> None:
//| """Automatically deinitializes when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
//|
STATIC mp_obj_t keypad_shiftregisterkeys___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_keypad_shiftregisterkeys_deinit(args[0]);
return MP_ROM_NONE;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(keypad_shiftregisterkeys___exit___obj, 4, 4, keypad_shiftregisterkeys___exit__);
STATIC void check_for_deinit(keypad_shiftregisterkeys_obj_t *self) {
if (common_hal_keypad_shiftregisterkeys_deinited(self)) {
raise_deinited_error();
}
}
//| num_keys: int
//| """The number of keys that are being scanned. (read-only)
//| """
//|
STATIC mp_obj_t keypad_shiftregisterkeys_get_num_keys(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_shiftregisterkeys_get_num_keys(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_get_num_keys_obj, keypad_shiftregisterkeys_get_num_keys);
const mp_obj_property_t keypad_shiftregisterkeys_num_keys_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_shiftregisterkeys_get_num_keys_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
//| 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 `events` `EventQueue`.
//| """
//| ...
//|
STATIC mp_obj_t keypad_shiftregisterkeys_pressed(mp_obj_t self_in, mp_obj_t key_num_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_int_t key_num = mp_obj_get_int(key_num_in);
if (key_num < 0 || (size_t)key_num >= common_hal_keypad_shiftregisterkeys_get_num_keys(self)) {
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_key_num);
}
return mp_obj_new_bool(common_hal_keypad_shiftregisterkeys_pressed(self, (mp_uint_t)key_num));
}
MP_DEFINE_CONST_FUN_OBJ_2(keypad_shiftregisterkeys_pressed_obj, keypad_shiftregisterkeys_pressed);
//| def store_states(self, states: _typing.WriteableBuffer) -> None:
//| """Write the states of all the keys into ``states``.
//| Write a ``1`` if pressed, and ``0`` if released.
//| The ``length`` of ``states`` must be `num_keys`.
//| This is a debounced read of the state of all the keys, and bypasses the `events` `EventQueue`.
//| The read is done atomically.
//| """
//| ...
//|
STATIC mp_obj_t keypad_shiftregisterkeys_store_states(mp_obj_t self_in, mp_obj_t pressed) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(pressed, &bufinfo, MP_BUFFER_WRITE);
if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
mp_raise_ValueError_varg(translate("%q must store bytes"), MP_QSTR_pressed);
}
if (bufinfo.len != common_hal_keypad_shiftregisterkeys_get_num_keys(self)) {
mp_raise_ValueError_varg(translate("%q length must be %q"), MP_QSTR_pressed, MP_QSTR_num_keys);
}
common_hal_keypad_shiftregisterkeys_store_states(self, (uint8_t *)bufinfo.buf);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_2(keypad_shiftregisterkeys_store_states_obj, keypad_shiftregisterkeys_store_states);
//| events: EventQueue
//| """The `EventQueue` associated with this `Keys` object. (read-only)
//| """
//|
STATIC mp_obj_t keypad_shiftregisterkeys_get_events(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_keypad_shiftregisterkeys_get_events(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_get_events_obj, keypad_shiftregisterkeys_get_events);
const mp_obj_property_t keypad_shiftregisterkeys_events_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_shiftregisterkeys_get_events_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};
STATIC const mp_rom_map_elem_t keypad_shiftregisterkeys_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_shiftregisterkeys_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_shiftregisterkeys___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_shiftregisterkeys_events_obj) },
{ MP_ROM_QSTR(MP_QSTR_num_keys), MP_ROM_PTR(&keypad_shiftregisterkeys_num_keys_obj) },
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_shiftregisterkeys_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_store_states), MP_ROM_PTR(&keypad_shiftregisterkeys_store_states_obj) },
};
STATIC MP_DEFINE_CONST_DICT(keypad_shiftregisterkeys_locals_dict, keypad_shiftregisterkeys_locals_dict_table);
const mp_obj_type_t keypad_shiftregisterkeys_type = {
{ &mp_type_type },
.name = MP_QSTR_ShiftRegisterKeys,
.make_new = keypad_shiftregisterkeys_make_new,
.locals_dict = (mp_obj_t)&keypad_shiftregisterkeys_locals_dict,
};

View File

@ -0,0 +1,45 @@
/*
* 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.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H
#include "py/objlist.h"
#include "shared-module/keypad/ShiftRegisterKeys.h"
extern const mp_obj_type_t keypad_shiftregisterkeys_type;
void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_t *self, mcu_pin_obj_t *clock_pin, mcu_pin_obj_t *data_pin, mcu_pin_obj_t *latch_pin, size_t num_keys, bool value_when_pressed, size_t max_events);
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self);
bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t *self);
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self);
mp_uint_t common_hal_keypad_shiftregisterkeys_get_num_keys(keypad_shiftregisterkeys_obj_t *self);
bool common_hal_keypad_shiftregisterkeys_pressed(keypad_shiftregisterkeys_obj_t *self, mp_uint_t key_num);
void common_hal_keypad_shiftregisterkeys_store_states(keypad_shiftregisterkeys_obj_t *self, uint8_t *states);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H

View File

@ -31,20 +31,24 @@
#include "shared-bindings/keypad/EventQueue.h"
#include "shared-bindings/keypad/KeyMatrix.h"
#include "shared-bindings/keypad/Keys.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h"
//| """Support for scanning keys and key matrices
//|
//| The `keypad` module provides native support to scan sets of keys or buttons,
//| connected independently to individual pins, or connected in a row-and-column matrix.
//| connected independently to individual pins,
//| connected to a shift register,
//| or connected in a row-and-column matrix.
//| """
//|
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_Event), MP_OBJ_FROM_PTR(&keypad_event_type) },
{ MP_ROM_QSTR(MP_QSTR_EventQueue), MP_OBJ_FROM_PTR(&keypad_eventqueue_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___name__), MP_OBJ_NEW_QSTR(MP_QSTR_keypad) },
{ MP_ROM_QSTR(MP_QSTR_Event), MP_OBJ_FROM_PTR(&keypad_event_type) },
{ MP_ROM_QSTR(MP_QSTR_EventQueue), MP_OBJ_FROM_PTR(&keypad_eventqueue_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_ShiftRegisterKeys), MP_OBJ_FROM_PTR(&keypad_shiftregisterkeys_type) },
};
STATIC MP_DEFINE_MUTABLE_DICT(keypad_module_globals, keypad_module_globals_table);

View File

@ -0,0 +1,157 @@
/*
* 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 <string.h>
#include "py/gc.h"
#include "py/runtime.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/keypad/EventQueue.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "shared-bindings/keypad/__init__.h"
#include "supervisor/port.h"
#include "supervisor/shared/tick.h"
#define DEBOUNCE_TICKS (20)
void common_hal_keypad_shiftregisterkeys_construct(keypad_shiftregisterkeys_obj_t *self, mcu_pin_obj_t *clock_pin, mcu_pin_obj_t *data_pin, mcu_pin_obj_t *latch_pin, size_t num_keys, bool value_when_pressed, size_t max_events) {
digitalio_digitalinout_obj_t *clock = m_new_obj(digitalio_digitalinout_obj_t);
clock->base.type = &digitalio_digitalinout_type;
common_hal_digitalio_digitalinout_construct(clock, clock_pin);
common_hal_digitalio_digitalinout_switch_to_output(clock, false, DRIVE_MODE_PUSH_PULL);
self->clock = clock;
digitalio_digitalinout_obj_t *data = m_new_obj(digitalio_digitalinout_obj_t);
data->base.type = &digitalio_digitalinout_type;
common_hal_digitalio_digitalinout_construct(data, data_pin);
common_hal_digitalio_digitalinout_switch_to_input(data, PULL_NONE);
self->data = data;
digitalio_digitalinout_obj_t *latch = m_new_obj(digitalio_digitalinout_obj_t);
latch->base.type = &digitalio_digitalinout_type;
common_hal_digitalio_digitalinout_construct(latch, latch_pin);
common_hal_digitalio_digitalinout_switch_to_output(latch, true, DRIVE_MODE_PUSH_PULL);
self->latch = latch;
self->currently_pressed = (bool *)gc_alloc(sizeof(bool) * num_keys, false, false);
self->previously_pressed = (bool *)gc_alloc(sizeof(bool) * num_keys, false, false);
self->value_when_pressed = value_when_pressed;
self->num_keys = num_keys;
self->last_scan_ticks = port_get_raw_ticks(NULL);
keypad_eventqueue_obj_t *events = m_new_obj(keypad_eventqueue_obj_t);
events->base.type = &keypad_eventqueue_type;
common_hal_keypad_eventqueue_construct(events, max_events);
self->events = events;
// Add self to the list of active keypad scanners.
keypad_register_scanner((keypad_scanner_obj_t *)self);
supervisor_enable_tick();
}
void common_hal_keypad_shiftregisterkeys_deinit(keypad_shiftregisterkeys_obj_t *self) {
if (common_hal_keypad_shiftregisterkeys_deinited(self)) {
return;
}
// Remove self from the list of active keypad scanners first.
keypad_deregister_scanner((keypad_scanner_obj_t *)self);
common_hal_digitalio_digitalinout_deinit(self->clock);
self->clock = MP_ROM_NONE;
common_hal_digitalio_digitalinout_deinit(self->data);
self->data = MP_ROM_NONE;
common_hal_digitalio_digitalinout_deinit(self->latch);
self->latch = MP_ROM_NONE;
}
bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t *self) {
return self->clock == MP_ROM_NONE;
}
size_t common_hal_keypad_shiftregisterkeys_get_num_keys(keypad_shiftregisterkeys_obj_t *self) {
return self->num_keys;
}
bool common_hal_keypad_shiftregisterkeys_pressed(keypad_shiftregisterkeys_obj_t *self, mp_uint_t key_num) {
return self->currently_pressed[key_num];
}
// The length of states has already been validated.
void common_hal_keypad_shiftregisterkeys_store_states(keypad_shiftregisterkeys_obj_t *self, uint8_t *states) {
// Read the state atomically.
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
memcpy(states, self->currently_pressed, common_hal_keypad_shiftregisterkeys_get_num_keys(self));
supervisor_release_lock(&keypad_scanners_linked_list_lock);
}
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self) {
return MP_OBJ_FROM_PTR(self->events);
}
void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < DEBOUNCE_TICKS) {
// Too soon. Wait longer to debounce.
return;
}
self->last_scan_ticks = now;
// Latch (freeze) the current state of the input pins.
common_hal_digitalio_digitalinout_set_value(self->latch, true);
for (mp_uint_t key_num = 0; key_num < common_hal_keypad_shiftregisterkeys_get_num_keys(self); key_num++) {
// Zero-th data appears on on the data pin immediately, without shifting.
common_hal_digitalio_digitalinout_set_value(self->clock, false);
// 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->data) == self->value_when_pressed;
self->currently_pressed[key_num] = current;
// Trigger a shift to get the next bit.
common_hal_digitalio_digitalinout_set_value(self->clock, true);
// Record any transitions.
if (previous != current) {
keypad_eventqueue_record(self->events, key_num, current);
}
}
// Start reading the input pins again.
common_hal_digitalio_digitalinout_set_value(self->latch, false);
}

View File

@ -0,0 +1,54 @@
/*
* 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.
*/
#ifndef MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_SHIFTREGISTERKEYS_H
#define MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_SHIFTREGISTERKEYS_H
#include "py/obj.h"
#include "py/objtuple.h"
#include "common-hal/digitalio/DigitalInOut.h"
#include "shared-module/keypad/__init__.h"
#include "shared-module/keypad/EventQueue.h"
typedef struct {
mp_obj_base_t base;
// All scanners have a next field here, to keep a linked list of active scanners.
keypad_scanner_obj_t *next;
digitalio_digitalinout_obj_t *clock;
digitalio_digitalinout_obj_t *data;
digitalio_digitalinout_obj_t *latch;
size_t num_keys;
uint64_t last_scan_ticks;
bool *previously_pressed;
bool *currently_pressed;
keypad_eventqueue_obj_t *events;
bool value_when_pressed;
} keypad_shiftregisterkeys_obj_t;
void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_MODULE_KEYPAD_SHIFTREGISTERKEYS_H

View File

@ -26,13 +26,14 @@
#include "shared-bindings/keypad/Keys.h"
#include "shared-bindings/keypad/KeyMatrix.h"
#include "shared-bindings/keypad/ShiftRegisterKeys.h"
#include "supervisor/shared/lock.h"
#include "supervisor/shared/tick.h"
supervisor_lock_t keypad_scanners_linked_list_lock;
void keypad_tick(void) {
// Fast path. Return immediately if there are no scanners.
// Fast path. Return immediately there are no scanners.
if (!MP_STATE_VM(keypad_scanners_linked_list)) {
return;
}
@ -45,6 +46,8 @@ void keypad_tick(void) {
keypad_keys_scan((keypad_keys_obj_t *)scanner);
} else if (mp_obj_is_type(scanner, &keypad_keymatrix_type)) {
keypad_keymatrix_scan((keypad_keymatrix_obj_t *)scanner);
} else if (mp_obj_is_type(scanner, &keypad_shiftregisterkeys_type)) {
keypad_shiftregisterkeys_scan((keypad_shiftregisterkeys_obj_t *)scanner);
}
scanner = ((keypad_scanner_obj_t *)scanner)->next;