Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebuilt battery scripts #645

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions auto-cpufreq.conf-example
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ turbo = auto
# enable thresholds true or false
#enable_thresholds = true
#
# start threshold (defaults to 0 ) can be 0 - 100
# start threshold (0 is off ) can be 0-99
#start_threshold = 0
#
# stop threshold (defaults to 100) this value must be greater or equal to 65
# stop threshold (100 is off) can be 1-100
#stop_threshold = 100
11 changes: 7 additions & 4 deletions auto_cpufreq/battery_scripts/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


def lsmod(module):
output = subprocess.run(['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output = subprocess.run(
['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if module in output.stdout:
return True
else:
Expand Down Expand Up @@ -38,9 +39,11 @@ def battery_setup():
if conf.has_option("battery", "enable_thresholds"):
if conf["battery"]["enable_thresholds"] == "true":
if lsmod("thinkpad_acpi"):
thinkpad_setup(battery_start_threshold(), battery_stop_threshold())
thinkpad_setup(battery_start_threshold(),
battery_stop_threshold())
elif lsmod("ideapad_acpi"):
ideapad_setup(battery_start_threshold(), battery_stop_threshold())
ideapad_setup(battery_start_threshold(),
battery_stop_threshold())
else:
pass
else:
Expand All @@ -53,7 +56,7 @@ def battery_get_thresholds():
conf = get_config()
if conf.has_option("battery", "enable_thresholds"):
if conf["battery"]["enable_thresholds"] == "true":
print("-" * 30 )
print("-" * 30)
if lsmod("thinkpad_acpi"):
thinkpad_print_thresholds()
elif lsmod("ideapad_acpi"):
Expand Down
39 changes: 17 additions & 22 deletions auto_cpufreq/battery_scripts/ideapad.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
#!/usr/bin/env python3
import os
import subprocess
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()
def set_battery(value, mode, bat):
file_path = f'/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold'
try:
subprocess.check_output(f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True)
except Exception as e:
print(f"Error writing to {file_path}: {e}")

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_setup(start_threshold, stop_threshold):
root_check()
battery_count = len([name for name in os.listdir("/sys/class/power_supply/") if name.startswith('BAT')])
for bat in range(battery_count):
set_battery(start_threshold, "start", bat)
set_battery(stop_threshold, "stop", bat)


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')])
battery_count = len([name for name in os.listdir(
"/sys/class/power_supply/") 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:
with open(f'/sys/class/power_supply/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:
with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f:
print(f'battery{b} stop threshold is set to {f.read()}')
f.close()

Expand Down
46 changes: 17 additions & 29 deletions auto_cpufreq/battery_scripts/thinkpad.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
#!/usr/bin/env python3
import os
import subprocess
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):
def set_battery(value, mode, bat):
file_path = f'/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold'
try:
subprocess.check_output(f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True)
except Exception as e:
print(f"Error writing to {file_path}: {e}")

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_setup(start_threshold, stop_threshold):
root_check()
battery_count = len([name for name in os.listdir("/sys/class/power_supply/") if name.startswith('BAT')])
for bat in range(battery_count):
set_battery(start_threshold, "start", bat)
set_battery(stop_threshold, "stop", bat)


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')])
battery_count = len([name for name in os.listdir(
"/sys/class/power_supply/") 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:
with open(f'/sys/class/power_supply/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:
with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f:
print(f'battery{b} stop threshold is set to {f.read()}')
f.close()

Expand Down