Inital Commit
All checks were successful
Geekwom NASPi Daemon / Geekwom NASPi Daemon [arm64] (push) Successful in 10s

This commit is contained in:
Cantibra
2025-10-26 21:11:05 +01:00
commit 6b1dc41997
15 changed files with 801 additions and 0 deletions

10
root/DEBIAN/control Normal file
View File

@@ -0,0 +1,10 @@
Package: geekworm-naspi
Version: 1.0.0
Architecture:
Installed-Size:
Depends: gpiod
Priority: optional
Section: misc
Homepage: http://www.privlab.it
Maintainer: PrivLab <hostmaster@privlab.it>
Description: Geekworm NASPi Daemon

31
root/DEBIAN/postinst Normal file
View File

@@ -0,0 +1,31 @@
#!/usr/bin/sh
set -e
set -u
if [ -d '/run/systemd/system' ]; then
/usr/bin/systemctl --system daemon-reload > '/dev/null' 2>&1 || \
/usr/bin/true
fi
case "${1}" in
configure)
if [ -x '/usr/bin/deb-systemd-helper' ]; then
/usr/bin/deb-systemd-helper unmask 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-helper unmask 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
fi
if /usr/bin/deb-systemd-helper --quiet was-enabled ''; then
/usr/bin/deb-systemd-helper enable 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-helper enable 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
else
/usr/bin/deb-systemd-helper update-state 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-helper update-state 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
fi
;;
esac

31
root/DEBIAN/postrm Normal file
View File

@@ -0,0 +1,31 @@
#!/usr/bin/sh
set -e
set -u
if [ -d '/run/systemd/system' ]; then
/usr/bin/systemctl --system daemon-reload > '/dev/null' 2>&1 || \
/usr/bin/true
fi
case "${1}" in
remove|purge)
if [ -x '/usr/bin/deb-systemd-helper' ]; then
/usr/bin/deb-systemd-helper purge 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-helper unmask 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-helper purge 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-helper unmask 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
fi
BOOT=$(/usr/bin/grep '/boot' '/etc/fstab' | /usr/bin/mawk '$1 !~ /^#/ && $2 ~ /^[/]/ {print $2}')
PATTERN="## pwm-2chan\n## Load the Pulse-Width Modulation driver.\n##\ndtoverlay=pwm-2chan"
/usr/bin/test -n "${BOOT}"
/usr/bin/sed --in-place "/## pwm-2chan/{
N;N;N
s/${PATTERN}//g}" "${BOOT}/config.txt"
/usr/bin/sed --in-place --expression=':a' --expression='/^\n*$/{$d;N;ba' --expression='}' "${BOOT}/config.txt"
;;
esac

21
root/DEBIAN/preinst Normal file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/sh
set -e
set -u
case "${1}" in
install)
BOOT=$(/usr/bin/grep '/boot' '/etc/fstab' | /usr/bin/mawk '$1 !~ /^#/ && $2 ~ /^[/]/ {print $2}')
/usr/bin/test -n "${BOOT}"
/usr/bin/echo '' >> "${BOOT}/config.txt"
/usr/bin/echo '## pwm-2chan' >> "${BOOT}/config.txt"
/usr/bin/echo '## Load the Pulse-Width Modulation driver.' >> "${BOOT}/config.txt"
/usr/bin/echo '##' >> "${BOOT}/config.txt"
/usr/bin/echo 'dtoverlay=pwm-2chan' >> "${BOOT}/config.txt"
/usr/bin/echo '##'
/usr/bin/echo '##'
/usr/bin/echo '## Restart the device to load the necessary drivers.'
/usr/bin/echo '##'
/usr/bin/echo '##'
;;
esac

