From e7775c1ef23e859e4353a9df9af7bf2c73399d42 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 17 Feb 2024 16:48:24 -0600 Subject: [PATCH 1/5] adding support for lenovo ideapad_laptop devices --- auto_cpufreq/battery_scripts/battery.py | 54 ++--------- auto_cpufreq/battery_scripts/ideapad.py | 39 -------- auto_cpufreq/battery_scripts/ideapad_acpi.py | 59 ++++++++++++ .../battery_scripts/ideapad_laptop.py | 96 +++++++++++++++++++ auto_cpufreq/battery_scripts/thinkpad.py | 40 ++++++-- 5 files changed, 194 insertions(+), 94 deletions(-) delete mode 100644 auto_cpufreq/battery_scripts/ideapad.py create mode 100644 auto_cpufreq/battery_scripts/ideapad_acpi.py create mode 100644 auto_cpufreq/battery_scripts/ideapad_laptop.py diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 0cc6fe5d..45ae52e8 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,9 +1,9 @@ #!/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 * +from auto_cpufreq.battery_scripts.ideapad_laptop import * def lsmod(module): @@ -15,55 +15,19 @@ def lsmod(module): 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_setup(): + if lsmod("thinkpad_acpi"): + thinkpad_setup() -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 + elif lsmod("ideapad_acpi"): + ideapad_setup() + elif lsmod("ideapad_laptop"): + ideapad_laptop_setup() -def battery_setup(): - root_check() - conf = get_config() - 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()) - elif lsmod("ideapad_acpi"): - ideapad_setup(battery_start_threshold(), - battery_stop_threshold()) - else: - pass - else: - pass else: - pass + return def battery_get_thresholds(): - conf = get_config() - if conf.has_option("battery", "enable_thresholds"): - 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 - else: - return diff --git a/auto_cpufreq/battery_scripts/ideapad.py b/auto_cpufreq/battery_scripts/ideapad.py deleted file mode 100644 index b19ed878..00000000 --- a/auto_cpufreq/battery_scripts/ideapad.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 -import os -import subprocess -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): - 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() - 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'/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'/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() - - except Exception as e: - print(f"Error reading battery thresholds: {e}") diff --git a/auto_cpufreq/battery_scripts/ideapad_acpi.py b/auto_cpufreq/battery_scripts/ideapad_acpi.py new file mode 100644 index 00000000..858e1912 --- /dev/null +++ b/auto_cpufreq/battery_scripts/ideapad_acpi.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +import os +import subprocess +from auto_cpufreq.core import get_config + + +def set_battery(value, mode, bat): + 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 get_threshold_value(mode): + + config = get_config() + if config.has_option("battery", f"{mode}_threshold"): + return config["battery"][f"{mode}_threshold"] + else: + if mode == "start": + + return 0 + else: + return 100 + + +def ideapad_setup(): + config = get_config() + + if not config.had_option("battery", "enable_thresholds"): + return + if not config["battery"]["enable_thresholds"] == "true": + return + + 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(get_threshold_value("start"), "start", bat) + set_battery(get_threshold_value("stop"), "stop", bat) + + +def ideapad_print_thresholds(): + 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'/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'/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() + + except Exception as e: + print(f"Error reading battery thresholds: {e}") diff --git a/auto_cpufreq/battery_scripts/ideapad_laptop.py b/auto_cpufreq/battery_scripts/ideapad_laptop.py new file mode 100644 index 00000000..061b7a0f --- /dev/null +++ b/auto_cpufreq/battery_scripts/ideapad_laptop.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +import os +import subprocess +from auto_cpufreq.core import get_config + + +def set_battery(value, mode, bat): + 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 get_threshold_value(mode): + + config = get_config() + if config.has_option("battery", f"{mode}_threshold"): + return config["battery"][f"{mode}_threshold"] + else: + if mode == "start": + + return 0 + else: + return 100 + + +conservation_mode(value): + try: + subprocess.check_output( + f"echo {value} | tee /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True) + print(f"conservation_mode is {value}") + return + except: + print("unable to set conservation mode") + return + + +check_conservation_mode(): + try: + value = subprocess.check_output( + "cat /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True) + if value == "1": + return True + elif value == "0": + return False + else: + print("could not get value from conservation mode") + return True + except: + print("could not get the value from conservation mode") + return False + + +def ideapad_laptop_pad_setup(): + config = get_config() + + if not config.had_option("battery", "enable_thresholds"): + return + if not config["battery"]["enable_thresholds"] == "true": + return + + battery_count = len([name for name in os.listdir( + "/sys/class/power_supply/") if name.startswith('BAT')]) + + if config.has_option("battery", "ideapad_laptop_conservation_mode"): + if config["battery"]["ideapad_laptop_conservation_mode"] == "true": + conservation_mode(1) + return + elif config["battery"]["ideapad_laptop_conservation_mode"] == "false": + conservation_mode(0) + + if check_conservation_mode() == False: + for bat in range(battery_count): + 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") + + +def ideapad_laptop_print_thresholds(): + 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'/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'/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() + + except Exception as e: + print(f"Error reading battery thresholds: {e}") diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index be1c8b47..00ab9ca6 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -1,27 +1,47 @@ #!/usr/bin/env python3 import os import subprocess -from auto_cpufreq.core import root_check +from auto_cpufreq.core import get_config 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) + 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}") + print(f"Error writing to file_path: {e}") -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')]) +def get_threshold_value(mode): + + config = get_config() + if config.has_option("battery", f"{mode}_threshold"): + return config["battery"][f"{mode}_threshold"] + else: + if mode == "start": + + return 0 + else: + return 100 + + +def thinkpad_setup(): + config = get_config() + + if not config.had_option("battery", "enable_thresholds"): + return + if not config["battery"]["enable_thresholds"] == "true": + return + + 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) + set_battery(get_threshold_value("start"), "start", bat) + set_battery(get_threshold_value("stop"), "stop", bat) def thinkpad_print_thresholds(): - root_check() battery_count = len([name for name in os.listdir( "/sys/class/power_supply/") if name.startswith('BAT')]) print(f"number of batteries = {battery_count}") From 32b10ea46f08bdd31b18d500ea60d9de90c2ba71 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 17 Feb 2024 17:02:04 -0600 Subject: [PATCH 2/5] testing lenovo_laptop --- auto_cpufreq/battery_scripts/ideapad_laptop.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/auto_cpufreq/battery_scripts/ideapad_laptop.py b/auto_cpufreq/battery_scripts/ideapad_laptop.py index 061b7a0f..da65d17d 100644 --- a/auto_cpufreq/battery_scripts/ideapad_laptop.py +++ b/auto_cpufreq/battery_scripts/ideapad_laptop.py @@ -19,13 +19,12 @@ def get_threshold_value(mode): return config["battery"][f"{mode}_threshold"] else: if mode == "start": - return 0 else: return 100 -conservation_mode(value): +def conservation_mode(value): try: subprocess.check_output( f"echo {value} | tee /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True) @@ -36,7 +35,7 @@ def get_threshold_value(mode): return -check_conservation_mode(): +def check_conservation_mode(): try: value = subprocess.check_output( "cat /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True) @@ -46,7 +45,7 @@ def get_threshold_value(mode): return False else: print("could not get value from conservation mode") - return True + return None except: print("could not get the value from conservation mode") return False @@ -67,10 +66,10 @@ def ideapad_laptop_pad_setup(): if config["battery"]["ideapad_laptop_conservation_mode"] == "true": conservation_mode(1) return - elif config["battery"]["ideapad_laptop_conservation_mode"] == "false": + if config["battery"]["ideapad_laptop_conservation_mode"] == "false": conservation_mode(0) - if check_conservation_mode() == False: + if check_conservation_mode() is False: for bat in range(battery_count): set_battery(get_threshold_value("start"), "start", bat) set_battery(get_threshold_value("stop"), "stop", bat) @@ -79,6 +78,10 @@ def ideapad_laptop_pad_setup(): def ideapad_laptop_print_thresholds(): + if check_conservation_mode() is True: + print("conservation mode is on") + return + battery_count = len([name for name in os.listdir( "/sys/class/power_supply/") if name.startswith('BAT')]) print(f"number of batteries = {battery_count}") From 2eb2110a9b39a0e40aa3fa4a15689fe069fd1a3d Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 17 Feb 2024 17:09:47 -0600 Subject: [PATCH 3/5] renamed functions --- auto_cpufreq/battery_scripts/battery.py | 21 +++++++++++++++---- auto_cpufreq/battery_scripts/ideapad_acpi.py | 4 ++-- .../battery_scripts/ideapad_laptop.py | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 45ae52e8..392f017c 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 import subprocess -from auto_cpufreq.battery_scripts.thinkpad import * -from auto_cpufreq.battery_scripts.ideapad import * -from auto_cpufreq.battery_scripts.ideapad_laptop import * +from auto_cpufreq.battery_scripts.thinkpad import thinkpad_setup, thinkpad_print_thresholds +from auto_cpufreq.battery_scripts.ideapad import ideapad_acpi_setup, ideapad_acpi_print_thresholds +from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_setup, ideapad_laptop_print_thresholds def lsmod(module): @@ -21,7 +21,7 @@ def battery_setup(): thinkpad_setup() elif lsmod("ideapad_acpi"): - ideapad_setup() + ideapad_acpi_setup() elif lsmod("ideapad_laptop"): ideapad_laptop_setup() @@ -31,3 +31,16 @@ def battery_setup(): def battery_get_thresholds(): + if lsmod("thinkpad_acpi"): + thinkpad_print_thresholds() + + elif lsmod("ideapad_acpi"): + ideapad_acpi_print_thresholds() + + elif lsmod("ideapad_laptop"): + ideapad_laptop_print_thresholds() + + else: + return + + diff --git a/auto_cpufreq/battery_scripts/ideapad_acpi.py b/auto_cpufreq/battery_scripts/ideapad_acpi.py index 858e1912..594ba5c9 100644 --- a/auto_cpufreq/battery_scripts/ideapad_acpi.py +++ b/auto_cpufreq/battery_scripts/ideapad_acpi.py @@ -25,7 +25,7 @@ def get_threshold_value(mode): return 100 -def ideapad_setup(): +def ideapad_acpi_setup(): config = get_config() if not config.had_option("battery", "enable_thresholds"): @@ -41,7 +41,7 @@ def ideapad_setup(): set_battery(get_threshold_value("stop"), "stop", bat) -def ideapad_print_thresholds(): +def ideapad_acpi_print_thresholds(): battery_count = len([name for name in os.listdir( "/sys/class/power_supply/") if name.startswith('BAT')]) print(f"number of batteries = {battery_count}") diff --git a/auto_cpufreq/battery_scripts/ideapad_laptop.py b/auto_cpufreq/battery_scripts/ideapad_laptop.py index da65d17d..7963e68c 100644 --- a/auto_cpufreq/battery_scripts/ideapad_laptop.py +++ b/auto_cpufreq/battery_scripts/ideapad_laptop.py @@ -51,7 +51,7 @@ def check_conservation_mode(): return False -def ideapad_laptop_pad_setup(): +def ideapad_laptop_setup(): config = get_config() if not config.had_option("battery", "enable_thresholds"): From f3beb67e8f1f40a38a04523f48d7ae530e573958 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 17 Feb 2024 22:53:51 -0600 Subject: [PATCH 4/5] fix code bugs --- auto_cpufreq/battery_scripts/battery.py | 2 +- auto_cpufreq/battery_scripts/ideapad_acpi.py | 2 +- auto_cpufreq/battery_scripts/ideapad_laptop.py | 2 +- auto_cpufreq/battery_scripts/thinkpad.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 392f017c..7ebb9d0e 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -2,7 +2,7 @@ import subprocess from auto_cpufreq.battery_scripts.thinkpad import thinkpad_setup, thinkpad_print_thresholds -from auto_cpufreq.battery_scripts.ideapad import ideapad_acpi_setup, ideapad_acpi_print_thresholds +from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_setup, ideapad_acpi_print_thresholds from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_setup, ideapad_laptop_print_thresholds diff --git a/auto_cpufreq/battery_scripts/ideapad_acpi.py b/auto_cpufreq/battery_scripts/ideapad_acpi.py index 594ba5c9..ca5f9288 100644 --- a/auto_cpufreq/battery_scripts/ideapad_acpi.py +++ b/auto_cpufreq/battery_scripts/ideapad_acpi.py @@ -28,7 +28,7 @@ def get_threshold_value(mode): def ideapad_acpi_setup(): config = get_config() - if not config.had_option("battery", "enable_thresholds"): + if not config.has_option("battery", "enable_thresholds"): return if not config["battery"]["enable_thresholds"] == "true": return diff --git a/auto_cpufreq/battery_scripts/ideapad_laptop.py b/auto_cpufreq/battery_scripts/ideapad_laptop.py index 7963e68c..f424a832 100644 --- a/auto_cpufreq/battery_scripts/ideapad_laptop.py +++ b/auto_cpufreq/battery_scripts/ideapad_laptop.py @@ -54,7 +54,7 @@ def check_conservation_mode(): def ideapad_laptop_setup(): config = get_config() - if not config.had_option("battery", "enable_thresholds"): + if not config.has_option("battery", "enable_thresholds"): return if not config["battery"]["enable_thresholds"] == "true": return diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index 00ab9ca6..214ed402 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -28,7 +28,7 @@ def get_threshold_value(mode): def thinkpad_setup(): config = get_config() - if not config.had_option("battery", "enable_thresholds"): + if not config.has_option("battery", "enable_thresholds"): return if not config["battery"]["enable_thresholds"] == "true": return From 941b35b90751ed3fee32f6c0ec1166565bef4e95 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Thu, 7 Mar 2024 22:03:46 -0600 Subject: [PATCH 5/5] updated readme with lenovo laptop conservation mode. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 34bc7b28..bdb67690 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,11 @@ start_threshold = 20 stop_threshold = 80 ``` +### Lenovo_laptop conservation mode + +this works only with `lenovo_laptop` kernel module compatable laptops. + +add `ideapad_laptop_conservation_mode = true` to your `auto-cpufreq.conf` file ## Troubleshooting