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.

4

2 回答 2

1

AFAIK,实现这一点的能力与 Python 或其他编程语言没有太大关系。它基本上是笔记本电脑硬件的一种功能,可能根本不存在。如果它确实存在,那么它需要由制造商的驱动程序公开,您可能需要一些低级操作系统特定的魔法才能从 Python 实际调用驱动程序 API,因为没有操作系统抽象。鉴于您拥有 API 的文档,该文档可能不公开...

如果您对破解和逆向 PC 硬件的内部结构感兴趣,这可能是一个不错的项目,这当然很有趣 :)

于 2013-12-31T17:52:07.720 回答
1

一个非常简单的方法是使用带有arduino的继电器开关,并通过python通过串行通信给它命令

于 2015-11-21T23:56:27.743 回答