From b3411f75b70f1a3b6f875d452e22c7d7cf5b674a Mon Sep 17 00:00:00 2001 From: Aonghus O Nia Date: Sat, 25 Apr 2026 22:18:42 -0400 Subject: [PATCH 1/2] prevent race condition when using wait_for_event and callbacks at the same time raise error if using wait_for_event when callbacks are assigned. --- sense_hat/stick.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sense_hat/stick.py b/sense_hat/stick.py index 896a9f8..93f5f9d 100644 --- a/sense_hat/stick.py +++ b/sense_hat/stick.py @@ -189,6 +189,9 @@ def wait_for_event(self, emptybuffer=False): events will be thrown away first. This is most useful if you are only interested in "pressed" events. """ + if self._callbacks: + raise RuntimeError('Cannot use wait_for_event when callbacks are assigned') + if emptybuffer: while self._wait(0): self._read() From 12b7d7c4b600b362f6c7807a78ab6cbc6338a025 Mon Sep 17 00:00:00 2001 From: Aonghus O Nia Date: Sat, 25 Apr 2026 22:28:54 -0400 Subject: [PATCH 2/2] Update documentation for joystick Clarify usage restrictions for joystick functions and events. --- docs/api.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index c2d2ea1..4032860 100644 --- a/docs/api.md +++ b/docs/api.md @@ -672,7 +672,10 @@ or the type of a parameter. ### wait_for_event Blocks execution until a joystick event occurs, then returns an `InputEvent` -representing the event that occurred. +representing the event that occurred. + +wait_for_event should not be used at the same time as direction_up, direction_left, +direction_right, direction_down, direction_middle or direction_any functions. ```python from sense_hat import SenseHat @@ -709,6 +712,9 @@ print("The joystick was {} {}".format(event.action, event.direction)) Returns a list of `InputEvent` tuples representing all events that have occurred since the last call to `get_events` or `wait_for_event`. +get_events should not be used at the same time as direction_up, direction_left, +direction_right, direction_down, direction_middle or direction_any functions. + ```python from sense_hat import SenseHat @@ -726,6 +732,8 @@ joystick is pushed in the associated direction (or in any direction in the case of `direction_any`). The function assigned must either take no parameters or must take a single parameter which will be passed the associated `InputEvent`. +These functions should not be used at the same time as wait_for_event or get_events. + ```python from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED from signal import pause