From cf7c36ac35c8afdf0f00689d16b19ad2116b8592 Mon Sep 17 00:00:00 2001 From: Joan Date: Wed, 31 Dec 2025 14:00:18 +0100 Subject: [PATCH] Fix swipe direction, menu select timeout, and button mapping --- matrix.py | 66 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/matrix.py b/matrix.py index b65890c..87450f8 100644 --- a/matrix.py +++ b/matrix.py @@ -169,46 +169,66 @@ class Matrix64Display(SampleBase): self.feedback_until = time.time() + duration def on_button_press(self, button_id): - """Handle button press for swipe detection.""" + """Handle button press for swipe detection. + Physical layout: Button 1 = left, Button 0 = right + So swipe left→right = 1→0, swipe right→left = 0→1 + """ now = time.time() # Check for swipe gesture if self.last_button is not None and (now - self.last_button_time) < self.swipe_timeout: - if self.last_button == 0 and button_id == 1: - # Swipe 0→1 + if self.last_button == 1 and button_id == 0: + # Swipe left→right (1→0): Next view / Menu Down if self.menu_mode: - self.menu_selection = (self.menu_selection - 1) % NUM_VIEWS # Up + self.menu_selection = (self.menu_selection + 1) % NUM_VIEWS else: - self.current_view = (self.current_view + 1) % NUM_VIEWS # Next + self.current_view = (self.current_view + 1) % NUM_VIEWS self.last_view_change = now self.show_feedback(VIEW_NAMES[self.current_view], 1.5) self.last_button = None return - elif self.last_button == 1 and button_id == 0: - # Swipe 1→0 + elif self.last_button == 0 and button_id == 1: + # Swipe right→left (0→1): Prev view / Menu Up if self.menu_mode: - self.menu_selection = (self.menu_selection + 1) % NUM_VIEWS # Down + self.menu_selection = (self.menu_selection - 1) % NUM_VIEWS else: - self.current_view = (self.current_view - 1) % NUM_VIEWS # Prev + self.current_view = (self.current_view - 1) % NUM_VIEWS self.last_view_change = now self.show_feedback(VIEW_NAMES[self.current_view], 1.5) self.last_button = None return - # Single button press (for menu select) - if self.menu_mode and button_id == 1: - # Select highlighted view + # Record button press for potential swipe or single press + self.last_button = button_id + self.last_button_time = now + + def check_button_timeout(self): + """Check if pending button press has timed out (single press).""" + if self.last_button is not None: + if (time.time() - self.last_button_time) > self.swipe_timeout: + # Timeout reached, treat as single press + if self.menu_mode and self.last_button == 1: + # Single press on Left button (1) selects in menu + self.on_menu_select() + + self.last_button = None + + def on_menu_select(self): + """Called when user wants to select in menu (right button alone after timeout).""" + if self.menu_mode: self.current_view = self.menu_selection self.menu_mode = False self.show_feedback(VIEW_NAMES[self.current_view], 1.5) - self.last_button = None - return - - self.last_button = button_id - self.last_button_time = now def on_button0_held(self): - """Long press button 0 = open menu.""" + """Long press right button = toggle auto-cycle.""" + if not self.menu_mode: + self.auto_cycle = not self.auto_cycle + self.show_feedback("AUTO ON" if self.auto_cycle else "AUTO OFF", 2) + self.last_button = None + + def on_button1_held(self): + """Long press left button = open menu.""" self.menu_mode = not self.menu_mode if self.menu_mode: self.menu_selection = self.current_view @@ -216,13 +236,6 @@ class Matrix64Display(SampleBase): else: self.show_feedback("Cerrado", 1) self.last_button = None - - def on_button1_held(self): - """Long press button 1 = toggle auto-cycle.""" - if not self.menu_mode: - self.auto_cycle = not self.auto_cycle - self.show_feedback("AUTO ON" if self.auto_cycle else "AUTO OFF", 2) - self.last_button = None def update_data(self): try: @@ -525,6 +538,9 @@ class Matrix64Display(SampleBase): while True: current_time = time.time() + + # Check for button timeout (single press detection) + self.check_button_timeout() if current_time - self.last_update >= 60: self.update_data()