Skip to content

Commit

Permalink
Added streaming support to input system
Browse files Browse the repository at this point in the history
  • Loading branch information
CRImier committed Jan 5, 2017
1 parent fce2151 commit 44cffaf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
Empty file.
Empty file.
30 changes: 30 additions & 0 deletions apps/example_apps/streaming_test/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
menu_name = "Refresher app"

from ui import Refresher

counter = 0
keys_called = []

def process_key(key, *args):
global counter, keys_called
if len(keys_called) >= o.rows:
keys_called = keys_called[1:]
keys_called.append([counter, key])
counter += 1
refresher.refresh() #Makes changes appear faster

def get_keys():
return ["{}:{}".format(num, key) for num, key in keys_called]

refresher = None
callback = None
i = None #Input device
o = None #Output device

def init_app(input, output):
global refresher, callback, i, o
i = input; o = output
i.set_streaming(process_key)
refresher = Refresher(get_keys, i, o, 1, name="Key monitor")
callback = refresher.activate

1 change: 0 additions & 1 deletion docs/plans.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Input devices
* Make an input emulator for development tasks
* Add key remapping to HID driver
* Pressed/released/held button states
* Input streaming mode (pass all keycodes received to one callback)

==============
Output devices
Expand Down
13 changes: 13 additions & 0 deletions input/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class InputListener():
keymap = {}
maskable_keymap = {}
nonmaskable_keymap = {}
streaming = None
reserved_keys = ["KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN", "KEY_ENTER", "KEY_KPENTER"]

def __init__(self, drivers, keymap=None):
Expand All @@ -37,6 +38,16 @@ def receive_key(self, key):
except:
raise #Just collecting possible exceptions for now

def set_streaming(self, callback):
"""Sets a callback for streaming key events. The callback will be called
with key_name as first argument but should support arbitrary number
of positional arguments if compatibility with future versions is desired."""
self.streaming = callback

def remove_streaming(self):
"""Removes a callback for streaming key events, if previously set by any app/UI element."""
self.streaming = None

def set_callback(self, key_name, callback):
"""Sets a single callback of the listener"""
self.keymap[key_name] = callback
Expand Down Expand Up @@ -118,6 +129,8 @@ def process_key(self, key):
elif key in self.maskable_keymap:
callback = self.maskable_keymap[key]
self.handle_callback(callback, key)
elif callable(self.streaming):
self.streaming(key)

def handle_callback(self, callback, key):
try:
Expand Down
4 changes: 2 additions & 2 deletions ui/refresher.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def process_keymap(self, keymap):
keymap[key] = callback
if not "KEY_LEFT" in keymap:
keymap["KEY_LEFT"] = self.deactivate
if not "KEY_RIGHT" in keymap:
keymap["KEY_RIGHT"] = self.print_name
#if not "KEY_RIGHT" in keymap and:
# keymap["KEY_RIGHT"] = self.print_name
return keymap

def set_keymap(self, keymap):
Expand Down

0 comments on commit 44cffaf

Please sign in to comment.