From 947662415c17c85bf0917049ab00e41b233f34b1 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Thu, 11 Apr 2019 14:42:38 +0200 Subject: Disable gamepad for feather_m0_express_crickit --- ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ports') diff --git a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk index fa39de33f..4bd77b6e6 100644 --- a/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express_crickit/mpconfigboard.mk @@ -19,6 +19,8 @@ CHIP_FAMILY = samd21 CFLAGS_INLINE_LIMIT = 50 +CIRCUITPY_GAMEPAD = 0 + # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit -- cgit v1.2.3 From c927e6b938ff35fe0f42180d1dc0f9d58f8e571c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 15 Apr 2019 15:40:06 -0700 Subject: Split GamePadShift from GamePad to save space on most boards. --- ports/atmel-samd/boards/pybadge/mpconfigboard.mk | 2 + py/circuitpy_defns.mk | 7 +- py/circuitpy_mpconfig.h | 8 ++ py/circuitpy_mpconfig.mk | 5 + shared-bindings/gamepad/GamePad.c | 6 +- shared-bindings/gamepad/GamePad.h | 5 + shared-bindings/gamepad/GamePadShift.c | 122 ---------------------- shared-bindings/gamepad/GamePadShift.h | 33 ------ shared-bindings/gamepad/__init__.c | 8 +- shared-bindings/gamepad/__init__.h | 4 +- shared-bindings/gamepadshift/GamePadShift.c | 125 +++++++++++++++++++++++ shared-bindings/gamepadshift/GamePadShift.h | 42 ++++++++ shared-bindings/gamepadshift/__init__.c | 54 ++++++++++ shared-bindings/gamepadshift/__init__.h | 31 ++++++ shared-module/gamepad/GamePad.c | 9 +- shared-module/gamepad/GamePad.h | 5 +- shared-module/gamepad/GamePadShift.c | 43 -------- shared-module/gamepad/GamePadShift.h | 48 --------- shared-module/gamepad/__init__.c | 33 +++--- shared-module/gamepadshift/GamePadShift.c | 64 ++++++++++++ shared-module/gamepadshift/GamePadShift.h | 45 ++++++++ shared-module/gamepadshift/__init__.c | 27 +++++ 22 files changed, 442 insertions(+), 284 deletions(-) delete mode 100644 shared-bindings/gamepad/GamePadShift.c delete mode 100644 shared-bindings/gamepad/GamePadShift.h create mode 100644 shared-bindings/gamepadshift/GamePadShift.c create mode 100644 shared-bindings/gamepadshift/GamePadShift.h create mode 100644 shared-bindings/gamepadshift/__init__.c create mode 100644 shared-bindings/gamepadshift/__init__.h delete mode 100644 shared-module/gamepad/GamePadShift.c delete mode 100644 shared-module/gamepad/GamePadShift.h create mode 100644 shared-module/gamepadshift/GamePadShift.c create mode 100644 shared-module/gamepadshift/GamePadShift.h create mode 100644 shared-module/gamepadshift/__init__.c (limited to 'ports') diff --git a/ports/atmel-samd/boards/pybadge/mpconfigboard.mk b/ports/atmel-samd/boards/pybadge/mpconfigboard.mk index 8828c17b2..63510b52a 100644 --- a/ports/atmel-samd/boards/pybadge/mpconfigboard.mk +++ b/ports/atmel-samd/boards/pybadge/mpconfigboard.mk @@ -14,5 +14,7 @@ CIRCUITPY_AUDIOBUSIO = 0 # No touch on SAMD51 yet CIRCUITPY_TOUCHIO = 0 +CIRCUITPY_GAMEPADSHIFT = 1 + CHIP_VARIANT = SAMD51J19A CHIP_FAMILY = samd51 diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 41c73430f..be7ff0b11 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -131,6 +131,10 @@ SRC_PATTERNS += frequencyio/% endif ifeq ($(CIRCUITPY_GAMEPAD),1) SRC_PATTERNS += gamepad/% + # gamepadshift depends on gamepad + ifeq ($(CIRCUITPY_GAMEPADSHIFT),1) + SRC_PATTERNS += gamepadshift/% + endif endif ifeq ($(CIRCUITPY_I2CSLAVE),1) SRC_PATTERNS += i2cslave/% @@ -317,8 +321,9 @@ $(filter $(SRC_PATTERNS), \ fontio/BuiltinFont.c \ fontio/__init__.c \ gamepad/GamePad.c \ - gamepad/GamePadShift.c \ gamepad/__init__.c \ + gamepadshift/GamePadShift.c \ + gamepadshift/__init__.c \ os/__init__.c \ random/__init__.c \ socket/__init__.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 441dd5bad..3952b5e06 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -322,6 +322,13 @@ extern const struct _mp_obj_module_t gamepad_module; #define GAMEPAD_MODULE #endif +#if CIRCUITPY_GAMEPADSHIFT +extern const struct _mp_obj_module_t gamepadshift_module; +#define GAMEPADSHIFT_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_gamepadshift),(mp_obj_t)&gamepadshift_module }, +#else +#define GAMEPADSHIFT_MODULE +#endif + #if CIRCUITPY_I2CSLAVE extern const struct _mp_obj_module_t i2cslave_module; #define I2CSLAVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_i2cslave), (mp_obj_t)&i2cslave_module }, @@ -554,6 +561,7 @@ extern const struct _mp_obj_module_t ustack_module; ERRNO_MODULE \ FREQUENCYIO_MODULE \ GAMEPAD_MODULE \ + GAMEPADSHIFT_MODULE \ I2CSLAVE_MODULE \ JSON_MODULE \ MATH_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 8ea72f0bf..b436929e0 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -102,6 +102,11 @@ CIRCUITPY_GAMEPAD = $(CIRCUITPY_FULL_BUILD) endif CFLAGS += -DCIRCUITPY_GAMEPAD=$(CIRCUITPY_GAMEPAD) +ifndef CIRCUITPY_GAMEPADSHIFT +CIRCUITPY_GAMEPADSHIFT = 0 +endif +CFLAGS += -DCIRCUITPY_GAMEPADSHIFT=$(CIRCUITPY_GAMEPADSHIFT) + ifndef CIRCUITPY_I2CSLAVE CIRCUITPY_I2CSLAVE = $(CIRCUITPY_FULL_BUILD) endif diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index 8b88471ee..c80aa7f7c 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -100,7 +100,7 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, mp_raise_TypeError(translate("argument num/types mismatch")); } for (size_t i = 0; i < n_args; ++i) { - pin_io(args[i]); + assert_digitalinout(args[i]); } gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); if (!gamepad_singleton || @@ -110,7 +110,7 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, gamepad_singleton = gc_make_long_lived(gamepad_singleton); MP_STATE_VM(gamepad_singleton) = gamepad_singleton; } - gamepad_init(gamepad_singleton, args, n_args); + common_hal_gamepad_gamepad_init(gamepad_singleton, args, n_args); return MP_OBJ_FROM_PTR(gamepad_singleton); } @@ -138,7 +138,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed); //| Disable button scanning. //| STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { - gamepad_reset(); + common_hal_gamepad_gamepad_deinit(self_in); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); diff --git a/shared-bindings/gamepad/GamePad.h b/shared-bindings/gamepad/GamePad.h index 172c95ace..3bbad4c97 100644 --- a/shared-bindings/gamepad/GamePad.h +++ b/shared-bindings/gamepad/GamePad.h @@ -28,6 +28,11 @@ #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H #define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H +#include "shared-module/gamepad/GamePad.h" + extern const mp_obj_type_t gamepad_type; +void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad, const mp_obj_t pins[], size_t n_pins); +void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *gamepad); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H diff --git a/shared-bindings/gamepad/GamePadShift.c b/shared-bindings/gamepad/GamePadShift.c deleted file mode 100644 index c24a9162a..000000000 --- a/shared-bindings/gamepad/GamePadShift.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include "py/obj.h" -#include "py/runtime.h" -#include "py/mphal.h" -#include "py/gc.h" -#include "py/mpstate.h" -#include "shared-module/gamepad/__init__.h" -#include "shared-module/gamepad/GamePadShift.h" -#include "supervisor/shared/translate.h" -#include "GamePadShift.h" -#include "__init__.h" - -//| .. currentmodule:: gamepad -//| -//| :class:`GamePadShift` -- Scan buttons for presses -//| ================================================= -//| -//| .. class:: GamePadShift(data, clock, latch) -//| -//| Initializes button scanning routines. -//| -//| The ``data``, ``clock`` and ``latch`` parameters are ``DigitalInOut`` -//| objects connected to the shift register controlling the buttons. -//| -//| They button presses are accumulated, until the ``get_pressed`` method -//| is called, at which point the button state is cleared, and the new -//| button presses start to be recorded. -//| -STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_args, - const mp_obj_t *pos_args, mp_map_t *kw_args) { - - enum { ARG_data, ARG_clock, ARG_latch }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ}, - { MP_QSTR_latch, MP_ARG_REQUIRED | MP_ARG_OBJ}, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), - allowed_args, args); - - digitalio_digitalinout_obj_t *data_pin = pin_io(args[ARG_data].u_obj); - digitalio_digitalinout_obj_t *clock_pin = pin_io(args[ARG_clock].u_obj); - digitalio_digitalinout_obj_t *latch_pin = pin_io(args[ARG_latch].u_obj); - - gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); - if (!gamepad_singleton || - !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(gamepad_singleton), - &gamepadshift_type)) { - gamepad_singleton = m_new_obj(gamepadshift_obj_t); - gamepad_singleton->base.type = &gamepadshift_type; - gamepad_singleton = gc_make_long_lived(gamepad_singleton); - MP_STATE_VM(gamepad_singleton) = gamepad_singleton; - } - gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin); - return MP_OBJ_FROM_PTR(gamepad_singleton); -} - -//| .. method:: get_pressed() -//| -//| Get the status of buttons pressed since the last call and clear it. -//| -//| Returns an 8-bit number, with bits that correspond to buttons, -//| which have been pressed (or held down) since the last call to this -//| function set to 1, and the remaining bits set to 0. Then it clears -//| the button state, so that new button presses (or buttons that are -//| held down) can be recorded for the next call. -//| -STATIC mp_obj_t gamepadshift_get_pressed(mp_obj_t self_in) { - gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); - mp_obj_t pressed = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed); - gamepad_singleton->pressed = 0; - return pressed; -} -MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_get_pressed_obj, gamepadshift_get_pressed); - -//| .. method:: deinit() -//| -//| Disable button scanning. -//| -STATIC mp_obj_t gamepadshift_deinit(mp_obj_t self_in) { - gamepad_reset(); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_deinit_obj, gamepadshift_deinit); - - -STATIC const mp_rom_map_elem_t gamepadshift_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepadshift_get_pressed_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepadshift_deinit_obj)}, -}; -STATIC MP_DEFINE_CONST_DICT(gamepadshift_locals_dict, gamepadshift_locals_dict_table); -const mp_obj_type_t gamepadshift_type = { - { &mp_type_type }, - .name = MP_QSTR_GamePadShift, - .make_new = gamepadshift_make_new, - .locals_dict = (mp_obj_dict_t*)&gamepadshift_locals_dict, -}; diff --git a/shared-bindings/gamepad/GamePadShift.h b/shared-bindings/gamepad/GamePadShift.h deleted file mode 100644 index 68c8de876..000000000 --- a/shared-bindings/gamepad/GamePadShift.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - - -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H - -extern const mp_obj_type_t gamepadshift_type; - -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c index 0806a142e..e61f36cc2 100644 --- a/shared-bindings/gamepad/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -26,14 +26,12 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mphal.h" -#include "GamePad.h" -#include "GamePadShift.h" -#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/gamepad/GamePad.h" #include "shared-bindings/util.h" // Helper for validating digitalio.DigitalInOut arguments -digitalio_digitalinout_obj_t *pin_io(mp_obj_t obj) { +digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj) { if (!MP_OBJ_IS_TYPE(obj, &digitalio_digitalinout_type)) { mp_raise_TypeError(translate("argument num/types mismatch")); } @@ -55,12 +53,10 @@ digitalio_digitalinout_obj_t *pin_io(mp_obj_t obj) { //| :maxdepth: 3 //| //| GamePad -//| GamePadShift //| STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) }, { MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_GamePadShift), MP_ROM_PTR(&gamepadshift_type)}, }; STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals, gamepad_module_globals_table); diff --git a/shared-bindings/gamepad/__init__.h b/shared-bindings/gamepad/__init__.h index 12e38edc4..40cf4e6de 100644 --- a/shared-bindings/gamepad/__init__.h +++ b/shared-bindings/gamepad/__init__.h @@ -28,6 +28,8 @@ #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H #define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H -digitalio_digitalinout_obj_t *pin_io(mp_obj_t obj); +#include "shared-bindings/digitalio/DigitalInOut.h" + +digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H diff --git a/shared-bindings/gamepadshift/GamePadShift.c b/shared-bindings/gamepadshift/GamePadShift.c new file mode 100644 index 000000000..94b71036f --- /dev/null +++ b/shared-bindings/gamepadshift/GamePadShift.c @@ -0,0 +1,125 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/gc.h" +#include "py/mpstate.h" +#include "shared-bindings/gamepad/__init__.h" +#include "shared-bindings/gamepadshift/GamePadShift.h" +#include "shared-bindings/gamepadshift/__init__.h" +#include "shared-module/gamepadshift/GamePadShift.h" +#include "supervisor/shared/translate.h" + +//| .. currentmodule:: gamepadshift +//| +//| :class:`GamePadShift` -- Scan buttons for presses through a shift register +//| =========================================================================== +//| +//| .. class:: GamePadShift(data, clock, latch) +//| +//| Initializes button scanning routines. +//| +//| The ``data``, ``clock`` and ``latch`` parameters are ``DigitalInOut`` +//| objects connected to the shift register controlling the buttons. +//| +//| They button presses are accumulated, until the ``get_pressed`` method +//| is called, at which point the button state is cleared, and the new +//| button presses start to be recorded. +//| +//| Only one gamepad (`gamepad.GamePad` or `gamepadshift.GamePadShift`) +//| may be used at a time. +//| +STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_args, + const mp_obj_t *pos_args, mp_map_t *kw_args) { + + enum { ARG_data, ARG_clock, ARG_latch }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ}, + { MP_QSTR_latch, MP_ARG_REQUIRED | MP_ARG_OBJ}, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), + allowed_args, args); + + digitalio_digitalinout_obj_t *data_pin = assert_digitalinout(args[ARG_data].u_obj); + digitalio_digitalinout_obj_t *clock_pin = assert_digitalinout(args[ARG_clock].u_obj); + digitalio_digitalinout_obj_t *latch_pin = assert_digitalinout(args[ARG_latch].u_obj); + + gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); + if (!gamepad_singleton || + !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(gamepad_singleton), + &gamepadshift_type)) { + gamepad_singleton = m_new_obj(gamepadshift_obj_t); + gamepad_singleton->base.type = &gamepadshift_type; + gamepad_singleton = gc_make_long_lived(gamepad_singleton); + MP_STATE_VM(gamepad_singleton) = gamepad_singleton; + } + common_hal_gamepadshift_gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin); + return MP_OBJ_FROM_PTR(gamepad_singleton); +} + +//| .. method:: get_pressed() +//| +//| Get the status of buttons pressed since the last call and clear it. +//| +//| Returns an 8-bit number, with bits that correspond to buttons, +//| which have been pressed (or held down) since the last call to this +//| function set to 1, and the remaining bits set to 0. Then it clears +//| the button state, so that new button presses (or buttons that are +//| held down) can be recorded for the next call. +//| +STATIC mp_obj_t gamepadshift_get_pressed(mp_obj_t self_in) { + gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); + mp_obj_t pressed = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed); + gamepad_singleton->pressed = 0; + return pressed; +} +MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_get_pressed_obj, gamepadshift_get_pressed); + +//| .. method:: deinit() +//| +//| Disable button scanning. +//| +STATIC mp_obj_t gamepadshift_deinit(mp_obj_t self_in) { + common_hal_gamepadshift_gamepadshift_deinit(self_in); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_deinit_obj, gamepadshift_deinit); + + +STATIC const mp_rom_map_elem_t gamepadshift_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepadshift_get_pressed_obj)}, + { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepadshift_deinit_obj)}, +}; +STATIC MP_DEFINE_CONST_DICT(gamepadshift_locals_dict, gamepadshift_locals_dict_table); +const mp_obj_type_t gamepadshift_type = { + { &mp_type_type }, + .name = MP_QSTR_GamePadShift, + .make_new = gamepadshift_make_new, + .locals_dict = (mp_obj_dict_t*)&gamepadshift_locals_dict, +}; diff --git a/shared-bindings/gamepadshift/GamePadShift.h b/shared-bindings/gamepadshift/GamePadShift.h new file mode 100644 index 000000000..2668ec83f --- /dev/null +++ b/shared-bindings/gamepadshift/GamePadShift.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT_GAMEPADSHIFT_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT_GAMEPADSHIFT_H + +#include "shared-module/gamepadshift/GamePadShift.h" + +extern const mp_obj_type_t gamepadshift_type; + +void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, + digitalio_digitalinout_obj_t *data_pin, + digitalio_digitalinout_obj_t *clock_pin, + digitalio_digitalinout_obj_t *latch_pin); + +void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT_GAMEPADSHIFT_H diff --git a/shared-bindings/gamepadshift/__init__.c b/shared-bindings/gamepadshift/__init__.c new file mode 100644 index 000000000..2d3667726 --- /dev/null +++ b/shared-bindings/gamepadshift/__init__.c @@ -0,0 +1,54 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "py/obj.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "shared-bindings/gamepadshift/GamePadShift.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/util.h" + +//| :mod:`gamepadshift` --- Tracks button presses read through a shift register +//| =========================================================================== +//| +//| .. module:: gamepadshift +//| :synopsis: Tracks button presses read through a shift register +//| :platform: SAMD21, SAMD51 +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| GamePadShift +//| +STATIC const mp_rom_map_elem_t gamepadshift_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepadshift) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_GamePadShift), MP_ROM_PTR(&gamepadshift_type)}, +}; +STATIC MP_DEFINE_CONST_DICT(gamepadshift_module_globals, gamepadshift_module_globals_table); + +const mp_obj_module_t gamepadshift_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&gamepadshift_module_globals, +}; diff --git a/shared-bindings/gamepadshift/__init__.h b/shared-bindings/gamepadshift/__init__.h new file mode 100644 index 000000000..4b4be756a --- /dev/null +++ b/shared-bindings/gamepadshift/__init__.h @@ -0,0 +1,31 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT___INIT___H + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT___INIT___H diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index 23addb397..b3e3fabf6 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -24,10 +24,11 @@ * THE SOFTWARE. */ +#include "py/mpstate.h" #include "shared-bindings/digitalio/DigitalInOut.h" -#include "GamePad.h" +#include "shared-bindings/gamepad/GamePad.h" -void gamepad_init(gamepad_obj_t *gamepad, +void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad, const mp_obj_t pins[], size_t n_pins) { for (size_t i = 0; i < 8; ++i) { gamepad->pins[i] = NULL; @@ -49,3 +50,7 @@ void gamepad_init(gamepad_obj_t *gamepad, gamepad->pins[i] = pin; } } + +void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) { + MP_STATE_VM(gamepad_singleton) = NULL; +} diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index dc8a7e87a..e8bfd9c6b 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -36,10 +36,7 @@ typedef struct { digitalio_digitalinout_obj_t* pins[8]; volatile uint8_t pressed; uint8_t pulls; + volatile uint8_t last; } gamepad_obj_t; - -void gamepad_init(gamepad_obj_t *gamepad, - const mp_obj_t pins[], size_t n_pins); - #endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H diff --git a/shared-module/gamepad/GamePadShift.c b/shared-module/gamepad/GamePadShift.c deleted file mode 100644 index 12083a86a..000000000 --- a/shared-module/gamepad/GamePadShift.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "shared-bindings/digitalio/DigitalInOut.h" -#include "GamePadShift.h" - - -void gamepadshift_init(gamepadshift_obj_t *gamepadshift, - digitalio_digitalinout_obj_t *data_pin, - digitalio_digitalinout_obj_t *clock_pin, - digitalio_digitalinout_obj_t *latch_pin) { - common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE); - gamepadshift->data_pin = data_pin; - common_hal_digitalio_digitalinout_switch_to_output(clock_pin, 0, - DRIVE_MODE_PUSH_PULL); - gamepadshift->clock_pin = clock_pin; - common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, - DRIVE_MODE_PUSH_PULL); - gamepadshift->latch_pin = latch_pin; -} diff --git a/shared-module/gamepad/GamePadShift.h b/shared-module/gamepad/GamePadShift.h deleted file mode 100644 index 05235f318..000000000 --- a/shared-module/gamepad/GamePadShift.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_GAMEPAD_GAMEPADSHIFT_H -#define MICROPY_INCLUDED_GAMEPAD_GAMEPADSHIFT_H - -#include - -#include "shared-bindings/digitalio/DigitalInOut.h" - -typedef struct { - mp_obj_base_t base; - digitalio_digitalinout_obj_t* data_pin; - digitalio_digitalinout_obj_t* clock_pin; - digitalio_digitalinout_obj_t* latch_pin; - volatile uint8_t pressed; -} gamepadshift_obj_t; - - -void gamepadshift_init(gamepadshift_obj_t *gamepadshift, - digitalio_digitalinout_obj_t *data_pin, - digitalio_digitalinout_obj_t *clock_pin, - digitalio_digitalinout_obj_t *latch_pin); - -#endif // MICROPY_INCLUDED_GAMEPAD_GAMEPADSHIFT_H diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index f92042d4b..8a4f4eae9 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -27,17 +27,17 @@ #include #include "py/mpstate.h" -#include "__init__.h" -#include "GamePad.h" -#include "GamePadShift.h" +#include "shared-bindings/gamepad/__init__.h" #include "shared-bindings/gamepad/GamePad.h" -#include "shared-bindings/gamepad/GamePadShift.h" + +#if CIRCUITPY_GAMEPADSHIFT +#include "shared-bindings/gamepadshift/GamePadShift.h" +#endif #include "shared-bindings/digitalio/DigitalInOut.h" void gamepad_tick(void) { - static uint8_t last = 0; uint8_t current = 0; uint8_t bit = 1; @@ -59,24 +59,15 @@ void gamepad_tick(void) { bit <<= 1; } current ^= self->pulls; - self->pressed |= last & current; - } else if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { + self->pressed |= self->last & current; + self->last = current; + } + #if CIRCUITPY_GAMEPADSHIFT + else if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { // buttons connected to a shift register - gamepadshift_obj_t *self = singleton; - - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); - for (int i = 0; i < 8; ++i) { - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); - if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { - current |= bit; - } - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); - bit <<= 1; - } - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); - self->pressed |= last & current; + gamepadshift_tick(singleton); } - last = current; + #endif } void gamepad_reset(void) { diff --git a/shared-module/gamepadshift/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c new file mode 100644 index 000000000..1dadfb9b2 --- /dev/null +++ b/shared-module/gamepadshift/GamePadShift.c @@ -0,0 +1,64 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpstate.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-module/gamepadshift/GamePadShift.h" + +void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, + digitalio_digitalinout_obj_t *data_pin, + digitalio_digitalinout_obj_t *clock_pin, + digitalio_digitalinout_obj_t *latch_pin) { + common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE); + gamepadshift->data_pin = data_pin; + common_hal_digitalio_digitalinout_switch_to_output(clock_pin, 0, + DRIVE_MODE_PUSH_PULL); + gamepadshift->clock_pin = clock_pin; + common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, + DRIVE_MODE_PUSH_PULL); + gamepadshift->latch_pin = latch_pin; +} + +void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) { + MP_STATE_VM(gamepad_singleton) = NULL; +} + +void gamepadshift_tick(gamepadshift_obj_t *self) { + uint8_t current = 0; + uint8_t bit = 1; + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); + for (int i = 0; i < 8; ++i) { + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); + if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { + current |= bit; + } + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); + bit <<= 1; + } + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); + self->pressed |= self->last & current; + self->last = current; +} diff --git a/shared-module/gamepadshift/GamePadShift.h b/shared-module/gamepadshift/GamePadShift.h new file mode 100644 index 000000000..ccbf5ca06 --- /dev/null +++ b/shared-module/gamepadshift/GamePadShift.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H +#define MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H + +#include + +#include "shared-bindings/digitalio/DigitalInOut.h" + +typedef struct { + mp_obj_base_t base; + digitalio_digitalinout_obj_t* data_pin; + digitalio_digitalinout_obj_t* clock_pin; + digitalio_digitalinout_obj_t* latch_pin; + volatile uint8_t pressed; + volatile uint8_t last; +} gamepadshift_obj_t; + +void gamepadshift_tick(gamepadshift_obj_t *self); + +#endif // MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H diff --git a/shared-module/gamepadshift/__init__.c b/shared-module/gamepadshift/__init__.c new file mode 100644 index 000000000..c3bb83e34 --- /dev/null +++ b/shared-module/gamepadshift/__init__.c @@ -0,0 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +// Nothing now. -- cgit v1.2.3 From 0e03a321e4c4fba6c5d700bfef830c6196aec106 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 16 Apr 2019 10:11:54 -0700 Subject: Fully split gamepadshift from gamepad --- ports/atmel-samd/supervisor/port.c | 6 +++++ py/circuitpy_defns.mk | 7 +++--- py/circuitpy_mpconfig.h | 12 +++++++--- shared-bindings/digitalio/DigitalInOut.c | 11 +++++++++ shared-bindings/digitalio/DigitalInOut.h | 1 + shared-bindings/gamepad/__init__.c | 13 ----------- shared-bindings/gamepad/__init__.h | 4 ---- shared-bindings/gamepadshift/GamePadShift.c | 12 +++++----- shared-bindings/gamepadshift/GamePadShift.h | 2 +- shared-module/gamepad/__init__.c | 10 --------- shared-module/gamepadshift/GamePadShift.c | 19 +--------------- shared-module/gamepadshift/GamePadShift.h | 2 -- shared-module/gamepadshift/__init__.c | 35 ++++++++++++++++++++++++++++- shared-module/gamepadshift/__init__.h | 33 +++++++++++++++++++++++++++ 14 files changed, 105 insertions(+), 62 deletions(-) create mode 100644 shared-module/gamepadshift/__init__.h (limited to 'ports') diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index f8d830d8b..4f53d30f9 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -71,6 +71,9 @@ #if CIRCUITPY_GAMEPAD #include "shared-module/gamepad/__init__.h" #endif +#if CIRCUITPY_GAMEPADSHIFT +#include "shared-module/gamepadshift/__init__.h" +#endif #include "shared-module/_pew/PewPew.h" extern volatile bool mp_msc_enabled; @@ -229,6 +232,9 @@ void reset_port(void) { #if CIRCUITPY_GAMEPAD gamepad_reset(); #endif +#if CIRCUITPY_GAMEPADSHIFT + gamepadshift_reset(); +#endif #if CIRCUITPY_PEW pew_reset(); #endif diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index be7ff0b11..1aca33901 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -131,10 +131,9 @@ SRC_PATTERNS += frequencyio/% endif ifeq ($(CIRCUITPY_GAMEPAD),1) SRC_PATTERNS += gamepad/% - # gamepadshift depends on gamepad - ifeq ($(CIRCUITPY_GAMEPADSHIFT),1) - SRC_PATTERNS += gamepadshift/% - endif +endif +ifeq ($(CIRCUITPY_GAMEPADSHIFT),1) +SRC_PATTERNS += gamepadshift/% endif ifeq ($(CIRCUITPY_I2CSLAVE),1) SRC_PATTERNS += i2cslave/% diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 3952b5e06..a101aa527 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -315,8 +315,6 @@ extern const struct _mp_obj_module_t frequencyio_module; #if CIRCUITPY_GAMEPAD extern const struct _mp_obj_module_t gamepad_module; -// Scan gamepad every 32ms -#define CIRCUITPY_GAMEPAD_TICKS 0x1f #define GAMEPAD_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, #else #define GAMEPAD_MODULE @@ -329,6 +327,14 @@ extern const struct _mp_obj_module_t gamepadshift_module; #define GAMEPADSHIFT_MODULE #endif +#if CIRCUITPY_GAMEPAD || CIRCUITPY_GAMEPADSHIFT +// Scan gamepad every 32ms +#define CIRCUITPY_GAMEPAD_TICKS 0x1f +#define GAMEPAD_ROOT_POINTERS mp_obj_t gamepad_singleton; +#else +#define GAMEPAD_ROOT_POINTERS +#endif + #if CIRCUITPY_I2CSLAVE extern const struct _mp_obj_module_t i2cslave_module; #define I2CSLAVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_i2cslave), (mp_obj_t)&i2cslave_module }, @@ -612,7 +618,7 @@ extern const struct _mp_obj_module_t ustack_module; const char *readline_hist[8]; \ vstr_t *repl_line; \ mp_obj_t rtc_time_source; \ - mp_obj_t gamepad_singleton; \ + GAMEPAD_ROOT_POINTERS \ mp_obj_t pew_singleton; \ mp_obj_t terminal_tilegrid_tiles; \ BOARD_I2C_ROOT_POINTER \ diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c index 2fcbefe11..1ced15137 100644 --- a/shared-bindings/digitalio/DigitalInOut.c +++ b/shared-bindings/digitalio/DigitalInOut.c @@ -374,3 +374,14 @@ const mp_obj_type_t digitalio_digitalinout_type = { .make_new = digitalio_digitalinout_make_new, .locals_dict = (mp_obj_t)&digitalio_digitalinout_locals_dict, }; + +// Helper for validating digitalio.DigitalInOut arguments +digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj) { + if (!MP_OBJ_IS_TYPE(obj, &digitalio_digitalinout_type)) { + mp_raise_TypeError(translate("argument num/types mismatch")); + } + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(obj); + raise_error_if_deinited( + common_hal_digitalio_digitalinout_deinited(pin)); + return pin; +} diff --git a/shared-bindings/digitalio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h index 037979098..eee0d5801 100644 --- a/shared-bindings/digitalio/DigitalInOut.h +++ b/shared-bindings/digitalio/DigitalInOut.h @@ -53,5 +53,6 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitali void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, digitalio_pull_t pull); digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self); void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self); +digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c index e61f36cc2..cea0b4ee9 100644 --- a/shared-bindings/gamepad/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -29,19 +29,6 @@ #include "shared-bindings/gamepad/GamePad.h" #include "shared-bindings/util.h" - -// Helper for validating digitalio.DigitalInOut arguments -digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj) { - if (!MP_OBJ_IS_TYPE(obj, &digitalio_digitalinout_type)) { - mp_raise_TypeError(translate("argument num/types mismatch")); - } - digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(obj); - raise_error_if_deinited( - common_hal_digitalio_digitalinout_deinited(pin)); - return pin; -} - - //| :mod:`gamepad` --- Button handling //| ================================== //| diff --git a/shared-bindings/gamepad/__init__.h b/shared-bindings/gamepad/__init__.h index 40cf4e6de..2ae5efb3a 100644 --- a/shared-bindings/gamepad/__init__.h +++ b/shared-bindings/gamepad/__init__.h @@ -28,8 +28,4 @@ #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H #define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H -#include "shared-bindings/digitalio/DigitalInOut.h" - -digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj); - #endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H diff --git a/shared-bindings/gamepadshift/GamePadShift.c b/shared-bindings/gamepadshift/GamePadShift.c index 94b71036f..de3813b08 100644 --- a/shared-bindings/gamepadshift/GamePadShift.c +++ b/shared-bindings/gamepadshift/GamePadShift.c @@ -39,11 +39,11 @@ //| :class:`GamePadShift` -- Scan buttons for presses through a shift register //| =========================================================================== //| -//| .. class:: GamePadShift(data, clock, latch) +//| .. class:: GamePadShift(clock, data, latch) //| //| Initializes button scanning routines. //| -//| The ``data``, ``clock`` and ``latch`` parameters are ``DigitalInOut`` +//| The ``clock``, ``data`` and ``latch`` parameters are ``DigitalInOut`` //| objects connected to the shift register controlling the buttons. //| //| They button presses are accumulated, until the ``get_pressed`` method @@ -56,18 +56,18 @@ STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_data, ARG_clock, ARG_latch }; + enum { ARG_clock, ARG_data, ARG_latch }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ}, + { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_latch, MP_ARG_REQUIRED | MP_ARG_OBJ}, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - digitalio_digitalinout_obj_t *data_pin = assert_digitalinout(args[ARG_data].u_obj); digitalio_digitalinout_obj_t *clock_pin = assert_digitalinout(args[ARG_clock].u_obj); + digitalio_digitalinout_obj_t *data_pin = assert_digitalinout(args[ARG_data].u_obj); digitalio_digitalinout_obj_t *latch_pin = assert_digitalinout(args[ARG_latch].u_obj); gamepadshift_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); @@ -79,7 +79,7 @@ STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_args, gamepad_singleton = gc_make_long_lived(gamepad_singleton); MP_STATE_VM(gamepad_singleton) = gamepad_singleton; } - common_hal_gamepadshift_gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin); + common_hal_gamepadshift_gamepadshift_init(gamepad_singleton, clock_pin, data_pin, latch_pin); return MP_OBJ_FROM_PTR(gamepad_singleton); } diff --git a/shared-bindings/gamepadshift/GamePadShift.h b/shared-bindings/gamepadshift/GamePadShift.h index 2668ec83f..3e8ea9693 100644 --- a/shared-bindings/gamepadshift/GamePadShift.h +++ b/shared-bindings/gamepadshift/GamePadShift.h @@ -33,8 +33,8 @@ extern const mp_obj_type_t gamepadshift_type; void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, - digitalio_digitalinout_obj_t *data_pin, digitalio_digitalinout_obj_t *clock_pin, + digitalio_digitalinout_obj_t *data_pin, digitalio_digitalinout_obj_t *latch_pin); void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift); diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 8a4f4eae9..46630fd32 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -30,10 +30,6 @@ #include "shared-bindings/gamepad/__init__.h" #include "shared-bindings/gamepad/GamePad.h" -#if CIRCUITPY_GAMEPADSHIFT -#include "shared-bindings/gamepadshift/GamePadShift.h" -#endif - #include "shared-bindings/digitalio/DigitalInOut.h" @@ -62,12 +58,6 @@ void gamepad_tick(void) { self->pressed |= self->last & current; self->last = current; } - #if CIRCUITPY_GAMEPADSHIFT - else if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { - // buttons connected to a shift register - gamepadshift_tick(singleton); - } - #endif } void gamepad_reset(void) { diff --git a/shared-module/gamepadshift/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c index 1dadfb9b2..f9303938f 100644 --- a/shared-module/gamepadshift/GamePadShift.c +++ b/shared-module/gamepadshift/GamePadShift.c @@ -29,8 +29,8 @@ #include "shared-module/gamepadshift/GamePadShift.h" void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, - digitalio_digitalinout_obj_t *data_pin, digitalio_digitalinout_obj_t *clock_pin, + digitalio_digitalinout_obj_t *data_pin, digitalio_digitalinout_obj_t *latch_pin) { common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE); gamepadshift->data_pin = data_pin; @@ -45,20 +45,3 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift, void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) { MP_STATE_VM(gamepad_singleton) = NULL; } - -void gamepadshift_tick(gamepadshift_obj_t *self) { - uint8_t current = 0; - uint8_t bit = 1; - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); - for (int i = 0; i < 8; ++i) { - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); - if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { - current |= bit; - } - common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); - bit <<= 1; - } - common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); - self->pressed |= self->last & current; - self->last = current; -} diff --git a/shared-module/gamepadshift/GamePadShift.h b/shared-module/gamepadshift/GamePadShift.h index ccbf5ca06..b4b26b7a9 100644 --- a/shared-module/gamepadshift/GamePadShift.h +++ b/shared-module/gamepadshift/GamePadShift.h @@ -40,6 +40,4 @@ typedef struct { volatile uint8_t last; } gamepadshift_obj_t; -void gamepadshift_tick(gamepadshift_obj_t *self); - #endif // MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H diff --git a/shared-module/gamepadshift/__init__.c b/shared-module/gamepadshift/__init__.c index c3bb83e34..728c2187e 100644 --- a/shared-module/gamepadshift/__init__.c +++ b/shared-module/gamepadshift/__init__.c @@ -24,4 +24,37 @@ * THE SOFTWARE. */ -// Nothing now. +#include "shared-module/gamepadshift/__init__.h" + +#include "py/mpstate.h" +#include "shared-bindings/gamepadshift/GamePadShift.h" + +void gamepadshift_tick(void) { + void* singleton = MP_STATE_VM(gamepad_singleton); + if (!singleton) { + return; + } + + if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { + // buttons connected to a shift register + gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton); + uint8_t current = 0; + uint8_t bit = 1; + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); + for (int i = 0; i < 8; ++i) { + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 0); + if (common_hal_digitalio_digitalinout_get_value(self->data_pin)) { + current |= bit; + } + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); + bit <<= 1; + } + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); + self->pressed |= self->last & current; + self->last = current; + } +} + +void gamepadshift_reset(void) { + MP_STATE_VM(gamepad_singleton) = NULL; +} diff --git a/shared-module/gamepadshift/__init__.h b/shared-module/gamepadshift/__init__.h new file mode 100644 index 000000000..225db7336 --- /dev/null +++ b/shared-module/gamepadshift/__init__.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_GAMEPADSHIFT___INIT___H +#define MICROPY_INCLUDED_GAMEPADSHIFT___INIT___H + +void gamepadshift_tick(void); +void gamepadshift_reset(void); + +#endif // MICROPY_INCLUDED_GAMEPADSHIFT___INIT___H -- cgit v1.2.3 From 55782901d09167e591d45699cd5db48c5619e1e9 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 16 Apr 2019 11:15:42 -0700 Subject: Actually call gamepadshift_tick --- ports/atmel-samd/tick.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'ports') diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index 149347793..dde473375 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -29,10 +29,16 @@ #include "peripheral_clk_config.h" #include "supervisor/shared/autoreload.h" -#include "shared-module/gamepad/__init__.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Processor.h" +#if CIRCUITPY_GAMEPAD +#include "shared-module/gamepad/__init__.h" +#endif + +#if CIRCUITPY_GAMEPADSHIFT +#include "shared-module/gamepadshift/__init__.h" +#endif // Global millisecond tick count volatile uint64_t ticks_ms = 0; @@ -51,7 +57,12 @@ void SysTick_Handler(void) { #endif #ifdef CIRCUITPY_GAMEPAD_TICKS if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) { + #if CIRCUITPY_GAMEPAD gamepad_tick(); + #endif + #if CIRCUITPY_GAMEPADSHIFT + gamepadshift_tick(); + #endif } #endif } -- cgit v1.2.3