Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions code.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def set_app(index):
sleep_remaining -= elapsed_seconds()
event = macropad.keys.events.get()

if event or last_position != macropad.encoder or macropad.encoder_switch_debounced.released:
if (event and event.pressed) or last_position != macropad.encoder or macropad.encoder_switch_debounced.released:
keys.release(Keys.KEY_SLEEP) # Don't go to sleep!
sleep_remaining = apps[app_index].timeout
if sleep_remaining <= 0: # Go to sleep and slow down
Expand All @@ -100,14 +100,19 @@ def set_app(index):
last_position = macropad.encoder # Push down and turn (right)
set_app((app_index + 1) % len(apps))
macro_changed = True
elif macropad.encoder < last_position: # Rotary counter-clockwise
last_position = macropad.encoder
keys.press(Keys.KEY_ENC_LEFT)
elif macropad.encoder < last_position: # Encoder counter-clockwise
while macropad.encoder < last_position:
keys.press(Keys.KEY_ENC_LEFT)
last_position -= 1
keys.release(Keys.KEY_ENC_LEFT)
elif macropad.encoder > last_position: # Rotary clockwise
last_position = macropad.encoder
keys.press(Keys.KEY_ENC_RIGHT)
elif macropad.encoder > last_position: # Encoder clockwise
while macropad.encoder > last_position:
keys.press(Keys.KEY_ENC_RIGHT)
last_position += 1
keys.release(Keys.KEY_ENC_RIGHT)
elif macropad.encoder_switch_debounced.released:
if macro_changed: macro_changed = False # Land on the selected macro page
else: keys.press(Keys.KEY_ENC_BUTTON) # Encoder button "pressed"
elif macropad.encoder_switch_debounced.released and macro_changed:
keys.press(Keys.KEY_LAUNCH) # Press the "new page" button
keys.release(Keys.KEY_LAUNCH)
macro_changed = False
elif macropad.encoder_switch_debounced.released: # Encoder button "pressed"
keys.press(Keys.KEY_ENC_BUTTON)
17 changes: 8 additions & 9 deletions keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ class Keys:
KEY_ENC_BUTTON = 12 # Virtual key for encoder press
KEY_ENC_LEFT = 13 # Virtual key for encoder rotation left
KEY_ENC_RIGHT = 14 # Virtual key for encoder rotation right
KEY_SLEEP = 15 # Hidden key for sleeping
KEY_LAUNCH = 15 # Hidden key for launching a new page
KEY_SLEEP = 16 # Hidden key for sleeping

listeners = []
keys = []
app = None

def __init__(self, app):
self.app = app

self.keys = [None] * 16
for i in range(len(self.app.macros)):
color, label, macro = self.app.macros[i]
self.keys = [None] * 17
for i in range(len(app.macros)):
color, label, macro = app.macros[i]
self.keys[i] = Key(macro, label, color)
self.keys[15] = Key(Sleep())

self.keys[Keys.KEY_LAUNCH] = Key(app.launch[2]) if app.launch else Key([])
self.keys[Keys.KEY_SLEEP] = Key(Sleep())

def __del__(self):
self.listeners.clear()
self.keys.clear()
self.app = None

def __bool__(self):
return len(self.keys) > 0
Expand Down
4 changes: 4 additions & 0 deletions pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ def pressed(self, keys, index):
self.highlight(index)

commands = keys[index].commands
if not commands: return

if isinstance(commands[0], Sleep):
self.sleep()

def released(self, keys, index):
self.reset(index)

commands = keys[index].commands
if not commands: return

if isinstance(commands[0], Sleep):
self.resume()

Expand Down
4 changes: 4 additions & 0 deletions screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ def pressed(self, keys, index):
self.highlight(index)

commands = keys[index].commands
if not commands: return

if isinstance(commands[0], Sleep):
self.sleep()

def released(self, keys, index):
self.reset(index)

commands = keys[index].commands
if not commands: return

if isinstance(commands[0], Sleep):
self.resume()

Expand Down
25 changes: 24 additions & 1 deletion tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_init(self):
keys = Keys(app)
keys.addListener(MockListener())

self.assertEqual(len(keys.keys), 16)
self.assertEqual(len(keys.keys), 17)
self.assertEqual(keys.keys[0].color, 0x0F0F0F)
self.assertIsInstance(keys.keys[0].commands, Commands)

Expand Down Expand Up @@ -60,3 +60,26 @@ def test_release(self):
listenerOne.pressed.assert_not_called()
listenerTwo.released.assert_called_once()
listenerTwo.pressed.assert_not_called()

def test_no_launch(self):
listenerOne = MockListener()
app = MockApp()
keys = Keys(app)
keys.addListener(listenerOne)
keys.press(Keys.KEY_LAUNCH)
keys.release(Keys.KEY_LAUNCH)

listenerOne.released.assert_called_once()
listenerOne.pressed.assert_called_once()

def test_launch(self):
listenerOne = MockListener()
app = MockApp()
app.launch = 0x000000, None, [],
keys = Keys(app)
keys.addListener(listenerOne)
keys.press(Keys.KEY_LAUNCH)
keys.release(Keys.KEY_LAUNCH)

listenerOne.released.assert_called_once()
listenerOne.pressed.assert_called_once()