Skip to content

Commit d4349bc

Browse files
author
Dexter Industries
authored
Merge pull request #13 from RobertLucian/feature/static-module-for-autodetect-rpi
module for auto-detecting [rpi model]
2 parents 0662835 + 8e6643b commit d4349bc

2 files changed

Lines changed: 146 additions & 1 deletion

File tree

auto_detect_rpi.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
'''
2+
## License
3+
4+
The MIT License (MIT)
5+
6+
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
7+
Copyright (C) 2017 Dexter Industries
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
'''
27+
28+
# RPI_VARIANTS was inspired from http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version/
29+
# This module is meant for retrieving the Raspberry Pi's generation model, PCB model (dimension-wise) and PCB revision
30+
# Works with Python 3 & 2 !!!
31+
32+
# Each key represents the hardware revision number
33+
# This isn't the same as the RaspberryPi revision
34+
# Having the hardware revision number is useful when working with hardware or software.
35+
36+
RPI_VARIANTS = {
37+
"0002" : ["Model B Rev 1", "RPI1"],
38+
39+
"0003" : ["Model B Rev 1 ECN0001 (no fuses, D14 removed)", "RPI1"],
40+
41+
"0004" : ["Model B Rev 2", "RPI1"],
42+
"0005" : ["Model B Rev 2", "RPI1"],
43+
"0006" : ["Model B Rev 2", "RPI1"],
44+
45+
"0007" : ["Model A", "RPI1"],
46+
"0008" : ["Model A", "RPI1"],
47+
"0009" : ["Model A", "RPI1"],
48+
49+
"000d" : ["Model B Rev 2", "RPI1"],
50+
"000e" : ["Model B Rev 2", "RPI1"],
51+
"000f" : ["Model B Rev 2", "RPI1"],
52+
53+
"0010" : ["Model B+", "RPI1"],
54+
"0013" : ["Model B+", "RPI1"],
55+
"900032" : ["Model B+", "RPI1"],
56+
57+
"0011" : ["Compute Module", "RPI-COMPUTE-MODULE"],
58+
"0014" : ["Compute Module", "RPI-COMPUTE-MODULE"],
59+
60+
"0012" : ["Model A+", "RPI1"],
61+
"0015" : ["Model A+", "RPI1"],
62+
63+
"a01041" : ["Pi 2 Model B v1.1", "RPI2"],
64+
"a21041" : ["Pi 2 Model B v1.1", "RPI2"],
65+
66+
"a22042" : ["Pi 2 Model B v1.2", "RPI2"],
67+
68+
"900092" : ["Pi Zero v1.2", "RPI0"],
69+
70+
"900093" : ["Pi Zero v1.3", "RPI0"],
71+
72+
"0x9000C1" : ["Pi Zero W", "RPI0"],
73+
74+
"a02082" : ["Pi 3 Model B", "RPI3"],
75+
"a22082" : ["Pi 3 Model B", "RPI3"],
76+
}
77+
78+
# represents indexes for each corresponding key in the above dictionary
79+
RPI_MODEL_AND_PCBREV = 0
80+
RPI_GENERATION_MODEL = 1
81+
82+
def getRPIHardwareRevCode():
83+
"""
84+
Returns the hardware revision of the Raspberry Pi.
85+
If it can't find anything, it returns "NOT_FOUND".
86+
If there's an error while reading the file, it returns a None.
87+
Examples of strings returned : "Model B Rev 2", "Model A+", "Pi 3 Model B", etc.
88+
Look into the dictionary to see all the possible variants.
89+
"""
90+
cpuinfo_lines = readLinesFromFile("/proc/cpuinfo")
91+
rpi_description = ""
92+
93+
if not cpuinfo_lines is None:
94+
revision_line = cpuinfo_lines[-2]
95+
revision = revision_line.split(":")[-1]
96+
revision = revision.strip()
97+
98+
if revision in RPI_VARIANTS.keys():
99+
rpi_description = RPI_VARIANTS[revision][RPI_MODEL_AND_PCBREV]
100+
else:
101+
rpi_description = "NOT_FOUND"
102+
103+
return rpi_description
104+
105+
def getRPIGenerationCode():
106+
"""
107+
Returns the Raspberry Pi's generation model.
108+
If it can't find anything, it returns "NOT_FOUND".
109+
If there's an error while reading the file, it returns a None.
110+
Depending on the Raspberry Pi's model, the function can return the following strings:
111+
"RPI0"
112+
"RPI1"
113+
"RPi2"
114+
"RPI3"
115+
"RPI-COMPUTE-MODULE"
116+
"""
117+
118+
cpuinfo_lines = readLinesFromFile("/proc/cpuinfo")
119+
rpi_description = ""
120+
121+
if not cpuinfo_lines is None:
122+
revision_line = cpuinfo_lines[-2]
123+
revision = revision_line.split(":")[-1]
124+
revision = revision.strip()
125+
126+
if revision in RPI_VARIANTS.keys():
127+
rpi_description = RPI_VARIANTS[revision][RPI_GENERATION_MODEL]
128+
else:
129+
rpi_description = "NOT_FOUND"
130+
131+
return rpi_description
132+
133+
def readLinesFromFile(filename):
134+
"""
135+
Returns the read file as a list of strings.
136+
Each element of the list represents a line.
137+
On error it returns None.
138+
"""
139+
try:
140+
with open(filename, "r") as input_file:
141+
lines = input_file.readlines()
142+
return lines
143+
144+
except EnvironmentError:
145+
return None

autodetect_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
description="Dexter Industries Robot Autodetection",
2828
author="Dexter Industries",
2929
url="http://www.dexterindustries.com/GoPiGo/",
30-
py_modules=['auto_detect_robot'],
30+
py_modules=['auto_detect_robot', 'auto_detect_rpi'],
3131
#install_requires=open('requirements.txt').readlines(),
3232
)

0 commit comments

Comments
 (0)