From: Christian Heller <c.heller@plomlompom.de>
Date: Sun, 3 Nov 2024 00:12:11 +0000 (+0100)
Subject: Improve Raspi setup.
X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7Bprefix%7D%7D/new_day?a=commitdiff_plain;h=6cfb3b5097ff3ed87a8514af39d2c2a8633ba6e8;p=config

Improve Raspi setup.
---

diff --git a/many_releases/etc/raspi/systemd/system/throttle_cpu.service b/many_releases/etc/raspi/systemd/system/throttle_cpu.service
new file mode 100644
index 0000000..58043aa
--- /dev/null
+++ b/many_releases/etc/raspi/systemd/system/throttle_cpu.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Monitor temperature to throttle CPU if necessary
+
+[Service]
+Type=simple
+ExecStart=/root/throttle_cpu.sh
+Restart=always
+RestartSec=10
+User=root
+
+[Install]
+WantedBy=multi-user.target
diff --git a/many_releases/home/raspi/.nonpath_bins/status.sh b/many_releases/home/raspi/.nonpath_bins/status.sh
index 418a52b..c58e8bf 100755
--- a/many_releases/home/raspi/.nonpath_bins/status.sh
+++ b/many_releases/home/raspi/.nonpath_bins/status.sh
@@ -1,11 +1,13 @@
 #!/bin/sh
 # see sway-bar(5) and swaybar-protocol(7)
+MEGA=1000
+GIGA=1000000
 printf '{ "version": 1 }\n[\n' 
 while true; do
   printf '  [\n'
   printf '    {"full_text": "%s"},\n' "$(ip -4 addr show scope global | grep -oP '(?<=inet\s)\d+(\.\d+){3}')"
-  printf '    {"full_text": "%d° C"},\n' $(calc "$(cat /sys/class/thermal/thermal_zone0/temp) // 1000")
-  printf '    {"full_text": "%.1f GHz"},\n' $(calc "$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) / 1000000")
+  printf '    {"full_text": "%d° C"},\n' $(calc "$(cat /sys/class/thermal/thermal_zone0/temp) // ${MEGA}")
+  printf '    {"full_text": "%.1f/%.1f GHz"},\n' $(calc "$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) / ${GIGA}") $(calc "$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq) / ${GIGA}") 
   printf '    {"full_text": "%s"},\n' "$(date +'%Y-%m-%d %X')"
   printf '  ],\n'
   sleep 1
diff --git a/many_releases/home/raspi_root/throttle_cpu.sh b/many_releases/home/raspi_root/throttle_cpu.sh
new file mode 100755
index 0000000..99f5549
--- /dev/null
+++ b/many_releases/home/raspi_root/throttle_cpu.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+set -e
+
+MED_TEMP=65000
+MAX_TEMP=80000
+
+MIN_FREQ=600000
+MED_FREQ=1000000
+MAX_FREQ=1500000
+
+PATH_TEMP='/sys/class/thermal/thermal_zone0/temp'
+
+set_max_freq() {
+  for cpu in /sys/devices/system/cpu/cpu[0-9]*; do
+    echo "$1" > "$cpu/cpufreq/scaling_max_freq" 
+  done
+  echo "$1"
+}
+
+freq_set=$(set_max_freq "${MIN_FREQ}")
+while true; do
+  temp=$(cat "${PATH_TEMP}")
+  if [ "${temp}" -ge "${MAX_TEMP}" ]; then
+    if [ "${freq_set}" -gt "${MIN_FREQ}" ]; then
+      freq_set=$(set_max_freq "${MIN_FREQ}")
+    fi
+  elif [ "${temp}" -ge "${MED_TEMP}" ]; then
+    if [ "${freq_set}" -gt "${MED_FREQ}" ]; then
+      freq_set=$(set_max_freq "${MED_FREQ}")
+    fi
+  elif [ "${freq_set}" -lt "${MAX_FREQ}" ]; then 
+    freq_set=$(set_max_freq "${MAX_FREQ}")
+  fi 
+  sleep 1
+done
diff --git a/many_releases/scripts/_misc.sh b/many_releases/scripts/_misc.sh
index 98b486f..756c9f5 100755
--- a/many_releases/scripts/_misc.sh
+++ b/many_releases/scripts/_misc.sh
@@ -148,7 +148,6 @@ setup_for_desktop() {
     for root in "${PATH_MANY}" '..'; do
         copy_dirtree "${root}${PATH_REL_HOME}" "${PATH_USER_HOME}" all desktop $@
     done
-    cp -a "${PATH_REPO}" "${PATH_USER_HOME}"
     mkdir "${PATH_USER_SSH}"
     cp "${PATH_AUTHORIZED_KEYS}" "${PATH_USER_SSH}"
     chown -R "${USERNAME}:${USERNAME}" "${PATH_USER_HOME}"
diff --git a/testing/scripts/setup_raspi.sh b/testing/scripts/setup_raspi.sh
index 995a822..9131b13 100755
--- a/testing/scripts/setup_raspi.sh
+++ b/testing/scripts/setup_raspi.sh
@@ -16,3 +16,7 @@ upgrade_from_older_release
 
 # actual setup
 setup_for_desktop raspi
+
+copy_dirtree "${PATH_MANY}${PATH_REL_HOME}" '/root' raspi_root
+# systemctl enable throttle_cpu.service
+# systemctl start throttle_cpu.service