Skip to content

Commit 4dc286f

Browse files
committed
Reorganize the gamepad code
1 parent 049e809 commit 4dc286f

12 files changed

Lines changed: 370 additions & 112 deletions

File tree

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ $(filter $(SRC_PATTERNS), \
317317
fontio/BuiltinFont.c \
318318
fontio/__init__.c \
319319
gamepad/GamePad.c \
320+
gamepad/GamePadShift.c \
320321
gamepad/__init__.c \
321322
os/__init__.c \
322323
random/__init__.c \

shared-bindings/gamepad/GamePad.c

Lines changed: 6 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,10 @@
3131
#include "shared-module/gamepad/__init__.h"
3232
#include "shared-module/gamepad/GamePad.h"
3333
#include "shared-bindings/digitalio/DigitalInOut.h"
34-
#include "shared-bindings/util.h"
3534
#include "supervisor/shared/translate.h"
3635
#include "GamePad.h"
36+
#include "__init__.h"
3737

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-
}
4738

4839
//| .. currentmodule:: gamepad
4940
//|
@@ -109,86 +100,20 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
109100
mp_raise_TypeError(translate("argument num/types mismatch"));
110101
}
111102
for (size_t i = 0; i < n_args; ++i) {
112-
validate_pin(args[i]);
103+
pin_io(args[i]);
113104
}
114105
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)) {
116108
gamepad_singleton = m_new_obj(gamepad_obj_t);
117109
gamepad_singleton->base.type = &gamepad_type;
118110
gamepad_singleton = gc_make_long_lived(gamepad_singleton);
119111
MP_STATE_VM(gamepad_singleton) = gamepad_singleton;
120112
}
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);
189115
}
190116

191-
192117
//| .. method:: get_pressed()
193118
//|
194119
//| Get the status of buttons pressed since the last call and clear it.
@@ -230,15 +155,3 @@ const mp_obj_type_t gamepad_type = {
230155
.make_new = gamepad_make_new,
231156
.locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict,
232157
};
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-
};

shared-bindings/gamepad/GamePad.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H
3030

3131
extern const mp_obj_type_t gamepad_type;
32-
extern const mp_obj_type_t gamepadshift_type;
3332

3433
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#include "py/obj.h"
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "py/gc.h"
30+
#include "py/mpstate.h"
31+
#include "shared-module/gamepad/__init__.h"
32+
#include "shared-module/gamepad/GamePadShift.h"
33+
#include "supervisor/shared/translate.h"
34+
#include "GamePadShift.h"
35+
#include "__init__.h"
36+
37+
38+
//| .. class:: GamePadShift(data, clock, latch)
39+
//|
40+
//| Initializes button scanning routines.
41+
//|
42+
//| The ``data``, ``clock`` and ``latch`` parameters are ``DigitalInOut``
43+
//| objects connected to the shift register controlling the buttons.
44+
//|
45+
//| They button presses are accumulated, until the ``get_pressed`` method
46+
//| is called, at which point the button state is cleared, and the new
47+
//| button presses start to be recorded.
48+
//|
49+
STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_args,
50+
const mp_obj_t *pos_args, mp_map_t *kw_args) {
51+
52+
enum { ARG_data, ARG_clock, ARG_latch };
53+
static const mp_arg_t allowed_args[] = {
54+
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
55+
{ MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ},
56+
{ MP_QSTR_latch, MP_ARG_REQUIRED | MP_ARG_OBJ},
57+
};
58+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
59+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
60+
allowed_args, args);
61+
62+
digitalio_digitalinout_obj_t *data_pin = pin_io(args[ARG_data].u_obj);
63+
digitalio_digitalinout_obj_t *clock_pin = pin_io(args[ARG_clock].u_obj);
64+
digitalio_digitalinout_obj_t *latch_pin = pin_io(args[ARG_latch].u_obj);
65+
66+
gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton);
67+
if (!gamepad_singleton ||
68+
!MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(gamepad_singleton),
69+
&gamepadshift_type)) {
70+
gamepad_singleton = m_new_obj(gamepadshift_obj_t);
71+
gamepad_singleton->base.type = &gamepadshift_type;
72+
gamepad_singleton = gc_make_long_lived(gamepad_singleton);
73+
MP_STATE_VM(gamepad_singleton) = gamepad_singleton;
74+
}
75+
gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin);
76+
return MP_OBJ_FROM_PTR(gamepad_singleton);
77+
}
78+
79+
//| .. method:: get_pressed()
80+
//|
81+
//| Get the status of buttons pressed since the last call and clear it.
82+
//|
83+
//| Returns an 8-bit number, with bits that correspond to buttons,
84+
//| which have been pressed (or held down) since the last call to this
85+
//| function set to 1, and the remaining bits set to 0. Then it clears
86+
//| the button state, so that new button presses (or buttons that are
87+
//| held down) can be recorded for the next call.
88+
//|
89+
STATIC mp_obj_t gamepadshift_get_pressed(mp_obj_t self_in) {
90+
gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton);
91+
mp_obj_t pressed = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed);
92+
gamepad_singleton->pressed = 0;
93+
return pressed;
94+
}
95+
MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_get_pressed_obj, gamepadshift_get_pressed);
96+
97+
//| .. method:: deinit()
98+
//|
99+
//| Disable button scanning.
100+
//|
101+
STATIC mp_obj_t gamepadshift_deinit(mp_obj_t self_in) {
102+
gamepad_reset();
103+
return mp_const_none;
104+
}
105+
MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_deinit_obj, gamepadshift_deinit);
106+
107+
108+
STATIC const mp_rom_map_elem_t gamepadshift_locals_dict_table[] = {
109+
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepadshift_get_pressed_obj)},
110+
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepadshift_deinit_obj)},
111+
};
112+
STATIC MP_DEFINE_CONST_DICT(gamepadshift_locals_dict, gamepadshift_locals_dict_table);
113+
const mp_obj_type_t gamepadshift_type = {
114+
{ &mp_type_type },
115+
.name = MP_QSTR_GamePadShift,
116+
.make_new = gamepadshift_make_new,
117+
.locals_dict = (mp_obj_dict_t*)&gamepadshift_locals_dict,
118+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
28+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H
29+
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H
30+
31+
extern const mp_obj_type_t gamepadshift_type;
32+
33+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H

shared-bindings/gamepad/__init__.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
#include "py/runtime.h"
2828
#include "py/mphal.h"
2929
#include "GamePad.h"
30+
#include "GamePadShift.h"
31+
#include "shared-bindings/digitalio/DigitalInOut.h"
32+
#include "shared-bindings/util.h"
33+
34+
35+
// Helper for validating digitalio.DigitalInOut arguments
36+
digitalio_digitalinout_obj_t *pin_io(mp_obj_t obj) {
37+
if (!MP_OBJ_IS_TYPE(obj, &digitalio_digitalinout_type)) {
38+
mp_raise_TypeError(translate("argument num/types mismatch"));
39+
}
40+
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(obj);
41+
raise_error_if_deinited(
42+
common_hal_digitalio_digitalinout_deinited(pin));
43+
return pin;
44+
}
3045

3146

3247
//| :mod:`gamepad` --- Button handling

shared-bindings/gamepad/__init__.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
28+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H
29+
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H
30+
31+
digitalio_digitalinout_obj_t *pin_io(mp_obj_t obj);
32+
33+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H

0 commit comments

Comments
 (0)