Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibreELEC 10 Compatibility #6

Merged
merged 3 commits into from Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@
---
# Manual installation

## change the permissions for the script
## change the permissions for the script

`sudo chmod +x ups.sh`

Expand Down
102 changes: 51 additions & 51 deletions scripts/gpiod/ups-gpiod.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
##################################################################
# HiPi.io UPS hat service script
# https://github.com/hipi-io/ups-hat
Expand All @@ -14,78 +14,78 @@
# Description: Starts the HiPi-io UPS HAT Monitor
### END INIT INFO

#GPIO17 (input) used to read current power status.
#0 - normal (or battery power switched on manually).
#1 - power fault, switched to battery.
#echo 17 > /sys/class/gpio/export;
#echo in > /sys/class/gpio/gpio17/direction;
#GPIO17 (input) used to read current power status.
#0 - normal (or battery power switched on manually).
#1 - power fault, switched to battery.
#echo 17 > /sys/class/gpio/export
#echo in > /sys/class/gpio/gpio17/direction
# use gpioget gpiochip0 17

#GPIO27 (input) used to indicate that UPS is online
#echo 27 > /sys/class/gpio/export;
#echo in > /sys/class/gpio/gpio27/direction;
#echo 27 > /sys/class/gpio/export
#echo in > /sys/class/gpio/gpio27/direction
# use gpioget gpiochip0 27

#GPIO18 used to inform UPS that Pi is still working. After power-off this pin returns to Hi-Z state.
#echo 18 > /sys/class/gpio/export;
#echo out > /sys/class/gpio/gpio18/direction;
#echo 0 > /sys/class/gpio/gpio18/value;
#GPIO18 used to inform UPS that Pi is still working. After power-off this pin returns to Hi-Z state.
#echo 18 > /sys/class/gpio/export
#echo out > /sys/class/gpio/gpio18/direction
#echo 0 > /sys/class/gpio/gpio18/value
gpioset gpiochip0 18=0

power_timer=0;
inval_power="0";
power_timer=0
inval_power="0"

ups_online1="0";
ups_online2="0";
ups_online_timer="0";
ups_online1="0"
ups_online2="0"
ups_online_timer="0"

while true
do
#read GPIO27 pin value
#normally, UPS toggles this pin every 0.5s
ups_online1=$(gpioget gpiochip0 27);
sleep 0.1;
ups_online2=$(gpioget gpiochip0 27);
ups_online_timer=$((ups_online_timer+1));
ups_online1=$(gpioget gpiochip0 27)

sleep 0.1

ups_online2=$(gpioget gpiochip0 27)

ups_online_timer=$((ups_online_timer+1))

#toggled?
if (( "$ups_online1" != "$ups_online2" )); then
ups_online_timer=0;
if [ "$ups_online1" -ne "$ups_online2" ]; then
ups_online_timer=0
fi

#reset all timers if ups is offline longer than 3s (no toggling detected)
if (("$ups_online_timer" > 30));
if [ "$ups_online_timer" -gt 30 ]
then
echo "$ups_online_timer";
ups_online_timer=30;
power_timer=0;
inval_power=0;
#echo "UPS offline. Exit";
echo "$ups_online_timer"

ups_online_timer=30
power_timer=0
inval_power=0
#echo "UPS offline. Exit"
#gpioset gpiochip0 18=1 # Tell UPS hat it is no longer monitored (disables the 10-seconds power disconnect)
#exit;
fi
#exit
fi

#read GPIO17 pin value
inval_power=$(gpioget gpiochip0 17);
# echo $inval_power;
if (( "$inval_power" == 1 )); then
power_timer=$((power_timer+1));
else
power_timer=0;
inval_power=$(gpioget gpiochip0 17)

# echo $inval_power

if [ "$inval_power" -eq 1 ]; then
power_timer=$((power_timer+1))
else
power_timer=0
fi

