@@ -51,6 +51,7 @@ class Chip:
51
51
52
52
def __init__ (self , detector ):
53
53
self .detector = detector
54
+ self ._chip_id = None
54
55
55
56
@property
56
57
def id (
@@ -59,6 +60,11 @@ def id(
59
60
"""Return a unique id for the detected chip, if any."""
60
61
# There are some times we want to trick the platform detection
61
62
# 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
+
62
68
try :
63
69
return os .environ ["BLINKA_FORCECHIP" ]
64
70
except KeyError : # no forced chip, continue with testing!
@@ -75,14 +81,16 @@ def id(
75
81
"BLINKA_FT232H environment variable "
76
82
+ "set, but no FT232H device found"
77
83
)
78
- return chips .FT232H
84
+ self ._chip_id = chips .FT232H
85
+ return self ._chip_id
79
86
if os .environ .get ("BLINKA_MCP2221" ):
80
87
import hid
81
88
82
89
# look for it based on PID/VID
83
90
for dev in hid .enumerate ():
84
91
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
86
94
raise RuntimeError (
87
95
"BLINKA_MCP2221 environment variable "
88
96
+ "set, but no MCP2221 device found"
@@ -91,23 +99,29 @@ def id(
91
99
import usb
92
100
93
101
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
95
104
raise RuntimeError (
96
105
"BLINKA_GREATFET environment variable "
97
106
+ "set, but no GreatFET device found"
98
107
)
99
108
if os .environ .get ("BLINKA_NOVA" ):
100
- return chips .BINHO
109
+ self ._chip_id = chips .BINHO
110
+ return self ._chip_id
101
111
102
112
platform = sys .platform
103
113
if platform in ("linux" , "linux2" ):
104
- return self ._linux_id ()
114
+ self ._chip_id = self ._linux_id ()
115
+ return self ._chip_id
105
116
if platform == "esp8266" :
106
- return chips .ESP8266
117
+ self ._chip_id = chips .ESP8266
118
+ return self ._chip_id
107
119
if platform == "samd21" :
108
- return chips .SAMD21
120
+ self ._chip_id = chips .SAMD21
121
+ return self ._chip_id
109
122
if platform == "pyboard" :
110
- return chips .STM32F405
123
+ self ._chip_id = chips .STM32F405
124
+ return self ._chip_id
111
125
# nothing found!
112
126
return None
113
127
0 commit comments