Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 28 additions & 43 deletions auto_cpufreq/battery_scripts/asus.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,35 @@
#!/usr/bin/env python3
import os
from subprocess import check_output

from auto_cpufreq.battery_scripts.shared import BatteryDevice
from auto_cpufreq.config.config import config
from auto_cpufreq.globals import POWER_SUPPLY_DIR

def set_battery(value, mode, bat):
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
fallback_mode = "start" if mode == "start" else "end"
fallback_path = f"{POWER_SUPPLY_DIR}{bat}/charge_control_{fallback_mode}_threshold"
if os.path.isfile(path): check_output(f"echo {value} | tee {path}", shell=True, text=True)
elif os.path.isfile(fallback_path): check_output(f"echo {value} | tee {fallback_path}", shell=True, text=True)
else: print(f"WARNING: {path} does NOT exist")

def get_threshold_value(mode):
conf = config.get_config()
return conf["battery"][f"{mode}_threshold"] if conf.has_option("battery", f"{mode}_threshold") else (0 if mode == "start" else 100)

def asus_setup():
conf = config.get_config()

if not (conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true"): return

if os.path.exists(POWER_SUPPLY_DIR):
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')]

for bat in batteries:
set_battery(get_threshold_value("start"), "start", bat)
set_battery(get_threshold_value("stop"), "stop", bat)
else: print(f"WARNING {POWER_SUPPLY_DIR} does NOT esixt")


def asus_print_thresholds():
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')]
print("\n-------------------------------- Battery Info ---------------------------------\n")
print(f"battery count = {len(batteries)}")
for bat in batteries:
try:
primary_start = f"{POWER_SUPPLY_DIR}{bat}/charge_start_threshold"
primary_stop = f"{POWER_SUPPLY_DIR}{bat}/charge_stop_threshold"
fallback_start = f"{POWER_SUPPLY_DIR}{bat}/charge_control_start_threshold"
fallback_stop = f"{POWER_SUPPLY_DIR}{bat}/charge_control_end_threshold"
if os.path.isfile(primary_start): print(bat, "start threshold =", check_output(["cat", primary_start]))
elif os.path.isfile(fallback_start): print(bat, "start threshold =", check_output(["cat", fallback_start]))
else: print(f"{bat} start threshold: file not found")
if os.path.isfile(primary_stop): print(bat, "stop threshold =", check_output(["cat", primary_stop]))
elif os.path.isfile(fallback_stop): print(bat, "stop threshold =", check_output(["cat", fallback_stop]))
else: print(f"{bat} stop threshold: file not found")
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))
class AsusBatteryDevice(BatteryDevice):
def __init__(self):
self.config = config.get_config()
self.batteries = [
name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")
]
self.start_paths = {
bat: [
os.path.join(POWER_SUPPLY_DIR, bat, "charge_start_threshold"),
os.path.join(
POWER_SUPPLY_DIR, bat, "charge_control_start_threshold"
), # Fallback
]
for bat in self.batteries
}
self.stop_paths = {
bat: [
os.path.join(POWER_SUPPLY_DIR, bat, "charge_stop_threshold"),
os.path.join(
POWER_SUPPLY_DIR, bat, "charge_control_end_threshold"
), # Fallback
]
for bat in self.batteries
}

self.start_config_value = self.get_threshold_config_value("start")
self.stop_config_value = self.get_threshold_config_value("stop")
45 changes: 30 additions & 15 deletions auto_cpufreq/battery_scripts/battery.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
#!/usr/bin/env python3
from subprocess import PIPE, run

from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_print_thresholds, ideapad_acpi_setup
from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_print_thresholds, ideapad_laptop_setup
from auto_cpufreq.battery_scripts.thinkpad import thinkpad_print_thresholds, thinkpad_setup
from auto_cpufreq.battery_scripts.asus import asus_print_thresholds, asus_setup
from auto_cpufreq.battery_scripts.asus import AsusBatteryDevice
from auto_cpufreq.battery_scripts.ideapad_laptop import IdeapadBatteryDevice
from auto_cpufreq.battery_scripts.shared import BatteryDevice


def lsmod(module):
return (
module in run(["lsmod"], stdout=PIPE, stderr=PIPE, text=True, shell=True).stdout
)

def lsmod(module): return module in run(['lsmod'], stdout=PIPE, stderr=PIPE, text=True, shell=True).stdout

def battery_get_thresholds():
if lsmod("ideapad_acpi"): ideapad_acpi_print_thresholds()
elif lsmod("ideapad_laptop"): ideapad_laptop_print_thresholds()
elif lsmod("thinkpad_acpi"): thinkpad_print_thresholds()
elif lsmod("asus_wmi"): asus_print_thresholds()
else: return
if lsmod("ideapad_acpi"):
BatteryDevice().print_thresholds()
elif lsmod("ideapad_laptop"):
IdeapadBatteryDevice().print_thresholds()
elif lsmod("thinkpad_acpi"):
BatteryDevice().print_thresholds()
elif lsmod("asus_wmi"):
AsusBatteryDevice.print_thresholds()
else:
return


