Skip to content

Commit

Permalink
Rebuilt battery scripts (AdnanHodzic#645)
Browse files Browse the repository at this point in the history
* rebuilt thinkpad and ideapad

* updated conf example
  • Loading branch information
PurpleWazard authored and rootCircle committed Feb 16, 2024
1 parent 27944dc commit b5fa98b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 57 deletions.
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

0 comments on commit b5fa98b

Please sign in to comment.