|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | import os
|
| 3 | +import subprocess |
3 | 4 | from auto_cpufreq.core import root_check
|
4 | 5 |
|
5 | 6 |
|
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}") |
14 | 13 |
|
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) |
22 | 14 |
|
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) |
32 | 21 |
|
33 | 22 |
|
34 | 23 | def thinkpad_print_thresholds():
|
35 | 24 | 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')]) |
39 | 27 | print(f"number of batteries = {battery_count}")
|
40 | 28 | for b in range(battery_count):
|
41 | 29 | 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: |
43 | 31 | print(f'battery{b} start threshold is set to {f.read()}')
|
44 | 32 | f.close()
|
45 | 33 |
|
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: |
47 | 35 | print(f'battery{b} stop threshold is set to {f.read()}')
|
48 | 36 | f.close()
|
49 | 37 |
|
|
0 commit comments