diff --git a/README.md b/README.md index f194941..50a0ee3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # PowerTools -Power tweaks for Steam Deck. +![plugin_demo](https://raw.githubusercontent.com/NGnius/PowerTools/master/extras/ui.png) + +Steam Deck power tweaks for power users. This is generated from the template plugin for the [SteamOS Plugin Loader](https://github.com/SteamDeckHomebrew/PluginLoader). diff --git a/extras/ui.png b/extras/ui.png new file mode 100644 index 0000000..f467948 Binary files /dev/null and b/extras/ui.png differ diff --git a/main.py b/main.py index 9ae293d..52025be 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import time class Plugin: CPU_COUNT = 8 + SCALING_FREQUENCIES = [1700000, 2400000, 2800000] # call from main_view.html with setCPUs(onclick_event) or call_plugin_method("set_cpus", count) async def set_cpus(self, count) -> int: @@ -32,6 +33,23 @@ class Plugin: async def get_boost(self) -> bool: return read_from_sys("/sys/devices/system/cpu/cpufreq/boost") == "1" + async def set_max_boost(self, index) -> int: + if index >= len(self.SCALING_FREQUENCIES): + return False + selected_freq = self.SCALING_FREQUENCIES[index] + updated = 0 + for cpu in range(0, self.CPU_COUNT): + if cpu == 0 or status_cpu(cpu): + path = cpu_freq_scaling_path(cpu) + write_to_sys(path, selected_freq) + updated += 1 + return updated + + async def get_max_boost(self) -> int: + path = cpu_freq_scaling_path(0) + freq = int(read_from_sys(path, amount=-1).strip()) + return self.SCALING_FREQUENCIES.index(freq) + # Asyncio-compatible long-running code, executed in a task when the plugin is loaded async def _main(self): with open("/home/deck/PowerTools.log", "w") as f: @@ -42,15 +60,18 @@ class Plugin: # these are stateless (well, the state is not saved internally) functions, so there's no need for these to be called like a class method def cpu_online_path(cpu_number: int) -> str: - return f"/sys/devices/system/cpu/cpu{cpu_number}/online" + return f"/sys/devices/system/cpu/cpu{cpu_number}/online" + +def cpu_freq_scaling_path(cpu_number: int) -> str: + return f"/sys/devices/system/cpu/cpu{cpu_number}/cpufreq/scaling_setspeed" def write_to_sys(path, value: int): with open(path, mode="w") as f: f.write(str(value)) -def read_from_sys(path): +def read_from_sys(path, amount=1): with open(path, mode="r") as f: - return f.read(1) + return f.read(amount) def enable_cpu(cpu_number: int): filepath = cpu_online_path(cpu_number) diff --git a/main_view.html b/main_view.html index 9a100d0..3c7b0bb 100644 --- a/main_view.html +++ b/main_view.html @@ -21,41 +21,28 @@ function getCPUBoost() { return call_plugin_method("get_boost", {}); } + + function setMaxBoost(index) { + return call_plugin_method("set_max_boost", {"index": index}); + } + + function getMaxBoost() { + return call_plugin_method("get_max_boost", {}); + } // other logic - async function onSetCPUs() { - let target = document.getElementById("cpu_threads"); - let ok = await setCPUs(target.value); - target.value = await getCPUs(); - } - async function onReady() { - document.getElementById("cpu_threads").value = await getCPUs(); let boostToggle = document.getElementById("boostToggle"); setToggleState(boostToggle, await getCPUBoost()); + selectNotch("cpuThreadsNotch", await getCPUs() - 1, 8); + selectNotch("frequencyNotch", await getMaxBoost(), 3); } - - async function decrementCPUs() { - let target = document.getElementById("cpu_threads"); - if (target.value >= 2) { - target.value -= 1; - } - await onSetCPUs(); - } - - async function incrementCPUs() { - let target = document.getElementById("cpu_threads"); - if (target.value <= 7) { - target.value += 1; - } - await onSetCPUs(); - } - - async function resetCPUs() { - let target = document.getElementById("cpu_threads"); - target.value = 8; - await onSetCPUs(); + + async function setCPUNotch(index) { + const ROOT_ID = "cpuThreadsNotch"; + await setCPUs(index); + selectNotch(ROOT_ID, await getCPUs() - 1, 8); } function setToggleState(toggle, state) { @@ -79,29 +66,87 @@ await setCPUBoost(!isActive); setToggleState(toggle, await getCPUBoost()); } + + async function setBoostNotch(index) { + const ROOT_ID = "frequencyNotch"; + await setMaxBoost(index); + selectNotch(ROOT_ID, await getMaxBoost(), 3); + } + + function selectNotch(rootId, index, elements) { + // WARNING: this yeets any style in div of slider + const ENABLED_CLASS = "gamepadslider_TickActive_j418S"; + //console.log("Selecting notches up to " + index); + let root = document.getElementById(rootId); + root.style = "--normalized-slider-value:" + index/(elements-1) + ";"; + for (let i = 0; i < elements; i++) { + let notch = document.getElementById(rootId + i); + if (notch.classList.contains(ENABLED_CLASS) && i > index) { + notch.classList.remove(ENABLED_CLASS); + } else if (!notch.classList.contains(ENABLED_CLASS) && i <= index) { + notch.classList.add(ENABLED_CLASS); + } + } + } - + -
- - - - - + +
+
+
Threads
+
+
+
+
+
+
+
+
+
+
+
+
1
+
+
+
+
2
+
+
+
+
3
+
+
+
+
4
+
+
+
+
5
+
+
+
+
6
+
+
+
+
7
+
+
+
+
8
+
+
+
+
-
+
CPU Boost @@ -116,5 +161,35 @@
+ +
+
+
Max Frequency
+
+
+
+
+
+
+
+
+
+
+
+
+
1.7GHz
+
+
+
+
2.4GHz
+
+
+
+
2.8GHz
+
+
+
+
+
diff --git a/plugin.json b/plugin.json index b116408..6189f5c 100644 --- a/plugin.json +++ b/plugin.json @@ -6,7 +6,7 @@ "flags": ["root", "_debug"], "publish": { "discord_id": "106537989684887552", - "description": "Additional power management settings", + "description": "Power tweaks for power users", "tags": [ "utility", "power-management" ] } }