From 9bcc1057ca3796e8ba8aa9805cf9d140ee7f51d3 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Sun, 22 Oct 2017 04:39:20 +0200 Subject: [PATCH] Gracefully reset the gamepad module (#356) If a soft reset happens while the gamepad module is scanning for button presses, there is a moment when the pins get de-initialized, but the gamepad module is still trying to read them, which ends in a crash. We can avoid it by disabling scanning on reset. --- atmel-samd/main.c | 4 ++++ shared-module/gamepad/__init__.c | 4 ++++ shared-module/gamepad/__init__.h | 1 + 3 files changed, 9 insertions(+) diff --git a/atmel-samd/main.c b/atmel-samd/main.c index 79b5604e83..7bc4a87ed6 100644 --- a/atmel-samd/main.c +++ b/atmel-samd/main.c @@ -61,6 +61,7 @@ #include "common-hal/pulseio/PWMOut.h" #include "common-hal/touchio/TouchIn.h" #include "common-hal/usb_hid/__init__.h" +#include "shared-module/gamepad/__init__.h" #include "autoreload.h" #include "flash_api.h" @@ -202,6 +203,9 @@ void reset_samd21(void) { pwmout_reset(); analogin_reset(); +#ifdef CIRCUITPY_GAMEPAD_TICKS + gamepad_reset(); +#endif // TODO: move this to analogout_reset() // Wait for the DAC to sync then reset. diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c index 1aebf611d3..b6c8be2157 100644 --- a/shared-module/gamepad/__init__.c +++ b/shared-module/gamepad/__init__.c @@ -49,3 +49,7 @@ void gamepad_tick(void) { gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current; gamepad_singleton->last = gamepad_current; } + +void gamepad_reset(void) { + gamepad_singleton = NULL; +} diff --git a/shared-module/gamepad/__init__.h b/shared-module/gamepad/__init__.h index eacd723669..1fae570f98 100644 --- a/shared-module/gamepad/__init__.h +++ b/shared-module/gamepad/__init__.h @@ -28,5 +28,6 @@ #define MICROPY_INCLUDED_GAMEPAD_H void gamepad_tick(void); +void gamepad_reset(void); #endif // MICROPY_INCLUDED_GAMEPAD_H