mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-19 04:48:12 +00:00
style, fix possible exception in run_forever
This commit is contained in:
parent
36dad0137b
commit
20dad5a300
@ -3,7 +3,6 @@ import utime
|
|||||||
from uheapq import heappop, heappush
|
from uheapq import heappop, heappush
|
||||||
from .utils import type_gen
|
from .utils import type_gen
|
||||||
from . import msg
|
from . import msg
|
||||||
from . import ui
|
|
||||||
from . import log
|
from . import log
|
||||||
|
|
||||||
EVT_TSTART = const(-1)
|
EVT_TSTART = const(-1)
|
||||||
@ -11,8 +10,6 @@ EVT_TMOVE = const(-2)
|
|||||||
EVT_TEND = const(-3)
|
EVT_TEND = const(-3)
|
||||||
EVT_MSG = const(-4)
|
EVT_MSG = const(-4)
|
||||||
|
|
||||||
DO_NOTHING = const(-5)
|
|
||||||
|
|
||||||
evt_handlers = {
|
evt_handlers = {
|
||||||
EVT_TSTART: None,
|
EVT_TSTART: None,
|
||||||
EVT_TMOVE: None,
|
EVT_TMOVE: None,
|
||||||
@ -28,6 +25,7 @@ if __debug__:
|
|||||||
log_delay_rb_len = const(10)
|
log_delay_rb_len = const(10)
|
||||||
log_delay_rb = array.array('i', [0] * log_delay_rb_len)
|
log_delay_rb = array.array('i', [0] * log_delay_rb_len)
|
||||||
|
|
||||||
|
|
||||||
def __call_at(time, gen):
|
def __call_at(time, gen):
|
||||||
if __debug__:
|
if __debug__:
|
||||||
log.debug(__name__, 'Scheduling %s %s', time, gen)
|
log.debug(__name__, 'Scheduling %s %s', time, gen)
|
||||||
@ -36,6 +34,7 @@ def __call_at(time, gen):
|
|||||||
time = utime.ticks_us()
|
time = utime.ticks_us()
|
||||||
heappush(time_queue, (time, gen))
|
heappush(time_queue, (time, gen))
|
||||||
|
|
||||||
|
|
||||||
class Wait():
|
class Wait():
|
||||||
def __init__(self, gens, wait_for=1, exit_others=True):
|
def __init__(self, gens, wait_for=1, exit_others=True):
|
||||||
self.wait_for = wait_for
|
self.wait_for = wait_for
|
||||||
@ -69,6 +68,7 @@ class Wait():
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def sleep(us):
|
def sleep(us):
|
||||||
return utime.ticks_us() + us
|
return utime.ticks_us() + us
|
||||||
|
|
||||||
@ -100,19 +100,23 @@ def run_forever(start_gens):
|
|||||||
event = msg.select(delay)
|
event = msg.select(delay)
|
||||||
|
|
||||||
if event:
|
if event:
|
||||||
# run interrupt handler
|
# Run interrupt handler
|
||||||
log.info(__name__, "Received data: %s", event)
|
log.info(__name__, "Received data: %s", event)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
# run something from the time queue
|
if time_queue:
|
||||||
|
# Run something from the time queue
|
||||||
_, gen = heappop(time_queue)
|
_, gen = heappop(time_queue)
|
||||||
|
else:
|
||||||
|
# Sleep again
|
||||||
|
delay = delay_max
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret = gen.send(None)
|
ret = gen.send(None)
|
||||||
|
|
||||||
except StopIteration as e:
|
except StopIteration as e:
|
||||||
log.info(__name__, '%s ended', gen)
|
log.debug(__name__, '%s finished', gen)
|
||||||
# gen ended, forget it and go on
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -121,18 +125,18 @@ def run_forever(start_gens):
|
|||||||
|
|
||||||
if isinstance(ret, int):
|
if isinstance(ret, int):
|
||||||
if ret >= 0:
|
if ret >= 0:
|
||||||
# sleep until ret, call us later
|
# Sleep until ret, call us later
|
||||||
__call_at(ret, gen)
|
__call_at(ret, gen)
|
||||||
else:
|
else:
|
||||||
# wait for event
|
# Wait for event
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
elif isinstance(ret, Wait):
|
elif isinstance(ret, Wait):
|
||||||
log.info(__name__, 'Scheduling %s -> %s', gen, ret)
|
# Register the origin generator as a waiting callback
|
||||||
ret.callback = gen
|
ret.callback = gen
|
||||||
|
|
||||||
elif ret is None:
|
elif ret is None:
|
||||||
# just call us asap
|
# Just call us asap
|
||||||
__call_at(None, gen)
|
__call_at(None, gen)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user