19
root/DEBIAN/prerm Normal file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/sh
set -e
set -u
case "${1}" in
remove)
if [ -x '/usr/bin/deb-systemd-invoke' ]; then
/usr/bin/deb-systemd-invoke stop 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-invoke disable 'geekworm-naspi-pwmd.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-invoke stop 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
/usr/bin/deb-systemd-invoke disable 'geekworm-naspi-button.service' > '/dev/null' 2>&1 || \
/usr/bin/true
fi
;;
esac

View File

@@ -0,0 +1,2 @@
# redirect binary (poweroff) to gpio (gpioset)
alias poweroff="/usr/bin/gpioset --mode='time' --sec='3' '0' '27'='1'"

View File

@@ -0,0 +1,15 @@
[Unit]
Description=Geekworm NASPi Button Daemon
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/lib/geekworm-naspi/button 0 4 17
ProtectSystem=strict
Restart=always
RestartSec=15
RestartPreventExitStatus=0
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,15 @@
[Unit]
Description=Geekworm NASPi Power Management Daemon
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/lib/geekworm-naspi/pwmd
ProtectSystem=strict
Restart=always
RestartSec=15
RestartPreventExitStatus=0
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,42 @@
#!/usr/bin/bash
###
#
# Options Section
#
###
set -e
set -u
set -o pipefail
###
#
# Runtime Environment
#
###
/usr/bin/gpioset '0' '17'='1'
while [ 1 ]; do
SIGNAL=$(/usr/bin/gpioget '0' '4')
if [ "${SIGNAL}" -eq '0' ]; then
/usr/bin/sleep '0.2s'
else
PULSE=$(/bin/date '+%s%N' | /usr/bin/cut '-b1-13')
while [ "${SIGNAL}" -eq '1' ]; do
/usr/bin/sleep '0.02s'
if [ $(($(/bin/date '+%s%N' | /usr/bin/cut '-b1-13')-${PULSE})) -gt '600' ]; then
/usr/sbin/poweroff
exit 0
fi
SIGNAL=$(/usr/bin/gpioget '0' '4')
done
if [ $(($(/usr/bin/date '+%s%N' | /usr/bin/cut '-b1-13')-${PULSE})) -gt '200' ]; then
/usr/sbin/reboot
exit 0
fi
fi
done

View File

