Skip to content

Commit 250a96f

Browse files
authored
Fixes for DaVinci Resolve work (#36)
* Fixes for auto-launch * Fixes for keys with empty commands * Fixes to rotary encoder events
1 parent 9aa4001 commit 250a96f

File tree

5 files changed

+55
-20
lines changed

5 files changed

+55
-20
lines changed

code.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def set_app(index):
8282
sleep_remaining -= elapsed_seconds()
8383
event = macropad.keys.events.get()
8484

85-
if event or last_position != macropad.encoder or macropad.encoder_switch_debounced.released:
85+
if (event and event.pressed) or last_position != macropad.encoder or macropad.encoder_switch_debounced.released:
8686
keys.release(Keys.KEY_SLEEP) # Don't go to sleep!
8787
sleep_remaining = apps[app_index].timeout
8888
if sleep_remaining <= 0: # Go to sleep and slow down
@@ -100,14 +100,19 @@ def set_app(index):
100100
last_position = macropad.encoder # Push down and turn (right)
101101
set_app((app_index + 1) % len(apps))
102102
macro_changed = True
103-
elif macropad.encoder < last_position: # Rotary counter-clockwise
104-
last_position = macropad.encoder
105-
keys.press(Keys.KEY_ENC_LEFT)
103+
elif macropad.encoder < last_position: # Encoder counter-clockwise
104+
while macropad.encoder < last_position:
105+
keys.press(Keys.KEY_ENC_LEFT)
106+
last_position -= 1
106107
keys.release(Keys.KEY_ENC_LEFT)
107-
elif macropad.encoder > last_position: # Rotary clockwise
108-
last_position = macropad.encoder
109-
keys.press(Keys.KEY_ENC_RIGHT)
108+
elif macropad.encoder > last_position: # Encoder clockwise
109+
while macropad.encoder > last_position:
110+
keys.press(Keys.KEY_ENC_RIGHT)
111+
last_position += 1
110112
keys.release(Keys.KEY_ENC_RIGHT)
111-
elif macropad.encoder_switch_debounced.released:
112-
if macro_changed: macro_changed = False # Land on the selected macro page
113-
else: keys.press(Keys.KEY_ENC_BUTTON) # Encoder button "pressed"
113+
elif macropad.encoder_switch_debounced.released and macro_changed:
114+
keys.press(Keys.KEY_LAUNCH) # Press the "new page" button
115+
keys.release(Keys.KEY_LAUNCH)
116+
macro_changed = False
117+
elif macropad.encoder_switch_debounced.released: # Encoder button "pressed"
118+
keys.press(Keys.KEY_ENC_BUTTON)

keys.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@ class Keys:
1010
KEY_ENC_BUTTON = 12 # Virtual key for encoder press
1111
KEY_ENC_LEFT = 13 # Virtual key for encoder rotation left
1212
KEY_ENC_RIGHT = 14 # Virtual key for encoder rotation right
13-
KEY_SLEEP = 15 # Hidden key for sleeping
13+
KEY_LAUNCH = 15 # Hidden key for launching a new page
14+
KEY_SLEEP = 16 # Hidden key for sleeping
1415

1516
listeners = []
1617
keys = []
17-
app = None
1818

1919
def __init__(self, app):
20-
self.app = app
21-
22-
self.keys = [None] * 16
23-
for i in range(len(self.app.macros)):
24-
color, label, macro = self.app.macros[i]
20+
self.keys = [None] * 17
21+
for i in range(len(app.macros)):
22+
color, label, macro = app.macros[i]
2523
self.keys[i] = Key(macro, label, color)
26-
self.keys[15] = Key(Sleep())
24+
25+
self.keys[Keys.KEY_LAUNCH] = Key(app.launch[2]) if app.launch else Key([])
26+
self.keys[Keys.KEY_SLEEP] = Key(Sleep())
2727

2828
def __del__(self):
2929
self.listeners.clear()
3030
self.keys.clear()
31-
self.app = None
3231

3332
def __bool__(self):
3433
return len(self.keys) > 0

pixels.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ def pressed(self, keys, index):
3737
self.highlight(index)
3838

3939
commands = keys[index].commands
40+
if not commands: return
41+
4042
if isinstance(commands[0], Sleep):
4143
self.sleep()
4244

4345
def released(self, keys, index):
4446
self.reset(index)
4547

4648
commands = keys[index].commands
49+
if not commands: return
50+
4751
if isinstance(commands[0], Sleep):
4852
self.resume()
4953

screen.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ def pressed(self, keys, index):
5959
self.highlight(index)
6060

6161
commands = keys[index].commands
62+
if not commands: return
63+
6264
if isinstance(commands[0], Sleep):
6365
self.sleep()
6466

6567
def released(self, keys, index):
6668
self.reset(index)
6769

6870
commands = keys[index].commands
71+
if not commands: return
72+
6973
if isinstance(commands[0], Sleep):
7074
self.resume()
7175

tests/test_keys.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_init(self):
2929
keys = Keys(app)
3030
keys.addListener(MockListener())
3131

32-
self.assertEqual(len(keys.keys), 16)
32+
self.assertEqual(len(keys.keys), 17)
3333
self.assertEqual(keys.keys[0].color, 0x0F0F0F)
3434
self.assertIsInstance(keys.keys[0].commands, Commands)
3535

@@ -60,3 +60,26 @@ def test_release(self):
6060
listenerOne.pressed.assert_not_called()
6161
listenerTwo.released.assert_called_once()
6262
listenerTwo.pressed.assert_not_called()
63+
64+
def test_no_launch(self):
65+
listenerOne = MockListener()
66+
app = MockApp()
67+
keys = Keys(app)
68+
keys.addListener(listenerOne)
69+
keys.press(Keys.KEY_LAUNCH)
70+
keys.release(Keys.KEY_LAUNCH)
71+
72+
listenerOne.released.assert_called_once()
73+
listenerOne.pressed.assert_called_once()
74+
75+
def test_launch(self):
76+
listenerOne = MockListener()
77+
app = MockApp()
78+
app.launch = 0x000000, None, [],
79+
keys = Keys(app)
80+
keys.addListener(listenerOne)
81+
keys.press(Keys.KEY_LAUNCH)
82+
keys.release(Keys.KEY_LAUNCH)
83+
84+
listenerOne.released.assert_called_once()
85+
listenerOne.pressed.assert_called_once()

0 commit comments

Comments
 (0)