Add gamepad_singleton to root pointers

This commit is contained in:
Radomir Dopieralski 2018-05-24 11:25:21 +02:00
parent c37b69e1a5
commit b219ce6d37
5 changed files with 14 additions and 12 deletions

View File

@ -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 playing_audio[AUDIO_DMA_CHANNEL_COUNT]; \
mp_obj_t rtc_time_source; \ mp_obj_t rtc_time_source; \
FLASH_ROOT_POINTERS \ FLASH_ROOT_POINTERS \
mp_obj_t gamepad_singleton; \
void run_background_tasks(void); void run_background_tasks(void);
#define MICROPY_VM_HOOK_LOOP run_background_tasks(); #define MICROPY_VM_HOOK_LOOP run_background_tasks();

View File

@ -27,14 +27,14 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "py/gc.h" #include "py/gc.h"
#include "py/mpstate.h"
#include "shared-module/gamepad/__init__.h"
#include "shared-module/gamepad/GamePad.h" #include "shared-module/gamepad/GamePad.h"
#include "shared-bindings/digitalio/DigitalInOut.h" #include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/util.h" #include "shared-bindings/util.h"
#include "GamePad.h" #include "GamePad.h"
gamepad_obj_t* gamepad_singleton = NULL;
//| .. currentmodule:: gamepad //| .. currentmodule:: gamepad
//| //|
//| :class:`GamePad` -- Scan buttons for presses //| :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( raise_error_if_deinited(
common_hal_digitalio_digitalinout_deinited(pin)); common_hal_digitalio_digitalinout_deinited(pin));
} }
if (!gamepad_singleton) { if (!MP_STATE_VM(gamepad_singleton)) {
gamepad_singleton = m_new_obj(gamepad_obj_t); gamepad_obj_t* gamepad_singleton = m_new_obj(gamepad_obj_t);
gamepad_singleton->base.type = &gamepad_type; 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); 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. //| held down) can be recorded for the next call.
//| //|
STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) { 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); mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(gamepad_singleton->pressed);
gamepad_singleton->pressed = 0; gamepad_singleton->pressed = 0;
return gamepad; return gamepad;
@ -139,14 +140,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed);
//| Disable button scanning. //| Disable button scanning.
//| //|
STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) { STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) {
gamepad_singleton = NULL; gamepad_reset();
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit); 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[] = { 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_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)},

View File

@ -26,6 +26,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "py/mpstate.h"
#include "__init__.h" #include "__init__.h"
#include "GamePad.h" #include "GamePad.h"
@ -35,6 +36,7 @@
void gamepad_init(size_t n_pins, const mp_obj_t* pins) { 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) { for (size_t i = 0; i < 8; ++i) {
gamepad_singleton->pins[i] = NULL; gamepad_singleton->pins[i] = NULL;
} }

View File

@ -39,8 +39,6 @@ typedef struct {
uint8_t pulls; uint8_t pulls;
} gamepad_obj_t; } gamepad_obj_t;
extern gamepad_obj_t* gamepad_singleton;
void gamepad_init(size_t n_pins, const mp_obj_t* pins); void gamepad_init(size_t n_pins, const mp_obj_t* pins);
#endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H #endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H

View File

@ -26,6 +26,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "py/mpstate.h"
#include "__init__.h" #include "__init__.h"
#include "GamePad.h" #include "GamePad.h"
@ -33,6 +34,7 @@
void gamepad_tick(void) { void gamepad_tick(void) {
gamepad_obj_t* gamepad_singleton = MP_STATE_VM(gamepad_singleton);
if (!gamepad_singleton) { if (!gamepad_singleton) {
return; return;
} }
@ -54,5 +56,5 @@ void gamepad_tick(void) {
} }
void gamepad_reset(void) { void gamepad_reset(void) {
gamepad_singleton = NULL; MP_STATE_VM(gamepad_singleton) = NULL;
} }