beqa Posted June 17, 2014 Posted June 17, 2014 hello all. i want to make a script which will play sounds when charger is connected or disconnected. below is the code whic works only on script start $bsound = _BatteryQueryEx() Switch $bsound[0] Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch after that is udf code. how i can to make it to monitor status and if charger wil disconnect it'll play a sound. thanks
JohnOne Posted June 17, 2014 Posted June 17, 2014 Undefined function -> _BatteryQueryEx() AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
water Posted June 17, 2014 Posted June 17, 2014 You need to download the >UDF from the forum. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
beqa Posted June 17, 2014 Author Posted June 17, 2014 i have an udf i didn't paste the whole code. scripts works but only if you run script, but aftr that connecting disconnecting cable doesn't give efect
beqa Posted June 17, 2014 Author Posted June 17, 2014 this is whole code. $batt = _BatteryQueryEx() Switch $batt[0] Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch #include-once ;====================================================== ; _BatteryQueryEx() ; original author PsaltyDS <a href='#531988' class='bbc_url' title=''>http://www.autoitscript.com/forum/index.php?showtopic=10993&view=findpost&p=531988</a> ; Rajesh V R - addition of BatteryFullLifeTime ; Return information on the Battery ; Sets @Error on error ; Returns an array: ; $array[0] = ACPower(0=offline, 1=online, 255=unknown) ; $array[1] = BatteryFlag(1=High>66%, 2=Low<33%, 4=Critical<5%, ; 8=Charging 128=No Battery, 255=Unknown ; Use BitAnd to test, ie BitAnd($array[1],128) ; $array[2] = BatteryLife %(0-100, 255=unknown) ; $array[3] = Seconds left of charge, estimate(4294967295=unknown) ; $array[4] = BatteryLife @ Full Capacity (Seconds of battery life if fully charged -1 if info not available Not applicable for AC Source) ; @Link : <a href='http://msdn.microsoft.com/en-us/library/aa373232(VS.85' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/aa373232(VS.85</a>).aspx ;====================================================== Func _BatteryQueryEx() Local $SystemPower, $ret, $array[5] ; Setup $array and $SystemPower $SystemPower = DllStructCreate("ubyte;ubyte;ubyte;ubyte;ulong;ulong") If @error Then SetError(-1) Return $array EndIf ; make the DllCall $ret = DllCall("kernel32.dll", "int", "GetSystemPowerStatus", "ptr", DllStructGetPtr($SystemPower)) If @error Then;DllCall Failed SetError(-2) $SystemPower = 0 Return $array EndIf If Not $ret[0] Then; GetSystemPowerStatus Failed SetError(-3) $SystemPower = 0 Return $array EndIf ; Fill the array $array[0] = DllStructGetData($SystemPower, 1); AC $array[1] = DllStructGetData($SystemPower, 2); Battery Charge $array[2] = DllStructGetData($SystemPower, 3); Battery Charge % $array[3] = DllStructGetData($SystemPower, 5); Sec Battery Left $array[4] = DllStructGetData($SystemPower, 6); Sec Battery Capacity @ Full Charge ; free the struct $SystemPower = 0 Return $array EndFunc;==>_BatteryQueryEx
JohnOne Posted June 17, 2014 Posted June 17, 2014 You'll need to save the initial state into a variable then loop the code until that state changes. Save the new state. That's the loop. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
JohnOne Posted June 17, 2014 Posted June 17, 2014 $batt = _BatteryQueryEx() $State = $batt[0] While 3 ;loop here ;$batt = _BatteryQueryEx() ;If $batt[0] changes set $State to $batt[0] and play appropriate sound ;add a sleep WEnd AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
beqa Posted June 17, 2014 Author Posted June 17, 2014 While 1 Switch $batt[0] Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch sleep(10) WEnd is it right?
JohnOne Posted June 17, 2014 Posted June 17, 2014 Almost. You have to test $batt[0] against $State and only play sound if it changes. As it stands that will continue to play the sound. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
beqa Posted June 17, 2014 Author Posted June 17, 2014 and can't understand what is going $batt = _BatteryQueryEx() $state = $batt[0] While 1 if $batt[0]=0 then $state=0 elif batt[0]=1 then $state=1 endif sleep(10) wend Switch $state Case 0 SoundPlay(@WindowsDir & "mediatada.wav", 1) Case 1 SoundPlay(@WindowsDir & "mediading.wav", 1) EndSwitch
Developers Jos Posted June 17, 2014 Developers Posted June 17, 2014 (edited) Just ask yourself a question: Where in the loop am I updating $batt ? Edited June 17, 2014 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Solution JohnOne Posted June 17, 2014 Solution Posted June 17, 2014 Try this. $batt = _BatteryQueryEx() $state = $batt[0] While 1 $batt = _BatteryQueryEx() If $batt[0] <> $state Then $state = $batt[0] Switch $state Case 0 SoundPlay(@WindowsDir & "\media\tada.wav", 1) Case 1 SoundPlay(@WindowsDir & "\media\ding.wav", 1) EndSwitch EndIf Sleep(10) WEnd AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
beqa Posted June 17, 2014 Author Posted June 17, 2014 thanks, it works. sorry, missed var in the loop.
JohnOne Posted June 17, 2014 Posted June 17, 2014 Ace. I'd recommend a longer sleep, maybe 1000. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
JohnOne Posted June 17, 2014 Posted June 17, 2014 (edited) In my world, 1 second is enough to test such a thing, I'm a minimalist. Edited June 17, 2014 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now