Rebuilt battery scripts (#645)

* rebuilt thinkpad and ideapad

* updated conf example
This commit is contained in:
PurpleWazard 2024-02-13 12:34:11 -06:00 committed by GitHub
parent 005b4aa178
commit 539914a545
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 57 deletions

View File

@ -54,8 +54,8 @@ turbo = auto
# enable thresholds true or false # enable thresholds true or false
#enable_thresholds = true #enable_thresholds = true
# #
# start threshold (defaults to 0 ) can be 0 - 100 # start threshold (0 is off ) can be 0-99
#start_threshold = 0 #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 #stop_threshold = 100

View File

@ -7,7 +7,8 @@ from auto_cpufreq.battery_scripts.ideapad import *
def lsmod(module): 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: if module in output.stdout:
return True return True
else: else:
@ -38,9 +39,11 @@ def battery_setup():
if conf.has_option("battery", "enable_thresholds"): if conf.has_option("battery", "enable_thresholds"):
if conf["battery"]["enable_thresholds"] == "true": if conf["battery"]["enable_thresholds"] == "true":
if lsmod("thinkpad_acpi"): if lsmod("thinkpad_acpi"):
thinkpad_setup(battery_start_threshold(), battery_stop_threshold()) thinkpad_setup(battery_start_threshold(),
battery_stop_threshold())
elif lsmod("ideapad_acpi"): elif lsmod("ideapad_acpi"):
ideapad_setup(battery_start_threshold(), battery_stop_threshold()) ideapad_setup(battery_start_threshold(),
battery_stop_threshold())
else: else:
pass pass
else: else:

View File

@ -1,42 +1,37 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import subprocess
from auto_cpufreq.core import root_check from auto_cpufreq.core import root_check
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}")
def ideapad_setup(start_threshold, stop_threshold): def ideapad_setup(start_threshold, stop_threshold):
root_check() root_check()
# this path is specific to ideapads battery_count = len([name for name in os.listdir("/sys/class/power_supply/") if name.startswith('BAT')])
path_to_bats = '/sys/class/power_supply/' for bat in range(battery_count):
# gets the numb of batteries set_battery(start_threshold, "start", bat)
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')]) set_battery(stop_threshold, "stop", 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(): def ideapad_print_thresholds():
root_check() root_check()
path_to_bats = '/sys/class/power_supply/' battery_count = len([name for name in os.listdir(
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')]) "/sys/class/power_supply/") if name.startswith('BAT')])
print(f"number of batteries = {battery_count}") print(f"number of batteries = {battery_count}")
for b in range(battery_count): for b in range(battery_count):
try: 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()}') print(f'battery{b} start threshold is set to {f.read()}')
f.close() 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()}') print(f'battery{b} stop threshold is set to {f.read()}')
f.close() f.close()

View File

@ -1,49 +1,37 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import subprocess
from auto_cpufreq.core import root_check from auto_cpufreq.core import root_check
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}")
def thinkpad_setup(start_threshold, stop_threshold): def thinkpad_setup(start_threshold, stop_threshold):
root_check() root_check()
# this path is specific to thinkpads battery_count = len([name for name in os.listdir("/sys/class/power_supply/") if name.startswith('BAT')])
path_to_bats = '/sys/class/power_supply/' for bat in range(battery_count):
# gets the numb of batteries set_battery(start_threshold, "start", bat)
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')]) set_battery(stop_threshold, "stop", 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(): def thinkpad_print_thresholds():
root_check() root_check()
# this path is specific to thinkpads battery_count = len([name for name in os.listdir(
path_to_bats = '/sys/class/power_supply/' "/sys/class/power_supply/") if name.startswith('BAT')])
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
print(f"number of batteries = {battery_count}") print(f"number of batteries = {battery_count}")
for b in range(battery_count): for b in range(battery_count):
try: 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()}') print(f'battery{b} start threshold is set to {f.read()}')
f.close() 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()}') print(f'battery{b} stop threshold is set to {f.read()}')
f.close() f.close()