From ebcb4a0cb25cff2c326eff2b9ce607ede238c84b Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Fri, 11 Sep 2020 08:58:45 -0700 Subject: [PATCH] Implemented simple chip id and board id caching --- adafruit_platformdetect/board.py | 7 +++++++ adafruit_platformdetect/chip.py | 30 ++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index 538e0b32..f0337394 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -50,6 +50,7 @@ class Board: def __init__(self, detector): self.detector = detector + self._board_id = None # pylint: disable=invalid-name, protected-access @property @@ -57,6 +58,11 @@ def id(self): """Return a unique id for the detected board, if any.""" # There are some times we want to trick the platform detection # say if a raspberry pi doesn't have the right ID, or for testing + + # Caching + if self._board_id: + return self._board_id + try: return os.environ["BLINKA_FORCEBOARD"] except KeyError: # no forced board, continue with testing! @@ -128,6 +134,7 @@ def id(self): elif chip_id == chips.STM32MP157: board_id = self._stm32mp1_id() + self._board_id = board_id return board_id # pylint: enable=invalid-name diff --git a/adafruit_platformdetect/chip.py b/adafruit_platformdetect/chip.py index 3c912327..eb3ce5b2 100644 --- a/adafruit_platformdetect/chip.py +++ b/adafruit_platformdetect/chip.py @@ -51,6 +51,7 @@ class Chip: def __init__(self, detector): self.detector = detector + self._chip_id = None @property def id( @@ -59,6 +60,11 @@ def id( """Return a unique id for the detected chip, if any.""" # There are some times we want to trick the platform detection # say if a raspberry pi doesn't have the right ID, or for testing + + # Caching + if self._chip_id: + return self._chip_id + try: return os.environ["BLINKA_FORCECHIP"] except KeyError: # no forced chip, continue with testing! @@ -75,14 +81,16 @@ def id( "BLINKA_FT232H environment variable " + "set, but no FT232H device found" ) - return chips.FT232H + self._chip_id = chips.FT232H + return self._chip_id if os.environ.get("BLINKA_MCP2221"): import hid # look for it based on PID/VID for dev in hid.enumerate(): if dev["vendor_id"] == 0x04D8 and dev["product_id"] == 0x00DD: - return chips.MCP2221 + self._chip_id = chips.MCP2221 + return self._chip_id raise RuntimeError( "BLINKA_MCP2221 environment variable " + "set, but no MCP2221 device found" @@ -91,23 +99,29 @@ def id( import usb if usb.core.find(idVendor=0x1D50, idProduct=0x60E6) is not None: - return chips.LPC4330 + self._chip_id = chips.LPC4330 + return self._chip_id raise RuntimeError( "BLINKA_GREATFET environment variable " + "set, but no GreatFET device found" ) if os.environ.get("BLINKA_NOVA"): - return chips.BINHO + self._chip_id = chips.BINHO + return self._chip_id platform = sys.platform if platform in ("linux", "linux2"): - return self._linux_id() + self._chip_id = self._linux_id() + return self._chip_id if platform == "esp8266": - return chips.ESP8266 + self._chip_id = chips.ESP8266 + return self._chip_id if platform == "samd21": - return chips.SAMD21 + self._chip_id = chips.SAMD21 + return self._chip_id if platform == "pyboard": - return chips.STM32F405 + self._chip_id = chips.STM32F405 + return self._chip_id # nothing found! return None