Skip to content

Commit

Permalink
Add battery charge thresholds limit (#637)
Browse files Browse the repository at this point in the history
* added battery files

* finished main battery script

* added battery_script in auto_cpufreq

* fix some errors with batterys

* setup some battery stat printing

* added battery config printing to monitor and live modes

* fix some bugs now works on daemon and monitor mode

* cleaned up battery.py

* started to fix writing issue

* still debuging fixes

* bug fixed thinkpad stop threshold cant do below 65

* updated auto-cpufreq.conf-example with battery thresholds
  • Loading branch information
PurpleWazard committed Feb 7, 2024
1 parent 68dec52 commit 1abad4d
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 1 deletion.
14 changes: 14 additions & 0 deletions auto-cpufreq.conf-example
Expand Up @@ -45,3 +45,17 @@ energy_performance_preference = power

# turbo boost setting. possible values: always, auto, never
turbo = auto

# experimental

# Add battery charging threshold (currently only available to Lenovo)
# checkout README.md for more info

# enable thresholds true or false
#enable_thresholds = true
#
# start threshold (defaults to 0 ) can be 0 - 100
#start_threshold = 0
#
# stop threshold (defaults to 100) this value must be greater or equal to 65
#stop_threshold = 100
60 changes: 60 additions & 0 deletions auto_cpufreq/battery_scripts/battery.py
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
import subprocess
from auto_cpufreq.core import get_config, root_check

from auto_cpufreq.battery_scripts.thinkpad import *
from auto_cpufreq.battery_scripts.ideapad import *


def lsmod(module):
output = subprocess.run(['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if module in output.stdout:
return True
else:
return False


def battery_start_threshold():
conf = get_config()
if conf.has_option("battery", "start_threshold"):
start_threshold = conf["battery"]["start_threshold"]
return int(start_threshold)
else:
return 0


def battery_stop_threshold():
conf = get_config()
if conf.has_option("battery", "stop_threshold"):
stop_threshold = conf["battery"]["stop_threshold"]
return int(stop_threshold)
else:
return 100


def battery_setup():
root_check()
conf = get_config()
if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true":
if lsmod("thinkpad_acpi"):
thinkpad_setup(battery_start_threshold(), battery_stop_threshold())
elif lsmod("ideapad_acpi"):
ideapad_setup(battery_start_threshold(), battery_stop_threshold())
else:
pass
else:
pass


def battery_get_thresholds():
conf = get_config()
if conf["battery"]["enable_thresholds"] == "true":
print("-" * 30)
if lsmod("thinkpad_acpi"):
thinkpad_print_thresholds()
elif lsmod("ideapad_acpi"):
ideapad_print_thresholds()
else:
pass
else:
return
44 changes: 44 additions & 0 deletions auto_cpufreq/battery_scripts/ideapad.py
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import os
from auto_cpufreq.core import root_check


def ideapad_setup(start_threshold, stop_threshold):
root_check()
# this path is specific to ideapads
path_to_bats = '/sys/class/power_supply/'
# gets the numb of batteries
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])

for b in range(battery_count):

try:
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'w') as f:
f.write(str(start_threshold) + '\n')
f.close()

with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'w') as f:
f.write(str(stop_threshold) + '\n')
f.close()

except Exception:
pass


def ideapad_print_thresholds():
root_check()
path_to_bats = '/sys/class/power_supply/'
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
print(f"number of batteries = {battery_count}")
for b in range(battery_count):
try:
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'r') as f:
print(f'battery{b} start threshold is set to {f.read()}')
f.close()

with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'r') as f:
print(f'battery{b} stop threshold is set to {f.read()}')
f.close()

except Exception as e:
print(f"Error reading battery thresholds: {e}")
51 changes: 51 additions & 0 deletions auto_cpufreq/battery_scripts/thinkpad.py
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
import os
from auto_cpufreq.core import root_check


def thinkpad_setup(start_threshold, stop_threshold):
root_check()
# this path is specific to thinkpads
path_to_bats = '/sys/class/power_supply/'
# gets the numb of batteries
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])

for b in range(battery_count):

try:
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'w') as f:
f.write(str(start_threshold) + '\n')
f.close()
except Exception as e:
print(f"could not write to BAT{b} start threshold")
print(e)

try:
with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'w') as f:
f.write(str(stop_threshold) + '\n')
f.close()

except Exception as e:
print(f"could not write to BAT{b} stop threshold you might be setting it too low try < 65")
print(e)
pass


def thinkpad_print_thresholds():
root_check()
# this path is specific to thinkpads
path_to_bats = '/sys/class/power_supply/'
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
print(f"number of batteries = {battery_count}")
for b in range(battery_count):
try:
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'r') as f:
print(f'battery{b} start threshold is set to {f.read()}')
f.close()

with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'r') as f:
print(f'battery{b} stop threshold is set to {f.read()}')
f.close()

except Exception as e:
print(f"Error reading battery thresholds: {e}")
6 changes: 5 additions & 1 deletion auto_cpufreq/bin/auto_cpufreq.py
Expand Up @@ -13,7 +13,7 @@
# sys.path.append("../")
from auto_cpufreq.core import *
from auto_cpufreq.power_helper import *

from auto_cpufreq.battery_scripts.battery import *
# cli
@click.command()
@click.option("--monitor", is_flag=True, help="Monitor and see suggestions for CPU optimizations")
Expand Down Expand Up @@ -67,6 +67,7 @@ def config_info_dialog():
if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
gnome_power_detect_snap()
tlp_service_detect_snap()
battery_setup()
while True:
footer()
gov_check()
Expand All @@ -78,6 +79,7 @@ def config_info_dialog():
elif os.getenv("PKG_MARKER") != "SNAP":
gnome_power_detect()
tlp_service_detect()
battery_setup()
while True:
footer()
gov_check()
Expand All @@ -94,6 +96,8 @@ def config_info_dialog():
config_info_dialog()
root_check()
print('\nNote: You can quit monitor mode by pressing "ctrl+c"')
battery_setup()
battery_get_thresholds()
if os.getenv("PKG_MARKER") == "SNAP":
gnome_power_detect_snap()
tlp_service_detect_snap()
Expand Down

0 comments on commit 1abad4d

Please sign in to comment.