Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1658eb92b3 | |||
| 94ef650f80 |
+6
-2
@@ -25,6 +25,7 @@ from .RescuePicoKey import RescuePicoKey
|
||||
from .RescueMonitor import RescueMonitor, RescueMonitorObserver
|
||||
from .PhyData import PhyData
|
||||
from .core import NamedIntEnum
|
||||
import usb.core
|
||||
|
||||
class Platform(NamedIntEnum):
|
||||
RP2040 = 0
|
||||
@@ -130,7 +131,7 @@ class PicoKey:
|
||||
def __init__(self, device):
|
||||
self.__device = device
|
||||
|
||||
def update(self, actions):
|
||||
def update(self, actions: tuple[Optional[usb.core.Device], Optional[usb.core.Device]]):
|
||||
(connected, disconnected) = actions
|
||||
if connected:
|
||||
pass
|
||||
@@ -139,7 +140,7 @@ class PicoKey:
|
||||
try:
|
||||
self.__card = RescuePicoKey()
|
||||
self.__connection_type = ConnectionType.RESCUE
|
||||
self.__observer = PicoRescueObserver(self.__card)
|
||||
self.__observer = PicoRescueObserver(self)
|
||||
self.__monitor = RescueMonitor(device=self.__card, cls_callback=self.__observer)
|
||||
except Exception:
|
||||
raise Exception('time-out: no card inserted')
|
||||
@@ -169,6 +170,9 @@ class PicoKey:
|
||||
if (not self.__card):
|
||||
return
|
||||
if isinstance(self.__card, RescuePicoKey):
|
||||
self.__monitor.stop()
|
||||
self.__monitor = None
|
||||
self.__observer = None
|
||||
self.__card.close()
|
||||
else:
|
||||
self.__card.disconnect()
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
import usb.core
|
||||
import threading
|
||||
import time
|
||||
@@ -26,15 +27,15 @@ class RescueMonitorObserver:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def notifyObservers(self, actions):
|
||||
def notifyObservers(self, actions: tuple[Optional[usb.core.Device], Optional[usb.core.Device]]):
|
||||
func = getattr(self, "update", None)
|
||||
if callable(func):
|
||||
func(actions)
|
||||
|
||||
def on_connect(self, device):
|
||||
def on_connect(self, device: Optional[usb.core.Device]):
|
||||
self.notifyObservers((device, None))
|
||||
|
||||
def on_disconnect(self, device):
|
||||
def on_disconnect(self, device: Optional[usb.core.Device]):
|
||||
self.notifyObservers((None, device))
|
||||
|
||||
class RescueMonitor:
|
||||
@@ -56,8 +57,8 @@ class RescueMonitor:
|
||||
|
||||
def stop(self):
|
||||
self._running = False
|
||||
if self._thread:
|
||||
self._thread.join()
|
||||
#if self._thread:
|
||||
# self._thread.join()
|
||||
|
||||
def _run(self):
|
||||
while self._running:
|
||||
@@ -73,6 +74,6 @@ class RescueMonitor:
|
||||
# Device disconnected
|
||||
self._device_present = False
|
||||
if self._cls_callback:
|
||||
self._cls_callback.on_disconnect(self._dev)
|
||||
self._cls_callback.on_disconnect(self._dev.device)
|
||||
|
||||
time.sleep(self.interval)
|
||||
|
||||
@@ -86,6 +86,10 @@ class RescuePicoKey:
|
||||
def close(self):
|
||||
if self.__dev:
|
||||
usb.util.dispose_resources(self.__dev)
|
||||
self.__dev = None
|
||||
|
||||
def has_card(self):
|
||||
return self.__dev is not None
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
self.close()
|
||||
|
||||
+1
-1
@@ -18,4 +18,4 @@
|
||||
*/
|
||||
"""
|
||||
|
||||
__version__ = "1.1.6"
|
||||
__version__ = "1.1.7"
|
||||
|
||||
Reference in New Issue
Block a user