Skip to content

Commit eab68f0

Browse files
authored
Merge pull request #40 from sommersoft/ubuntu-disco
RFC: Attempt To Recognize Raspberry Pi Running Non-Raspbian OS
2 parents a2c9e3e + d943f95 commit eab68f0

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

adafruit_platformdetect/board.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Detect boards."""
22
import os
3+
import re
34
import adafruit_platformdetect.chip as ap_chip
45

56
# Allow for aligned constant definitions:
@@ -367,6 +368,30 @@ def _pi_id(self):
367368
for model, codes in _PI_REV_CODES.items():
368369
if pi_rev_code in codes:
369370
return model
371+
372+
# We may be on a non-Raspbian OS, so try to lazily determine
373+
# the version based on `get_device_model`
374+
else:
375+
pi_model = self.detector.get_device_model()
376+
if pi_model:
377+
pi_model = pi_model.upper().replace(' ', '_')
378+
if "PLUS" in pi_model:
379+
re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)(PLUS)',
380+
pi_model)
381+
elif "CM" in pi_model: # untested for Compute Module
382+
re_model = re.search(r'(RASPBERRY_PI_CM)(\d)',
383+
pi_model)
384+
else: # untested for non-plus models
385+
re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)',
386+
pi_model)
387+
388+
if re_model:
389+
pi_model = "".join(re_model.groups())
390+
available_models = _PI_REV_CODES.keys()
391+
for model in available_models:
392+
if model == pi_model:
393+
return model
394+
370395
return None
371396

372397
def _pi_rev_code(self):

adafruit_platformdetect/chip.py

+40-23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
MIPS24KC = "MIPS24KC"
2626
MIPS24KEC = "MIPS24KEC"
2727

28+
BCM_RANGE = {'BCM2708', 'BCM2709', 'BCM2835', 'BCM2837', 'bcm2708', 'bcm2709',
29+
'bcm2835', 'bcm2837'}
30+
2831
class Chip:
2932
"""Attempt detection of current chip / CPU."""
3033
def __init__(self, detector):
@@ -61,13 +64,13 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s
6164
return BINHO
6265

6366
platform = sys.platform
64-
if platform == "linux" or platform == "linux2":
67+
if platform in ('linux', 'linux2'):
6568
return self._linux_id()
66-
if platform == "esp8266":
69+
if platform == 'esp8266':
6770
return ESP8266
68-
if platform == "samd21":
71+
if platform == 'samd21':
6972
return SAMD21
70-
if platform == "pyboard":
73+
if platform == 'pyboard':
7174
return STM32
7275
# nothing found!
7376
return None
@@ -76,18 +79,18 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s
7679
def _linux_id(self): # pylint: disable=too-many-branches
7780
"""Attempt to detect the CPU on a computer running the Linux kernel."""
7881

79-
if self.detector.check_dt_compatible_value("qcom,apq8016"):
82+
if self.detector.check_dt_compatible_value('qcom,apq8016'):
8083
return APQ8016
8184

82-
if self.detector.check_dt_compatible_value("fu500"):
85+
if self.detector.check_dt_compatible_value('fu500'):
8386
return HFU540
8487

8588
linux_id = None
86-
hardware = self.detector.get_cpuinfo_field("Hardware")
89+
hardware = self.detector.get_cpuinfo_field('Hardware')
8790

8891
if hardware is None:
89-
vendor_id = self.detector.get_cpuinfo_field("vendor_id")
90-
if vendor_id in ("GenuineIntel", "AuthenticAMD"):
92+
vendor_id = self.detector.get_cpuinfo_field('vendor_id')
93+
if vendor_id in ('GenuineIntel', 'AuthenticAMD'):
9194
linux_id = GENERIC_X86
9295

9396
compatible = self.detector.get_device_compatible()
@@ -112,20 +115,34 @@ def _linux_id(self): # pylint: disable=too-many-branches
112115
elif "MIPS 24KEc" in cpu_model:
113116
linux_id = MIPS24KEC
114117

115-
elif hardware in ("BCM2708", "BCM2709", "BCM2835"):
116-
linux_id = BCM2XXX
117-
elif "AM33XX" in hardware:
118-
linux_id = AM33XX
119-
elif "sun8i" in hardware:
120-
linux_id = SUN8I
121-
elif "ODROIDC" in hardware:
122-
linux_id = S805
123-
elif "ODROID-C2" in hardware:
124-
linux_id = S905
125-
elif "ODROID-N2" in hardware:
126-
linux_id = S922X
127-
elif "SAMA5" in hardware:
128-
linux_id = SAMA5
118+
# we still haven't identified the hardware, so
119+
# convert it to a list and let the remaining
120+
# conditions attempt.
121+
if not linux_id:
122+
hardware = [
123+
entry.replace('\x00', '') for entry in compatible.split(',')
124+
]
125+
126+
if not linux_id:
127+
if 'AM33XX' in hardware:
128+
linux_id = AM33XX
129+
elif 'sun8i' in hardware:
130+
linux_id = SUN8I
131+
elif 'ODROIDC' in hardware:
132+
linux_id = S805
133+
elif 'ODROID-C2' in hardware:
134+
linux_id = S905
135+
elif 'ODROID-N2' in hardware:
136+
linux_id = S922X
137+
elif 'SAMA5' in hardware:
138+
linux_id = SAMA5
139+
else:
140+
if isinstance(hardware, str):
141+
if hardware in BCM_RANGE:
142+
linux_id = BCM2XXX
143+
elif isinstance(hardware, list):
144+
if set(hardware) & BCM_RANGE:
145+
linux_id = BCM2XXX
129146

130147
return linux_id
131148

0 commit comments

Comments
 (0)