summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/atmel-samd/supervisor/port.c6
-rw-r--r--py/circuitpy_defns.mk7
-rw-r--r--py/circuitpy_mpconfig.h12
-rw-r--r--shared-bindings/digitalio/DigitalInOut.c11
-rw-r--r--shared-bindings/digitalio/DigitalInOut.h1
-rw-r--r--shared-bindings/gamepad/__init__.c13
-rw-r--r--shared-bindings/gamepad/__init__.h4
-rw-r--r--shared-bindings/gamepadshift/GamePadShift.c12
-rw-r--r--shared-bindings/gamepadshift/GamePadShift.h2
-rw-r--r--shared-module/gamepad/__init__.c10
-rw-r--r--shared-module/gamepadshift/GamePadShift.c19
-rw-r--r--shared-module/gamepadshift/GamePadShift.h2
-rw-r--r--shared-module/gamepadshift/__init__.c35
-rw-r--r--shared-module/gamepadshift/__init__.h33
14 files changed, 105 insertions, 62 deletions
diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c
index f8d830d8b..4f53d30f9 100644
--- a/ports/atmel-samd/supervisor/port.c
+++ b/ports/atmel-samd/supervisor/port.c
@@ -71,6 +71,9 @@
#if CIRCUITPY_GAMEPAD
#include "shared-module/gamepad/__init__.h"
#endif
+#if CIRCUITPY_GAMEPADSHIFT
+#include "shared-module/gamepadshift/__init__.h"
+#endif
#include "shared-module/_pew/PewPew.h"
extern volatile bool mp_msc_enabled;
@@ -229,6 +232,9 @@ void reset_port(void) {
#if CIRCUITPY_GAMEPAD
gamepad_reset();
#endif
+#if CIRCUITPY_GAMEPADSHIFT
+ gamepadshift_reset();
+#endif
#if CIRCUITPY_PEW
pew_reset();
#endif
diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk
index be7ff0b11..1aca33901 100644
--- a/py/circuitpy_defns.mk
+++ b/py/circuitpy_defns.mk
@@ -131,10 +131,9 @@ 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_GAMEPADSHIFT),1)
+SRC_PATTERNS += gamepadshift/%
endif
ifeq ($(CIRCUITPY_I2CSLAVE),1)
SRC_PATTERNS += i2cslave/%
diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h
index 3952b5e06..a101aa527 100644
--- a/py/circuitpy_mpconfig.h
+++ b/py/circuitpy_mpconfig.h
@@ -315,8 +315,6 @@ extern const struct _mp_obj_module_t frequencyio_module;
#if CIRCUITPY_GAMEPAD
extern const struct _mp_obj_module_t gamepad_module;
-// Scan gamepad every 32ms
-#define CIRCUITPY_GAMEPAD_TICKS 0x1f
#define GAMEPAD_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module },
#else
#define GAMEPAD_MODULE
@@ -329,6 +327,14 @@ extern const struct _mp_obj_module_t gamepadshift_module;
#define GAMEPADSHIFT_MODULE
#endif
+#if CIRCUITPY_GAMEPAD || CIRCUITPY_GAMEPADSHIFT
+// Scan gamepad every 32ms
+#define CIRCUITPY_GAMEPAD_TICKS 0x1f
+#define GAMEPAD_ROOT_POINTERS mp_obj_t gamepad_singleton;
+#else
+#define GAMEPAD_ROOT_POINTERS
+#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 },
@@ -612,7 +618,7 @@ extern const struct _mp_obj_module_t ustack_module;
const char *readline_hist[8]; \
vstr_t *repl_line; \
mp_obj_t rtc_time_source; \
- mp_obj_t gamepad_singleton; \
+ GAMEPAD_ROOT_POINTERS \
mp_obj_t pew_singleton; \
mp_obj_t terminal_tilegrid_tiles; \
BOARD_I2C_ROOT_POINTER \
diff --git a/shared-bindings/digitalio/DigitalInOut.c b/shared-bindings/digitalio/DigitalInOut.c
index 2fcbefe11..1ced15137 100644
--- a/shared-bindings/digitalio/DigitalInOut.c
+++ b/shared-bindings/digitalio/DigitalInOut.c
@@ -374,3 +374,14 @@ const mp_obj_type_t digitalio_digitalinout_type = {
.make_new = digitalio_digitalinout_make_new,
.locals_dict = (mp_obj_t)&digitalio_digitalinout_locals_dict,
};
+
+// Helper for validating digitalio.DigitalInOut arguments
+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"));
+ }
+ digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(obj);
+ raise_error_if_deinited(
+ common_hal_digitalio_digitalinout_deinited(pin));
+ return pin;
+}
diff --git a/shared-bindings/digitalio/DigitalInOut.h b/shared-bindings/digitalio/DigitalInOut.h
index 037979098..eee0d5801 100644
--- a/shared-bindings/digitalio/DigitalInOut.h
+++ b/shared-bindings/digitalio/DigitalInOut.h
@@ -53,5 +53,6 @@ digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitali
void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, digitalio_pull_t pull);
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self);
+digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H
diff --git a/shared-bindings/gamepad/__init__.c b/shared-bindings/gamepad/__init__.c
index e61f36cc2..cea0b4ee9 100644
--- a/shared-bindings/gamepad/__init__.c
+++ b/shared-bindings/gamepad/__init__.c
@@ -29,19 +29,6 @@
#include "shared-bindings/gamepad/GamePad.h"
#include "shared-bindings/util.h"
-
-// Helper for validating digitalio.DigitalInOut arguments
-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"));
- }
- 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
index 40cf4e6de..2ae5efb3a 100644
--- a/shared-bindings/gamepad/__init__.h
+++ b/shared-bindings/gamepad/__init__.h
@@ -28,8 +28,4 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H
-#include "shared-bindings/digitalio/DigitalInOut.h"
-
-digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj);
-
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD___INIT___H
diff --git a/shared-bindings/gamepadshift/GamePadShift.c b/shared-bindings/gamepadshift/GamePadShift.c
index 94b71036f..de3813b08 100644
--- a/shared-bindings/gamepadshift/GamePadShift.c
+++ b/shared-bindings/gamepadshift/GamePadShift.c
@@ -39,11 +39,11 @@
//| :class:`GamePadShift` -- Scan buttons for presses through a shift register
//| ===========================================================================
//|
-//| .. class:: GamePadShift(data, clock, latch)
+//| .. class:: GamePadShift(clock, data, latch)
//|
//| Initializes button scanning routines.
//|
-//| The ``data``, ``clock`` and ``latch`` parameters are ``DigitalInOut``
+//| The ``clock``, ``data`` and ``latch`` parameters are ``DigitalInOut``
//| objects connected to the shift register controlling the buttons.
//|
//| They button presses are accumulated, until the ``get_pressed`` method
@@ -56,18 +56,18 @@
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 };
+ enum { ARG_clock, ARG_data, 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_data, 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 = assert_digitalinout(args[ARG_data].u_obj);
digitalio_digitalinout_obj_t *clock_pin = assert_digitalinout(args[ARG_clock].u_obj);
+ digitalio_digitalinout_obj_t *data_pin = assert_digitalinout(args[ARG_data].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);
@@ -79,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;
}
- common_hal_gamepadshift_gamepadshift_init(gamepad_singleton, data_pin, clock_pin, latch_pin);
+ common_hal_gamepadshift_gamepadshift_init(gamepad_singleton, clock_pin, data_pin, latch_pin);
return MP_OBJ_FROM_PTR(gamepad_singleton);
}
diff --git a/shared-bindings/gamepadshift/GamePadShift.h b/shared-bindings/gamepadshift/GamePadShift.h
index 2668ec83f..3e8ea9693 100644
--- a/shared-bindings/gamepadshift/GamePadShift.h
+++ b/shared-bindings/gamepadshift/GamePadShift.h
@@ -33,8 +33,8 @@
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 *data_pin,
digitalio_digitalinout_obj_t *latch_pin);
void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift);
diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c
index 8a4f4eae9..46630fd32 100644
--- a/shared-module/gamepad/__init__.c
+++ b/shared-module/gamepad/__init__.c
@@ -30,10 +30,6 @@
#include "shared-bindings/gamepad/__init__.h"
#include "shared-bindings/gamepad/GamePad.h"
-#if CIRCUITPY_GAMEPADSHIFT
-#include "shared-bindings/gamepadshift/GamePadShift.h"
-#endif
-
#include "shared-bindings/digitalio/DigitalInOut.h"
@@ -62,12 +58,6 @@ void gamepad_tick(void) {
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_tick(singleton);
- }
- #endif
}
void gamepad_reset(void) {
diff --git a/shared-module/gamepadshift/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c
index 1dadfb9b2..f9303938f 100644
--- a/shared-module/gamepadshift/GamePadShift.c
+++ b/shared-module/gamepadshift/GamePadShift.c
@@ -29,8 +29,8 @@
#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 *data_pin,
digitalio_digitalinout_obj_t *latch_pin) {
common_hal_digitalio_digitalinout_switch_to_input(data_pin, PULL_NONE);
gamepadshift->data_pin = data_pin;
@@ -45,20 +45,3 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift,
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/gamepadshift/GamePadShift.h b/shared-module/gamepadshift/GamePadShift.h
index ccbf5ca06..b4b26b7a9 100644
--- a/shared-module/gamepadshift/GamePadShift.h
+++ b/shared-module/gamepadshift/GamePadShift.h
@@ -40,6 +40,4 @@ typedef struct {
volatile uint8_t last;
} gamepadshift_obj_t;
-void gamepadshift_tick(gamepadshift_obj_t *self);
-
#endif // MICROPY_INCLUDED_GAMEPADSHIFT_GAMEPADSHIFT_H
diff --git a/shared-module/gamepadshift/__init__.c b/shared-module/gamepadshift/__init__.c
index c3bb83e34..728c2187e 100644
--- a/shared-module/gamepadshift/__init__.c
+++ b/shared-module/gamepadshift/__init__.c
@@ -24,4 +24,37 @@
* THE SOFTWARE.
*/
-// Nothing now.
+#include "shared-module/gamepadshift/__init__.h"
+
+#include "py/mpstate.h"
+#include "shared-bindings/gamepadshift/GamePadShift.h"
+
+void gamepadshift_tick(void) {
+ void* singleton = MP_STATE_VM(gamepad_singleton);
+ if (!singleton) {
+ return;
+ }
+
+ if (MP_OBJ_IS_TYPE(MP_OBJ_FROM_PTR(singleton), &gamepadshift_type)) {
+ // buttons connected to a shift register
+ gamepadshift_obj_t *self = MP_OBJ_TO_PTR(singleton);
+ 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;
+ }
+}
+
+void gamepadshift_reset(void) {
+ MP_STATE_VM(gamepad_singleton) = NULL;
+}
diff --git a/shared-module/gamepadshift/__init__.h b/shared-module/gamepadshift/__init__.h
new file mode 100644
index 000000000..225db7336
--- /dev/null
+++ b/shared-module/gamepadshift/__init__.h
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft 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_GAMEPADSHIFT___INIT___H
+#define MICROPY_INCLUDED_GAMEPADSHIFT___INIT___H
+
+void gamepadshift_tick(void);
+void gamepadshift_reset(void);
+
+#endif // MICROPY_INCLUDED_GAMEPADSHIFT___INIT___H