Skip to content

Commit f498167

Browse files
deshiputannewt
authored andcommitted
Add a gamepad module for handling buttons in the background. (micropython#295)
The `GamePad` singleton monitors buttons in the background to make sure a button press is never missed and debouncing happens consistently.
1 parent c478c10 commit f498167

File tree

11 files changed

+422
-2
lines changed

11 files changed

+422
-2
lines changed

atmel-samd/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ SRC_SHARED_MODULE = \
289289
bitbangio/OneWire.c \
290290
bitbangio/SPI.c \
291291
busio/OneWire.c \
292+
gamepad/__init__.c \
293+
gamepad/GamePad.c \
292294
os/__init__.c \
293295
random/__init__.c \
294296
storage/__init__.c \

atmel-samd/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ extern const struct _mp_obj_module_t neopixel_write_module;
152152
extern const struct _mp_obj_module_t uheap_module;
153153
extern const struct _mp_obj_module_t ustack_module;
154154
extern const struct _mp_obj_module_t samd_module;
155+
extern const struct _mp_obj_module_t gamepad_module;
155156
extern const struct _mp_obj_module_t touchio_module;
156157
extern const struct _mp_obj_module_t usb_hid_module;
157158

@@ -171,10 +172,13 @@ extern const struct _mp_obj_module_t usb_hid_module;
171172
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
172173
#define MICROPY_PY_SYS_MAXSIZE (1)
173174
#define MICROPY_CPYTHON_COMPAT (1)
175+
// Scan gamepad every 32ms
176+
#define CIRCUITPY_GAMEPAD_TICKS 0x1f
174177

175178
#define EXTRA_BUILTIN_MODULES \
176179
{ MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \
177180
{ MP_OBJ_NEW_QSTR(MP_QSTR_audiobusio), (mp_obj_t)&audiobusio_module }, \
181+
{ MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \
178182
{ MP_OBJ_NEW_QSTR(MP_QSTR_nvm), (mp_obj_t)&cpy_nvm_module }, \
179183
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
180184
{ MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }

atmel-samd/tick.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "autoreload.h"
2+
#include "shared-module/gamepad/__init__.h"
23

34
#include "tick.h"
45

@@ -17,6 +18,11 @@ static void ms_tick(struct tc_module *const module_inst) {
1718
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
1819
autoreload_tick();
1920
#endif
21+
#ifdef CIRCUITPY_GAMEPAD_TICKS
22+
if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
23+
gamepad_tick();
24+
}
25+
#endif
2026
}
2127

2228
void tick_init() {

shared-bindings/gamepad/GamePad.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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 "shared-module/gamepad/GamePad.h"
30+
#include "GamePad.h"
31+
32+
33+
gamepad_obj_t* gamepad_singleton = NULL;
34+
35+
//| .. currentmodule:: gamepad
36+
//|
37+
//| :class:`GamePad` -- Scan buttons for presses
38+
//| ============================================
39+
//|
40+
//| Usage::
41+
//|
42+
//| import board
43+
//| import digitalio
44+
//| import gamepad
45+
//| import time
46+
//|
47+
//| B_UP = 1 << 0
48+
//| B_DOWN = 1 << 1
49+
//|
50+
//|
51+
//| pad = gamepad.GamePad(
52+
//| digitalio.DigitalInOut(board.D0),
53+
//| digitalio.DigitalInOut(board.D1),
54+
//| )
55+
//|
56+
//| y = 0
57+
//| while True:
58+
//| buttons = pad.get_pressed()
59+
//| if buttons & B_UP:
60+
//| y -= 1
61+
//| print(y)
62+
//| elif buttons & B_DOWN:
63+
//| y += 1
64+
//| print(y)
65+
//| time.sleep(0.1)
66+
//| while pad.get_pressed():
67+
//| # Wait for all buttons to be released.
68+
//| time.sleep(0.1)
69+
//|
70+
71+
//| .. class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]])
72+
//|
73+
//| Initializes button scanning routines.
74+
//|
75+
//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which
76+
//| immediately get switched to input with a pull-up, and then scanned
77+
//| regularly for button presses. The order is the same as the order of
78+
//| bits returned by the ``get_pressed`` function. You can re-initialize
79+
//| it with different keys, then the new object will replace the previous
80+
//| one.
81+
//|
82+
//| The basic feature required here is the ability to poll the keys at
83+
//| regular intervals (so that de-bouncing is consistent) and fast enough
84+
//| (so that we don't miss short button presses) while at the same time
85+
//| letting the user code run normally, call blocking functions and wait
86+
//| on delays.
87+
//|
88+
//| They button presses are accumulated, until the ``get_pressed`` method
89+
//| is called, at which point the button state is cleared, and the new
90+
//| button presses start to be recorded.
91+
//|
92+
STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
93+
size_t n_kw, const mp_obj_t *args) {
94+
if (!gamepad_singleton) {
95+
gamepad_singleton = m_new_obj(gamepad_obj_t);
96+
gamepad_singleton->base.type = &gamepad_type;
97+
}
98+
gamepad_init(n_args, args);
99+
return MP_OBJ_FROM_PTR(gamepad_singleton);
100+
}
101+
102+
103+
//| .. method:: get_pressed()
104+
//|
105+
//| Get the status of buttons pressed since the last call and clear it.
106+
//|
107+
//| Returns an 8-bit number, with bits that correspond to buttons,
108+
//| which have been pressed (or held down) since the last call to this
109+
//| function set to 1, and the remaining bits set to 0. Then it clears
110+
//| the button state, so that new button presses (or buttons that are
111+
//| held down) can be recorded for the next call.
112+
//|
113+
STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) {
114+
gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in);
115+
mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed);
116+
self->pressed = 0;
117+
return gamepad;
118+
}
119+
MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed);
120+
121+
122+
//| .. method:: deinit()
123+
//|
124+
//| Disable button scanning.
125+
//|
126+
STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) {
127+
gamepad_singleton = NULL;
128+
return mp_const_none;
129+
}
130+
MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit);
131+
132+
133+
STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
134+
size_t n_kw, const mp_obj_t *args);
135+
STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = {
136+
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)},
137+
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)},
138+
};
139+
STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table);
140+
const mp_obj_type_t gamepad_type = {
141+
{ &mp_type_type },
142+
.name = MP_QSTR_GamePad,
143+
.make_new = gamepad_make_new,
144+
.locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict,
145+
};
146+

