|
31 | 31 | #include "shared-module/gamepad/__init__.h" |
32 | 32 | #include "shared-module/gamepad/GamePad.h" |
33 | 33 | #include "shared-bindings/digitalio/DigitalInOut.h" |
34 | | -#include "shared-bindings/util.h" |
35 | 34 | #include "supervisor/shared/translate.h" |
36 | 35 | #include "GamePad.h" |
| 36 | +#include "__init__.h" |
37 | 37 |
|
38 | | -STATIC digitalio_digitalinout_obj_t *validate_pin(mp_obj_t obj) { |
39 | | - if (!MP_OBJ_IS_TYPE(obj, &digitalio_digitalinout_type)) { |
40 | | - mp_raise_TypeError(translate("argument num/types mismatch")); |
41 | | - } |
42 | | - digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(obj); |
43 | | - raise_error_if_deinited( |
44 | | - common_hal_digitalio_digitalinout_deinited(pin)); |
45 | | - return pin; |
46 | | -} |
47 | 38 |
|
48 | 39 | //| .. currentmodule:: gamepad |
49 | 40 | //| |
@@ -109,86 +100,20 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, |
109 | 100 | mp_raise_TypeError(translate("argument num/types mismatch")); |
110 | 101 | } |
111 | 102 | for (size_t i = 0; i < n_args; ++i) { |
112 | | - validate_pin(args[i]); |
| 103 | + pin_io(args[i]); |
113 | 104 | } |
114 | 105 | gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); |
115 | | - if (!gamepad_singleton) { |
| 106 | + if (!gamepad_singleton || |
| 107 | + !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(gamepad_singleton), &gamepad_type)) { |
116 | 108 | gamepad_singleton = m_new_obj(gamepad_obj_t); |
117 | 109 | gamepad_singleton->base.type = &gamepad_type; |
118 | 110 | gamepad_singleton = gc_make_long_lived(gamepad_singleton); |
119 | 111 | MP_STATE_VM(gamepad_singleton) = gamepad_singleton; |
120 | 112 | } |
121 | | - for (size_t i = 0; i < 8; ++i) { |
122 | | - gamepad_singleton->pins[i] = NULL; |
123 | | - } |
124 | | - gamepad_singleton->pulls = 0; |
125 | | - for (size_t i = 0; i < n_args; ++i) { |
126 | | - digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(args[i]); |
127 | | - if (common_hal_digitalio_digitalinout_get_direction(pin) != |
128 | | - DIRECTION_INPUT) { |
129 | | - common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP); |
130 | | - } |
131 | | - digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin); |
132 | | - if (pull == PULL_NONE) { |
133 | | - common_hal_digitalio_digitalinout_set_pull(pin, PULL_UP); |
134 | | - } |
135 | | - if (pull != PULL_DOWN) { |
136 | | - gamepad_singleton->pulls |= 1 << i; |
137 | | - } |
138 | | - gamepad_singleton->pins[i] = pin; |
139 | | - } |
140 | | - return MP_OBJ_FROM_PTR(MP_STATE_VM(gamepad_singleton)); |
141 | | -} |
142 | | - |
143 | | - |
144 | | -//| .. class:: GamePadShift(data, clock, latch) |
145 | | -//| |
146 | | -//| Initializes button scanning routines. |
147 | | -//| |
148 | | -//| The ``data``, ``clock`` and ``latch`` parameters are ``DigitalInOut`` |
149 | | -//| objects connected to the shift register controlling the buttons. |
150 | | -//| |
151 | | -//| They button presses are accumulated, until the ``get_pressed`` method |
152 | | -//| is called, at which point the button state is cleared, and the new |
153 | | -//| button presses start to be recorded. |
154 | | -//| |
155 | | -STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_args, |
156 | | - const mp_obj_t *pos_args, mp_map_t *kw_args) { |
157 | | - |
158 | | - enum { ARG_data, ARG_clock, ARG_latch }; |
159 | | - static const mp_arg_t allowed_args[] = { |
160 | | - { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
161 | | - { MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ}, |
162 | | - { MP_QSTR_latch, MP_ARG_REQUIRED | MP_ARG_OBJ}, |
163 | | - }; |
164 | | - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
165 | | - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), |
166 | | - allowed_args, args); |
167 | | - |
168 | | - digitalio_digitalinout_obj_t *data_pin = validate_pin(args[ARG_data].u_obj); |
169 | | - digitalio_digitalinout_obj_t *clock_pin = validate_pin(args[ARG_clock].u_obj); |
170 | | - digitalio_digitalinout_obj_t *latch_pin = validate_pin(args[ARG_latch].u_obj); |
171 | | - |
172 | | - gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); |
173 | | - if (!gamepad_singleton) { |
174 | | - gamepad_singleton = m_new_obj(gamepad_obj_t); |
175 | | - gamepad_singleton->base.type = &gamepadshift_type; |
176 | | - gamepad_singleton = gc_make_long_lived(gamepad_singleton); |
177 | | - MP_STATE_VM(gamepad_singleton) = gamepad_singleton; |
178 | | - } |
179 | | - gamepad_singleton->pins[0] = NULL; |
180 | | - common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE); |
181 | | - gamepad_singleton->pins[1] = data_pin; |
182 | | - common_hal_digitalio_digitalinout_switch_to_output(clock_pin, 0, |
183 | | - DRIVE_MODE_PUSH_PULL); |
184 | | - gamepad_singleton->pins[2] = clock_pin; |
185 | | - common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, |
186 | | - DRIVE_MODE_PUSH_PULL); |
187 | | - gamepad_singleton->pins[3] = latch_pin; |
188 | | - return MP_OBJ_FROM_PTR(MP_STATE_VM(gamepad_singleton)); |
| 113 | + gamepad_init(gamepad_singleton, args, n_args); |
| 114 | + return MP_OBJ_FROM_PTR(gamepad_singleton); |
189 | 115 | } |
190 | 116 |
|
191 | | - |
192 | 117 | //| .. method:: get_pressed() |
193 | 118 | //| |
194 | 119 | //| Get the status of buttons pressed since the last call and clear it. |
@@ -230,15 +155,3 @@ const mp_obj_type_t gamepad_type = { |
230 | 155 | .make_new = gamepad_make_new, |
231 | 156 | .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, |
232 | 157 | }; |
233 | | - |
234 | | -STATIC const mp_rom_map_elem_t gamepadshift_locals_dict_table[] = { |
235 | | - { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, |
236 | | - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, |
237 | | -}; |
238 | | -STATIC MP_DEFINE_CONST_DICT(gamepadshift_locals_dict, gamepadshift_locals_dict_table); |
239 | | -const mp_obj_type_t gamepadshift_type = { |
240 | | - { &mp_type_type }, |
241 | | - .name = MP_QSTR_GamePadShift, |
242 | | - .make_new = gamepadshift_make_new, |
243 | | - .locals_dict = (mp_obj_dict_t*)&gamepadshift_locals_dict, |
244 | | -}; |
0 commit comments