Skip to content

Commit ebcb4a0

Browse files
committed
Implemented simple chip id and board id caching
1 parent 699f911 commit ebcb4a0

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

adafruit_platformdetect/board.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,19 @@ class Board:
5050

5151
def __init__(self, detector):
5252
self.detector = detector
53+
self._board_id = None
5354

5455
# pylint: disable=invalid-name, protected-access
5556
@property
5657
def id(self):
5758
"""Return a unique id for the detected board, if any."""
5859
# There are some times we want to trick the platform detection
5960
# say if a raspberry pi doesn't have the right ID, or for testing
61+
62+
# Caching
63+
if self._board_id:
64+
return self._board_id
65+
6066
try:
6167
return os.environ["BLINKA_FORCEBOARD"]
6268
except KeyError: # no forced board, continue with testing!
@@ -128,6 +134,7 @@ def id(self):
128134
elif chip_id == chips.STM32MP157:
129135
board_id = self._stm32mp1_id()
130136

137+
self._board_id = board_id
131138
return board_id
132139

133140
# pylint: enable=invalid-name

adafruit_platformdetect/chip.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Chip:
5151

5252
def __init__(self, detector):
5353
self.detector = detector
54+
self._chip_id = None
5455

5556
@property
5657
def id(
@@ -59,6 +60,11 @@ def id(
5960
"""Return a unique id for the detected chip, if any."""
6061
# There are some times we want to trick the platform detection
6162
# say if a raspberry pi doesn't have the right ID, or for testing
63+
64+
# Caching
65+
if self._chip_id:
66+
return self._chip_id
67+
6268
try:
6369
return os.environ["BLINKA_FORCECHIP"]
6470
except KeyError: # no forced chip, continue with testing!
@@ -75,14 +81,16 @@ def id(
7581
"BLINKA_FT232H environment variable "
7682
+ "set, but no FT232H device found"
7783
)
78-
return chips.FT232H
84+
self._chip_id = chips.FT232H
85+
return self._chip_id
7986
if os.environ.get("BLINKA_MCP2221"):
8087
import hid
8188

8289
# look for it based on PID/VID
8390
for dev in hid.enumerate():
8491
if dev["vendor_id"] == 0x04D8 and dev["product_id"] == 0x00DD:
85-
return chips.MCP2221
92+
self._chip_id = chips.MCP2221
93+
return self._chip_id
8694
raise RuntimeError(
8795
"BLINKA_MCP2221 environment variable "
8896
+ "set, but no MCP2221 device found"
@@ -91,23 +99,29 @@ def id(
9199
import usb
92100

93101
if usb.core.find(idVendor=0x1D50, idProduct=0x60E6) is not None:
94-
return chips.LPC4330
102+
self._chip_id = chips.LPC4330
103+
return self._chip_id
95104
raise RuntimeError(
96105
"BLINKA_GREATFET environment variable "
97106
+ "set, but no GreatFET device found"
98107
)
99108
if os.environ.get("BLINKA_NOVA"):
100-
return chips.BINHO
109+
self._chip_id = chips.BINHO
110+
return self._chip_id
101111

102112
platform = sys.platform
103113
if platform in ("linux", "linux2"):
104-
return self._linux_id()
114+
self._chip_id = self._linux_id()
115+
return self._chip_id
105116
if platform == "esp8266":
106-
return chips.ESP8266
117+
self._chip_id = chips.ESP8266
118+
return self._chip_id
107119
if platform == "samd21":
108-
return chips.SAMD21
120+
self._chip_id = chips.SAMD21
121+
return self._chip_id
109122
if platform == "pyboard":
110-
return chips.STM32F405
123+
self._chip_id = chips.STM32F405
124+
return self._chip_id
111125
# nothing found!
112126
return None
113127

0 commit comments

Comments
 (0)