Skip to content

Commit

Permalink
add battery optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindimas05 committed Sep 26, 2024
1 parent d429ff6 commit aa33203
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
VERSION=0.3
VERSION=0.4
RELEASE_URL=https://api.github.com/repos/alvindimas05/AMDHelper/releases
INSTALL_URL=https://raw.githubusercontent.com/alvindimas05/AMDHelper/main/install.sh
INSTALL_URL=https://raw.githubusercontent.com/alvindimas05/AMDHelper/main/install.sh
INSTALL_RYZENADJ_URL=https://raw.githubusercontent.com/alvindimas05/AMDHelper/main/install_ryzenadj.sh
10 changes: 10 additions & 0 deletions src/commandline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {PatchType} from "@src/types";
import Chromium, { bashPath, patchChromiumApps, removePatchChromiumApps } from "./patches/chromium";
import { exec } from "./utils";
import child_process from "child_process";
import Ryzenadj from "./scripts/ryzenadj";

export default class CommandLine {
basePath = "/Applications/";
Expand All @@ -31,6 +32,10 @@ export default class CommandLine {
console.log("(A) Patch all apps")
}
console.log(`(C) ${global.commandlineChromiumMode ? "Exit" : "Enter"} Chromium apps mode (${chalk.rgb(255,99,71)("EXPERIMENTAL")})`)

const ryzenadj = new Ryzenadj();
console.log(`(O) ${ryzenadj.enabled() ? "Remove" : "Apply"} battery optimization (${chalk.rgb(255,99,71)("REQUIRES PASSWORD")})`)

console.log("(Q) Quit")

// @ts-ignore
Expand Down Expand Up @@ -72,6 +77,11 @@ export default class CommandLine {
child_process.spawn("bash", [bashPath], { stdio: "ignore", detached: true }).unref();
break;
}
case "o":
const ryzenadj = new Ryzenadj();
if (ryzenadj.enabled()) ryzenadj.remove();
else await ryzenadj.apply();
break;
case "r":
if(global.commandlineChromiumMode){
removePatchChromiumApps();
Expand Down
1 change: 0 additions & 1 deletion src/patches/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
amdhelperChromiumPlistName
} from "@src/scripts/chromium";
import {escapePathSpaces, exec} from "@src/utils";
import child_process from "child_process";

interface ChromiumConfig {
browser?: {
Expand Down
Empty file added src/ryzenadj.ts
Empty file.
File renamed without changes.
57 changes: 57 additions & 0 deletions src/scripts/ryzenadj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { escapePathSpaces, exec } from "@src/utils";
import fs from "fs";
import inquirer from "inquirer";
import path from "path";

const bashPath = path.join("/", "Library", "amdhelper", "amdhelper_ryzenadj");
const plistPath = path.join("/", "Library", "LaunchAgents", "org.alvindimas05.amdhelper_ryzenadj.plist");
const tuning = "--stapm-limit=13000 --slow-limit=14000 --fast-limit=15000 --vrm-current=11000 --vrmmax-current=16000 --vrmsoc-current=0 --vrmsocmax-current=0 --vrmgfx-current=0 --psi0-current=0 --psi0soc-current=0 --tctl-temp=59 --apu-skin-temp=59 --stapm-time=64 --slow-time=128 --power-saving"
export default class Ryzenadj {
enabled(){
return fs.existsSync(bashPath) || fs.existsSync(plistPath);
}
async apply(){
// @ts-ignore
const answers = await inquirer.prompt([{ type: "input", name: "password", message: "Enter Password: " }]);

await this.installRyzenadj();

try { await exec("sudo ryzenadj " + tuning) } catch {}

console.log("Applying battery optimization...");

fs.mkdirSync(path.join(bashPath, ".."), { recursive: true });
fs.writeFileSync(bashPath, `echo '${answers.password}' | sudo -S /usr/local/bin/ryzenadj ${tuning}`);
await exec(`sudo chmod +x ${escapePathSpaces(bashPath)}`);

fs.writeFileSync(plistPath, plist);
}
async installRyzenadj(){
const curl = await exec(`curl -sL ${process.env.INSTALL_RYZENADJ_URL}`);
for(let cmd of curl.stdout.split("\n")){
if(cmd.length === 0) return;
const { stdout } = await exec(cmd);
if(stdout != "") console.log(`${stdout.slice(0, -1)}`);
}
}
async remove(){
console.log("Removing battery optimization...");
try { fs.rmSync(bashPath) } catch {}
try { fs.rmSync(plistPath) } catch {}
}
}
const plist = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.alvindimas05.amdhelper_ryzenadj</string>
<key>ProgramArguments</key>
<array>
<string>/Library/amdhelper/amdhelper_ryzenadj</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>`
1 change: 1 addition & 0 deletions src/scripts/ryzenadj/amdhelper_ryzenadj.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo 'password' | sudo -S /usr/local/bin/ryzenadj --stapm-limit=13000 --slow-limit=14000 --fast-limit=15000 --vrm-current=11000 --vrmmax-current=16000 --vrmsoc-current=0 --vrmsocmax-current=0 --vrmgfx-current=0 --psi0-current=0 --psi0soc-current=0 --tctl-temp=59 --apu-skin-temp=59 --stapm-time=64 --slow-time=128 --power-saving
14 changes: 14 additions & 0 deletions src/scripts/ryzenadj/org.alvindimas05.amdhelper_ryzenadj.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.alvindimas05.amdhelper_ryzenadj</string>
<key>ProgramArguments</key>
<array>
<string>/Library/amdhelper/amdhelper_ryzenadj</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

0 comments on commit aa33203

Please sign in to comment.