Skip to content

Commit a8ea1eb

Browse files
committed
Update I2C revision detection to use logic from newer GPIO library. Fixes bug with A+ misidentification.
1 parent ab9b67b commit a8ea1eb

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

Adafruit_I2C/Adafruit_I2C.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/python
2-
2+
import re
33
import smbus
44

55
# ===========================================================================
@@ -11,14 +11,21 @@ class Adafruit_I2C(object):
1111
@staticmethod
1212
def getPiRevision():
1313
"Gets the version number of the Raspberry Pi board"
14-
# Courtesy quick2wire-python-api
15-
# https://github.com/quick2wire/quick2wire-python-api
16-
# Updated revision info from: http://elinux.org/RPi_HardwareHistory#Board_Revision_History
14+
# Revision list available at: http://elinux.org/RPi_HardwareHistory#Board_Revision_History
1715
try:
18-
with open('/proc/cpuinfo','r') as f:
19-
for line in f:
20-
if line.startswith('Revision'):
21-
return 1 if line.rstrip()[-1] in ['2','3'] else 2
16+
with open('/proc/cpuinfo', 'r') as infile:
17+
for line in infile:
18+
# Match a line of the form "Revision : 0002" while ignoring extra
19+
# info in front of the revsion (like 1000 when the Pi was over-volted).
20+
match = re.match('Revision\s+:\s+.*(\w{4})$', line)
21+
if match and match.group(1) in ['0000', '0002', '0003']:
22+
# Return revision 1 if revision ends with 0000, 0002 or 0003.
23+
return 1
24+
elif match:
25+
# Assume revision 2 if revision ends with any other 4 chars.
26+
return 2
27+
# Couldn't find the revision, assume revision 0 like older code for compatibility.
28+
return 0
2229
except:
2330
return 0
2431

0 commit comments

Comments
 (0)