python: common base handle for SPI + USB (#1231)

* base handle

* usb handle

* rm

* more types
This commit is contained in:
Adeeb Shihadeh
2023-01-28 16:01:54 -08:00
committed by GitHub
parent e7f36a2992
commit 6ec0c80754
4 changed files with 59 additions and 4 deletions

View File

@@ -16,7 +16,8 @@ from itertools import accumulate
from .constants import McuType
from .dfu import PandaDFU
from .isotp import isotp_send, isotp_recv
from .spi import SpiHandle, PandaSpiException
from .spi import PandaSpiHandle, PandaSpiException
from .usb import PandaUsbHandle
__version__ = '0.0.10'
@@ -291,7 +292,7 @@ class Panda:
handle = None
spi_serial = None
try:
handle = SpiHandle()
handle = PandaSpiHandle()
dat = handle.controlRead(Panda.REQUEST_IN, 0xc3, 0, 0, 12)
spi_serial = binascii.hexlify(dat).decode()
except PandaSpiException:
@@ -342,7 +343,11 @@ class Panda:
break
context = usb1.USBContext() # New context needed so new devices show up
return handle, usb_serial, bootstub, bcd
usb_handle = None
if handle is not None:
usb_handle = PandaUsbHandle(handle)
return usb_handle, usb_serial, bootstub, bcd
@staticmethod
def list():

25
python/base.py Normal file
View File

@@ -0,0 +1,25 @@
from abc import ABC, abstractmethod
from typing import List
# This mimics the handle given by libusb1 for easy interoperability
class BaseHandle(ABC):
@abstractmethod
def close(self) -> None:
...
@abstractmethod
def controlWrite(self, request_type: int, request: int, value: int, index: int, data, timeout: int = 0) -> int:
...
@abstractmethod
def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = 0) -> bytes:
...
@abstractmethod
def bulkWrite(self, endpoint: int, data: List[int], timeout: int = 0) -> int:
...
@abstractmethod
def bulkRead(self, endpoint: int, length: int, timeout: int = 0) -> bytes:
...

View File

@@ -9,6 +9,8 @@ from contextlib import contextmanager
from functools import reduce
from typing import List
from .base import BaseHandle
try:
import spidev
except ImportError:
@@ -78,7 +80,7 @@ class SpiDevice:
self._spidev.close()
class SpiHandle:
class PandaSpiHandle(BaseHandle):
"""
A class that mimics a libusb1 handle for panda SPI communications.
"""

23
python/usb.py Normal file
View File

@@ -0,0 +1,23 @@
from typing import List
from .base import BaseHandle
class PandaUsbHandle(BaseHandle):
def __init__(self, libusb_handle):
self._libusb_handle = libusb_handle
def close(self):
self._libusb_handle.close()
def controlWrite(self, request_type: int, request: int, value: int, index: int, data, timeout: int = 0):
return self._libusb_handle.controlWrite(request_type, request, value, index, data, timeout)
def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = 0):
return self._libusb_handle.controlRead(request_type, request, value, index, length, timeout)
def bulkWrite(self, endpoint: int, data: List[int], timeout: int = 0) -> int:
return self._libusb_handle.bulkWrite(endpoint, data, timeout)
def bulkRead(self, endpoint: int, length: int, timeout: int = 0) -> bytes:
return self._libusb_handle.bulkRead(endpoint, length, timeout)