34
34
_CMD_LED_BRIGHTNESS = const (12 )
35
35
_CMD_BLE_BUILD_ID = const (16 )
36
36
_CMD_BLE_HASH = const (17 )
37
- CHARGING = False
38
37
CHARING_TYPE = 0 # 1 VIA USB / 2 VIA WIRELESS
39
38
SCREEN : PairCodeDisplay | None = None
40
39
BLE_ENABLED : bool | None = None
@@ -139,7 +138,6 @@ async def handle_fingerprint():
139
138
140
139
141
140
async def handle_usb_state ():
142
- global CHARGING
143
141
while True :
144
142
try :
145
143
previous_usb_bus_state = usb .bus .state ()
@@ -153,21 +151,21 @@ async def handle_usb_state():
153
151
# prompt.show()
154
152
StatusBar .get_instance ().show_usb (True )
155
153
# deal with charging state
156
- CHARGING = True
154
+ utils . CHARGING = True
157
155
StatusBar .get_instance ().show_charging (True )
158
156
if utils .BATTERY_CAP :
159
157
StatusBar .get_instance ().set_battery_img (
160
- utils .BATTERY_CAP , CHARGING
158
+ utils .BATTERY_CAP , utils . CHARGING
161
159
)
162
160
motor .vibrate ()
163
161
else :
164
162
StatusBar .get_instance ().show_usb (False )
165
163
# deal with charging state
166
- CHARGING = False
164
+ utils . CHARGING = False
167
165
StatusBar .get_instance ().show_charging ()
168
166
if utils .BATTERY_CAP :
169
167
StatusBar .get_instance ().set_battery_img (
170
- utils .BATTERY_CAP , CHARGING
168
+ utils .BATTERY_CAP , utils . CHARGING
171
169
)
172
170
_request_charging_status ()
173
171
current_usb_bus_state = usb .bus .state ()
@@ -250,7 +248,7 @@ async def process_push() -> None:
250
248
# current battery level, 0-100 only effective when not charging
251
249
res = ustruct .unpack (">B" , value )[0 ]
252
250
utils .BATTERY_CAP = res
253
- StatusBar .get_instance ().set_battery_img (res , CHARGING )
251
+ StatusBar .get_instance ().set_battery_img (res , utils . CHARGING )
254
252
255
253
elif cmd == _CMD_SIDE_BUTTON_PRESS :
256
254
# 1 short press 2 long press
@@ -341,27 +339,30 @@ async def _deal_button_press(value: bytes) -> None:
341
339
BUTTON_PRESSING = False
342
340
343
341
342
+ def _turn_on_lcd ():
343
+ if display .backlight () == 0 :
344
+ utils .lcd_resume ()
345
+
346
+
344
347
async def _deal_charging_state (value : bytes ) -> None :
345
348
"""THIS DOESN'T WORK CORRECT DUE TO THE PUSHED STATE, ONLY USED AS A FALLBACK WHEN
346
349
CHARGING WITH A CHARGER NOW.
347
350
348
351
"""
349
- global CHARGING , CHARING_TYPE
352
+ global CHARING_TYPE
350
353
res , CHARING_TYPE = ustruct .unpack (">BB" , value )
351
354
352
- if display .backlight () == 0 :
353
- utils .lcd_resume ()
354
-
355
355
if res in (
356
356
CHARGE_START ,
357
357
_POWER_STATUS_CHARGING ,
358
358
):
359
- if CHARGING :
359
+ if utils . CHARGING :
360
360
return
361
- CHARGING = True
361
+ _turn_on_lcd ()
362
+ utils .CHARGING = True
362
363
StatusBar .get_instance ().show_charging (True )
363
364
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 )
365
366
if CHARING_TYPE == CHARGE_BY_WIRELESS :
366
367
if utils .CHARGEING_BY_WIRELESS :
367
368
return
@@ -390,13 +391,15 @@ async def _deal_charging_state(value: bytes) -> None:
390
391
elif res in (_USB_STATUS_PLUG_OUT , _POWER_STATUS_CHARGING_FINISHED ):
391
392
if utils .CHARGEING_BY_WIRELESS :
392
393
utils .CHARGEING_BY_WIRELESS = False
393
- # if not CHARGING:
394
+ # if not utils. CHARGING:
394
395
# return
395
- CHARGING = False
396
+ utils .CHARGING = False
397
+ ctrl_charge_switch (True )
398
+ _turn_on_lcd ()
396
399
StatusBar .get_instance ().show_charging (False )
397
400
StatusBar .get_instance ().show_usb (False )
398
401
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 )
400
403
401
404
402
405
async def _deal_pair_res (value : bytes ) -> None :
@@ -501,10 +504,12 @@ def _request_charging_status():
501
504
"""Request charging status."""
502
505
BLE_CTRL .ctrl (0x82 , b"\x05 " )
503
506
507
+
504
508
def disconnect_ble ():
505
509
if utils .BLE_CONNECTED :
506
510
BLE_CTRL .ctrl (0x81 , b"\x03 " )
507
511
512
+
508
513
async def fetch_all ():
509
514
"""Request some important data."""
510
515
while True :
@@ -627,6 +632,7 @@ def ctrl_charge_switch(enable: bool) -> None:
627
632
BLE_CTRL .ctrl (0x82 , b"\x07 " )
628
633
utils .CHARGE_ENABLE = False
629
634
635
+
630
636
def ctrl_wireless_charge (enable : bool ) -> None :
631
637
"""Request to open or close charge.
632
638
@param enable: True to open, False to close
0 commit comments