3636//| class ShiftRegisterKeys:
3737//| """Manage a set of keys attached to an incoming shift register."""
3838//|
39- //| def __init__(self, clock: microcontroller.Pin, data: microcontroller.Pin, latch: microcontroller.Pin, value_when_pressed: bool, interval: float = 0.020, max_events: int = 64) -> None:
39+ //| def __init__(self, *, clock: microcontroller.Pin, data: microcontroller.Pin, latch: microcontroller.Pin, value_to_latch: bool = True, num_keys: int , value_when_pressed: bool, interval: float = 0.020, max_events: int = 64) -> None:
4040//| """
4141//| Create a `Keys` object that will scan keys attached to a parallel-in serial-out shift register
42- //| like the 74HC165 or equivalent .
42+ //| like the 74HC165 or CD4021 .
4343//| Note that you may chain shift registers to load in as many values as you need.
4444//|
4545//| Key number 0 is the first (or more properly, the zero-th) bit read. In the
5353//| The shift register should clock on a low-to-high transition.
5454//| :param microcontroller.Pin data: the incoming shift register data pin
5555//| :param microcontroller.Pin latch:
56- //| Pin used to trigger loading parallel data pins into the shift register.
57- //| Active low: pull low to load the data.
56+ //| Pin used to latch parallel data going into the shift register.
57+ //| :param bool value_to_latch: Pin state to latch data being read.
58+ //| ``True`` if the data is latched when ``latch`` goes high
59+ //| ``False`` if the data is latched when ``latch goes low.
60+ //| The default is ``True``, which is how the 74HC165 operates. The CD4021 latch is the opposite.
61+ //| Once the data is latched, it will be shifted out by toggling the clock pin.
5862//| :param int num_keys: number of data lines to clock in
5963//| :param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
6064//| ``False`` if the pin reads low (is grounded) when the key is pressed.
7074STATIC 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 ) {
7175 keypad_shiftregisterkeys_obj_t * self = m_new_obj (keypad_shiftregisterkeys_obj_t );
7276 self -> base .type = & keypad_shiftregisterkeys_type ;
73- enum { ARG_clock , ARG_data , ARG_latch , ARG_num_keys , ARG_value_when_pressed , ARG_interval , ARG_max_events };
77+ enum { ARG_clock , ARG_data , ARG_latch , ARG_value_to_latch , ARG_num_keys , ARG_value_when_pressed , ARG_interval , ARG_max_events };
7478 static const mp_arg_t allowed_args [] = {
7579 { MP_QSTR_clock , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
7680 { MP_QSTR_data , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
7781 { MP_QSTR_latch , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
82+ { MP_QSTR_value_to_latch , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = true} },
7883 { MP_QSTR_num_keys , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
7984 { MP_QSTR_value_when_pressed , MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
8085 { MP_QSTR_interval , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
@@ -86,6 +91,7 @@ STATIC mp_obj_t keypad_shiftregisterkeys_make_new(const mp_obj_type_t *type, siz
8691 mcu_pin_obj_t * clock = validate_obj_is_free_pin (args [ARG_clock ].u_obj );
8792 mcu_pin_obj_t * data = validate_obj_is_free_pin (args [ARG_data ].u_obj );
8893 mcu_pin_obj_t * latch = validate_obj_is_free_pin (args [ARG_latch ].u_obj );
94+ const bool value_to_latch = args [ARG_value_to_latch ].u_bool ;
8995
9096 const size_t num_keys = (size_t )mp_arg_validate_int_min (args [ARG_num_keys ].u_int , 1 , MP_QSTR_num_keys );
9197 const bool value_when_pressed = args [ARG_value_when_pressed ].u_bool ;
@@ -94,7 +100,7 @@ STATIC mp_obj_t keypad_shiftregisterkeys_make_new(const mp_obj_type_t *type, siz
94100 const size_t max_events = (size_t )mp_arg_validate_int_min (args [ARG_max_events ].u_int , 1 , MP_QSTR_max_events );
95101
96102 common_hal_keypad_shiftregisterkeys_construct (
97- self , clock , data , latch , num_keys , value_when_pressed , interval , max_events );
103+ self , clock , data , latch , value_to_latch , num_keys , value_when_pressed , interval , max_events );
98104
99105 return MP_OBJ_FROM_PTR (self );
100106}
0 commit comments