shared-bindings/gamepad/GamePad.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_GAMEPAD_H
29+
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H
30+
31+
extern const mp_obj_type_t gamepad_type;
32+
33+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H

shared-bindings/gamepad/__init__.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 "GamePad.h"
30+
31+
32+
//| :mod:`gamepad` --- Button handling
33+
//| ==================================
34+
//|
35+
//| .. module:: gamepad
36+
//| :synopsis: Button handling
37+
//| :platform: SAMD21
38+
//|
39+
//| .. toctree::
40+
//| :maxdepth: 3
41+
//|
42+
//| GamePad
43+
//|
44+
STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = {
45+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) },
46+
{ MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)},
47+
};
48+
STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals,
49+
gamepad_module_globals_table);
50+
51+
const mp_obj_module_t gamepad_module = {
52+
.base = { &mp_type_module },
53+
.globals = (mp_obj_dict_t*)&gamepad_module_globals,
54+
};

shared-bindings/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Module / Port SAMD21 SAMD21 Express ESP8266
2121
`board` **Yes** **Yes** **Yes**
2222
`busio` **Yes** **Yes** **Yes**
2323
`digitalio` **Yes** **Yes** **Yes**
24-
`microcontroller` **Yes** **Yes** **Yes**
24+
`gamepad` No **Yes** No
25+
`microcontroller` **Yes** **Yes** **Yes**
2526
`multiterminal` No No **Yes**
26-
`neopixel_write` **Yes** **Yes** **Yes**
27+
`neopixel_write` **Yes** **Yes** **Yes**
2728
`nvm` No **Yes** No
2829
`os` **Yes** **Yes** **Yes**
2930
`pulseio` No **Yes** No

shared-module/gamepad/GamePad.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#include <stdbool.h>
28+
29+
#include "__init__.h"
30+
#include "GamePad.h"
31+
32+
#include "shared-bindings/digitalio/Pull.h"
33+
#include "shared-bindings/digitalio/DigitalInOut.h"
34+
35+
36+
void gamepad_init(size_t n_pins, const mp_obj_t* pins) {
37+
for (size_t i=0; i<8; ++i) {
38+
gamepad_singleton->pins[i] = NULL;
39+
}
40+
for (size_t i=0; i<n_pins; ++i) {
41+
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pins[i]);
42+
gamepad_singleton->pins[i] = pin;
43+
common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP);
44+
}
45+
gamepad_singleton->last = 0;
46+
}

shared-module/gamepad/GamePad.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#ifndef MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H
28+
#define MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H
29+
30+
#include <stdint.h>
31+
32+
#include "shared-bindings/digitalio/DigitalInOut.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
digitalio_digitalinout_obj_t* pins[8];
37+
volatile uint8_t last;
38+
volatile uint8_t pressed;
39+
} gamepad_obj_t;
40+
41+
extern gamepad_obj_t* gamepad_singleton;
42+
43+
void gamepad_init(size_t n_pins, const mp_obj_t* pins);
44+
45+
#endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H

0 commit comments

Comments
 (0)