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

View File

@ -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)},

View File

@ -26,6 +26,7 @@
#include <stdbool.h>
#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;
}

View File

@ -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

View File

@ -26,6 +26,7 @@
#include <stdbool.h>
#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;
}