From d726a1e13c4bf7f7c955bc3dee2c4049dc2b0d40 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Wed, 31 Jan 2024 09:02:53 -0500 Subject: [PATCH] click on USB plug area, becomes voltage change event --- unix/README.md | 13 +++++++++++++ unix/simulator.py | 8 ++++++++ unix/variant/machine.py | 6 +++++- unix/variant/sim_battery.py | 19 +++++++++++++------ unix/variant/touch.py | 8 +++++++- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/unix/README.md b/unix/README.md index a2c122e0..f1b22ade 100644 --- a/unix/README.md +++ b/unix/README.md @@ -62,6 +62,19 @@ wallet (on testnet, always with the same seed). But there are other options: See `variant/sim_settings.py` for the details of settings-related options. +## Clicking on Simulated Device + +### Q + +- send keystrokes when clicking on keys, but much easier to use real keyboard +- click screen to send current clipboard contents as a QR reading +- click on area near USB plug to cycle through battery levels and USB plugged/not + +### Mk4 + +- sends keystrokes + + ## Requirements - uses good olde `xterm` for console input and output diff --git a/unix/simulator.py b/unix/simulator.py index 2672131a..5409cc26 100755 --- a/unix/simulator.py +++ b/unix/simulator.py @@ -564,6 +564,11 @@ def q1_click_to_keynum(x, y): if (90 <= x <= 430) and (90 <= y <= 345): # click on screen return 'SCREEN' + + # detect click near USB to simulate unplug/plug events + if (230 <= x <= 290) and (810 <= y <= 852): + # click near USB connector + return 'PLUGGER' # keypad area left = 29 @@ -650,6 +655,9 @@ def handle_q1_key_events(event, numpad_tx, data_tx): data_tx.write(txt + b'\n') return None + if kn == 'PLUGGER': + kn = 0xfe # see variant/touch.py + if kn is None: return if is_press: diff --git a/unix/variant/machine.py b/unix/variant/machine.py index 23cfe5b0..5703a9f2 100644 --- a/unix/variant/machine.py +++ b/unix/variant/machine.py @@ -43,7 +43,11 @@ class Pin: if self.name == 'LCD_TEAR': from touch import Touch Touch() - pass + else: + self.irq_handler = a[0] + + def simulate_irq(self): + self.irq_handler(self) def __call__(self, new_val=UNSPEC): if new_val==UNSPEC: diff --git a/unix/variant/sim_battery.py b/unix/variant/sim_battery.py index 8eb8bc38..ac68046c 100644 --- a/unix/variant/sim_battery.py +++ b/unix/variant/sim_battery.py @@ -4,14 +4,21 @@ # import battery, sys -battery.setup_battery = lambda: None - -battery.setup_adc = lambda: None +fake_voltage = 0 if ('--plugged' in sys.argv) else 4.0 def mock_get_batt_level(): - if '--plugged' in sys.argv: - return None - return 3.3 + return fake_voltage if fake_voltage != 0 else None + +def sim_plug_toggler(): + # user clicked on simulator's plug, so show different voltages + global fake_voltage + + # defined by battery.get_batt_threshold() + levels = [0, 2.9, 3.5, 4.0, 4.5] + + fake_voltage = levels[(levels.index(fake_voltage) + 1) % len(levels)] + + battery.nbat_pin.simulate_irq() battery.get_batt_level = mock_get_batt_level diff --git a/unix/variant/touch.py b/unix/variant/touch.py index 2d86a4a3..28c0b57f 100644 --- a/unix/variant/touch.py +++ b/unix/variant/touch.py @@ -36,10 +36,16 @@ class Touch: # - pad with -1 pressed = await s.read(5) + if 0xfe in pressed: + # see PLUGGER in simulator.py + from sim_battery import sim_plug_toggler + sim_plug_toggler() + continue + try: new_presses = set(kn for kn in pressed if kn!=255 and not numpad.is_pressed[kn]) except IndexError: - # some bug brings us here + # some bugs bring us here print("wrong kn: %r" % kn) continue