Skip to content

Commit

Permalink
fixup! refactor(core): unify touch and button handling, enable usage …
Browse files Browse the repository at this point in the history
…of both in one model
  • Loading branch information
TychoVrahe committed Jun 20, 2023
1 parent 1035c24 commit 9a0c1a3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
30 changes: 15 additions & 15 deletions core/embed/extmod/modtrezorio/modtrezorio-poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

#define USB_DATA_IFACE (253)
#define INPUT_IFACE (255)
#define TOUCH_INPUT_FLAG (0x40000000)
#define BUTTON_INPUT_FLAG (0x80000000)
#define TOUCH_INPUT_FLAG (0x1)
#define BUTTON_INPUT_FLAG (0x2)
#define POLL_READ (0x0000)
#define POLL_WRITE (0x0100)

Expand Down Expand Up @@ -87,16 +87,16 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
#ifdef USE_BUTTON
evt = button_read();
if (evt & (BTN_EVT_DOWN | BTN_EVT_UP)) {
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
uint32_t etype =
((evt >> 24) & 0x3U) | BUTTON_INPUT_FLAG; // button down/up
uint32_t en = evt & 0xFFFF; // button number
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
uint32_t etype = ((evt >> 24) & 0x3U); // button down/up
uint32_t en = evt & 0xFFFF; // button number
if (display_orientation(-1) == 180) {
en = (en == BTN_LEFT) ? BTN_RIGHT : BTN_LEFT;
}
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(etype);
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(en);
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(BUTTON_INPUT_FLAG);
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(etype);
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(en);
tuple->items[3] = MP_OBJ_NEW_SMALL_INT(0);
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = MP_OBJ_FROM_PTR(tuple);
return mp_const_true;
Expand All @@ -105,9 +105,8 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
#ifdef USE_TOUCH
evt = touch_read();
if (evt) {
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
const uint32_t etype =
((evt >> 24) & 0xFFU) | TOUCH_INPUT_FLAG; // event type
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
const uint32_t etype = ((evt >> 24) & 0xFFU); // event type
const uint32_t ex = (evt >> 12) & 0xFFFU; // x position
const uint32_t ey = evt & 0xFFFU; // y position
uint32_t exr; // rotated x position
Expand All @@ -130,9 +129,10 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
eyr = ey;
break;
}
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(etype);
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(exr);
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(eyr);
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(TOUCH_INPUT_FLAG);
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(etype);
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(exr);
tuple->items[3] = MP_OBJ_NEW_SMALL_INT(eyr);
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
ret->items[1] = MP_OBJ_FROM_PTR(tuple);
return mp_const_true;
Expand Down
2 changes: 2 additions & 0 deletions core/embed/extmod/modtrezorio/modtrezorio.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorio_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_sdcard), MP_ROM_PTR(&mod_trezorio_sdcard_module)},
#endif
{MP_ROM_QSTR(MP_QSTR_INPUT), MP_ROM_INT(INPUT_IFACE)},
{MP_ROM_QSTR(MP_QSTR_INPUT_IF_TOUCH), MP_ROM_INT(TOUCH_INPUT_FLAG)},
{MP_ROM_QSTR(MP_QSTR_INPUT_IF_BUTTON), MP_ROM_INT(BUTTON_INPUT_FLAG)},
#ifdef USE_TOUCH
{MP_ROM_QSTR(MP_QSTR_TOUCH_START),
MP_ROM_INT(((TOUCH_START >> 24) & 0xFFU) | TOUCH_INPUT_FLAG)},
Expand Down
4 changes: 2 additions & 2 deletions core/embed/rust/src/ui/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl ButtonEvent {
1 => PhysicalButton::Right,
_ => return Err(error::Error::OutOfRange),
};
let result = match event & 0xFF {
let result = match event {
1 => Self::ButtonPressed(button),
2 => Self::ButtonReleased(button),
_ => return Err(error::Error::OutOfRange),
Expand All @@ -50,7 +50,7 @@ pub enum TouchEvent {
impl TouchEvent {
pub fn new(event: u32, x: u32, y: u32) -> Result<Self, error::Error> {
let point = Point::new(x.try_into()?, y.try_into()?);
let result = match event & 0xFF {
let result = match event {
1 => Self::TouchStart(point),
2 => Self::TouchMove(point),
4 => Self::TouchEnd(point),
Expand Down
2 changes: 1 addition & 1 deletion core/src/trezor/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def handle_input(self) -> Generator:
input_ = loop.wait(io.INPUT)
while True:
# Using `yield` instead of `await` to avoid allocations.
event, x, y = yield input_
interface, event, x, y = yield input_
workflow.idle_timer.touch()
self.dispatch(event, x, y)
# We dispatch a render event right after the input. Quick and dirty
Expand Down
2 changes: 1 addition & 1 deletion core/src/trezor/ui/layouts/tr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def handle_input_and_rendering(self) -> loop.Task: # type: ignore [awaitable-is
self._first_paint()
while True:
# Using `yield` instead of `await` to avoid allocations.
event, button_num = yield button
interface, event, button_num = yield button
workflow.idle_timer.touch()
msg = None
if event in (io.BUTTON_PRESSED, io.BUTTON_RELEASED):
Expand Down
10 changes: 3 additions & 7 deletions core/src/trezor/ui/layouts/tt_v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,12 @@ def handle_input_and_rendering(self) -> loop.Task: # type: ignore [awaitable-is
self._first_paint()
while True:
# Using `yield` instead of `await` to avoid allocations.
event, p0, p1 = yield input
interface, event, p0, p1 = yield input
workflow.idle_timer.touch()
msg = None
if utils.USE_TOUCH and event in (
io.TOUCH_START,
io.TOUCH_MOVE,
io.TOUCH_END,
):
if utils.USE_TOUCH and interface == io.INPUT_IF_TOUCH:
msg = self.layout.touch_event(event, p0, p1)
if utils.USE_BUTTON and event in (io.BUTTON_PRESSED, io.BUTTON_RELEASED):
if utils.USE_TOUCH and interface == io.INPUT_IF_BUTTON:
msg = self.layout.button_event(event, p0)
if msg is not None:
raise ui.Result(msg)
Expand Down

0 comments on commit 9a0c1a3

Please sign in to comment.