2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
5
- # Class to help with Raspberry Pi Rev Codes
6
- # Written by Melissa LeBlanc-Williams for Adafruit Industries
7
- #
8
- # Data values from https://github.com/raspberrypi/documentation/blob/develop/
9
- # documentation/asciidoc/computers/raspberry-pi/revision-codes.adoc#new-style-revision-codes
5
+ """
6
+ `adafruit_platformdetect.revcodes`
7
+ ================================================================================
8
+
9
+ Class to help with Raspberry Pi Rev Codes
10
+
11
+ * Author(s): Melissa LeBlanc-Williams
12
+
13
+ Implementation Notes
14
+ --------------------
15
+
16
+ **Software and Dependencies:**
17
+
18
+ * Linux and Python 3.7 or Higher
19
+
20
+ Data values from https://github.com/raspberrypi/documentation/blob/develop/
21
+ documentation/asciidoc/computers/raspberry-pi/revision-codes.adoc#new-style-revision-codes
22
+
23
+ """
10
24
11
25
NEW_OVERVOLTAGE = (
12
26
"Overvoltage allowed" ,
135
149
0x15 : (2 , 1.1 , 2 , 2 ),
136
150
}
137
151
152
+
138
153
class PiDecoder :
154
+ """Raspberry Pi Revision Code Decoder"""
155
+
139
156
def __init__ (self , rev_code ):
140
157
try :
141
158
self .rev_code = int (rev_code , 16 ) & 0xFFFFFFFF
142
159
except ValueError :
143
160
print ("Invalid revision code. It should be a hexadecimal value." )
144
161
145
162
def is_valid_code (self ):
146
- # This is a check intended to quickly check the validity of a code
163
+ """Quickly check the validity of a code"""
147
164
if self .is_new_format ():
148
- for format in NEW_REV_STRUCTURE .values ():
149
- lower_bit , bit_size , values = format
165
+ for code_format in NEW_REV_STRUCTURE .values ():
166
+ lower_bit , bit_size , values = code_format
150
167
prop_value = (self .rev_code >> lower_bit ) & ((1 << bit_size ) - 1 )
151
168
if not self ._valid_value (prop_value , values ):
152
169
return False
153
170
else :
154
171
if (self .rev_code & 0xFFFF ) not in OLD_REV_LUT .keys ():
155
172
return False
156
- for format in OLD_REV_STRUCTURE .values ():
157
- index , values = format
158
- format = OLD_REV_LUT [self .rev_code & 0xFFFF ]
159
- if index >= len (format ):
173
+ for code_format in OLD_REV_STRUCTURE .values ():
174
+ index , values = code_format
175
+ code_format = OLD_REV_LUT [self .rev_code & 0xFFFF ]
176
+ if index >= len (code_format ):
160
177
return False
161
- if not self ._valid_value (format [index ], values ):
178
+ if not self ._valid_value (code_format [index ], values ):
162
179
return False
163
180
return True
164
181
165
- def _get_rev_prop_value (self , name , structure = NEW_REV_STRUCTURE , raw = False ):
182
+ def _get_rev_prop_value (self , name , structure = None , raw = False ):
183
+ if structure is None :
184
+ structure = NEW_REV_STRUCTURE
166
185
if name not in structure .keys ():
167
186
raise ValueError (f"Unknown property { name } " )
168
187
lower_bit , bit_size , values = structure [name ]
@@ -189,12 +208,14 @@ def _get_old_rev_prop_value(self, name, raw=False):
189
208
return data [index ]
190
209
return self ._format_value (data [index ], values )
191
210
192
- def _format_value (self , value , valid_values ):
211
+ @staticmethod
212
+ def _format_value (value , valid_values ):
193
213
if valid_values is float or valid_values is int :
194
214
return valid_values (value )
195
215
return valid_values [value ]
196
216
197
- def _valid_value (self , value , valid_values ):
217
+ @staticmethod
218
+ def _valid_value (value , valid_values ):
198
219
if valid_values is float or valid_values is int :
199
220
return isinstance (value , valid_values )
200
221
if isinstance (valid_values , (tuple , list )) and 0 <= value < len (valid_values ):
@@ -208,57 +229,68 @@ def _get_property(self, name, raw=False):
208
229
raise ValueError (f"Unknown property { name } " )
209
230
if self .is_new_format ():
210
231
return self ._get_rev_prop_value (name , raw = raw )
211
- elif name in OLD_REV_EXTRA_PROPS :
232
+ if name in OLD_REV_EXTRA_PROPS :
212
233
return self ._get_rev_prop_value (
213
234
name , structure = OLD_REV_EXTRA_PROPS , raw = raw
214
235
)
215
- else :
216
- return self ._get_old_rev_prop_value (name , raw = raw )
236
+ return self ._get_old_rev_prop_value (name , raw = raw )
217
237
218
238
def is_new_format (self ):
239
+ """Check if the code is in the new format"""
219
240
return self ._get_rev_prop_value ("rev_style" , raw = True ) == 1
220
241
221
242
@property
222
243
def overvoltage (self ):
244
+ """Overvoltage allowed/disallowed"""
223
245
return self ._get_property ("overvoltage" )
224
246
225
247
@property
226
248
def warranty_bit (self ):
249
+ """Warranty bit"""
227
250
return self ._get_property ("warranty" )
228
251
229
252
@property
230
253
def otp_program (self ):
254
+ """OTP programming allowed/disallowed"""
231
255
return self ._get_property ("otp_program" )
232
256
233
257
@property
234
258
def otp_read (self ):
259
+ """OTP reading allowed/disallowed"""
235
260
return self ._get_property ("otp_read" )
236
261
237
262
@property
238
263
def rev_style (self ):
264
+ """Revision Code style"""
239
265
# Force new style for Rev Style
240
266
return self ._get_rev_prop_value ("rev_style" )
241
267
242
268
@property
243
269
def memory_size (self ):
270
+ """Memory size"""
244
271
return self ._get_property ("memory_size" )
245
272
246
273
@property
247
274
def manufacturer (self ):
275
+ """Manufacturer"""
248
276
return self ._get_property ("manufacturer" )
249
277
250
278
@property
251
279
def processor (self ):
280
+ """Processor"""
252
281
return self ._get_property ("processor" )
253
282
254
283
@property
255
284
def type (self ):
285
+ """Specific Model"""
256
286
return self ._get_property ("type" )
257
287
258
288
@property
259
289
def type_raw (self ):
260
- return self ._get_property ("type" , raw = True )
290
+ """Raw Value of Specific Model"""
291
+ return self ._get_property ("type" , raw = True )
261
292
262
293
@property
263
294
def revision (self ):
295
+ """Revision Number"""
264
296
return self ._get_property ("revision" )
0 commit comments