Skip to content

Commit 4b10429

Browse files
authored
adjust screen illumination on charging status change. (#117)
1 parent e0ed2d5 commit 4b10429

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

core/src/trezor/uart.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
_CMD_LED_BRIGHTNESS = const(12)
3535
_CMD_BLE_BUILD_ID = const(16)
3636
_CMD_BLE_HASH = const(17)
37-
CHARGING = False
3837
CHARING_TYPE = 0 # 1 VIA USB / 2 VIA WIRELESS
3938
SCREEN: PairCodeDisplay | None = None
4039
BLE_ENABLED: bool | None = None
@@ -139,7 +138,6 @@ async def handle_fingerprint():
139138

140139

141140
async def handle_usb_state():
142-
global CHARGING
143141
while True:
144142
try:
145143
previous_usb_bus_state = usb.bus.state()
@@ -153,21 +151,21 @@ async def handle_usb_state():
153151
# prompt.show()
154152
StatusBar.get_instance().show_usb(True)
155153
# deal with charging state
156-
CHARGING = True
154+
utils.CHARGING = True
157155
StatusBar.get_instance().show_charging(True)
158156
if utils.BATTERY_CAP:
159157
StatusBar.get_instance().set_battery_img(
160-
utils.BATTERY_CAP, CHARGING
158+
utils.BATTERY_CAP, utils.CHARGING
161159
)
162160
motor.vibrate()
163161
else:
164162
StatusBar.get_instance().show_usb(False)
165163
# deal with charging state
166-
CHARGING = False
164+
utils.CHARGING = False
167165
StatusBar.get_instance().show_charging()
168166
if utils.BATTERY_CAP:
169167
StatusBar.get_instance().set_battery_img(
170-
utils.BATTERY_CAP, CHARGING
168+
utils.BATTERY_CAP, utils.CHARGING
171169
)
172170
_request_charging_status()
173171
current_usb_bus_state = usb.bus.state()
@@ -250,7 +248,7 @@ async def process_push() -> None:
250248
# current battery level, 0-100 only effective when not charging
251249
res = ustruct.unpack(">B", value)[0]
252250
utils.BATTERY_CAP = res
253-
StatusBar.get_instance().set_battery_img(res, CHARGING)
251+
StatusBar.get_instance().set_battery_img(res, utils.CHARGING)
254252

255253
elif cmd == _CMD_SIDE_BUTTON_PRESS:
256254
# 1 short press 2 long press
@@ -341,27 +339,30 @@ async def _deal_button_press(value: bytes) -> None:
341339
BUTTON_PRESSING = False
342340

343341

342+
def _turn_on_lcd():
343+
if display.backlight() == 0:
344+
utils.lcd_resume()
345+
346+
344347
async def _deal_charging_state(value: bytes) -> None:
345348
"""THIS DOESN'T WORK CORRECT DUE TO THE PUSHED STATE, ONLY USED AS A FALLBACK WHEN
346349
CHARGING WITH A CHARGER NOW.
347350
348351
"""
349-
global CHARGING, CHARING_TYPE
352+
global CHARING_TYPE
350353
res, CHARING_TYPE = ustruct.unpack(">BB", value)
351354

352-
if display.backlight() == 0:
353-
utils.lcd_resume()
354-
355355
if res in (
356356
CHARGE_START,
357357
_POWER_STATUS_CHARGING,
358358
):
359-
if CHARGING:
359+
if utils.CHARGING:
360360
return
361-
CHARGING = True
361+
_turn_on_lcd()
362+
utils.CHARGING = True
362363
StatusBar.get_instance().show_charging(True)
363364
if utils.BATTERY_CAP:
364-
StatusBar.get_instance().set_battery_img(utils.BATTERY_CAP, CHARGING)
365+
StatusBar.get_instance().set_battery_img(utils.BATTERY_CAP, utils.CHARGING)
365366
if CHARING_TYPE == CHARGE_BY_WIRELESS:
366367
if utils.CHARGEING_BY_WIRELESS:
367368
return
@@ -390,13 +391,15 @@ async def _deal_charging_state(value: bytes) -> None:
390391
elif res in (_USB_STATUS_PLUG_OUT, _POWER_STATUS_CHARGING_FINISHED):
391392
if utils.CHARGEING_BY_WIRELESS:
392393
utils.CHARGEING_BY_WIRELESS = False
393-
# if not CHARGING:
394+
# if not utils.CHARGING:
394395
# return
395-
CHARGING = False
396+
utils.CHARGING = False
397+
ctrl_charge_switch(True)
398+
_turn_on_lcd()
396399
StatusBar.get_instance().show_charging(False)
397400
StatusBar.get_instance().show_usb(False)
398401
if utils.BATTERY_CAP:
399-
StatusBar.get_instance().set_battery_img(utils.BATTERY_CAP, CHARGING)
402+
StatusBar.get_instance().set_battery_img(utils.BATTERY_CAP, utils.CHARGING)
400403

401404

402405
async def _deal_pair_res(value: bytes) -> None:
@@ -501,10 +504,12 @@ def _request_charging_status():
501504
"""Request charging status."""
502505
BLE_CTRL.ctrl(0x82, b"\x05")
503506

507+
504508
def disconnect_ble():
505509
if utils.BLE_CONNECTED:
506510
BLE_CTRL.ctrl(0x81, b"\x03")
507511

512+
508513
async def fetch_all():
509514
"""Request some important data."""
510515
while True:
@@ -627,6 +632,7 @@ def ctrl_charge_switch(enable: bool) -> None:
627632
BLE_CTRL.ctrl(0x82, b"\x07")
628633
utils.CHARGE_ENABLE = False
629634

635+
630636
def ctrl_wireless_charge(enable: bool) -> None:
631637
"""Request to open or close charge.
632638
@param enable: True to open, False to close

core/src/trezor/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def board_version() -> str:
7474
_COLOR_FLAG: str | None = None
7575
CHARGEING_BY_WIRELESS = False
7676
CHARGE_ENABLE = True
77+
CHARGING = False
7778

7879
if __debug__:
7980
MAX_FP_ATTEMPTS = 50

0 commit comments

Comments
 (0)