diff --git a/auto-cpufreq.conf-example b/auto-cpufreq.conf-example index 86afa584..925e79ea 100644 --- a/auto-cpufreq.conf-example +++ b/auto-cpufreq.conf-example @@ -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 diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 36920947..0cc6fe5d 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -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: @@ -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: @@ -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"): diff --git a/auto_cpufreq/battery_scripts/ideapad.py b/auto_cpufreq/battery_scripts/ideapad.py index 5f078219..b19ed878 100644 --- a/auto_cpufreq/battery_scripts/ideapad.py +++ b/auto_cpufreq/battery_scripts/ideapad.py @@ -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() diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index 23270914..be1c8b47 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -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()