Skip to content

Commit 4d12148

Browse files
committed
Improve rpi_info script to run interactively
1 parent 633203a commit 4d12148

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed

bin/rpi_info.py

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,69 @@
2323
"""
2424

2525
import sys
26+
import argparse
2627
import adafruit_platformdetect
2728
from adafruit_platformdetect.revcodes import PiDecoder
2829

2930
detector = adafruit_platformdetect.Detector()
30-
pi_rev_code = detector.board._pi_rev_code() # pylint: disable=protected-access
31-
32-
if pi_rev_code is None:
33-
print("Raspberry Pi not detected. Using interactive mode")
34-
pi_rev_code = input("Enter a Raspberry Pi revision code (e.g. d03114 or 000f): ")
35-
36-
try:
37-
decoder = PiDecoder(pi_rev_code)
38-
except ValueError as e:
39-
print("Invalid revision code. It should be a hexadecimal value.")
40-
sys.exit(1)
41-
42-
if not decoder.is_valid_code():
43-
print(
44-
"Code is invalid. This rev code includes at least one "
45-
"value that is outside of the expected range."
46-
)
47-
sys.exit(1)
31+
parser = argparse.ArgumentParser()
4832

4933

5034
def print_property(label, value):
51-
"Format and print a property"
35+
"""Format and print a property"""
5236
print(f"{label}: {value}")
5337

5438

55-
if decoder.is_new_format():
56-
print_property("Overvoltage", decoder.overvoltage)
57-
print_property("OTP Program", decoder.otp_program)
58-
print_property("OTP Read", decoder.otp_read)
59-
print_property("Warranty bit", decoder.warranty_bit)
60-
print_property("New flag", decoder.rev_style)
61-
print_property("Memory size", decoder.memory_size)
62-
print_property("Manufacturer", decoder.manufacturer)
63-
print_property("Processor", decoder.processor)
64-
print_property("Type", decoder.type)
65-
print_property("Revision", decoder.revision)
66-
else:
67-
print_property("Warranty bit", decoder.warranty_bit)
68-
print_property("Model", decoder.type)
69-
print_property("Revision", decoder.revision)
70-
print_property("RAM", decoder.memory_size)
71-
print_property("Manufacturer", decoder.manufacturer)
39+
def main(interactive):
40+
"""Run the program"""
41+
pi_rev_code = detector.board._pi_rev_code() # pylint: disable=protected-access
42+
43+
if pi_rev_code is None:
44+
print("Raspberry Pi not detected. Using interactive mode")
45+
46+
if pi_rev_code is None or interactive:
47+
pi_rev_code = input(
48+
"Enter a Raspberry Pi revision code (e.g. d03114 or 000f): "
49+
)
50+
51+
print_property("Revision Code", pi_rev_code)
52+
53+
try:
54+
decoder = PiDecoder(pi_rev_code)
55+
except ValueError:
56+
print("Invalid revision code. It should be a hexadecimal value.")
57+
sys.exit(1)
58+
59+
if not decoder.is_valid_code():
60+
print(
61+
"Code is invalid. This rev code includes at least one "
62+
"value that is outside of the expected range."
63+
)
64+
sys.exit(1)
65+
66+
if decoder.is_new_format():
67+
print_property("Overvoltage", decoder.overvoltage)
68+
print_property("OTP Program", decoder.otp_program)
69+
print_property("OTP Read", decoder.otp_read)
70+
print_property("Warranty bit", decoder.warranty_bit)
71+
print_property("New flag", decoder.rev_style)
72+
print_property("Memory size", decoder.memory_size)
73+
print_property("Manufacturer", decoder.manufacturer)
74+
print_property("Processor", decoder.processor)
75+
print_property("Type", decoder.type)
76+
print_property("Revision", decoder.revision)
77+
else:
78+
print_property("Warranty bit", decoder.warranty_bit)
79+
print_property("Model", decoder.type)
80+
print_property("Revision", decoder.revision)
81+
print_property("RAM", decoder.memory_size)
82+
print_property("Manufacturer", decoder.manufacturer)
83+
84+
85+
# Main function
86+
if __name__ == "__main__":
87+
parser.add_argument(
88+
"-i", "--interactive", help="Interactive Mode", action="store_true"
89+
)
90+
args = parser.parse_args()
91+
main(args.interactive)

0 commit comments

Comments
 (0)