diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2023-05-22 08:40:47 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2023-05-22 08:40:47 +0200 |
commit | 597b007333d8ec0d9cfd29e6941fcbe57379108a (patch) | |
tree | 0cf87e1ac487e7deb91acf7f2bec70bd4dd06703 /src/actions.py |
Initial commit
Diffstat (limited to 'src/actions.py')
-rw-r--r-- | src/actions.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/actions.py b/src/actions.py new file mode 100644 index 0000000..1e40bbd --- /dev/null +++ b/src/actions.py @@ -0,0 +1,72 @@ +from supervisor import ticks_ms +import ticks +import time +from random import randrange + +class Action(object): + def __new__(cls): + if not hasattr(cls, 'instance'): + cls.instance = super(Action, cls).__new__(cls) + cls.instance.events = [] + return cls.instance + + + def __init__(self): + """ Do nothing and do not get any constructor. + This is intended because it allow to call the class directly like : + Action().key( … ) + """ + pass + + def set_macropad(self, macropad): + self.macropad = macropad + + def key(self, pressed, sequence): + """ Send a key to the host + """ + + if pressed: + action = self.macropad.keyboard.press + s = sequence + else: + action = self.macropad.keyboard.release + s = reversed(sequence) + for item in s: + action(item) + + def sequence(self, keys): + """ Send a sequence of keys to the host + """ + for key in keys: + self.key(True, key) + time.sleep( randrange(25, 74)/400. ) + self.key(False, key) + + def delay(self, duration, callback): + "Register a delayed task" + time = ticks_ms() + next_tick = ticks.add( + ticks_ms(), + duration + ) + self.events.append((next_tick, callback)) + + def flash(self, key, release_color): + "Flash the key with a high light for 10ms" + def restore(): + if self.macropad.pixels[key] == (127, 127, 127): + self.macropad.pixels[key] = release_color + self.macropad.pixels.show() + self.delay(10, restore) + self.macropad.pixels[key] = (127, 127, 127) + + def collect_tasks(self): + "Execute all the delayed tasks" + remain = [] + time = ticks_ms() + for event in self.events: + if not ticks.less(event[0]): + event[1]() + else: + remain.append(event) + self.events = remain |