[Tutor] Battery Level Monitoring and Voice

FT chester_lab at fltg.net
Wed Jan 19 15:50:30 CET 2011


Hello everyone,

    Since I got a simple answer on the Python Win32 web site I decided to post it here for those who would want that information, especially those who can not see.

    The first 2 lines are all that is needed along with the battery level instances using that object,
( BatteryPercentage = wmi.InstancesOf('win32_battery'))

    This program uses the computer voices and will list them if the specified one is not found. In this case I used Karen. When the list is displayed you do not need to insert everything in the name, just the obvious part of the name, like Karen. Just make sure you spell it correctly or you will always get the list printed on the screen.

    this also will sound off every minute because of the sleep amounts I use. It can also be changed to voice the percentage every percent change, just do what you want to do.

        Bruce

#BatteryLevel.py
import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
from random import randint, uniform
import msvcrt #INPUT KEYBOARD COMMANDS!
import sys
import time
import pyTTS

#VOICE SETUP!

tts = pyTTS.Create()
purge = pyTTS.tts_purge_before_speak
async = pyTTS.tts_async
PITCH=0
tts.Volume = 100
tts.Rate = 2
VOICECOUNT = len(tts.GetVoiceNames())
VOICES=[]
for v in range( VOICECOUNT):
     VOICES.append( tts.GetVoiceNames()[v])

def setVoice2Name( name):
    "SET VOICE BY NAME!"
    t=-1
    for v in range( VOICECOUNT):
        if name in VOICES[ v]:
            tts.SetVoiceByName( VOICES[v])
            t=v
    if t==-1:
        tts.Speak( " %d Voices with %s Name Not Found! " % (VOICECOUNT, name))
        print " %d Voices with %s Name Not Found! " % (VOICECOUNT, name)
        print "Voices:"
        for v in range( VOICECOUNT):
            print "%d= %s" % (v, VOICES[ v])

def Speak(text, pitch=999):
    if pitch==999:
        pitch=PITCH
    tts.Speak( "<pitch absmiddle='%s'/> %s" % (pitch, text), async, purge)

def SpeakNext(text, pitch=999):
    if pitch==999:
        pitch=PITCH
    tts.Speak( "<pitch absmiddle='%s'/> %s" % (pitch, text), async)

def setRate( rate):
    tts.Rate= rate

if __name__ == '__main__':
    setVoice2Name( "Karen")
    PITCH=3
    setRate( 2)
    # clear the keyboard buffer
    while msvcrt.kbhit():
        ch = msvcrt.getch()
    ch=""; ch1=""; sch=""
    while ch != chr(27) and ch != chr(13):
        while msvcrt.kbhit():
            ch1 = msvcrt.getch()
            ch = ch1
            if ch1 == chr(0) or ch1 == chr(224):
                ch = msvcrt.getch()
                sch = ch1+ch #SAVE ANY MULTIKEYS!
        if ch != chr(27) and ch != chr(13):
            BatteryPercentage = wmi.InstancesOf('win32_battery')
            print (" %d Percent battery charge remaining! " % BatteryPercentage[0].EstimatedChargeRemaining)
            print "Hay! Battery power is getting low! Plug in the power cord:"
            print "Please Press Enter To Exit This Battery Alert!"
            SpeakNext(" %d Percent battery charge remaining! " % BatteryPercentage[0].EstimatedChargeRemaining)
            SpeakNext( "Hay! Battery power is getting low! Plug in the power cord:")
            SpeakNext( "Please Press Enter To Exit This Battery Alert!")
#            time.sleep(10)
            c = 0
            while c < 30:
                if msvcrt.kbhit():
                    SpeakNext( "Key Was Hit!")
                    c = 100
                    time.sleep(1)
                else:
                    time.sleep(2)
                    c += 1
#                if msvcrt.kbhit():
#                    SpeakNext( "Key Was Hit!")
#                    time.sleep(1)
#                else:
#                    time.sleep(5)
    SpeakNext( "OK, Goodbye!")
    time.sleep(2)
    #RELEASE VOICE INSTANCE FROM MEMORY!
    del tts
    sys.exit()



More information about the Tutor mailing list