Skip to content

Commit 539914a

Browse files
authored
Rebuilt battery scripts (#645)
* rebuilt thinkpad and ideapad * updated conf example
1 parent 005b4aa commit 539914a

File tree

4 files changed

+43
-57
lines changed

4 files changed

+43
-57
lines changed

auto-cpufreq.conf-example

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ turbo = auto
5454
# enable thresholds true or false
5555
#enable_thresholds = true
5656
#
57-
# start threshold (defaults to 0 ) can be 0 - 100
57+
# start threshold (0 is off ) can be 0-99
5858
#start_threshold = 0
5959
#
60-
# stop threshold (defaults to 100) this value must be greater or equal to 65
60+
# stop threshold (100 is off) can be 1-100
6161
#stop_threshold = 100

auto_cpufreq/battery_scripts/battery.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88

99
def lsmod(module):
10-
output = subprocess.run(['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
10+
output = subprocess.run(
11+
['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
1112
if module in output.stdout:
1213
return True
1314
else:
@@ -38,9 +39,11 @@ def battery_setup():
3839
if conf.has_option("battery", "enable_thresholds"):
3940
if conf["battery"]["enable_thresholds"] == "true":
4041
if lsmod("thinkpad_acpi"):
41-
thinkpad_setup(battery_start_threshold(), battery_stop_threshold())
42+
thinkpad_setup(battery_start_threshold(),
43+
battery_stop_threshold())
4244
elif lsmod("ideapad_acpi"):
43-
ideapad_setup(battery_start_threshold(), battery_stop_threshold())
45+
ideapad_setup(battery_start_threshold(),
46+
battery_stop_threshold())
4447
else:
4548
pass
4649
else:
@@ -53,7 +56,7 @@ def battery_get_thresholds():
5356
conf = get_config()
5457
if conf.has_option("battery", "enable_thresholds"):
5558
if conf["battery"]["enable_thresholds"] == "true":
56-
print("-" * 30 )
59+
print("-" * 30)
5760
if lsmod("thinkpad_acpi"):
5861
thinkpad_print_thresholds()
5962
elif lsmod("ideapad_acpi"):

auto_cpufreq/battery_scripts/ideapad.py

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
#!/usr/bin/env python3
22
import os
3+
import subprocess
34
from auto_cpufreq.core import root_check
45

56

6-
def ideapad_setup(start_threshold, stop_threshold):
7-
root_check()
8-
# this path is specific to ideapads
9-
path_to_bats = '/sys/class/power_supply/'
10-
# gets the numb of batteries
11-
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
12-
13-
for b in range(battery_count):
14-
15-
try:
16-
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'w') as f:
17-
f.write(str(start_threshold) + '\n')
18-
f.close()
7+
def set_battery(value, mode, bat):
8+
file_path = f'/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold'
9+
try:
10+
subprocess.check_output(f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True)
11+
except Exception as e:
12+
print(f"Error writing to {file_path}: {e}")
1913

20-
with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'w') as f:
21-
f.write(str(stop_threshold) + '\n')
22-
f.close()
2314

24-
except Exception:
25-
pass
15+
def ideapad_setup(start_threshold, stop_threshold):
16+
root_check()
17+
battery_count = len([name for name in os.listdir("/sys/class/power_supply/") if name.startswith('BAT')])
18+
for bat in range(battery_count):
19+
set_battery(start_threshold, "start", bat)
20+
set_battery(stop_threshold, "stop", bat)
2621

2722

2823
def ideapad_print_thresholds():
2924
root_check()
30-
path_to_bats = '/sys/class/power_supply/'
31-
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
25+
battery_count = len([name for name in os.listdir(
26+
"/sys/class/power_supply/") if name.startswith('BAT')])
3227
print(f"number of batteries = {battery_count}")
3328
for b in range(battery_count):
3429
try:
35-
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'r') as f:
30+
with open(f'/sys/class/power_supply/BAT{b}/charge_start_threshold', 'r') as f:
3631
print(f'battery{b} start threshold is set to {f.read()}')
3732
f.close()
3833

39-
with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'r') as f:
34+
with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f:
4035
print(f'battery{b} stop threshold is set to {f.read()}')
4136
f.close()
4237

auto_cpufreq/battery_scripts/thinkpad.py

+17-29
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,37 @@
11
#!/usr/bin/env python3
22
import os
3+
import subprocess
34
from auto_cpufreq.core import root_check
45

56

6-
def thinkpad_setup(start_threshold, stop_threshold):
7-
root_check()
8-
# this path is specific to thinkpads
9-
path_to_bats = '/sys/class/power_supply/'
10-
# gets the numb of batteries
11-
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
12-
13-
for b in range(battery_count):
7+
def set_battery(value, mode, bat):
8+
file_path = f'/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold'
9+
try:
10+
subprocess.check_output(f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True)
11+
except Exception as e:
12+
print(f"Error writing to {file_path}: {e}")
1413

15-
try:
16-
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'w') as f:
17-
f.write(str(start_threshold) + '\n')
18-
f.close()
19-
except Exception as e:
20-
print(f"could not write to BAT{b} start threshold")
21-
print(e)
2214

23-
try:
24-
with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'w') as f:
25-
f.write(str(stop_threshold) + '\n')
26-
f.close()
27-
28-
except Exception as e:
29-
print(f"could not write to BAT{b} stop threshold you might be setting it too low try < 65")
30-
print(e)
31-
pass
15+
def thinkpad_setup(start_threshold, stop_threshold):
16+
root_check()
17+
battery_count = len([name for name in os.listdir("/sys/class/power_supply/") if name.startswith('BAT')])
18+
for bat in range(battery_count):
19+
set_battery(start_threshold, "start", bat)
20+
set_battery(stop_threshold, "stop", bat)
3221

3322

3423
def thinkpad_print_thresholds():
3524
root_check()
36-
# this path is specific to thinkpads
37-
path_to_bats = '/sys/class/power_supply/'
38-
battery_count = len([name for name in os.listdir(path_to_bats) if name.startswith('BAT')])
25+
battery_count = len([name for name in os.listdir(
26+
"/sys/class/power_supply/") if name.startswith('BAT')])
3927
print(f"number of batteries = {battery_count}")
4028
for b in range(battery_count):
4129
try:
42-
with open(f'{path_to_bats}BAT{b}/charge_start_threshold', 'r') as f:
30+
with open(f'/sys/class/power_supply/BAT{b}/charge_start_threshold', 'r') as f:
4331
print(f'battery{b} start threshold is set to {f.read()}')
4432
f.close()
4533

46-
with open(f'{path_to_bats}BAT{b}/charge_stop_threshold', 'r') as f:
34+
with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f:
4735
print(f'battery{b} stop threshold is set to {f.read()}')
4836
f.close()
4937

0 commit comments

Comments
 (0)