Skip to content

Commit

Permalink
Add conservation mode to lenovo laptops (#652)
Browse files Browse the repository at this point in the history
* adding support for lenovo ideapad_laptop devices

* testing lenovo_laptop

* renamed functions

* fix code bugs

* updated readme with lenovo laptop conservation mode.
  • Loading branch information
PurpleWazard authored Mar 11, 2024
1 parent de12f9f commit 971f40f
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 94 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
67 changes: 22 additions & 45 deletions auto_cpufreq/battery_scripts/battery.py
Original file line number Diff line number Diff line change
@@ -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.thinkpad import thinkpad_setup, thinkpad_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


def lsmod(module):
Expand All @@ -15,55 +15,32 @@ 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_acpi_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
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


39 changes: 0 additions & 39 deletions auto_cpufreq/battery_scripts/ideapad.py

This file was deleted.

59 changes: 59 additions & 0 deletions auto_cpufreq/battery_scripts/ideapad_acpi.py
Original file line number Diff line number Diff line change
@@ -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_acpi_setup():
config = get_config()

if not config.has_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_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}")
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}")
99 changes: 99 additions & 0 deletions auto_cpufreq/battery_scripts/ideapad_laptop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/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 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


def 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 None
except:
print("could not get the value from conservation mode")
return False


def ideapad_laptop_setup():
config = get_config()

if not config.has_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
if config["battery"]["ideapad_laptop_conservation_mode"] == "false":
conservation_mode(0)

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)
else:
print("conservation mode is enabled unable to set thresholds")


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}")
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}")
40 changes: 30 additions & 10 deletions auto_cpufreq/battery_scripts/thinkpad.py
Original file line number Diff line number Diff line change
@@ -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.has_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}")
Expand Down

0 comments on commit 971f40f

Please sign in to comment.