2016-04-28 05:43:37 +00:00
|
|
|
import utime
|
|
|
|
|
2016-05-16 15:10:12 +00:00
|
|
|
from uheapq import heappop, heappush, heapify
|
2016-04-28 05:43:37 +00:00
|
|
|
from .utils import type_gen
|
2016-04-29 12:49:47 +00:00
|
|
|
from . import msg
|
2016-04-29 20:24:27 +00:00
|
|
|
from . import log
|
2016-04-28 05:43:37 +00:00
|
|
|
|
2016-04-29 19:48:59 +00:00
|
|
|
if __debug__:
|
|
|
|
# For performance stats
|
|
|
|
import array
|
|
|
|
log_delay_pos = 0
|
|
|
|
log_delay_rb_len = const(10)
|
|
|
|
log_delay_rb = array.array('i', [0] * log_delay_rb_len)
|
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
TOUCH_START = const(1)
|
|
|
|
TOUCH_MOVE = const(2)
|
|
|
|
TOUCH_END = const(4)
|
|
|
|
HID_READ = const(8)
|
|
|
|
|
2016-05-16 15:10:12 +00:00
|
|
|
blocked_gens = {} # event -> generator
|
|
|
|
time_queue = [] # [(int, int, generator, any)]
|
|
|
|
time_ticket = 0
|
2016-05-12 14:18:40 +00:00
|
|
|
|
2016-05-02 14:22:22 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
def schedule(gen, data=None, time=None):
|
2016-05-16 15:10:12 +00:00
|
|
|
global time_ticket
|
2016-04-29 19:48:59 +00:00
|
|
|
if not time:
|
|
|
|
time = utime.ticks_us()
|
2016-05-16 15:10:12 +00:00
|
|
|
heappush(time_queue, (time, time_ticket, gen, data))
|
|
|
|
time_ticket += 1
|
|
|
|
return gen
|
|
|
|
|
|
|
|
|
|
|
|
def unschedule(gen):
|
|
|
|
global time_queue
|
|
|
|
time_queue = [entry for entry in time_queue if entry[1] is not gen]
|
|
|
|
heapify(time_queue)
|
|
|
|
|
|
|
|
|
|
|
|
def block(gen, event):
|
|
|
|
curr_gen = blocked_gens.get(event, None)
|
|
|
|
if curr_gen is not None:
|
|
|
|
log.warning(__name__, 'Closing %s blocked on %s', curr_gen, event)
|
|
|
|
curr_gen.close()
|
|
|
|
blocked_gens[event] = gen
|
|
|
|
|
|
|
|
|
|
|
|
def unblock(gen):
|
|
|
|
for key in blocked_gens:
|
|
|
|
if blocked_gens[key] is gen:
|
|
|
|
blocked_gens[key] = None
|
2016-05-12 14:18:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Sleep():
|
|
|
|
|
|
|
|
def __init__(self, us):
|
|
|
|
self.time = utime.ticks_us() + us
|
|
|
|
|
|
|
|
|
|
|
|
class Select():
|
|
|
|
|
2016-05-16 15:10:12 +00:00
|
|
|
def __init__(self, *events):
|
2016-05-12 14:18:40 +00:00
|
|
|
self.events = events
|
2016-04-29 23:20:57 +00:00
|
|
|
|
2016-05-16 15:10:12 +00:00
|
|
|
def handle(self, gen):
|
|
|
|
for event in self.events:
|
|
|
|
block(gen, event)
|
|
|
|
|
2016-05-02 14:22:22 +00:00
|
|
|
|
2016-05-02 14:06:08 +00:00
|
|
|
class Wait():
|
2016-05-11 12:39:57 +00:00
|
|
|
|
2016-05-02 14:06:08 +00:00
|
|
|
def __init__(self, gens, wait_for=1, exit_others=True):
|
2016-05-16 15:10:12 +00:00
|
|
|
self.gens = gens
|
2016-05-02 14:06:08 +00:00
|
|
|
self.wait_for = wait_for
|
|
|
|
self.exit_others = exit_others
|
|
|
|
self.received = 0
|
2016-05-16 15:10:12 +00:00
|
|
|
self.scheduled = None
|
2016-05-02 14:06:08 +00:00
|
|
|
self.callback = None
|
|
|
|
|
2016-05-16 15:10:12 +00:00
|
|
|
def handle(self, gen):
|
|
|
|
self.scheduled = [schedule(self._wait(g)) for g in self.gens]
|
|
|
|
self.callback = gen
|
2016-05-02 14:06:08 +00:00
|
|
|
|
|
|
|
def _wait(self, gen):
|
2016-05-16 15:10:12 +00:00
|
|
|
result = yield from gen
|
|
|
|
self._finish(gen, result)
|
2016-04-29 23:20:57 +00:00
|
|
|
|
2016-05-16 15:10:12 +00:00
|
|
|
def _finish(self, gen, result):
|
2016-05-02 14:06:08 +00:00
|
|
|
self.received += 1
|
2016-05-02 14:21:50 +00:00
|
|
|
if self.received == self.wait_for:
|
2016-05-12 14:18:40 +00:00
|
|
|
schedule(self.callback, (gen, result))
|
2016-05-02 14:06:08 +00:00
|
|
|
if self.exit_others:
|
2016-05-16 15:10:12 +00:00
|
|
|
for g in self.scheduled:
|
|
|
|
if g is not gen:
|
|
|
|
unschedule(g)
|
|
|
|
unblock(g)
|
2016-05-11 12:39:57 +00:00
|
|
|
g.close()
|
2016-04-29 19:48:59 +00:00
|
|
|
|
2016-05-02 14:22:22 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
def run_forever():
|
2016-04-30 00:00:40 +00:00
|
|
|
if __debug__:
|
2016-05-12 14:18:40 +00:00
|
|
|
global log_delay_pos, log_delay_rb, log_delay_rb_len
|
|
|
|
global blocked_events, blocked_gen
|
2016-04-29 19:19:25 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
DELAY_MAX = const(1000000)
|
2016-04-29 19:48:59 +00:00
|
|
|
|
2016-04-29 19:19:25 +00:00
|
|
|
while True:
|
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
# Peek at how long we can sleep while waiting for an event
|
2016-04-29 19:19:25 +00:00
|
|
|
if time_queue:
|
2016-05-11 12:39:57 +00:00
|
|
|
t, _, _, _ = time_queue[0]
|
2016-04-29 19:19:25 +00:00
|
|
|
delay = t - utime.ticks_us()
|
|
|
|
else:
|
2016-05-12 14:18:40 +00:00
|
|
|
delay = DELAY_MAX
|
2016-04-29 19:19:25 +00:00
|
|
|
|
2016-04-30 00:00:40 +00:00
|
|
|
if __debug__:
|
|
|
|
# Adding current delay to ring buffer for performance stats
|
|
|
|
log_delay_rb[log_delay_pos] = delay
|
|
|
|
log_delay_pos = (log_delay_pos + 1) % log_delay_rb_len
|
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
message = msg.select(delay)
|
|
|
|
if message:
|
|
|
|
# Run interrupt handler right away, they have priority
|
|
|
|
event = message[0]
|
|
|
|
data = message
|
2016-05-16 15:10:12 +00:00
|
|
|
gen = blocked_gens.pop(event, None)
|
|
|
|
if not gen:
|
2016-05-03 16:35:11 +00:00
|
|
|
log.info(__name__, 'No handler for event: %s', event)
|
|
|
|
continue
|
2016-05-16 15:10:12 +00:00
|
|
|
# Cancel other registrations of this handler
|
|
|
|
unblock(gen)
|
2016-04-29 19:19:25 +00:00
|
|
|
else:
|
2016-05-12 14:18:40 +00:00
|
|
|
# Run something from the time queue
|
2016-05-02 14:22:22 +00:00
|
|
|
if time_queue:
|
2016-05-12 14:18:40 +00:00
|
|
|
_, _, gen, data = heappop(time_queue)
|
2016-05-02 14:22:22 +00:00
|
|
|
else:
|
|
|
|
continue
|
2016-04-29 19:19:25 +00:00
|
|
|
|
|
|
|
try:
|
2016-05-12 14:18:40 +00:00
|
|
|
result = gen.send(data)
|
2016-04-29 19:19:25 +00:00
|
|
|
except StopIteration as e:
|
2016-05-02 14:22:22 +00:00
|
|
|
log.debug(__name__, '%s finished', gen)
|
2016-04-29 19:19:25 +00:00
|
|
|
continue
|
2016-04-29 23:20:57 +00:00
|
|
|
except Exception as e:
|
2016-04-29 23:48:13 +00:00
|
|
|
log.exception(__name__, e)
|
2016-04-29 23:20:57 +00:00
|
|
|
continue
|
2016-04-29 19:19:25 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
if isinstance(result, Sleep):
|
|
|
|
# Sleep until result.time, call us later
|
2016-05-16 15:10:12 +00:00
|
|
|
schedule(gen, result, result.time)
|
2016-05-03 16:35:11 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
elif isinstance(result, Select):
|
|
|
|
# Wait for one or more types of event
|
2016-05-16 15:10:12 +00:00
|
|
|
result.handle(gen)
|
2016-04-29 19:19:25 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
elif isinstance(result, Wait):
|
2016-05-16 15:10:12 +00:00
|
|
|
# Register us as a waiting callback
|
|
|
|
result.handle(gen)
|
2016-04-30 13:57:08 +00:00
|
|
|
|
2016-05-12 14:18:40 +00:00
|
|
|
elif result is None:
|
2016-05-02 14:22:22 +00:00
|
|
|
# Just call us asap
|
2016-05-12 14:18:40 +00:00
|
|
|
schedule(gen)
|
2016-04-29 23:20:57 +00:00
|
|
|
|
2016-04-30 13:57:08 +00:00
|
|
|
else:
|
2016-05-12 14:18:40 +00:00
|
|
|
raise Exception('Unhandled result %s by %s' % (result, gen))
|