def battery_setup():
if lsmod("ideapad_acpi"): ideapad_acpi_setup()
elif lsmod("ideapad_laptop"): ideapad_laptop_setup()
elif lsmod("thinkpad_acpi"): thinkpad_setup()
elif lsmod("asus_wmi"): asus_setup()
else: return
if lsmod("ideapad_acpi"):
BatteryDevice().setup()
elif lsmod("ideapad_laptop"):
IdeapadBatteryDevice().setup()
elif lsmod("thinkpad_acpi"):
BatteryDevice().setup()
elif lsmod("asus_wmi"):
AsusBatteryDevice.setup()
else:
return
38 changes: 0 additions & 38 deletions auto_cpufreq/battery_scripts/ideapad_acpi.py

This file was deleted.

114 changes: 60 additions & 54 deletions auto_cpufreq/battery_scripts/ideapad_laptop.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,75 @@
#!/usr/bin/env python3
import os
from subprocess import check_output
from subprocess import CalledProcessError, check_output
import time

from auto_cpufreq.config.config import config
from auto_cpufreq.battery_scripts.shared import BatteryDevice
from auto_cpufreq.globals import CONSERVATION_MODE_FILE, POWER_SUPPLY_DIR

def set_battery(value, mode, bat):
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
if os.path.exists(path):
check_output(f"echo {value} | tee {POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold", shell=True, text=True)
else: print(f"WARNING: {path} does NOT exist")

def get_threshold_value(mode):
conf = config.get_config()
return conf["battery"][f"{mode}_threshold"] if conf.has_option("battery", f"{mode}_threshold") else (0 if mode == "start" else 100)

def conservation_mode(value):
try:
check_output(f"echo {value} | tee {CONSERVATION_MODE_FILE}", shell=True, text=True)
print(f"conservation_mode is {value}")
except: print("unable to set conservation mode")
return

def check_conservation_mode():
try:
value = check_output(["cat", CONSERVATION_MODE_FILE], text=True).rstrip()
if value == "1": return True
elif value == "0": return False
else:
print("could not get value from conservation mode")
return None
except:
print("could not get the value from conservation mode")
return False
class IdeapadBatteryDevice(BatteryDevice):
def check_conservation_mode(self):
try:
value = check_output(["cat", CONSERVATION_MODE_FILE], text=True).rstrip()
if value == "1":
return True
elif value == "0":
return False
else:
print(
f"WARNING: could not get value from conservation mode, unexpected value!: {value}"
)
return None
except CalledProcessError as e:
print(f"WARNING: could not get value from conservation mode: {e.output}")
return False

def ideapad_laptop_setup():
conf = config.get_config()
def set_conservation_mode(value):
try:
check_output(
f"echo {value} | tee {CONSERVATION_MODE_FILE}", shell=True, text=True
)
print(f"conservation_mode is {value}")
except CalledProcessError as e:
print(f"WARNING: unable to set conservation mode: {e.output}")
return

if not (conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true"): return
def setup(self):
if not (
self.config.has_option("battery", "enable_thresholds")
and self.config["battery"]["enable_thresholds"] == "true"
):
return

batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")]
if self.config.has_option("battery", "ideapad_laptop_conservation_mode"):
if self.config["battery"]["ideapad_laptop_conservation_mode"] == "true":
self.conservation_mode(1)
return
if self.config["battery"]["ideapad_laptop_conservation_mode"] == "false":
self.conservation_mode(0)

if conf.has_option("battery", "ideapad_laptop_conservation_mode"):
if conf["battery"]["ideapad_laptop_conservation_mode"] == "true":
conservation_mode(1)
if self.check_conservation_mode():
print("WARNING: conservation mode is enabled unable to set thresholds")
return
if conf["battery"]["ideapad_laptop_conservation_mode"] == "false": conservation_mode(0)

if not check_conservation_mode():
for bat in batteries:
set_battery(get_threshold_value("start"), "start", bat)
set_battery(get_threshold_value("stop"), "stop", bat)
else: print("conservation mode is enabled unable to set thresholds")
if not os.path.exists(POWER_SUPPLY_DIR):
print(f"WARNING: {POWER_SUPPLY_DIR} does NOT exist")
return

def ideapad_laptop_print_thresholds():
if check_conservation_mode():
print("conservation mode is on")
return
batteries = [
name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")
]

batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")]
for bat in batteries:
# First set 0 for start and 100 for stop, sometimes we can have conflicting values otherwise which will result in an 'invalid argument' upon writing.
self.set_battery(bat, "start", 0)
self.set_battery(bat, "stop", 100)
time.sleep(0.1)
self.set_battery(bat, "start", self.start_config_value)
self.set_battery(bat, "stop", self.stop_config_value)

print("\n-------------------------------- Battery Info ---------------------------------\n")
print(f"battery count = {len(batteries)}")
for bat in batteries:
try:
print(bat, "start threshold =", check_output(["cat", POWER_SUPPLY_DIR+bat+"/charge_start_threshold"]))
print(bat, "stop threshold =", check_output(["cat", POWER_SUPPLY_DIR+bat+"/charge_stop_threshold"]))
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))
def print_thresholds(self):
if self.check_conservation_mode():
print("WARNING: conservation mode is on")
return
super().print_thresholds()
Loading
Loading