-
Notifications
You must be signed in to change notification settings - Fork 161
/
joy-bonnet.py
executable file
·121 lines (101 loc) · 4.54 KB
/
joy-bonnet.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Adafruit Raspberry Pi Joy Bonnet Setup Script
(C) Adafruit Industries, Creative Commons 3.0 - Attribution Share Alike
Converted to Python by Melissa LeBlanc-Williams for Adafruit Industries
"""
try:
from adafruit_shell import Shell
except ImportError:
raise RuntimeError("The library 'adafruit_shell' was not found. To install, try typing: sudo pip3 install adafruit-python-shell")
shell = Shell()
shell.group = "JOY"
def main():
shell.clear()
print("""This script installs software for the Adafruit
Joy Bonnet for Raspberry Pi. Steps include:
- Update package index files (apt-get update).
- Install Python libraries: smbus, evdev.
- Install joyBonnet.py in /boot and
configure /etc/rc.local to auto-start script.
- Enable I2C bus.
- OPTIONAL: disable overscan.
Run time ~10 minutes. Reboot required.
EXISTING INSTALLATION, IF ANY, WILL BE OVERWRITTEN.
""")
if not shell.prompt("CONTINUE?", default='n'):
print("Canceled.")
shell.exit()
print("Continuing...")
disable_overscan = shell.prompt("Disable overscan?", default='n')
install_halt = shell.prompt("Install GPIO-halt utility?", default='n')
if install_halt:
#halt_pin = shell.prompt("Install GPIO-halt utility?"
halt_pin = input("GPIO pin for halt: ").strip()
print("\n")
if disable_overscan:
print("Overscan: disable.")
else:
print("Overscan: keep current setting.")
if install_halt:
print("Install GPIO-halt: YES (GPIO{})".format(halt_pin))
else:
print("Install GPIO-halt: NO")
if not shell.prompt("CONTINUE?", default='n'):
print("Canceled.")
shell.exit()
# START INSTALL ------------------------------------------------------------
# All selections are validated at this point...
print("""
Starting installation...
Updating package index files...""")
shell.run_command('sudo apt-get update', suppress_message=True)
print("Installing Python libraries...")
shell.run_command('sudo apt-get install -y python3-pip python3-dev python3-smbus')
shell.run_command('pip3 install evdev --upgrade')
print("Installing Adafruit code in /boot...")
shell.chdir("/tmp")
shell.run_command("curl -LO https://raw.githubusercontent.com/adafruit/Adafruit-Retrogame/master/joyBonnet.py")
# Moving between filesystems requires copy-and-delete:
shell.copy("joyBonnet.py", "/boot")
shell.remove("joyBonnet.py")
if install_halt:
print("Installing gpio-halt in /usr/local/bin...")
shell.run_command("curl -LO https://github.com/adafruit/Adafruit-GPIO-Halt/archive/master.zip")
shell.run_command("unzip -u master.zip")
shell.chdir("Adafruit-GPIO-Halt-master")
shell.run_command("make")
shell.move("gpio-halt", "/usr/local/bin")
shell.chdir("..")
shell.remove("Adafruit-GPIO-Halt-master")
# CONFIG -------------------------------------------------------------------
print("Configuring system...")
# Enable I2C using raspi-config
shell.run_command("sudo raspi-config nonint do_i2c 0")
# Disable overscan compensation (use full screen):
if disable_overscan:
shell.run_command("sudo raspi-config nonint do_overscan 1")
if install_halt:
if shell.pattern_search("/etc/rc.local", "gpio-halt"):
# gpio-halt already in rc.local, but make sure correct:
shell.pattern_replace("/etc/rc.local", "^.*gpio-halt.*$", "/usr/local/bin/gpio-halt {} &".format(halt_pin))
else:
# Insert gpio-halt into rc.local before final 'exit 0'
shell.pattern_replace("/etc/rc.local", "^exit 0", "/usr/local/bin/gpio-halt {} &\\nexit 0".format(halt_pin))
# Auto-start joyBonnet.py on boot
if shell.pattern_search("/etc/rc.local", "joyBonnet.py"):
# joyBonnet.py already in rc.local, but make sure correct:
shell.pattern_replace("/etc/rc.local", "^.*joyBonnet.py.*$", "cd /boot;python3 joyBonnet.py &")
else:
# Insert joyBonnet.py into rc.local before final 'exit 0'
shell.pattern_replace("/etc/rc.local", "^exit 0", "cd /boot;python3 joyBonnet.py &\\nexit 0")
# Add udev rule (will overwrite if present)
shell.write_text_file("/etc/udev/rules.d/10-retrogame.rules", "SUBSYSTEM==\"input\", ATTRS{name}==\"retrogame\", ENV{ID_INPUT_KEYBOARD}=\"1\"", append=False)
# PROMPT FOR REBOOT --------------------------------------------------------
print("""DONE.
Settings take effect on next boot.
""")
shell.prompt_reboot()
# Main function
if __name__ == "__main__":
shell.require_root()
main()