From 4dc286fa14e381f7ac2ca4dda1f006fac5bc0356 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Fri, 12 Apr 2019 20:38:51 +0200 Subject: [PATCH] Reorganize the gamepad code --- py/circuitpy_defns.mk | 1 + shared-bindings/gamepad/GamePad.c | 99 ++------------------- shared-bindings/gamepad/GamePad.h | 1 - shared-bindings/gamepad/GamePadShift.c | 118 +++++++++++++++++++++++++ shared-bindings/gamepad/GamePadShift.h | 33 +++++++ shared-bindings/gamepad/__init__.c | 15 ++++ shared-bindings/gamepad/__init__.h | 33 +++++++ shared-module/gamepad/GamePad.c | 51 +++++++++++ shared-module/gamepad/GamePad.h | 4 + shared-module/gamepad/GamePadShift.c | 43 +++++++++ shared-module/gamepad/GamePadShift.h | 48 ++++++++++ shared-module/gamepad/__init__.c | 36 ++++---- 12 files changed, 370 insertions(+), 112 deletions(-) create mode 100644 shared-bindings/gamepad/GamePadShift.c create mode 100644 shared-bindings/gamepad/GamePadShift.h create mode 100644 shared-bindings/gamepad/__init__.h create mode 100644 shared-module/gamepad/GamePadShift.c create mode 100644 shared-module/gamepad/GamePadShift.h diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 342c0ab0c4..41c73430fe 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -317,6 +317,7 @@ $(filter $(SRC_PATTERNS), \ fontio/BuiltinFont.c \ fontio/__init__.c \ gamepad/GamePad.c \ + gamepad/GamePadShift.c \ gamepad/__init__.c \ os/__init__.c \ random/__init__.c \ diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index f189437a1a..8b88471eee 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -31,19 +31,10 @@ #include "shared-module/gamepad/__init__.h" #include "shared-module/gamepad/GamePad.h" #include "shared-bindings/digitalio/DigitalInOut.h" -#include "shared-bindings/util.h" #include "supervisor/shared/translate.h" #include "GamePad.h" +#include "__init__.h" -STATIC digitalio_digitalinout_obj_t *validate_pin(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; -} //| .. currentmodule:: gamepad //| @@ -109,86 +100,20 @@ 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) { - validate_pin(args[i]); + pin_io(args[i]); } gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); - if (!gamepad_singleton) { + if (!gamepad_singleton || + !MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(gamepad_singleton), &gamepad_type)) { gamepad_singleton = m_new_obj(gamepad_obj_t); gamepad_singleton->base.type = &gamepad_type; gamepad_singleton = gc_make_long_lived(gamepad_singleton); MP_STATE_VM(gamepad_singleton) = gamepad_singleton; } - for (size_t i = 0; i < 8; ++i) { - gamepad_singleton->pins[i] = NULL; - } - gamepad_singleton->pulls = 0; - for (size_t i = 0; i < n_args; ++i) { - digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(args[i]); - if (common_hal_digitalio_digitalinout_get_direction(pin) != - DIRECTION_INPUT) { - common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP); - } - digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin); - if (pull == PULL_NONE) { - common_hal_digitalio_digitalinout_set_pull(pin, PULL_UP); - } - if (pull != PULL_DOWN) { - gamepad_singleton->pulls |= 1 << i; - } - gamepad_singleton->pins[i] = pin; - } - return MP_OBJ_FROM_PTR(MP_STATE_VM(gamepad_singleton)); + gamepad_init(gamepad_singleton, args, n_args); + return MP_OBJ_FROM_PTR(gamepad_singleton); } - -//| .. 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 = validate_pin(args[ARG_data].u_obj); - digitalio_digitalinout_obj_t *clock_pin = validate_pin(args[ARG_clock].u_obj); - digitalio_digitalinout_obj_t *latch_pin = validate_pin(args[ARG_latch].u_obj); - - gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); - if (!gamepad_singleton) { - gamepad_singleton = m_new_obj(gamepad_obj_t); - gamepad_singleton->base.type = &gamepadshift_type; - gamepad_singleton = gc_make_long_lived(gamepad_singleton); - MP_STATE_VM(gamepad_singleton) = gamepad_singleton; - } - gamepad_singleton->pins[0] = NULL; - common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE); - gamepad_singleton->pins[1] = data_pin; - common_hal_digitalio_digitalinout_switch_to_output(clock_pin, 0, - DRIVE_MODE_PUSH_PULL); - gamepad_singleton->pins[2] = clock_pin; - common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, - DRIVE_MODE_PUSH_PULL); - gamepad_singleton->pins[3] = latch_pin; - return MP_OBJ_FROM_PTR(MP_STATE_VM(gamepad_singleton)); -} - - //| .. method:: get_pressed() //| //| Get the status of buttons pressed since the last call and clear it. @@ -230,15 +155,3 @@ const mp_obj_type_t gamepad_type = { .make_new = gamepad_make_new, .locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict, }; - -STATIC const mp_rom_map_elem_t gamepadshift_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, - { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_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/GamePad.h b/shared-bindings/gamepad/GamePad.h index 3503311935..172c95ace8 100644 --- a/shared-bindings/gamepad/GamePad.h +++ b/shared-bindings/gamepad/GamePad.h @@ -29,6 +29,5 @@ #define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H extern const mp_obj_type_t gamepad_type; -extern const mp_obj_type_t gamepadshift_type; #endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H diff --git a/shared-bindings/gamepad/GamePadShift.c b/shared-bindings/gamepad/GamePadShift.c new file mode 100644 index 0000000000..07dd1b48e4 --- /dev/null +++ b/shared-bindings/gamepad/GamePadShift.c @@ -0,0 +1,118 @@ +/* + * 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" + + +//| .. 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 new file mode 100644 index 0000000000..68c8de876e --- /dev/null +++ b/shared-bindings/gamepad/GamePadShift.h @@ -0,0 +1,33 @@ +/* + * 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 0775d6abff..ecbcc793ea 100644 --- a/shared-bindings/gamepad/__init__.c +++ b/shared-bindings/gamepad/__init__.c @@ -27,6 +27,21 @@ #include "py/runtime.h" #include "py/mphal.h" #include "GamePad.h" +#include "GamePadShift.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/util.h" + + +// Helper for validating digitalio.DigitalInOut arguments +digitalio_digitalinout_obj_t *pin_io(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 new file mode 100644 index 0000000000..12e38edc4b --- /dev/null +++ b/shared-bindings/gamepad/__init__.h @@ -0,0 +1,33 @@ +/* + * 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___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H + +digitalio_digitalinout_obj_t *pin_io(mp_obj_t obj); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index e69de29bb2..23addb3976 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -0,0 +1,51 @@ +/* + * 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 "GamePad.h" + +void 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; + } + gamepad->pulls = 0; + for (size_t i = 0; i < n_pins; ++i) { + digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pins[i]); + if (common_hal_digitalio_digitalinout_get_direction(pin) != + DIRECTION_INPUT) { + common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP); + } + digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(pin); + if (pull == PULL_NONE) { + common_hal_digitalio_digitalinout_set_pull(pin, PULL_UP); + } + if (pull != PULL_DOWN) { + gamepad->pulls |= 1 << i; + } + gamepad->pins[i] = pin; + } +} diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index 746bba5675..dc8a7e87a9 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -38,4 +38,8 @@ typedef struct { uint8_t pulls; } 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 new file mode 100644 index 0000000000..12083a86ae --- /dev/null +++ b/shared-module/gamepad/GamePadShift.c @@ -0,0 +1,43 @@ +/* + * 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 new file mode 100644 index 0000000000..05235f3189 --- /dev/null +++ b/shared-module/gamepad/GamePadShift.h @@ -0,0 +1,48 @@ +/* + * 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 ccfbfae563..f92042d4bf 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -29,6 +29,9 @@ #include "py/mpstate.h" #include "__init__.h" #include "GamePad.h" +#include "GamePadShift.h" +#include "shared-bindings/gamepad/GamePad.h" +#include "shared-bindings/gamepad/GamePadShift.h" #include "shared-bindings/digitalio/DigitalInOut.h" @@ -37,18 +40,16 @@ void gamepad_tick(void) { static uint8_t last = 0; uint8_t current = 0; uint8_t bit = 1; - digitalio_digitalinout_obj_t* data_pin; - digitalio_digitalinout_obj_t* clock_pin; - digitalio_digitalinout_obj_t* latch_pin; - gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); - if (!gamepad_singleton) { + void* singleton = MP_STATE_VM(gamepad_singleton); + if (!singleton) { return; } - if (gamepad_singleton->pins[0]) { + if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepad_type)) { // buttons connected directly to pins + gamepad_obj_t *self = singleton; for (int i = 0; i < 8; ++i) { - digitalio_digitalinout_obj_t* pin = gamepad_singleton->pins[i]; + digitalio_digitalinout_obj_t* pin = self->pins[i]; if (!pin) { break; } @@ -57,25 +58,24 @@ void gamepad_tick(void) { } bit <<= 1; } - current ^= gamepad_singleton->pulls; - } else { + current ^= self->pulls; + self->pressed |= last & current; + } else if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) { // buttons connected to a shift register - data_pin = gamepad_singleton->pins[1]; - clock_pin = gamepad_singleton->pins[2]; - latch_pin = gamepad_singleton->pins[3]; + gamepadshift_obj_t *self = singleton; - common_hal_digitalio_digitalinout_set_value(latch_pin, 1); + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 1); for (int i = 0; i < 8; ++i) { - common_hal_digitalio_digitalinout_set_value(clock_pin, 0); - if (common_hal_digitalio_digitalinout_get_value(data_pin)) { + 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(clock_pin, 1); + common_hal_digitalio_digitalinout_set_value(self->clock_pin, 1); bit <<= 1; } - common_hal_digitalio_digitalinout_set_value(latch_pin, 0); + common_hal_digitalio_digitalinout_set_value(self->latch_pin, 0); + self->pressed |= last & current; } - gamepad_singleton->pressed |= last & current; last = current; }