Split GamePadShift from GamePad to save space on most boards.
This commit is contained in:
parent
eeef5359f9
commit
c927e6b938
@ -14,5 +14,7 @@ CIRCUITPY_AUDIOBUSIO = 0
|
||||
# No touch on SAMD51 yet
|
||||
CIRCUITPY_TOUCHIO = 0
|
||||
|
||||
CIRCUITPY_GAMEPADSHIFT = 1
|
||||
|
||||
CHIP_VARIANT = SAMD51J19A
|
||||
CHIP_FAMILY = samd51
|
||||
|
@ -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 \
|
||||
|
@ -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 \
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -28,16 +28,16 @@
|
||||
#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 "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"
|
||||
#include "GamePadShift.h"
|
||||
#include "__init__.h"
|
||||
|
||||
//| .. currentmodule:: gamepad
|
||||
//| .. currentmodule:: gamepadshift
|
||||
//|
|
||||
//| :class:`GamePadShift` -- Scan buttons for presses
|
||||
//| =================================================
|
||||
//| :class:`GamePadShift` -- Scan buttons for presses through a shift register
|
||||
//| ===========================================================================
|
||||
//|
|
||||
//| .. class:: GamePadShift(data, clock, latch)
|
||||
//|
|
||||
@ -50,6 +50,9 @@
|
||||
//| 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) {
|
||||
|
||||
@ -63,9 +66,9 @@ STATIC mp_obj_t gamepadshift_make_new(const mp_obj_type_t *type, size_t n_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);
|
||||
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 ||
|
||||
@ -76,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;
|
||||
}
|
||||
gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin);
|
||||
common_hal_gamepadshift_gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin);
|
||||
return MP_OBJ_FROM_PTR(gamepad_singleton);
|
||||
}
|
||||
|
||||
@ -103,7 +106,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_get_pressed_obj, gamepadshift_get_pressed
|
||||
//| Disable button scanning.
|
||||
//|
|
||||
STATIC mp_obj_t gamepadshift_deinit(mp_obj_t self_in) {
|
||||
gamepad_reset();
|
||||
common_hal_gamepadshift_gamepadshift_deinit(self_in);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(gamepadshift_deinit_obj, gamepadshift_deinit);
|
@ -24,20 +24,19 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "shared-bindings/digitalio/DigitalInOut.h"
|
||||
#include "GamePadShift.h"
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT_GAMEPADSHIFT_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_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) {
|
||||
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;
|
||||
}
|
||||
#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
|
54
shared-bindings/gamepadshift/__init__.c
Normal file
54
shared-bindings/gamepadshift/__init__.c
Normal file
@ -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,
|
||||
};
|
@ -25,9 +25,7 @@
|
||||
*/
|
||||
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT___INIT___H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT___INIT___H
|
||||
|
||||
extern const mp_obj_type_t gamepadshift_type;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPADSHIFT_H
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPADSHIFT___INIT___H
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)) {
|
||||
// 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;
|
||||
self->pressed |= self->last & current;
|
||||
self->last = current;
|
||||
}
|
||||
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) {
|
||||
|
64
shared-module/gamepadshift/GamePadShift.c
Normal file
64
shared-module/gamepadshift/GamePadShift.c
Normal file
@ -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;
|
||||
}
|
@ -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
|
27
shared-module/gamepadshift/__init__.c
Normal file
27
shared-module/gamepadshift/__init__.c
Normal file
@ -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.
|
Loading…
x
Reference in New Issue
Block a user