-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSteeringWheelButtons.py
54 lines (42 loc) · 1.47 KB
/
SteeringWheelButtons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
class SteeringWheelButtons:
listeners = {}
debouncers = {}
phone_calling = False
menu_opened = False
def debounce(self, button):
if button in self.debouncers:
if time.time() - self.debouncers[button] < 0.3:
return
self.debouncers[button] = time.time()
self.fire_event('button', button)
if button == 'menu':
if self.phone_calling:
print("[buttons] phone")
self.fire_event('phone', button)
else:
print("[buttons] menu")
self.fire_event('menu', button)
if button == 'up' or button == 'down' or button == 'mute':
if self.menu_opened:
print("[buttons] menu")
self.fire_event('menu', button)
else:
print("[buttons] media")
self.fire_event('media', button)
def on_phone(self, number):
if number is None:
self.phone_calling = False
else:
self.phone_calling = True
def on_menu_opened(self, is_open):
self.menu_opened = is_open
def fire_event(self, event, *args):
if event not in self.listeners:
return
for listener in self.listeners[event]:
listener(*args)
def on_event(self, event, callback):
if event not in self.listeners:
self.listeners[event] = []
self.listeners[event].append(callback)