Python script to charge and discharge laptop battery
Asked Answered
B

2

7

A friend of mine recently inherited an old laptop and has freshly installed windows 7, and wants to increase the battery life. Initially the battery lasted around 20mins, but by manually allowing the battery to fully discharge before recharging, a few times, he has managed to increase the lifetime to around an hour. We thought it would be fun to see just how much we could increase the performance of the battery! and I thought to write a script to cycle the battery overnight - and this may be useful to occasionally run on any computer to maintain battery health? I can get the battery status, but cannot see how to instruct the laptop to ignore the presence of the AC powerline and use the battery. I have a feeling the answer is out there: https://pypi.python.org/pypi/LaptopControlPanel but I am completely at my limit with regards to my understanding! Any help would be great.

import ctypes
from ctypes import wintypes


class SYSTEM_POWER_STATUS(ctypes.Structure):
    _fields_ = [
        ('ExternalPower', wintypes.BYTE),
        ('BatteryFlag', wintypes.BYTE),
        ('BatteryLifePercent', wintypes.BYTE),
        ('Reserved1', wintypes.BYTE),
        ('BatteryLifeTime', wintypes.DWORD)
        ]

SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)
GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
GetSystemPowerStatus.restype = wintypes.BOOL

status = SYSTEM_POWER_STATUS()
if not GetSystemPowerStatus(ctypes.pointer(status)):
        raise cytpes.WinError()
print 'ExternalPower', status.ExternalPower
#print 'BatteryFlag', status.BatteryFlag
print 'BatteryLifePercent', status.BatteryLifePercent
print 'BatteryLifeTime', status.BatteryLifeTime

if status.ExternalPower == True and status.BatteryLifePercent == 100:
    print 'Connected to mains and at 100% charge: Begining decharge'
    # This is where I would like to force battery use. Perhaps with a while
 #loop (that ticks every 60 seconds or so)
    if status.BatteryLifePercent > 10 :
        status.ExternalPower = 0

elif status.ExternalPower == True and status.BatteryLifePercent < 100:
    print 'Connected to mains and charging up to 100%'
    status.ExternalPower = 1

elif status.ExternalPower == False:
    print 'Not connected to mains'

else:
    print ' Unknown system status'

x = raw_input('Press ENTER to close:')

The first if statement is where I would like to force battery use... The above code is mostly stolen from In Python, how can I detect whether the computer is on battery power?.

Thank-you.

Bloodstone answered 23/12, 2013 at 17:58 Comment(3)
What kind of laptop is it? Lenovo/Thinkpads come with a utility for doing that. I bet you could find a utility online too. Is the problem with just unplugging the AC that you'd run the battery totally down to zero and you are trying to avoid this? Or you are just trying to have fun with Python?Cramped
I didn't even know you could use Python for that sort of thing. I'm interested if you ever get that working.Benefactor
@Cramped It's a Tobisha from around 2007 running Windows 7. No, the problem is not really about the manual unplugging, more that it would be nice to have to have the laptop plugged in (say overnight), and be able to cycle the battery charge as many times as is wanted. But also yes, the main reason behind this is to have fun with python - as of course manual unplugging works (he has been doing it).Bloodstone
M
2

AFAIK, ability to implement this has not much to do with Python or other programming languages. It is basically a capability of laptop's hardware, and it might not exist at all. If it does exist then it needs to be exposed by manufacturer's drivers and you will probably need a bit of low-level OS-specific wizardry to actually call the driver API from Python, because there is no OS abstraction for that. Given that you have the documentation for the API, which may not be public...

It can be a nice project if you are interested in hacking and reverse engineering the internals of PC hardware, which is certainly fun :)

Mallorymallow answered 31/12, 2013 at 17:52 Comment(0)
R
1

A very simple way to get this would be to use a Relay switch with arduino and give it commands by serial communication through python

Remarkable answered 21/11, 2015 at 23:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.