|
@@ -1,10 +1,12 @@
|
|
|
#!/bin/bash
|
|
|
#
|
|
|
-# MNT Reform 0.3+ Daemon for Battery, Lid and Fan Control
|
|
|
-# Copyright 2018 MNT Media and Technology UG, Berlin
|
|
|
+# MNT Reform 0.5.0 Daemon for Battery, and Sleep/Wake Control
|
|
|
+# Copyright 2018,2019 MNT Research GmbH, Berlin
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
#
|
|
|
|
|
|
+#set -x
|
|
|
+
|
|
|
set_fan_speed () {
|
|
|
set +e; echo 0 > /sys/class/pwm/pwmchip1/export 2>/dev/null ; set -e
|
|
|
echo 10000 > /sys/class/pwm/pwmchip1/pwm0/period
|
|
@@ -12,6 +14,24 @@ set_fan_speed () {
|
|
|
echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
|
|
|
}
|
|
|
|
|
|
+function get_soc_temperature {
|
|
|
+ read soc_temperature < /sys/class/thermal/thermal_zone0/temp
|
|
|
+}
|
|
|
+
|
|
|
+function regulate_fan {
|
|
|
+ get_soc_temperature
|
|
|
+
|
|
|
+ if [ "$soc_temperature" -lt 62000 ]
|
|
|
+ then
|
|
|
+ set_fan_speed 2900
|
|
|
+ fi
|
|
|
+
|
|
|
+ if [ "$soc_temperature" -gt 70000 ]
|
|
|
+ then
|
|
|
+ set_fan_speed 10000
|
|
|
+ fi
|
|
|
+}
|
|
|
+
|
|
|
function setup_serial {
|
|
|
# 2400 baud 8N1, raw
|
|
|
# cargoculted by exporting parameters with stty after using screen
|
|
@@ -27,18 +47,46 @@ function disable_echo {
|
|
|
set +e; timeout 2 head /dev/ttymxc1; set -e
|
|
|
}
|
|
|
|
|
|
-function get_battery_state {
|
|
|
+function enable_wake_events {
|
|
|
setup_serial
|
|
|
exec 99<>/dev/ttymxc1
|
|
|
:<&99
|
|
|
- printf "p\r" >&99
|
|
|
- IFS=$':\t\r'
|
|
|
- read -r -t 1 msg bat_capacity bat_volts bat_amps <&99
|
|
|
+ printf "1k\r" >&99
|
|
|
exec 99>&-
|
|
|
+ set +e; timeout 2 head /dev/ttymxc1; set -e
|
|
|
}
|
|
|
|
|
|
-function get_soc_temperature {
|
|
|
- read soc_temperature < /sys/class/thermal/thermal_zone0/temp
|
|
|
+function get_bat_capacity {
|
|
|
+ setup_serial
|
|
|
+ exec 99<>/dev/ttymxc1
|
|
|
+ :<&99
|
|
|
+ printf "c\r" >&99
|
|
|
+ IFS='\r\n'
|
|
|
+ read -r -t 1 bat_capacity <&99
|
|
|
+ bat_capacity=${bat_capacity%$'\r'}
|
|
|
+ exec 99>&-
|
|
|
+}
|
|
|
+
|
|
|
+function get_bat_volts {
|
|
|
+ setup_serial
|
|
|
+ exec 99<>/dev/ttymxc1
|
|
|
+ :<&99
|
|
|
+ printf "v\r" >&99
|
|
|
+ IFS='\r\n'
|
|
|
+ read -r -t 1 bat_volts <&99
|
|
|
+ bat_volts=${bat_volts%$'\r'}
|
|
|
+ exec 99>&-
|
|
|
+}
|
|
|
+
|
|
|
+function get_bat_amps {
|
|
|
+ setup_serial
|
|
|
+ exec 99<>/dev/ttymxc1
|
|
|
+ :<&99
|
|
|
+ printf "a\r" >&99
|
|
|
+ IFS='\r\n'
|
|
|
+ read -r -t 1 bat_amps <&99
|
|
|
+ bat_amps=${bat_amps%$'\r'}
|
|
|
+ exec 99>&-
|
|
|
}
|
|
|
|
|
|
function get_lid_state {
|
|
@@ -46,9 +94,9 @@ function get_lid_state {
|
|
|
exec 99<>/dev/ttymxc1
|
|
|
:<&99
|
|
|
printf "l\r" >&99
|
|
|
- IFS=$':\t\r'
|
|
|
- read -r -t 1 msg lid_state <&99
|
|
|
- lid_state=$(echo "$lid_state" | tr -dc '0-9')
|
|
|
+ IFS='\r\n'
|
|
|
+ read -r -t 1 lid_state <&99
|
|
|
+ lid_state=${lid_state%$'\r'}
|
|
|
|
|
|
exec 99>&-
|
|
|
}
|
|
@@ -57,7 +105,7 @@ function reset_bat_capacity {
|
|
|
setup_serial
|
|
|
exec 99<>/dev/ttymxc1
|
|
|
:<&99
|
|
|
- printf "0600c\r" >&99
|
|
|
+ printf "9999c\r" >&99
|
|
|
exec 99>&-
|
|
|
}
|
|
|
|
|
@@ -70,28 +118,15 @@ function system_suspend {
|
|
|
# drain serial port
|
|
|
set +e; timeout 1 head /dev/ttymxc1; set -e
|
|
|
|
|
|
+ # stop the fan
|
|
|
+ set +e
|
|
|
+ echo 0 > /sys/class/pwm/pwmchip1/pwm0/enable
|
|
|
+ set -e
|
|
|
+
|
|
|
# zzZzzZ
|
|
|
echo -n mem > /sys/power/state
|
|
|
-}
|
|
|
-
|
|
|
-function regulate_fan {
|
|
|
- get_soc_temperature
|
|
|
-
|
|
|
- if [ "$soc_temperature" -lt 65000 ]
|
|
|
- then
|
|
|
- set_fan_speed 5000
|
|
|
- fi
|
|
|
-
|
|
|
- if [ "$soc_temperature" -gt 70000 ]
|
|
|
- then
|
|
|
- set_fan_speed 10000
|
|
|
- fi
|
|
|
-}
|
|
|
-
|
|
|
-voltage_alert=0
|
|
|
-function regulate_voltage {
|
|
|
- # TODO low voltage/capacity alert
|
|
|
- echo "not yet implemented"
|
|
|
+
|
|
|
+ exit
|
|
|
}
|
|
|
|
|
|
function main {
|
|
@@ -103,17 +138,19 @@ function main {
|
|
|
# TODO actually append to log and rotate it out
|
|
|
# TODO interval?
|
|
|
timestamp=$(date +%s)
|
|
|
- get_soc_temperature
|
|
|
- get_battery_state
|
|
|
+ get_bat_volts
|
|
|
+ get_bat_amps
|
|
|
+ get_bat_capacity
|
|
|
get_lid_state
|
|
|
|
|
|
- if [ "$bat_amps" == "0.00" ]
|
|
|
+ # clamp bat capacity at 100%
|
|
|
+ if (( $bat_capacity >= 10000 ))
|
|
|
then
|
|
|
reset_bat_capacity
|
|
|
fi
|
|
|
|
|
|
- printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$timestamp" "$soc_temperature" "$bat_capacity" "$bat_volts" "$bat_amps" "$voltage_alert" "$lid_state"
|
|
|
- printf '%s,%s,%s,%s,%s,%s,%s\n' "$timestamp" "$soc_temperature" "$bat_capacity" "$bat_volts" "$bat_amps" "$voltage_alert" "$lid_state" > /var/log/reformd
|
|
|
+ printf '%d\t%d\t%d\t%d\t%d\n' "$timestamp" "$bat_capacity" "$bat_volts" "$bat_amps" "$lid_state"
|
|
|
+ printf '%d,%d,%d,%d,%d\n' "$timestamp" "$bat_capacity" "$bat_volts" "$bat_amps" "$lid_state" > /var/log/reformd
|
|
|
|
|
|
# 3. if the lid is closed, we want to suspend the system
|
|
|
# important: this works only if the kernel option no_console_suspend=1 is set!
|
|
@@ -127,6 +164,12 @@ function main {
|
|
|
}
|
|
|
|
|
|
disable_echo
|
|
|
+enable_wake_events
|
|
|
+
|
|
|
+# enable the fan in case we killed it last time
|
|
|
+set +e
|
|
|
+echo 1 > /sys/class/pwm/pwmchip1/pwm0/enable
|
|
|
+set -e
|
|
|
|
|
|
while true; do
|
|
|
main
|