From 5a0431e8e1c39e21eac6341c0ebe53065be26aff Mon Sep 17 00:00:00 2001 From: purple wazard Date: Fri, 2 Feb 2024 22:22:04 -0600 Subject: [PATCH 01/12] added battery files --- auto_cpufreq/battery_scripts/battery.py | 16 +++++++++ auto_cpufreq/battery_scripts/ideapad | 48 +++++++++++++++++++++++++ auto_cpufreq/battery_scripts/thinkpad | 47 ++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 auto_cpufreq/battery_scripts/battery.py create mode 100644 auto_cpufreq/battery_scripts/ideapad create mode 100644 auto_cpufreq/battery_scripts/thinkpad diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py new file mode 100644 index 00000000..faf73702 --- /dev/null +++ b/auto_cpufreq/battery_scripts/battery.py @@ -0,0 +1,16 @@ +import os +import thinkpad +import ideapad + + +def battery_thresholds(): + return {0, 100} + + +def battery_setup(): + thresholds = battery_thresholds() + lsmod = os.system("lsmod") + if lsmod.find("thinkpad_acpi") is not None: + thinkpad.thinkpad(thresholds[0], thresholds[1]) + elif lsmod.find("ideapad_acpi") is not None: + ideapad.ideapad_setup(thresholds[0], thresholds[1]) diff --git a/auto_cpufreq/battery_scripts/ideapad b/auto_cpufreq/battery_scripts/ideapad new file mode 100644 index 00000000..089c26cd --- /dev/null +++ b/auto_cpufreq/battery_scripts/ideapad @@ -0,0 +1,48 @@ +#! /bin/python +import os + + +def ideapad_setup(start_threshold, stop_threshold): + + # check if thinkpad_acpi is enabled if not this wont work + if os.system("lsmod | grep ideapad_latop") is not None: + # this path is specific to thinkpads + path_to_bats = '/sys/class/power_supply/' + # gets the numb of batterys + 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(): + path_to_bats = '/sys/class/power_supply/' + battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')]) + + print("Battery Info") + + for b in range(battery_count): + + try: + with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'r') as f: + print(f'BAT{b} start is set to {f.read()}') + f.close() + + with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'r') as f: + print(f'BAT{b} stop 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 b/auto_cpufreq/battery_scripts/thinkpad new file mode 100644 index 00000000..1212ee50 --- /dev/null +++ b/auto_cpufreq/battery_scripts/thinkpad @@ -0,0 +1,47 @@ +#! /bin/python +import os + + +def thinkpad_setup(start_threshold, stop_threshold): + + # check if thinkpad_acpi is enabled if not this wont work + if os.system("lsmod | grep thinkpad_acpi") is not None: + # this path is specific to thinkpads + path_to_bats = '/sys/class/power_supply/' + # gets the numb of batterys + 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 + + +# this is for testing and debuging + +def thinkpad_print_thresholds(): + path_to_bats = '/sys/class/power_supply/' + 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', 'r') as f: + print(f'BAT{b} start is set to {f.read()}') + f.close() + + with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'r') as f: + print(f'BAT{b} stop is set to {f.read()}') + f.close() + + except Exception as e: + print(f"Error reading battery thresholds: {e}") From 8c9bb16f78c7ce8b57c3328f0edad7b631c64e9a Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 3 Feb 2024 08:29:24 -0600 Subject: [PATCH 02/12] finished main battery script --- auto_cpufreq/battery_scripts/battery.py | 40 ++++++++++++++++++++----- auto_cpufreq/battery_scripts/ideapad | 2 +- auto_cpufreq/battery_scripts/thinkpad | 2 +- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index faf73702..1558f07b 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,16 +1,40 @@ import os import thinkpad import ideapad +from core import get_config -def battery_thresholds(): - return {0, 100} +def battery_start_threshold(): + conf = get_config() + if conf.has_option("battery", "start_threshold"): + start_threshold = conf["battery"]["start_threshold"] + return start_threshold + else: + return + + +def battery_stop_threshold(): + conf = get_config() + if conf.has_option("battery", "stop_threshold"): + stop_threshold = conf["battery"]["stop_threshold"] + return stop_threshold + else: + return def battery_setup(): - thresholds = battery_thresholds() - lsmod = os.system("lsmod") - if lsmod.find("thinkpad_acpi") is not None: - thinkpad.thinkpad(thresholds[0], thresholds[1]) - elif lsmod.find("ideapad_acpi") is not None: - ideapad.ideapad_setup(thresholds[0], thresholds[1]) + conf = get_config() + if conf.has_option("battery", "enable_thresholds") && conf["battery"]["enable_thresholds"] == True: + lsmod = os.system("lsmod") + if lsmod.find("thinkpad_acpi") is not None: + thinkpad.thinkpad_setup(battery_start_threshold(), battery_stop_threshold()) + elif lsmod.find("ideapad_acpi") is not None: + ideapad.ideapad_setup(battery_start_threshold(), battery_stop_threshold()) + else: + pass + else: + pass + + +# TODO +def battery_get_thresholds(): diff --git a/auto_cpufreq/battery_scripts/ideapad b/auto_cpufreq/battery_scripts/ideapad index 089c26cd..12493a46 100644 --- a/auto_cpufreq/battery_scripts/ideapad +++ b/auto_cpufreq/battery_scripts/ideapad @@ -26,7 +26,7 @@ def ideapad_setup(start_threshold, stop_threshold): pass - +#TODO cleanup and figure out how to display battery info def ideapad_print_thresholds(): path_to_bats = '/sys/class/power_supply/' battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')]) diff --git a/auto_cpufreq/battery_scripts/thinkpad b/auto_cpufreq/battery_scripts/thinkpad index 1212ee50..5b4c93ed 100644 --- a/auto_cpufreq/battery_scripts/thinkpad +++ b/auto_cpufreq/battery_scripts/thinkpad @@ -8,7 +8,7 @@ def thinkpad_setup(start_threshold, stop_threshold): if os.system("lsmod | grep thinkpad_acpi") is not None: # this path is specific to thinkpads path_to_bats = '/sys/class/power_supply/' - # gets the numb of batterys + # 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): From 7aac861cd130ed20decf4b7da4bb079b73fa67d3 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 3 Feb 2024 08:36:54 -0600 Subject: [PATCH 03/12] added battery_script in auto_cpufreq --- auto_cpufreq/bin/auto_cpufreq.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/auto_cpufreq/bin/auto_cpufreq.py b/auto_cpufreq/bin/auto_cpufreq.py index db2b84da..688b1e7e 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -13,7 +13,7 @@ # sys.path.append("../") from auto_cpufreq.core import * from auto_cpufreq.power_helper import * - +from battery_scripts.battery import * # cli @click.command() @click.option("--monitor", is_flag=True, help="Monitor and see suggestions for CPU optimizations") @@ -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() @@ -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() From 00b4225f017b1ee0b1e121eafc1700901f72d8a6 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 3 Feb 2024 08:55:50 -0600 Subject: [PATCH 04/12] fix some errors with batterys --- auto_cpufreq/battery_scripts/battery.py | 10 ++++++---- auto_cpufreq/battery_scripts/{ideapad => ideapad.py} | 2 +- auto_cpufreq/battery_scripts/{thinkpad => thinkpad.py} | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) rename auto_cpufreq/battery_scripts/{ideapad => ideapad.py} (98%) rename auto_cpufreq/battery_scripts/{thinkpad => thinkpad.py} (98%) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 1558f07b..a3ae7b63 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,7 +1,8 @@ +#!/usr/bin/env python3 import os -import thinkpad -import ideapad -from core import get_config +import auto_cpufreq.battery_scripts.thinkpad +import auto_cpufreq.battery_scripts.ideapad +from auto_cpufreq.core import get_config def battery_start_threshold(): @@ -24,7 +25,7 @@ def battery_stop_threshold(): def battery_setup(): conf = get_config() - if conf.has_option("battery", "enable_thresholds") && conf["battery"]["enable_thresholds"] == True: + if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == True: lsmod = os.system("lsmod") if lsmod.find("thinkpad_acpi") is not None: thinkpad.thinkpad_setup(battery_start_threshold(), battery_stop_threshold()) @@ -38,3 +39,4 @@ def battery_setup(): # TODO def battery_get_thresholds(): + pass diff --git a/auto_cpufreq/battery_scripts/ideapad b/auto_cpufreq/battery_scripts/ideapad.py similarity index 98% rename from auto_cpufreq/battery_scripts/ideapad rename to auto_cpufreq/battery_scripts/ideapad.py index 12493a46..9cc50f4e 100644 --- a/auto_cpufreq/battery_scripts/ideapad +++ b/auto_cpufreq/battery_scripts/ideapad.py @@ -1,4 +1,4 @@ -#! /bin/python +#!/usr/bin/env python3 import os diff --git a/auto_cpufreq/battery_scripts/thinkpad b/auto_cpufreq/battery_scripts/thinkpad.py similarity index 98% rename from auto_cpufreq/battery_scripts/thinkpad rename to auto_cpufreq/battery_scripts/thinkpad.py index 5b4c93ed..190c0254 100644 --- a/auto_cpufreq/battery_scripts/thinkpad +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -1,4 +1,4 @@ -#! /bin/python +#!/usr/bin/env python3 import os From 69898cd35bc9da7a75b417028c2b8750df8103ad Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 3 Feb 2024 10:35:06 -0600 Subject: [PATCH 05/12] setup some battery stat printing --- auto_cpufreq/battery_scripts/battery.py | 15 +++++++++++++-- auto_cpufreq/battery_scripts/ideapad.py | 12 ++++-------- auto_cpufreq/battery_scripts/thinkpad.py | 9 +++------ auto_cpufreq/bin/auto_cpufreq.py | 4 +++- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index a3ae7b63..d16c023e 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 import os +from auto_cpufreq.core import get_config + import auto_cpufreq.battery_scripts.thinkpad import auto_cpufreq.battery_scripts.ideapad -from auto_cpufreq.core import get_config def battery_start_threshold(): @@ -39,4 +40,14 @@ def battery_setup(): # TODO def battery_get_thresholds(): - pass + conf = get_config() + lsmod = os.system("lsmod") + if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == True: + if lsmod.find("thinkpad_acpi") is not None: + thinkpad.thinkpad_print_thresholds() + elif lsmod.find("ideapad_acpi") is not None: + ideapad.ideapad_print_thresholds() + else: + pass + else: + return diff --git a/auto_cpufreq/battery_scripts/ideapad.py b/auto_cpufreq/battery_scripts/ideapad.py index 9cc50f4e..96eb54e6 100644 --- a/auto_cpufreq/battery_scripts/ideapad.py +++ b/auto_cpufreq/battery_scripts/ideapad.py @@ -8,7 +8,7 @@ def ideapad_setup(start_threshold, stop_threshold): if os.system("lsmod | grep ideapad_latop") is not None: # this path is specific to thinkpads path_to_bats = '/sys/class/power_supply/' - # gets the numb of batterys + # 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): @@ -26,22 +26,18 @@ def ideapad_setup(start_threshold, stop_threshold): pass -#TODO cleanup and figure out how to display battery info def ideapad_print_thresholds(): path_to_bats = '/sys/class/power_supply/' battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')]) - - print("Battery Info") - + 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'BAT{b} start is set to {f.read()}') + 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'BAT{b} stop is set to {f.read()}') + print(f'battery{b} stop threshold is set to {f.read()}') f.close() except Exception as e: diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index 190c0254..7f31bd7f 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -26,21 +26,18 @@ def thinkpad_setup(start_threshold, stop_threshold): pass -# this is for testing and debuging - def thinkpad_print_thresholds(): 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'BAT{b} start is set to {f.read()}') + 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'BAT{b} stop is set to {f.read()}') + print(f'battery{b} stop threshold is set to {f.read()}') f.close() except Exception as e: diff --git a/auto_cpufreq/bin/auto_cpufreq.py b/auto_cpufreq/bin/auto_cpufreq.py index 688b1e7e..92dcb9e2 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -13,7 +13,7 @@ # sys.path.append("../") from auto_cpufreq.core import * from auto_cpufreq.power_helper import * -from battery_scripts.battery 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") @@ -96,6 +96,7 @@ def config_info_dialog(): config_info_dialog() root_check() print('\nNote: You can quit monitor mode by pressing "ctrl+c"') + battery_setup() if os.getenv("PKG_MARKER") == "SNAP": gnome_power_detect_snap() tlp_service_detect_snap() @@ -114,6 +115,7 @@ def config_info_dialog(): countdown(2) elif live: root_check() + battery_setup() config_info_dialog() print('\nNote: You can quit live mode by pressing "ctrl+c"') time.sleep(1) From e05498b1504e6b46d1d91c7579478d35846aeb66 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 3 Feb 2024 10:44:07 -0600 Subject: [PATCH 06/12] added battery config printing to monitor and live modes --- auto_cpufreq/battery_scripts/battery.py | 4 +++- auto_cpufreq/bin/auto_cpufreq.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index d16c023e..5e278b20 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -26,7 +26,7 @@ def battery_stop_threshold(): def battery_setup(): conf = get_config() - if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == True: + if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true": lsmod = os.system("lsmod") if lsmod.find("thinkpad_acpi") is not None: thinkpad.thinkpad_setup(battery_start_threshold(), battery_stop_threshold()) @@ -41,6 +41,8 @@ def battery_setup(): # TODO def battery_get_thresholds(): conf = get_config() + if conf["battery"]["enable_thresholds"] != "true": + return lsmod = os.system("lsmod") if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == True: if lsmod.find("thinkpad_acpi") is not None: diff --git a/auto_cpufreq/bin/auto_cpufreq.py b/auto_cpufreq/bin/auto_cpufreq.py index 92dcb9e2..9451eb33 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -97,6 +97,7 @@ def 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() @@ -116,6 +117,7 @@ def config_info_dialog(): elif live: root_check() battery_setup() + battery_get_thresholds() config_info_dialog() print('\nNote: You can quit live mode by pressing "ctrl+c"') time.sleep(1) From a77c8fbedc9d8cca727424c5658f1120a88988cb Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sat, 3 Feb 2024 12:10:46 -0600 Subject: [PATCH 07/12] fix some bugs now works on daemon and monitor mode --- auto_cpufreq/battery_scripts/battery.py | 43 +++++++++++++----------- auto_cpufreq/battery_scripts/ideapad.py | 31 ++++++++--------- auto_cpufreq/battery_scripts/thinkpad.py | 31 ++++++++--------- auto_cpufreq/bin/auto_cpufreq.py | 2 -- 4 files changed, 52 insertions(+), 55 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 5e278b20..c7891ec4 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,16 +1,24 @@ #!/usr/bin/env python3 -import os +import subprocess from auto_cpufreq.core import get_config -import auto_cpufreq.battery_scripts.thinkpad -import auto_cpufreq.battery_scripts.ideapad +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 start_threshold + return int(start_threshold) else: return @@ -19,19 +27,18 @@ def battery_stop_threshold(): conf = get_config() if conf.has_option("battery", "stop_threshold"): stop_threshold = conf["battery"]["stop_threshold"] - return stop_threshold + return int(stop_threshold) else: - return + return 0 def battery_setup(): conf = get_config() if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true": - lsmod = os.system("lsmod") - if lsmod.find("thinkpad_acpi") is not None: - thinkpad.thinkpad_setup(battery_start_threshold(), battery_stop_threshold()) - elif lsmod.find("ideapad_acpi") is not None: - ideapad.ideapad_setup(battery_start_threshold(), battery_stop_threshold()) + 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: @@ -41,14 +48,12 @@ def battery_setup(): # TODO def battery_get_thresholds(): conf = get_config() - if conf["battery"]["enable_thresholds"] != "true": - return - lsmod = os.system("lsmod") - if conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == True: - if lsmod.find("thinkpad_acpi") is not None: - thinkpad.thinkpad_print_thresholds() - elif lsmod.find("ideapad_acpi") is not None: - ideapad.ideapad_print_thresholds() + print(conf["battery"]["enable_thresholds"]) + if conf["battery"]["enable_thresholds"] == "true": + if lsmod("thinkpad_acpi"): + thinkpad_print_thresholds() + elif lsmod("ideapad_acpi"): + ideapad_print_thresholds() else: pass else: diff --git a/auto_cpufreq/battery_scripts/ideapad.py b/auto_cpufreq/battery_scripts/ideapad.py index 96eb54e6..4865922f 100644 --- a/auto_cpufreq/battery_scripts/ideapad.py +++ b/auto_cpufreq/battery_scripts/ideapad.py @@ -3,27 +3,24 @@ def ideapad_setup(start_threshold, stop_threshold): + # 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')]) - # check if thinkpad_acpi is enabled if not this wont work - if os.system("lsmod | grep ideapad_latop") is not None: - # 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): + 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() + 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() + 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 + except Exception: + pass def ideapad_print_thresholds(): diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index 7f31bd7f..251c21d2 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -3,27 +3,24 @@ def thinkpad_setup(start_threshold, stop_threshold): + # 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')]) - # check if thinkpad_acpi is enabled if not this wont work - if os.system("lsmod | grep thinkpad_acpi") is not None: - # 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): + 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() + 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() + 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 + except Exception: + pass def thinkpad_print_thresholds(): diff --git a/auto_cpufreq/bin/auto_cpufreq.py b/auto_cpufreq/bin/auto_cpufreq.py index 9451eb33..50056489 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -116,8 +116,6 @@ def config_info_dialog(): countdown(2) elif live: root_check() - battery_setup() - battery_get_thresholds() config_info_dialog() print('\nNote: You can quit live mode by pressing "ctrl+c"') time.sleep(1) From 42222f0bd453d938ed94cd3281fbd34789a922d6 Mon Sep 17 00:00:00 2001 From: PurpleWazard <155469001+PurpleWazard@users.noreply.github.com> Date: Sat, 3 Feb 2024 13:01:15 -0600 Subject: [PATCH 08/12] cleaned up battery.py --- auto_cpufreq/battery_scripts/battery.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index c7891ec4..efeba898 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -20,7 +20,7 @@ def battery_start_threshold(): start_threshold = conf["battery"]["start_threshold"] return int(start_threshold) else: - return + return 0 def battery_stop_threshold(): @@ -29,7 +29,7 @@ def battery_stop_threshold(): stop_threshold = conf["battery"]["stop_threshold"] return int(stop_threshold) else: - return 0 + return 100 def battery_setup(): @@ -45,7 +45,6 @@ def battery_setup(): pass -# TODO def battery_get_thresholds(): conf = get_config() print(conf["battery"]["enable_thresholds"]) From 723a492270ed51a18b0935ac413ff8e6f1170bf4 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Sun, 4 Feb 2024 22:52:48 -0600 Subject: [PATCH 09/12] started to fix writing issue --- auto_cpufreq/battery_scripts/battery.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index c7891ec4..fcb32821 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -20,7 +20,7 @@ def battery_start_threshold(): start_threshold = conf["battery"]["start_threshold"] return int(start_threshold) else: - return + return 0 def battery_stop_threshold(): @@ -29,14 +29,14 @@ def battery_stop_threshold(): stop_threshold = conf["battery"]["stop_threshold"] return int(stop_threshold) else: - return 0 + return 100 def battery_setup(): 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()) + thinkpad_setup(battery_start_thresyeshold(), battery_stop_threshold()) elif lsmod("ideapad_acpi"): ideapad_setup(battery_start_threshold(), battery_stop_threshold()) else: From 1cb0d95f7ab76d694d5ea5e8fa8e7eb1040fe4eb Mon Sep 17 00:00:00 2001 From: purple wazard Date: Mon, 5 Feb 2024 08:39:37 -0600 Subject: [PATCH 10/12] still debuging fixes --- auto_cpufreq/battery_scripts/battery.py | 8 ++++++-- auto_cpufreq/battery_scripts/ideapad.py | 3 +++ auto_cpufreq/battery_scripts/thinkpad.py | 12 +++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 8fb24cb1..56041cda 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 import subprocess -from auto_cpufreq.core import get_config +from auto_cpufreq.core import get_config, root_check from auto_cpufreq.battery_scripts.thinkpad import * from auto_cpufreq.battery_scripts.ideapad import * @@ -18,6 +18,7 @@ def battery_start_threshold(): conf = get_config() if conf.has_option("battery", "start_threshold"): start_threshold = conf["battery"]["start_threshold"] + print(f"start {type(start_threshold)} {start_threshold}") return int(start_threshold) else: return 0 @@ -27,16 +28,19 @@ def battery_stop_threshold(): conf = get_config() if conf.has_option("battery", "stop_threshold"): stop_threshold = conf["battery"]["stop_threshold"] + print(f"stop {type(stop_threshold)} {stop_threshold}") + print(f"stop {type(int(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_thresyeshold(), battery_stop_threshold()) + thinkpad_setup(battery_start_threshold(), battery_stop_threshold()) elif lsmod("ideapad_acpi"): ideapad_setup(battery_start_threshold(), battery_stop_threshold()) else: diff --git a/auto_cpufreq/battery_scripts/ideapad.py b/auto_cpufreq/battery_scripts/ideapad.py index 4865922f..5f078219 100644 --- a/auto_cpufreq/battery_scripts/ideapad.py +++ b/auto_cpufreq/battery_scripts/ideapad.py @@ -1,8 +1,10 @@ #!/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 @@ -24,6 +26,7 @@ def ideapad_setup(start_threshold, stop_threshold): 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}") diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index 251c21d2..72190f1c 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -1,8 +1,10 @@ #!/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 @@ -14,16 +16,24 @@ def thinkpad_setup(start_threshold, stop_threshold): 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: + except Exception as e: + print(f"could not write to BAT{b} stop threshold") + 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}") From 970b8566ce282ffecb9d6508a8b2b18fa88ab2d3 Mon Sep 17 00:00:00 2001 From: purple wazard Date: Mon, 5 Feb 2024 08:47:43 -0600 Subject: [PATCH 11/12] bug fixed thinkpad stop threshold cant do below 65 --- auto_cpufreq/battery_scripts/battery.py | 5 +---- auto_cpufreq/battery_scripts/thinkpad.py | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 56041cda..871863d4 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -18,7 +18,6 @@ def battery_start_threshold(): conf = get_config() if conf.has_option("battery", "start_threshold"): start_threshold = conf["battery"]["start_threshold"] - print(f"start {type(start_threshold)} {start_threshold}") return int(start_threshold) else: return 0 @@ -28,8 +27,6 @@ def battery_stop_threshold(): conf = get_config() if conf.has_option("battery", "stop_threshold"): stop_threshold = conf["battery"]["stop_threshold"] - print(f"stop {type(stop_threshold)} {stop_threshold}") - print(f"stop {type(int(stop_threshold))}") return int(stop_threshold) else: return 100 @@ -51,8 +48,8 @@ def battery_setup(): def battery_get_thresholds(): conf = get_config() - print(conf["battery"]["enable_thresholds"]) if conf["battery"]["enable_thresholds"] == "true": + print("-" * 30) if lsmod("thinkpad_acpi"): thinkpad_print_thresholds() elif lsmod("ideapad_acpi"): diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index 72190f1c..23270914 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -26,7 +26,7 @@ def thinkpad_setup(start_threshold, stop_threshold): f.close() except Exception as e: - print(f"could not write to BAT{b} stop threshold") + print(f"could not write to BAT{b} stop threshold you might be setting it too low try < 65") print(e) pass From f8363c9947fab731851d0304e364f8a05590685e Mon Sep 17 00:00:00 2001 From: purple wazard Date: Mon, 5 Feb 2024 12:39:02 -0600 Subject: [PATCH 12/12] updated auto-cpufreq.conf-example with battery thresholds --- auto-cpufreq.conf-example | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/auto-cpufreq.conf-example b/auto-cpufreq.conf-example index 3ede8ae5..86afa584 100644 --- a/auto-cpufreq.conf-example +++ b/auto-cpufreq.conf-example @@ -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