diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 15cb514049..cd05927b08 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -312,6 +312,7 @@ extern const struct _mp_obj_module_t usb_hid_module; mp_obj_t playing_audio[AUDIO_DMA_CHANNEL_COUNT]; \ mp_obj_t rtc_time_source; \ FLASH_ROOT_POINTERS \ + mp_obj_t gamepad_singleton; \ void run_background_tasks(void); #define MICROPY_VM_HOOK_LOOP run_background_tasks(); diff --git a/shared-bindings/gamepad/GamePad.c b/shared-bindings/gamepad/GamePad.c index b103c59b4e..1e95776d70 100644 --- a/shared-bindings/gamepad/GamePad.c +++ b/shared-bindings/gamepad/GamePad.c @@ -27,14 +27,14 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/gc.h" +#include "py/mpstate.h" +#include "shared-module/gamepad/__init__.h" #include "shared-module/gamepad/GamePad.h" #include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/util.h" #include "GamePad.h" -gamepad_obj_t* gamepad_singleton = NULL; - //| .. currentmodule:: gamepad //| //| :class:`GamePad` -- Scan buttons for presses @@ -106,13 +106,13 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, raise_error_if_deinited( common_hal_digitalio_digitalinout_deinited(pin)); } - if (!gamepad_singleton) { - gamepad_singleton = m_new_obj(gamepad_obj_t); + if (!MP_STATE_VM(gamepad_singleton)) { + gamepad_obj_t* gamepad_singleton = m_new_obj(gamepad_obj_t); gamepad_singleton->base.type = &gamepad_type; - gamepad_singleton = gc_make_long_lived(gamepad_singleton); + MP_STATE_VM(gamepad_singleton) = gc_make_long_lived(gamepad_singleton); } gamepad_init(n_args, args); - return MP_OBJ_FROM_PTR(gamepad_singleton); + return MP_OBJ_FROM_PTR(MP_STATE_VM(gamepad_singleton)); } @@ -127,6 +127,7 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, //| held down) can be recorded for the next call. //| STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { + gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed); gamepad_singleton->pressed = 0; return gamepad; @@ -139,14 +140,12 @@ 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_singleton = NULL; + gamepad_reset(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); -STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *args); STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c index 88ab23fc1e..2c14fa02a4 100644 --- a/shared-module/gamepad/GamePad.c +++ b/shared-module/gamepad/GamePad.c @@ -26,6 +26,7 @@ #include +#include "py/mpstate.h" #include "__init__.h" #include "GamePad.h" @@ -35,6 +36,7 @@ void gamepad_init(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; } diff --git a/shared-module/gamepad/GamePad.h b/shared-module/gamepad/GamePad.h index 7ce55cf984..9fd5c9626e 100644 --- a/shared-module/gamepad/GamePad.h +++ b/shared-module/gamepad/GamePad.h @@ -39,8 +39,6 @@ typedef struct { uint8_t pulls; } gamepad_obj_t; -extern gamepad_obj_t* gamepad_singleton; - void gamepad_init(size_t n_pins, const mp_obj_t* pins); #endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index b1cdac2a70..8414ddfbea 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -26,6 +26,7 @@ #include +#include "py/mpstate.h" #include "__init__.h" #include "GamePad.h" @@ -33,6 +34,7 @@ void gamepad_tick(void) { + gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton); if (!gamepad_singleton) { return; } @@ -54,5 +56,5 @@ void gamepad_tick(void) { } void gamepad_reset(void) { - gamepad_singleton = NULL; + MP_STATE_VM(gamepad_singleton) = NULL; }