diff options
| author | Radomir Dopieralski <openstack@sheep.art.pl> | 2019-04-11 12:05:49 +0200 |
|---|---|---|
| committer | Radomir Dopieralski <openstack@sheep.art.pl> | 2019-04-12 20:43:29 +0200 |
| commit | ae60968563ff844189ef17fcb8ba3ec9bb7b54f9 (patch) | |
| tree | 499a4d93cbba5705ec2a248e48198d9b593fa0c6 /shared-module | |
| parent | 7e89beeb3101ddb7fc1d3f35d492943ffb287ec2 (diff) | |
More refactoring
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/gamepad/GamePad.c | 77 | ||||
| -rw-r--r-- | shared-module/gamepad/GamePad.h | 9 | ||||
| -rw-r--r-- | shared-module/gamepad/__init__.c | 22 |
3 files changed, 12 insertions, 96 deletions
diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index 18a7c4d7b..e69de29bb 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -1,77 +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 <stdbool.h> - -#include "py/mpstate.h" -#include "__init__.h" -#include "GamePad.h" - -#include "shared-bindings/digitalio/Pull.h" -#include "shared-bindings/digitalio/DigitalInOut.h" -#include "shared-bindings/util.h" - - -void gamepad_init_pins(size_t n_pins, const mp_obj_t* pins) { - gamepad_obj_t* gamepad_singleton = MP_STATE_VM(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_pins; ++i) { - digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pins[i]); - digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(pin); - if (direction != 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; - } - gamepad_singleton->kind = GAMEPAD_KIND_PINS; -} - -void gamepad_init_shift(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); - - common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE); - gamepad_singleton->pins[0] = data_pin; - - common_hal_digitalio_digitalinout_switch_to_output(clock_pin, 0, DRIVE_MODE_PUSH_PULL); - gamepad_singleton->pins[1] = clock_pin; - - common_hal_digitalio_digitalinout_switch_to_output(latch_pin, 1, DRIVE_MODE_PUSH_PULL); - gamepad_singleton->pins[2] = latch_pin; - - gamepad_singleton->kind = GAMEPAD_KIND_SHIFT; -} diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index 8ba597a37..746bba567 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -36,15 +36,6 @@ typedef struct { digitalio_digitalinout_obj_t* pins[8]; volatile uint8_t pressed; uint8_t pulls; - uint8_t kind; } gamepad_obj_t; -#define GAMEPAD_KIND_PINS 0 -#define GAMEPAD_KIND_SHIFT 1 - -void gamepad_init_pins(size_t n_pins, const mp_obj_t* pins); -void gamepad_init_shift(digitalio_digitalinout_obj_t *data_pin, - digitalio_digitalinout_obj_t *clock_pin, - digitalio_digitalinout_obj_t *latch_pin); - #endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 24c299f86..ccfbfae56 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -37,12 +37,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) { return; } - switch (gamepad_singleton->kind) { - case GAMEPAD_KIND_PINS: + if (gamepad_singleton->pins[0]) { + // buttons connected directly to pins for (int i = 0; i < 8; ++i) { digitalio_digitalinout_obj_t* pin = gamepad_singleton->pins[i]; if (!pin) { @@ -54,12 +58,11 @@ void gamepad_tick(void) { bit <<= 1; } current ^= gamepad_singleton->pulls; - break; - case GAMEPAD_KIND_SHIFT: - bit = 1; // we need a statement after a label - digitalio_digitalinout_obj_t* data_pin = gamepad_singleton->pins[0]; - digitalio_digitalinout_obj_t* clock_pin = gamepad_singleton->pins[1]; - digitalio_digitalinout_obj_t* latch_pin = gamepad_singleton->pins[2]; + } else { + // buttons connected to a shift register + data_pin = gamepad_singleton->pins[1]; + clock_pin = gamepad_singleton->pins[2]; + latch_pin = gamepad_singleton->pins[3]; common_hal_digitalio_digitalinout_set_value(latch_pin, 1); for (int i = 0; i < 8; ++i) { @@ -67,11 +70,10 @@ void gamepad_tick(void) { if (common_hal_digitalio_digitalinout_get_value(data_pin)) { current |= bit; } - bit <<= 1; common_hal_digitalio_digitalinout_set_value(clock_pin, 1); + bit <<= 1; } common_hal_digitalio_digitalinout_set_value(latch_pin, 0); - break; } gamepad_singleton->pressed |= last & current; last = current; |
