Instantly share code, notes, and snippets.
Last activeNovember 27, 2024 15:36
Save supersonictw/046e10024038c18ccb46cbf3f1a88b79 to your computer and use it in GitHub Desktop.
Cold down the temperature of OPi automatically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# opi_fan.py - Cold down the temperature of OPi automatically. | |
# SPDX-License-Identifier: MIT (https://ncurl.xyz/s/o_o6DVqIR) | |
# https://gist.github.com/supersonictw/046e10024038c18ccb46cbf3f1a88b79 | |
fromsysimportargv | |
fromtimeimportsleep | |
fromgpiodimport ( | |
request_lines, | |
LineSettings | |
) | |
fromgpiod.lineimport ( | |
Direction, | |
Value, | |
) | |
""" /usr/local/sbin/opi_fan | |
#!/bin/sh | |
BASE_DIR="/opt/opi_fan" | |
INTERPRETER="$BASE_DIR/venv/bin/python" | |
CONTROLLER="$BASE_DIR/main.py" | |
"$INTERPRETER" "$CONTROLLER" $1 | |
""" | |
""" /lib/systemd/system/opi_fan.service | |
[Unit] | |
Description=Cold down the temperature of OPi automatically. | |
After=multi-user.target | |
[Service] | |
Type=simple | |
ExecStart=/usr/local/sbin/opi_fan daemon | |
[Install] | |
WantedBy=multi-user.target | |
""" | |
CHECK_TIME_GAP=60 | |
FAN_CONTROL_PIN=6 | |
FAN_ENABLE_TEMPERATURE=60 | |
COLD_TEMPERATURE_GAP=10 | |
SYS_GPIO_CHIP_FILE="/dev/gpiochip0" | |
SYS_TEMPERATURE_FILE="/sys/devices/virtual/thermal/thermal_zone0/temp" | |
classFan: | |
def__init__(self)->None: | |
self.request=request_lines( | |
SYS_GPIO_CHIP_FILE, | |
consumer="opi_fan", | |
config={ | |
FAN_CONTROL_PIN:LineSettings( | |
direction=Direction.OUTPUT, | |
output_value=Value.INACTIVE | |
) | |
}, | |
) | |
defenable(self): | |
self.request.set_value( | |
FAN_CONTROL_PIN, | |
Value.ACTIVE, | |
) | |
defdisable(self): | |
self.request.set_value( | |
FAN_CONTROL_PIN, | |
Value.INACTIVE, | |
) | |
defcleanup(self): | |
self.disable() | |
self.request.release() | |
deftemperature_read(): | |
withopen(SYS_TEMPERATURE_FILE,"r")asf: | |
temperature=f.read() | |
returnint(temperature)/1000 | |
deftemperature_loop(): | |
whiletemperature_read()> (FAN_ENABLE_TEMPERATURE-COLD_TEMPERATURE_GAP): | |
sleep(CHECK_TIME_GAP) | |
deftask_cold(): | |
fan=Fan() | |
try: | |
fan.enable() | |
whileTrue: | |
sleep(CHECK_TIME_GAP) | |
exceptKeyboardInterrupt: | |
print("Task interrupted.") | |
finally: | |
fan.cleanup() | |
deftask_daemon(): | |
fan=Fan() | |
whileTrue: | |
iftemperature_read()<FAN_ENABLE_TEMPERATURE: | |
sleep(CHECK_TIME_GAP) | |
continue | |
fan.enable() | |
temperature_loop() | |
fan.disable() | |
deftask_temp(): | |
temperature=temperature_read() | |
print(f"{temperature:.1f}°C") | |
if__name__=='__main__': | |
command_tasks= { | |
"cold":task_cold, | |
"daemon":task_daemon, | |
"temp":task_temp, | |
} | |
iflen(argv)>1andargv[1]incommand_tasks: | |
command_tasks[argv[1]]() | |
else: | |
print("No task specified.") |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment