سؤال

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.

هل كانت مفيدة؟

المحلول

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 :)

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top