36
36
//| class ShiftRegisterKeys:
37
37
//| """Manage a set of keys attached to an incoming shift register."""
38
38
//|
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:
40
40
//| """
41
41
//| 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 .
43
43
//| Note that you may chain shift registers to load in as many values as you need.
44
44
//|
45
45
//| Key number 0 is the first (or more properly, the zero-th) bit read. In the
53
53
//| The shift register should clock on a low-to-high transition.
54
54
//| :param microcontroller.Pin data: the incoming shift register data pin
55
55
//| :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.
58
62
//| :param int num_keys: number of data lines to clock in
59
63
//| :param bool value_when_pressed: ``True`` if the pin reads high when the key is pressed.
60
64
//| ``False`` if the pin reads low (is grounded) when the key is pressed.
70
74
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 ) {
71
75
keypad_shiftregisterkeys_obj_t * self = m_new_obj (keypad_shiftregisterkeys_obj_t );
72
76
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 };
74
78
static const mp_arg_t allowed_args [] = {
75
79
{ MP_QSTR_clock , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
76
80
{ MP_QSTR_data , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
77
81
{ 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} },
78
83
{ MP_QSTR_num_keys , MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
79
84
{ MP_QSTR_value_when_pressed , MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_BOOL },
80
85
{ 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
86
91
mcu_pin_obj_t * clock = validate_obj_is_free_pin (args [ARG_clock ].u_obj );
87
92
mcu_pin_obj_t * data = validate_obj_is_free_pin (args [ARG_data ].u_obj );
88
93
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 ;
89
95
90
96
const size_t num_keys = (size_t )mp_arg_validate_int_min (args [ARG_num_keys ].u_int , 1 , MP_QSTR_num_keys );
91
97
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
94
100
const size_t max_events = (size_t )mp_arg_validate_int_min (args [ARG_max_events ].u_int , 1 , MP_QSTR_max_events );
95
101
96
102
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 );
98
104
99
105
return MP_OBJ_FROM_PTR (self );
100
106
}
0 commit comments