diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2019-04-15 15:40:06 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2019-04-15 15:40:06 -0700 |
| commit | c927e6b938ff35fe0f42180d1dc0f9d58f8e571c (patch) | |
| tree | 7be059364206e22823c099bd6c5870dc1fd78392 /shared-module | |
| parent | eeef5359f90fc3fa8b4b736d8f751b034a448167 (diff) | |
Split GamePadShift from GamePad to save space on most boards.
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/gamepad/GamePad.c | 9 | ||||
| -rw-r--r-- | shared-module/gamepad/GamePad.h | 5 | ||||
| -rw-r--r-- | shared-module/gamepad/__init__.c | 33 | ||||
| -rw-r--r-- | shared-module/gamepadshift/GamePadShift.c (renamed from shared-module/gamepad/GamePadShift.c) | 33 | ||||
| -rw-r--r-- | shared-module/gamepadshift/GamePadShift.h (renamed from shared-module/gamepad/GamePadShift.h) | 13 | ||||
| -rw-r--r-- | shared-module/gamepadshift/__init__.c | 27 |
6 files changed, 79 insertions, 41 deletions
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/__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 <stdbool.h> #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/gamepad/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c index 12083a86a..1dadfb9b2 100644 --- a/shared-module/gamepad/GamePadShift.c +++ b/shared-module/gamepadshift/GamePadShift.c @@ -24,14 +24,14 @@ * THE SOFTWARE. */ +#include "py/mpstate.h" #include "shared-bindings/digitalio/DigitalInOut.h" -#include "GamePadShift.h" +#include "shared-module/gamepadshift/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) { +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, @@ -41,3 +41,24 @@ void gamepadshift_init(gamepadshift_obj_t *gamepadshift, 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/gamepad/GamePadShift.h b/shared-module/gamepadshift/GamePadShift.h index 05235f318..ccbf5ca06 100644 --- a/shared-module/gamepad/GamePadShift.h +++ b/shared-module/gamepadshift/GamePadShift.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_GAMEPAD_GAMEPADSHIFT_H -#define MICROPY_INCLUDED_GAMEPAD_GAMEPADSHIFT_H +#ifndef MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H +#define MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H #include <stdint.h> @@ -37,12 +37,9 @@ typedef struct { 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); -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 +#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. |
