-
Notifications
You must be signed in to change notification settings - Fork 161
/
adafruit_fanservice.py
93 lines (76 loc) · 3.01 KB
/
adafruit_fanservice.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
"""
Adafruit Raspberry Pi Fan Service Setup Script
(C) Adafruit Industries, Creative Commons 3.0 - Attribution Share Alike
Converted to Python by Melissa LeBlanc-Williams for Adafruit Industries
"""
import os
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 = 'ADAFRUIT'
def main():
shell.clear()
if shell.is_raspberry_pi():
print("""This script will enable the Raspberry Pi
fan service, which will turn on an
external fan controlled by a given pin
Operations performed include:
- Enable Fan Service
Run time < 1 minute. Reboot required.""")
else:
print("""This script will install Adafruit
fan service, which will turn on an
external fan controlled by a given pin
Operations performed include:
- Create a Fan Service File
- Enable Fan Service
- Automatically Start Fan Service
on System startup
Run time < 1 minute. Reboot not required.""")
if not shell.argument_exists('y'):
if not shell.prompt("CONTINUE?", default='n'):
print("Canceled.")
shell.exit()
print("Continuing...")
# check init system (technique borrowed from raspi-config):
shell.group = 'FAN'
shell.info('Checking init system...')
if shell.run_command("which systemctl", suppress_message=True) and shell.run_command("systemctl | grep '\-\.mount'", suppress_message=True):
print("Found systemd, OK!")
elif os.path.isfile("/etc/init.d/cron") and not os.path.islink("/etc/init.d/cron"):
shell.bail("Found sysvinit, but we require systemd")
else:
shell.bail("Unrecognised init system")
if shell.is_raspberry_pi():
shell.info('Enabling Raspberry Pi Fan Service on GPIO 4')
shell.run_command("sudo raspi-config nonint do_fan 0 4")
shell.info('Done!')
shell.prompt_reboot()
else:
shell.info('Adding adafruit_fan.service')
contents = """[Unit]
Description=Fan service for some Adafruit boards
After=network.target
[Service]
Type=oneshot
ExecStartPre=-/bin/bash -c 'echo 4 >/sys/class/gpio/export'
ExecStartPre=/bin/bash -c 'echo out >/sys/class/gpio/gpio4/direction'
ExecStart=/bin/bash -c 'echo 1 >/sys/class/gpio/gpio4/value'
RemainAfterExit=true
ExecStop=/bin/bash -c 'echo 0 >/sys/class/gpio/gpio4/value'
StandardOutput=journal
[Install]
WantedBy=multi-user.target"""
shell.write_text_file("/etc/systemd/system/adafruit_fan.service", contents, append=False)
shell.info('Enabling adafruit_fan.service')
shell.run_command("sudo systemctl enable adafruit_fan.service")
shell.run_command("sudo systemctl start adafruit_fan.service")
shell.info('Done!')
print("You can stop the fan service with 'sudo systemctl stop adafruit_fan.service'")
print("You can start the fan service with 'sudo systemctl start adafruit_fan.service'")
# Main function
if __name__ == "__main__":
shell.require_root()
main()