@@ -58,30 +58,31 @@ def listen(self):
5858 except FileNotFoundError :
5959 self .log (f"No gamepad found at { self .device } . Waiting for connection..." , level = 'warning' )
6060 time .sleep (1 )
61- except Exception as e :
62- self .log (f"Error: { e } " , level = 'error' )
63- time .sleep (1 )
6461
6562 def _handle_js_event (self , time_ms , value , type_ , number ):
6663 """Route joystick events to appropriate handlers."""
6764 is_init = (type_ & self .JS_EVENT_INIT ) != 0
6865 event_type = type_ & ~ self .JS_EVENT_INIT
6966
70- topic = None
71- args = {}
72-
67+ topics_args = []
7368 if event_type == self .JS_EVENT_BUTTON :
74- topic , args = self ._handle_button_event (value , number )
69+ topics_args = self ._handle_button_event (value , number , collect_topics_args = True )
7570 elif event_type == self .JS_EVENT_AXIS :
76- topic , args = self ._handle_axis_event (value , number )
77-
78- self .publish ('controller/event' , event = (time_ms , value , type_ , number , topic , args ))
79-
80- def _handle_button_event (self , value , number ):
71+ topics_args = self ._handle_axis_event (value , number , collect_topics_args = True )
72+ self .publish ('controller/event' , event = {
73+ 'time_ms' : time_ms ,
74+ 'value' : value ,
75+ 'type' : type_ ,
76+ 'number' : number ,
77+ 'topics_args' : topics_args
78+ })
79+
80+ def _handle_button_event (self , value , number , collect_topics_args = False ):
8181 """Handle button press/release events."""
8282 button = self .button_names .get (number , f'BTN_{ number } ' )
8383 topic = None
8484 args = {}
85+ topics_args = []
8586
8687 if value == 1 :
8788 self .pressed_buttons .add (button )
@@ -98,17 +99,29 @@ def _handle_button_event(self, value, number):
9899 args = mapping .get ('args' , {})
99100 self .publish (topic , ** args )
100101 self .log (f"Published to topic { topic } with args { args } (jsdev)" )
102+ topics_args .append ({'topic' : topic , 'args' : args })
101103
104+ # If collect_topics_args is set, return the list, else preserve old behavior
105+ import inspect
106+ frame = inspect .currentframe ().f_back
107+ if frame and 'collect_topics_args' in frame .f_locals and frame .f_locals ['collect_topics_args' ]:
108+ return topics_args
102109 return topic , args
103110
104- def _handle_axis_event (self , value , number ):
111+ def _handle_axis_event (self , value , number , collect_topics_args = False ):
105112 """Handle axis movement events."""
106113 axis = self .axis_names .get (number , f'AXIS_{ number } ' )
107114 topic = None
108115 args = {}
116+ topics_args = []
109117
110118 button_map = self ._get_active_mapping ()
111119 if axis not in button_map :
120+ # If collecting, return empty list
121+ import inspect
122+ frame = inspect .currentframe ().f_back
123+ if frame and 'collect_topics_args' in frame .f_locals and frame .f_locals ['collect_topics_args' ]:
124+ return topics_args
112125 return topic , args
113126
114127 for mapping in button_map [axis ]:
@@ -141,7 +154,13 @@ def _handle_axis_event(self, value, number):
141154
142155 self .publish (topic , ** args )
143156 self .log (f"Published to topic { topic } with args { args } (jsdev)" )
157+ topics_args .append ({'topic' : topic , 'args' : args })
144158
159+ # If collect_topics_args is set, return the list, else preserve old behavior
160+ import inspect
161+ frame = inspect .currentframe ().f_back
162+ if frame and 'collect_topics_args' in frame .f_locals and frame .f_locals ['collect_topics_args' ]:
163+ return topics_args
145164 return topic , args
146165
147166 # Keep public method for backwards compatibility with tests
0 commit comments