diff options
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/gamepad/GamePad.c | 51 | ||||
| -rw-r--r-- | shared-module/gamepad/GamePad.h | 4 | ||||
| -rw-r--r-- | shared-module/gamepad/GamePadShift.c | 43 | ||||
| -rw-r--r-- | shared-module/gamepad/GamePadShift.h | 48 | ||||
| -rw-r--r-- | shared-module/gamepad/__init__.c | 36 |
5 files changed, 164 insertions, 18 deletions
diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index e69de29bb..23addb397 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 746bba567..dc8a7e87a 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 000000000..12083a86a --- /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 000000000..05235f318 --- /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 <stdint.h> + +#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 ccfbfae56..f92042d4b 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; } |
