diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index 4caff304..3c74b74d 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -1,5 +1,6 @@ """Detect boards.""" import os +import re import adafruit_platformdetect.chip as ap_chip # Allow for aligned constant definitions: @@ -367,6 +368,30 @@ def _pi_id(self): for model, codes in _PI_REV_CODES.items(): if pi_rev_code in codes: return model + + # We may be on a non-Raspbian OS, so try to lazily determine + # the version based on `get_device_model` + else: + pi_model = self.detector.get_device_model() + if pi_model: + pi_model = pi_model.upper().replace(' ', '_') + if "PLUS" in pi_model: + re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)(PLUS)', + pi_model) + elif "CM" in pi_model: # untested for Compute Module + re_model = re.search(r'(RASPBERRY_PI_CM)(\d)', + pi_model) + else: # untested for non-plus models + re_model = re.search(r'(RASPBERRY_PI_\d).*([AB]_*)', + pi_model) + + if re_model: + pi_model = "".join(re_model.groups()) + available_models = _PI_REV_CODES.keys() + for model in available_models: + if model == pi_model: + return model + return None def _pi_rev_code(self): diff --git a/adafruit_platformdetect/chip.py b/adafruit_platformdetect/chip.py index 48f4253e..ad5ad5b5 100644 --- a/adafruit_platformdetect/chip.py +++ b/adafruit_platformdetect/chip.py @@ -25,6 +25,9 @@ MIPS24KC = "MIPS24KC" MIPS24KEC = "MIPS24KEC" +BCM_RANGE = {'BCM2708', 'BCM2709', 'BCM2835', 'BCM2837', 'bcm2708', 'bcm2709', + 'bcm2835', 'bcm2837'} + class Chip: """Attempt detection of current chip / CPU.""" def __init__(self, detector): @@ -61,13 +64,13 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s return BINHO platform = sys.platform - if platform == "linux" or platform == "linux2": + if platform in ('linux', 'linux2'): return self._linux_id() - if platform == "esp8266": + if platform == 'esp8266': return ESP8266 - if platform == "samd21": + if platform == 'samd21': return SAMD21 - if platform == "pyboard": + if platform == 'pyboard': return STM32 # nothing found! return None @@ -76,18 +79,18 @@ def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-s def _linux_id(self): # pylint: disable=too-many-branches """Attempt to detect the CPU on a computer running the Linux kernel.""" - if self.detector.check_dt_compatible_value("qcom,apq8016"): + if self.detector.check_dt_compatible_value('qcom,apq8016'): return APQ8016 - if self.detector.check_dt_compatible_value("fu500"): + if self.detector.check_dt_compatible_value('fu500'): return HFU540 linux_id = None - hardware = self.detector.get_cpuinfo_field("Hardware") + hardware = self.detector.get_cpuinfo_field('Hardware') if hardware is None: - vendor_id = self.detector.get_cpuinfo_field("vendor_id") - if vendor_id in ("GenuineIntel", "AuthenticAMD"): + vendor_id = self.detector.get_cpuinfo_field('vendor_id') + if vendor_id in ('GenuineIntel', 'AuthenticAMD'): linux_id = GENERIC_X86 compatible = self.detector.get_device_compatible() @@ -112,20 +115,34 @@ def _linux_id(self): # pylint: disable=too-many-branches elif "MIPS 24KEc" in cpu_model: linux_id = MIPS24KEC - elif hardware in ("BCM2708", "BCM2709", "BCM2835"): - linux_id = BCM2XXX - elif "AM33XX" in hardware: - linux_id = AM33XX - elif "sun8i" in hardware: - linux_id = SUN8I - elif "ODROIDC" in hardware: - linux_id = S805 - elif "ODROID-C2" in hardware: - linux_id = S905 - elif "ODROID-N2" in hardware: - linux_id = S922X - elif "SAMA5" in hardware: - linux_id = SAMA5 + # we still haven't identified the hardware, so + # convert it to a list and let the remaining + # conditions attempt. + if not linux_id: + hardware = [ + entry.replace('\x00', '') for entry in compatible.split(',') + ] + + if not linux_id: + if 'AM33XX' in hardware: + linux_id = AM33XX + elif 'sun8i' in hardware: + linux_id = SUN8I + elif 'ODROIDC' in hardware: + linux_id = S805 + elif 'ODROID-C2' in hardware: + linux_id = S905 + elif 'ODROID-N2' in hardware: + linux_id = S922X + elif 'SAMA5' in hardware: + linux_id = SAMA5 + else: + if isinstance(hardware, str): + if hardware in BCM_RANGE: + linux_id = BCM2XXX + elif isinstance(hardware, list): + if set(hardware) & BCM_RANGE: + linux_id = BCM2XXX return linux_id