#If power was not restored in 60 seconds
if (( "$power_timer" == 600 )); then
#echo $power_timer;
if [ "$power_timer" -eq 600 ]; then
#echo $power_timer
echo "Powering off..."
sleep 2;
sleep 2
systemctl poweroff; #turn off
exit;
fi
exit
fi
done
2 changes: 1 addition & 1 deletion scripts/ups-reset.py
Expand Up @@ -5,7 +5,7 @@
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Expand Down
2 changes: 1 addition & 1 deletion scripts/ups-set.py
Expand Up @@ -5,7 +5,7 @@
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Expand Down
154 changes: 77 additions & 77 deletions scripts/ups.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
##################################################################
# HiPi.io UPS hat service script
# https://github.com/hipi-io/ups-hat
Expand Down Expand Up @@ -26,129 +26,129 @@ gpio_reset_sysfs() {
gpio_reset_py() {
# Using a RPi.GPIO Python script allows pull-up/down control
# Installed by default with Raspberry Pi OS
/usr/local/sbin/hipi-io-ups-hat-gpio-reset;
/usr/local/sbin/hipi-io-ups-hat-gpio-reset
}

gpio_set_sysfs() {
### Configure the needed GPIO
# NOTE: SysFS has no method to handle Pull-up/downs!!!
#
# GPIO17 (input) used to read current power status.
# 0 - normal (or battery power switched on manually).
# 1 - power fault, switched to battery.
echo 17 > /sys/class/gpio/export;
echo in > /sys/class/gpio/gpio17/direction;
# GPIO17 (input) used to read current power status.
# 0 - normal (or battery power switched on manually).
# 1 - power fault, switched to battery.
echo 17 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio17/direction

# GPIO27 (input) used to indicate that UPS is online
# 1Hz = normal
echo 27 > /sys/class/gpio/export;
echo in > /sys/class/gpio/gpio27/direction;
# GPIO18 used to inform UPS that Pi is still working.
echo 27 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio27/direction

# GPIO18 used to inform UPS that Pi is still working.
# After power-off, this pin returns to Hi-Z state (read as high by hat).
# Returning to hi-Z/high starts a 10-seconds timer to power disconnect.
echo 18 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio18/direction;
echo 0 > /sys/class/gpio/gpio18/value;
echo 18 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio18/direction
echo 0 > /sys/class/gpio/gpio18/value
}

gpio_set_py() {
# GPIO27 (input) used to indicate that UPS is online
# 1Hz = normal
#echo 27 > /sys/class/gpio/export;
#echo in > /sys/class/gpio/gpio27/direction;
#echo 27 > /sys/class/gpio/export
#echo in > /sys/class/gpio/gpio27/direction

# Using a RPi.GPIO Python script allows pull-up/down control
# Installed by default with Raspberry Pi OS
/usr/local/sbin/hipi-io-ups-hat-gpio-set;
/usr/local/sbin/hipi-io-ups-hat-gpio-set
}

main_loop() {
power_timer=0;
inval_power="0";
ups_online1="0";
ups_online2="0";
ups_online_timer="0";
power_timer=0
inval_power="0"

ups_online1="0"
ups_online2="0"
ups_online_timer="0"

while true
do
# read GPIO27 pin value
# normally, UPS toggles this pin every 0.5s
ups_online1=$(cat /sys/class/gpio/gpio27/value);
sleep 0.1;
ups_online2=$(cat /sys/class/gpio/gpio27/value);
ups_online_timer=$((ups_online_timer+1));
ups_online1=$(cat /sys/class/gpio/gpio27/value)

sleep 0.1

ups_online2=$(cat /sys/class/gpio/gpio27/value)

ups_online_timer=$((ups_online_timer+1))

# toggled?
if (( "$ups_online1" != "$ups_online2" )); then
ups_online_timer=0;
if [ "$ups_online1" -ne "$ups_online2" ]; then
ups_online_timer=0
fi

# reset all timers if ups is offline longer than 3s (no toggling detected)
if (("$ups_online_timer" > 30));
if [ "$ups_online_timer" -gt 30 ]
then
echo "$ups_online_timer";
ups_online_timer=30;
power_timer=0;
inval_power=0;
#echo "UPS offline. Exit";
#exit;
fi
echo "$ups_online_timer"

ups_online_timer=30
power_timer=0
inval_power=0
#echo "UPS offline. Exit"
#exit
fi

# read GPIO17 pin value
inval_power=$(cat /sys/class/gpio/gpio17/value);
#echo "inval_power= "$inval_power;
if (( "$inval_power" == 1 )); then
power_timer=$((power_timer+1));
else
power_timer=0;
inval_power=$(cat /sys/class/gpio/gpio17/value)

#echo "inval_power= "$inval_power

if [ "$inval_power" -eq 1 ]; then
power_timer=$((power_timer+1))
else
power_timer=0
fi
#echo "power_timer= "$power_timer;

#echo "power_timer= "$power_timer

# If power was not restored in 60 seconds
if (( "$power_timer" == 600 )); then
if [ "$power_timer" -eq 600 ]; then
echo "Powering off..."
sleep 2;
sleep 2
systemctl poweroff; #turn off
exit;
exit
fi

if [ -e /tmp/ups-hat.exit ]
then
then
#break
echo "Shutting down service..."
gpio_reset_py;
gpio_reset_py
echo
echo "*** UPS service stopped ***"
echo
touch /tmp/ups-hat.quit;
#gpio_reset_py;
rm -v /tmp/ups-hat.exit;
exit;
touch /tmp/ups-hat.quit
#gpio_reset_py
rm -v /tmp/ups-hat.exit

exit

fi

done

}

main() {
gpio_reset_py;
gpio_reset_sysfs;
gpio_reset_py
gpio_reset_sysfs
if [ -e /tmp/ups-hat.exit ]; then rm -v /tmp/ups-hat.exit; fi
if [ -e /tmp/ups-hat.quit ]; then rm -v /tmp/ups-hat.quit; fi
gpio_set_sysfs;
gpio_set_py;
main_loop;
gpio_set_sysfs
gpio_set_py
main_loop
}

main
main
2 changes: 1 addition & 1 deletion systemd/hipi-io-ups-hat.service
Expand Up @@ -7,7 +7,7 @@ Type=exec
KillMode=none
ExecStart=/usr/local/sbin/hipi-io-ups-hat-service
ExecStop=/usr/bin/touch /tmp/ups-hat.exit
#ExecStop=/bin/kill -s QUIT $MAINPID;
#ExecStop=/bin/kill -s QUIT $MAINPID;
#ExecReload=/bin/kill -s QUIT $MAINPID
#ExitType=main

Expand Down