Fix swipe direction, menu select timeout, and button mapping
This commit is contained in:
68
matrix.py
68
matrix.py
@@ -169,46 +169,66 @@ class Matrix64Display(SampleBase):
|
|||||||
self.feedback_until = time.time() + duration
|
self.feedback_until = time.time() + duration
|
||||||
|
|
||||||
def on_button_press(self, button_id):
|
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()
|
now = time.time()
|
||||||
|
|
||||||
# Check for swipe gesture
|
# Check for swipe gesture
|
||||||
if self.last_button is not None and (now - self.last_button_time) < self.swipe_timeout:
|
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:
|
if self.last_button == 1 and button_id == 0:
|
||||||
# Swipe 0→1
|
# Swipe left→right (1→0): Next view / Menu Down
|
||||||
if self.menu_mode:
|
if self.menu_mode:
|
||||||
self.menu_selection = (self.menu_selection - 1) % NUM_VIEWS # Up
|
self.menu_selection = (self.menu_selection + 1) % NUM_VIEWS
|
||||||
else:
|
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.last_view_change = now
|
||||||
self.show_feedback(VIEW_NAMES[self.current_view], 1.5)
|
self.show_feedback(VIEW_NAMES[self.current_view], 1.5)
|
||||||
self.last_button = None
|
self.last_button = None
|
||||||
return
|
return
|
||||||
elif self.last_button == 1 and button_id == 0:
|
elif self.last_button == 0 and button_id == 1:
|
||||||
# Swipe 1→0
|
# Swipe right→left (0→1): Prev view / Menu Up
|
||||||
if self.menu_mode:
|
if self.menu_mode:
|
||||||
self.menu_selection = (self.menu_selection + 1) % NUM_VIEWS # Down
|
self.menu_selection = (self.menu_selection - 1) % NUM_VIEWS
|
||||||
else:
|
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.last_view_change = now
|
||||||
self.show_feedback(VIEW_NAMES[self.current_view], 1.5)
|
self.show_feedback(VIEW_NAMES[self.current_view], 1.5)
|
||||||
self.last_button = None
|
self.last_button = None
|
||||||
return
|
return
|
||||||
|
|
||||||
# Single button press (for menu select)
|
# Record button press for potential swipe or single press
|
||||||
if self.menu_mode and button_id == 1:
|
|
||||||
# Select highlighted view
|
|
||||||
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 = button_id
|
||||||
self.last_button_time = now
|
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)
|
||||||
|
|
||||||
def on_button0_held(self):
|
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
|
self.menu_mode = not self.menu_mode
|
||||||
if self.menu_mode:
|
if self.menu_mode:
|
||||||
self.menu_selection = self.current_view
|
self.menu_selection = self.current_view
|
||||||
@@ -217,13 +237,6 @@ class Matrix64Display(SampleBase):
|
|||||||
self.show_feedback("Cerrado", 1)
|
self.show_feedback("Cerrado", 1)
|
||||||
self.last_button = None
|
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):
|
def update_data(self):
|
||||||
try:
|
try:
|
||||||
weather = get_weather()
|
weather = get_weather()
|
||||||
@@ -526,6 +539,9 @@ class Matrix64Display(SampleBase):
|
|||||||
while True:
|
while True:
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
|
|
||||||
|
# Check for button timeout (single press detection)
|
||||||
|
self.check_button_timeout()
|
||||||
|
|
||||||
if current_time - self.last_update >= 60:
|
if current_time - self.last_update >= 60:
|
||||||
self.update_data()
|
self.update_data()
|
||||||
self.update_brightness()
|
self.update_brightness()
|
||||||
|
|||||||
Reference in New Issue
Block a user