@@ -0,0 +1,157 @@
#!/usr/bin/bash
###
#
# Options Section
#
###
set -e
set -o pipefail
###
#
# Variable Section
#
###
CURRENT_DUTY_CYCLE='-1'
PWM_CHIP_PATH='/sys/class/pwm/pwmchip0'
###
#
# Function Section
#
###
function num_compare () {
/usr/bin/mawk "BEGIN {exit !($*)}"
}
function pwm_get_chip_path () {
local SUB_PATH="${1}"
if [ -n "${SUB_PATH}" ]; then
/usr/bin/echo "${PWM_CHIP_PATH}/${SUB_PATH}"
else
/usr/bin/echo "${PWM_CHIP_PATH}"
fi
}
function pwm_get_channel_path () {
local CHANNEL="${1}"
local SUB_PATH="${2}"
local PWM_PATH="${PWM_CHIP_PATH}/pwm${CHANNEL}"
if [ -n "${SUB_PATH}" ]; then
/usr/bin/echo "${PWM_PATH}/${SUB_PATH}"
else
/usr/bin/echo "${PWM_PATH}"
fi
}
function pwm_create () {
local CHANNEL="${1}"
PWM_CHANNEL_PATH="$(pwm_get_channel_path "${CHANNEL}")"
if [[ ! -d "${PWM_CHANNEL_PATH}" ]]; then
/usr/bin/echo "${CHANNEL}" > "$(pwm_get_chip_path 'export')"
fi
}
function frequency_to_period () {
local FREQUENCY="${1}"
PERC=$(/usr/bin/mawk -v freq="${FREQUENCY}" 'BEGIN { printf "%.0f", 1 / freq * 1000 * 1000000; exit(0) }')
/usr/bin/echo "${PERC}"
}
function pwm_change_duty_cycle () {
local CHANNEL="${1}"
local FREQUENCY="${2}"
local DUTY_CYCLE="${3}"
if [[ "${DUTY_CYCLE}" -lt '0' ]] || [[ "${DUTY_CYCLE}" -gt '100' ]]; then
return 1
fi
PERIOD=$(frequency_to_period "${FREQUENCY}")
NEW_DUTY_CYCLE=$(/usr/bin/mawk -v period="${PERIOD}" -v dc="${DUTY_CYCLE}" 'BEGIN { printf "%.0f", period * dc / 100; exit(0) }')
/usr/bin/echo "${NEW_DUTY_CYCLE}" > "$(pwm_get_channel_path "${CHANNEL}" 'duty_cycle')"
}
function pwm_change_frequency () {
local CHANNEL="${1}"
local FREQUENCY="${2}"
if num_compare "${FREQUENCY} < 0.1"; then
return 1
fi
if [[ "${CURRENT_DUTY_CYCLE}" == '-1' ]]; then
CURRENT_DUTY_CYCLE='0'
elif [[ "${CURRENT_DUTY_CYCLE}" -gt '0' ]]; then
pwm_change_duty_cycle "${CHANNEL}" "${FREQUENCY}" '0'
fi
PERIOD=$(frequency_to_period "${FREQUENCY}")
/usr/bin/echo "${PERIOD}" > "$(pwm_get_channel_path "${CHANNEL}" 'period')"
pwm_change_duty_cycle "${CHANNEL}" "${FREQUENCY}" "${CURRENT_DUTY_CYCLE}"
}
function pwm_start () {
local CHANNEL="${1}"
/usr/bin/echo '1' > "$(pwm_get_channel_path "${CHANNEL}" 'enable')"
}
function pwm_stop () {
local CHANNEL="${1}"
/usr/bin/echo "${CHANNEL}" > "$(pwm_get_chip_path 'unexport')"
}
function pwm_init () {
local CHANNEL="${1}"
local FREQUENCY="${2}"
if [[ ! -d "${PWM_CHIP_PATH}" ]]; then
exit 1
fi
if [[ ! -w "$(pwm_get_chip_path 'export')" ]]; then
exit 1
fi
if [[ "${CHANNEL}" != '0' ]] && [[ "${CHANNEL}" != '1' ]]; then
exit 1
fi
pwm_create "${CHANNEL}"
pwm_change_frequency "${CHANNEL}" "${FREQUENCY}"
pwm_start "${CHANNEL}"
}
function get_temp () {
RAW_TEMP="$(/usr/bin/cat '/sys/class/thermal/thermal_zone0/temp')"
TEMP="$(/usr/bin/mawk -v temp="${RAW_TEMP}" 'BEGIN { printf "%0.2f", temp / 1000; exit(0) }')"
/usr/bin/echo "${TEMP}"
}
###
#
# Runtime Environment
#
###
trap "pwm_stop '0'" EXIT
pwm_init '0' '2000'
while true; do
TEMP="$(get_temp)"
DUTY_CYCLE='0'
printf -v 'CUR_TEMP' %0.0f "${TEMP}"
if [[ "${CUR_TEMP}" -ge '70' ]]; then
DUTY_CYCLE='100'
elif [[ "${CUR_TEMP}" -ge '60' ]]; then
DUTY_CYCLE='75'
elif [[ "${CUR_TEMP}" -ge '50' ]]; then
DUTY_CYCLE='50'
elif [[ "${CUR_TEMP}" -ge '25' ]]; then
DUTY_CYCLE='40'
fi
if [[ "${DUTY_CYCLE}" != "${CURRENT_DUTY_CYCLE}" ]]; then
pwm_change_duty_cycle '0' '2000' "${DUTY_CYCLE}"
CURRENT_DUTY_CYCLE="${DUTY_CYCLE}"
fi
/usr/bin/sleep '15s'
done