From 1664a4fc297e18b88c629940a23d739c92375511 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 29 Apr 2016 14:49:47 +0200 Subject: [PATCH] replace wait with msg.select --- extmod/modtrezormsg/modtrezormsg.c | 16 +++++++++++----- send_udp.py | 9 +++++++++ src/trezor/loop.py | 11 ++++++++--- src/trezor/msg.py | 4 ++-- vendor/micropython | 2 +- 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100755 send_udp.py diff --git a/extmod/modtrezormsg/modtrezormsg.c b/extmod/modtrezormsg/modtrezormsg.c index a22a8c2e15..6cd3bfe66c 100644 --- a/extmod/modtrezormsg/modtrezormsg.c +++ b/extmod/modtrezormsg/modtrezormsg.c @@ -47,10 +47,15 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_send(mp_obj_t self, mp_obj_t message) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_TrezorMsg_Msg_send_obj, mod_TrezorMsg_Msg_send); -// def Msg.select(self, timeout_ms: int) -> None/tuple/bytes -STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_ms) { - int to = mp_obj_get_int(timeout_ms); - while (--to >= 0) { +#define TICK_RESOLUTION 1000 + +// def Msg.select(self, timeout_us: int) -> None/tuple/bytes +STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_us) { + int to = mp_obj_get_int(timeout_us); + if (to < 0) { + to = 0; + } + while (to >= 0) { uint32_t e = msg_poll_ui_event(); if (e) { mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); @@ -66,7 +71,8 @@ STATIC mp_obj_t mod_TrezorMsg_Msg_select(mp_obj_t self, mp_obj_t timeout_ms) { memcpy(vstr.buf, m, 64); return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } - mp_hal_delay_ms(1); + mp_hal_delay_us_fast(TICK_RESOLUTION); + to -= TICK_RESOLUTION; } return mp_const_none; } diff --git a/send_udp.py b/send_udp.py new file mode 100755 index 0000000000..4b08f2419e --- /dev/null +++ b/send_udp.py @@ -0,0 +1,9 @@ +#!/usr/bin/python +import socket + +UDP_IP = '127.0.0.1' +UDP_PORT = 21324 +MESSAGE = b'Hello, World!' + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) diff --git a/src/trezor/loop.py b/src/trezor/loop.py index 09084a3a5a..84286c4f43 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -2,6 +2,7 @@ import utime import uheapq from .utils import type_gen +from . import msg if __debug__: import logging @@ -34,7 +35,10 @@ def wait(delay): log.debug("Sleeping for: %s", delay) last_sleep = delay - utime.sleep_us(delay) + m = msg.select(delay) + if m: + print('msg:', m) + return m def run_forever(): global q, cnt @@ -47,11 +51,12 @@ def run_forever(): tnow = utime.ticks_us() delay = t - tnow if delay > 0: - wait(delay) + m = wait(delay) else: - wait(-1) + m = wait(0) # Assuming IO completion scheduled some tasks continue + if callable(cb): ret = cb(*args) if __debug__ and isinstance(ret, type_gen): diff --git a/src/trezor/msg.py b/src/trezor/msg.py index e57b6172f4..f815899c3b 100644 --- a/src/trezor/msg.py +++ b/src/trezor/msg.py @@ -2,8 +2,8 @@ from TrezorMsg import Msg _msg = Msg() -def select(timeout_ms): - return _msg.select(timeout_ms) +def select(timeout_us): + return _msg.select(timeout_us) def send(msg): return _msg.send(msg) diff --git a/vendor/micropython b/vendor/micropython index da3fe60b7d..c4e00834d3 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit da3fe60b7dc12ce393e9a63c9d6ac366b9c81045 +Subproject commit c4e00834d394bf07e3c347494648c7fe73fee50b