22#
33# SPDX-License-Identifier: MIT
44
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+ """
1024
1125NEW_OVERVOLTAGE = (
1226 "Overvoltage allowed" ,
135149 0x15 : (2 , 1.1 , 2 , 2 ),
136150}
137151
152+
138153class PiDecoder :
154+ """Raspberry Pi Revision Code Decoder"""
155+
139156 def __init__ (self , rev_code ):
140157 try :
141158 self .rev_code = int (rev_code , 16 ) & 0xFFFFFFFF
142159 except ValueError :
143160 print ("Invalid revision code. It should be a hexadecimal value." )
144161
145162 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"""
147164 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
150167 prop_value = (self .rev_code >> lower_bit ) & ((1 << bit_size ) - 1 )
151168 if not self ._valid_value (prop_value , values ):
152169 return False
153170 else :
154171 if (self .rev_code & 0xFFFF ) not in OLD_REV_LUT .keys ():
155172 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 ):
160177 return False
161- if not self ._valid_value (format [index ], values ):
178+ if not self ._valid_value (code_format [index ], values ):
162179 return False
163180 return True
164181
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
166185 if name not in structure .keys ():
167186 raise ValueError (f"Unknown property { name } " )
168187 lower_bit , bit_size , values = structure [name ]
@@ -189,12 +208,14 @@ def _get_old_rev_prop_value(self, name, raw=False):
189208 return data [index ]
190209 return self ._format_value (data [index ], values )
191210
192- def _format_value (self , value , valid_values ):
211+ @staticmethod
212+ def _format_value (value , valid_values ):
193213 if valid_values is float or valid_values is int :
194214 return valid_values (value )
195215 return valid_values [value ]
196216
197- def _valid_value (self , value , valid_values ):
217+ @staticmethod
218+ def _valid_value (value , valid_values ):
198219 if valid_values is float or valid_values is int :
199220 return isinstance (value , valid_values )
200221 if isinstance (valid_values , (tuple , list )) and 0 <= value < len (valid_values ):
@@ -208,57 +229,68 @@ def _get_property(self, name, raw=False):
208229 raise ValueError (f"Unknown property { name } " )
209230 if self .is_new_format ():
210231 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 :
212233 return self ._get_rev_prop_value (
213234 name , structure = OLD_REV_EXTRA_PROPS , raw = raw
214235 )
215- else :
216- return self ._get_old_rev_prop_value (name , raw = raw )
236+ return self ._get_old_rev_prop_value (name , raw = raw )
217237
218238 def is_new_format (self ):
239+ """Check if the code is in the new format"""
219240 return self ._get_rev_prop_value ("rev_style" , raw = True ) == 1
220241
221242 @property
222243 def overvoltage (self ):
244+ """Overvoltage allowed/disallowed"""
223245 return self ._get_property ("overvoltage" )
224246
225247 @property
226248 def warranty_bit (self ):
249+ """Warranty bit"""
227250 return self ._get_property ("warranty" )
228251
229252 @property
230253 def otp_program (self ):
254+ """OTP programming allowed/disallowed"""
231255 return self ._get_property ("otp_program" )
232256
233257 @property
234258 def otp_read (self ):
259+ """OTP reading allowed/disallowed"""
235260 return self ._get_property ("otp_read" )
236261
237262 @property
238263 def rev_style (self ):
264+ """Revision Code style"""
239265 # Force new style for Rev Style
240266 return self ._get_rev_prop_value ("rev_style" )
241267
242268 @property
243269 def memory_size (self ):
270+ """Memory size"""
244271 return self ._get_property ("memory_size" )
245272
246273 @property
247274 def manufacturer (self ):
275+ """Manufacturer"""
248276 return self ._get_property ("manufacturer" )
249277
250278 @property
251279 def processor (self ):
280+ """Processor"""
252281 return self ._get_property ("processor" )
253282
254283 @property
255284 def type (self ):
285+ """Specific Model"""
256286 return self ._get_property ("type" )
257287
258288 @property
259289 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 )
261292
262293 @property
263294 def revision (self ):
295+ """Revision Number"""
264296 return self ._get_property ("revision" )
0 commit comments