Merge pull request #868 from pewpew-game/gamepad-longlive

Make the gamepad singleton long-living and add it to root pointers
crypto-aes
Scott Shawcroft 4 years ago committed by GitHub
commit 6479cb0806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      ports/atmel-samd/mpconfigport.h
  2. 1
      ports/nrf/mpconfigport.h
  3. 17
      shared-bindings/gamepad/GamePad.c
  4. 2
      shared-module/gamepad/GamePad.c
  5. 2
      shared-module/gamepad/GamePad.h
  6. 4
      shared-module/gamepad/__init__.c

@ -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();

@ -323,6 +323,7 @@ extern const struct _mp_obj_module_t ble_module;
struct _music_data_t *music_data; \
const struct _pwm_events *pwm_active_events; \
const struct _pwm_events *pwm_pending_events; \
mp_obj_t gamepad_singleton; \
#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)

@ -26,14 +26,15 @@
#include "py/obj.h"
#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
@ -105,12 +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;
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));
}
@ -125,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;
@ -137,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)},

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

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

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

Loading…
Cancel
Save