File: rpi_info.py

package info (click to toggle)
python-adafruit-platformdetect 3.87.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 368 kB
  • sloc: python: 2,607; sh: 6; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`bin.rpi_info`
================================================================================

Interactive mode will prompt for the revision code
Otherwise it will be detected automatically

* Author(s): Melissa LeBlanc-Williams

Implementation Notes
--------------------

**Software and Dependencies:**

* Linux and Python 3.7 or Higher

"""

import sys
import argparse
import adafruit_platformdetect
from adafruit_platformdetect.revcodes import PiDecoder

detector = adafruit_platformdetect.Detector()
parser = argparse.ArgumentParser()


def print_property(label, value):
    """Format and print a property"""
    print(f"{label}: {value}")


def main(interactive):
    """Run the program"""
    pi_rev_code = detector.board._pi_rev_code()  # pylint: disable=protected-access

    if pi_rev_code is None:
        print("Raspberry Pi not detected. Using interactive mode")

    if pi_rev_code is None or interactive:
        pi_rev_code = input(
            "Enter a Raspberry Pi revision code (e.g. d03114 or 000f): "
        )

    print_property("Revision Code", pi_rev_code)

    try:
        decoder = PiDecoder(pi_rev_code)
    except ValueError:
        print("Invalid revision code. It should be a hexadecimal value.")
        sys.exit(1)

    if not decoder.is_valid_code():
        print(
            "Code is invalid. This rev code includes at least one "
            "value that is outside of the expected range."
        )
        sys.exit(1)

    if decoder.is_new_format():
        print_property("Overvoltage", decoder.overvoltage)
        print_property("OTP Program", decoder.otp_program)
        print_property("OTP Read", decoder.otp_read)
        print_property("Warranty bit", decoder.warranty_bit)
        print_property("New flag", decoder.rev_style)
        print_property("Memory size", decoder.memory_size)
        print_property("Manufacturer", decoder.manufacturer)
        print_property("Processor", decoder.processor)
        print_property("Type", decoder.type)
        print_property("Revision", decoder.revision)
    else:
        print_property("Warranty bit", decoder.warranty_bit)
        print_property("Model", decoder.type)
        print_property("Revision", decoder.revision)
        print_property("RAM", decoder.memory_size)
        print_property("Manufacturer", decoder.manufacturer)


# Main function
if __name__ == "__main__":
    parser.add_argument(
        "-i", "--interactive", help="Interactive Mode", action="store_true"
    )
    args = parser.parse_args()
    main(args.interactive)