Skip to content

Commit

Permalink
Modify the usage method for wireless charging. (#116)
Browse files Browse the repository at this point in the history
* Modify the usage method for wireless charging.

* Check connection status before disconnecting Bluetooth.

* Do not lock PIN after fingerprint unlock is enabled.
  • Loading branch information
lihuanhuan authored May 29, 2024
1 parent dc7cedf commit cea87a9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
1 change: 0 additions & 1 deletion core/embed/trezorhal/low_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ void enter_stop_mode(bool restart, uint32_t shutdown_seconds) {
seconds = shutdown_seconds;
}
camera_power_off();
ble_disconnect();
fpsensor_irq_disable();
touch_enable_irq();
usart_enable_stop_wup();
Expand Down
2 changes: 1 addition & 1 deletion core/src/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def lock_device() -> None:
if storage.device.is_initialized() and config.has_pin():
from trezor.lvglui.scrs import fingerprints

if fingerprints.is_available() and fingerprints.is_unlocked():
if fingerprints.is_available():
fingerprints.lock()
else:
if __debug__:
Expand Down
16 changes: 14 additions & 2 deletions core/src/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from trezor.lvglui import lvgl_tick
from trezor.qr import handle_qr_ctx, handle_qr_task
from trezor.uart import (
ctrl_wireless_charge,
disconnect_ble,
fetch_all,
handle_ble_info,
handle_fingerprint,
Expand All @@ -27,19 +29,29 @@
apps.debug.boot()


def stop_mode(reset_timer: bool = False):
ctrl_wireless_charge(True)
disconnect_ble()
utils.enter_lowpower(reset_timer, storage.device.get_autoshutdown_delay_ms())


async def handle_stop_mode():
while True:
# leave enough time for usb to be detected
await loop.sleep(200)

if display.backlight(): # screen is on
ctrl_wireless_charge(False)
return
utils.enter_lowpower(False, storage.device.get_autoshutdown_delay_ms())
stop_mode(False)


# if the screen is off, enter low power mode after reloop
if display.backlight() == 0:
utils.enter_lowpower(True, storage.device.get_autoshutdown_delay_ms())
stop_mode(True)
else:
if utils.CHARGEING_BY_WIRELESS:
apps.base.screen_off_if_possible()

# run main event loop and specify which screen is the default
apps.base.set_homescreen()
Expand Down
66 changes: 61 additions & 5 deletions core/src/trezor/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
_FORMAT = ">HHB"
_HEADER_LEN = const(5)
# fmt: off
_CMD_BLE_NAME = _PRESS_SHORT = _USB_STATUS_PLUG_IN = _BLE_STATUS_CONNECTED = _BLE_PAIR_SUCCESS = const(1)
_PRESS_LONG = _USB_STATUS_PLUG_OUT = _BLE_STATUS_DISCONNECTED = _BLE_PAIR_FAILED = _CMD_BLE_STATUS = const(2)
_CMD_BLE_NAME = _PRESS_SHORT = _USB_STATUS_PLUG_IN = _BLE_STATUS_CONNECTED = _BLE_PAIR_SUCCESS = CHARGE_START = const(1)
_PRESS_LONG = _USB_STATUS_PLUG_OUT = _BLE_STATUS_DISCONNECTED = _BLE_PAIR_FAILED = _CMD_BLE_STATUS = CHARGE_BY_WIRELESS = const(2)
_BTN_PRESS = const(0x20)
_BTN_RELEASE = const(0x40)
# fmt: on
Expand Down Expand Up @@ -349,8 +349,11 @@ async def _deal_charging_state(value: bytes) -> None:
global CHARGING, CHARING_TYPE
res, CHARING_TYPE = ustruct.unpack(">BB", value)

if display.backlight() == 0:
utils.lcd_resume()

if res in (
_USB_STATUS_PLUG_IN,
CHARGE_START,
_POWER_STATUS_CHARGING,
):
if CHARGING:
Expand All @@ -359,16 +362,41 @@ async def _deal_charging_state(value: bytes) -> None:
StatusBar.get_instance().show_charging(True)
if utils.BATTERY_CAP:
StatusBar.get_instance().set_battery_img(utils.BATTERY_CAP, CHARGING)
if CHARING_TYPE == CHARGE_BY_WIRELESS:
if utils.CHARGEING_BY_WIRELESS:
return
utils.CHARGEING_BY_WIRELESS = True

if device.is_initialized():
if utils.is_initialization_processing():
return
utils.AUTO_POWER_OFF = True
from trezor.lvglui.scrs import fingerprints

if config.has_pin() and config.is_unlocked():
if fingerprints.is_available():
if fingerprints.is_unlocked():
fingerprints.lock()
else:
config.lock()
await loop.race(safe_reloop(), loop.sleep(200))
workflow.spawn(utils.internal_reloop())
return
else:
if utils.CHARGEING_BY_WIRELESS:
utils.CHARGEING_BY_WIRELESS = False
ctrl_charge_switch(True)

elif res in (_USB_STATUS_PLUG_OUT, _POWER_STATUS_CHARGING_FINISHED):
if utils.CHARGEING_BY_WIRELESS:
utils.CHARGEING_BY_WIRELESS = False
# if not CHARGING:
# return
CHARGING = False
StatusBar.get_instance().show_charging(False)
StatusBar.get_instance().show_usb(False)
if utils.BATTERY_CAP:
StatusBar.get_instance().set_battery_img(utils.BATTERY_CAP, CHARGING)
if display.backlight() == 0:
utils.lcd_resume()


async def _deal_pair_res(value: bytes) -> None:
Expand Down Expand Up @@ -473,6 +501,9 @@ def _request_charging_status():
"""Request charging status."""
BLE_CTRL.ctrl(0x82, b"\x05")

def disconnect_ble():
if utils.BLE_CONNECTED:
BLE_CTRL.ctrl(0x81, b"\x03")

async def fetch_all():
"""Request some important data."""
Expand Down Expand Up @@ -581,3 +612,28 @@ def get_ble_hash() -> bytes:

def is_ble_opened() -> bool:
return BLE_ENABLED if BLE_ENABLED is not None else True


def ctrl_charge_switch(enable: bool) -> None:
"""Request to open or close charge.
@param enable: True to open, False to close
"""
if enable:
if not utils.CHARGE_ENABLE:
BLE_CTRL.ctrl(0x82, b"\x06")
utils.CHARGE_ENABLE = True
else:
if utils.CHARGE_ENABLE:
BLE_CTRL.ctrl(0x82, b"\x07")
utils.CHARGE_ENABLE = False

def ctrl_wireless_charge(enable: bool) -> None:
"""Request to open or close charge.
@param enable: True to open, False to close
"""
if enable:
if utils.CHARGEING_BY_WIRELESS and not utils.CHARGE_ENABLE:
ctrl_charge_switch(True)
else:
if utils.CHARGEING_BY_WIRELESS and utils.CHARGE_ENABLE:
ctrl_charge_switch(False)
2 changes: 2 additions & 0 deletions core/src/trezor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def board_version() -> str:
FLASH_LED_BRIGHTNESS: int | None = None
_BACKUP_WITH_LITE_FIRST = False
_COLOR_FLAG: str | None = None
CHARGEING_BY_WIRELESS = False
CHARGE_ENABLE = True

if __debug__:
MAX_FP_ATTEMPTS = 50
Expand Down

0 comments on commit cea87a9

Please sign in to comment.