psynegy Posted March 31, 2009 Posted March 31, 2009 I have some rather bloated code here, I'm SURE there must be an easier way of achieving the same thing. I basically need to return an array of what 'buttons' are pressed on a controller, but it passes it as this annoying number, and this is the only way I can think to retrieve it. Func Buttons($value) Local $num =4096 Local $numbuttons = 12 Local $buttons[$numbuttons] For $i=0 to $numbuttons-1 $num = $num/2 $o_num = $value - $num If($o_num >= 0) Then $buttons[$i] = 1 $value = $value - $num Else $buttons[$i] = 0 EndIf Next $string = "" $i2 = $numbuttons -1 For $i=0 to $numbuttons-1 $string = $string & $i2 & ":" & $buttons[$i] & " - " $i2 = $i2-1 Next Return $string EndFunc MsgBox(0, "", Buttons(9))
Malkey Posted April 1, 2009 Posted April 1, 2009 (edited) I have some rather bloated code here, I'm SURE there must be an easier way of achieving the same thing. I basically need to return an array of what 'buttons' are pressed on a controller, but it passes it as this annoying number, and this is the only way I can think to retrieve it. Func Buttons($value) Local $num =4096 Local $numbuttons = 12 Local $buttons[$numbuttons] For $i=0 to $numbuttons-1 $num = $num/2 $o_num = $value - $num If($o_num >= 0) Then $buttons[$i] = 1 $value = $value - $num Else $buttons[$i] = 0 EndIf Next $string = "" $i2 = $numbuttons -1 For $i=0 to $numbuttons-1 $string = $string & $i2 & ":" & $buttons[$i] & " - " $i2 = $i2-1 Next Return $string EndFunc MsgBox(0, "", Buttons(9))This function gives the same answer. expandcollapse popup; MsgBox(0, "", "New function returns: " & Buttons(9) & @CRLF & _ " Old function returns: " & Buttons1(9)) Func Buttons($value) Local $num = 4096, $numbuttons = 12, $buttons[$numbuttons], $string = "", $i2 = $numbuttons - 1 For $i = 0 To $numbuttons - 1 $num = $num / 2 If ($value - $num) >= 0 Then $buttons[$i] = 1 $value -= $num Else $buttons[$i] = 0 EndIf $string &= $i2 & ":" & $buttons[$i] & " - " $i2 -= 1 Next Return $string EndFunc ;==>Buttons Func Buttons1($value) Local $num = 4096 Local $numbuttons = 12 Local $buttons[$numbuttons] For $i = 0 To $numbuttons - 1 $num = $num / 2 $o_num = $value - $num If ($o_num >= 0) Then $buttons[$i] = 1 $value = $value - $num Else $buttons[$i] = 0 EndIf Next $string = "" $i2 = $numbuttons - 1 For $i = 0 To $numbuttons - 1 $string = $string & $i2 & ":" & $buttons[$i] & " - " $i2 = $i2 - 1 Next Return $string EndFunc ;==>Buttons1 ; Edit: Skruge, you were correct about returning different results. I have resubmitted a modified script. Edited April 1, 2009 by Malkey
Skruge Posted April 1, 2009 Posted April 1, 2009 It would help if you posted a few other inputs and your expected output... I got different results between your function and Malkey's. Is the data bit-encoded? If so, here are a couple of options. MsgBox(0, "", _ShowPressedButtons(42)) MsgBox(0, "", _ShowAllButtons(9)) Func _ShowPressedButtons($iValue) Local $s_Buttons for $ix=1 to 12 if BitAND($iValue, BitShift(1,-$ix)) then $s_Buttons &= $ix & " " Next return StringStripWS($s_Buttons, 2) EndFunc Func _ShowAllButtons($iValue) Local $s_Buttons for $ix=1 to 12 $s_Buttons &= $ix & ":" & not(not(BitAND($iValue, BitShift(1,-$ix)))) & " " Next return StringStripWS($s_Buttons,2) EndFunc [font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]
psynegy Posted April 1, 2009 Author Posted April 1, 2009 (edited) It would help if you posted a few other inputs and your expected output...I got different results between your function and Malkey's.Basically, the button information is a compilation of different buttons 'values' or something.So:Button 1: 1Button 2: 2Button 3: 4Button 4: 8Button 5: 16etc...And I need an array returned with the buttons from 0-11 (so the value of each key in the array will be 1 or 0 depending on whether it's being pushed)Hope that makes it a little clearer?Edit: sorry, just realised I probably threw you with the string bit, I merely needed some sort of visual output. Edited April 1, 2009 by psynegy
WideBoyDixon Posted April 1, 2009 Posted April 1, 2009 From your original script try replacing this: $o_num = $value - $num If($o_num >= 0) Then $buttons[$i] = 1 $value = $value - $num Else $buttons[$i] = 0 EndIfoÝ÷ ÙhØb±«¢+ØÀÌØíÕÑѽ¹ÍlÀÌØí¥tô ¥Ñ9 ÀÌØíÙ±Õ°ÀÌØí¹Õ´¤¼ÀÌØí¹Õ [center]Wide by name, Wide by nature and Wide by girth[u]Scripts[/u]{Hot Folders} {Screen Calipers} {Screen Crosshairs} {Cross-Process Subclassing} {GDI+ Clock} {ASCII Art Signatures}{Another GDI+ Clock} {Desktop Goldfish} {Game of Life} {3D Pie Chart} {Stock Tracker}[u]UDFs[/u]{_FileReplaceText} {_ArrayCompare} {_ToBase}~ My Scripts On Google Code ~[/center]
picaxe Posted April 1, 2009 Posted April 1, 2009 Try#include <array.au3> $aRet = _Buttons(1023) _ArrayDisplay($aRet) Func _Buttons($iNumber) ;$iNumber = Abs(Number($iNumber)) If $iNumber > 4095 Then Return SetError(1, 0, 0) Local $sBinString = "" Do $sBinString = $sBinString & BitAND($iNumber, 1) $iNumber = BitShift($iNumber, 1) Until $iNumber = 0 $sBinString = StringLeft($sBinString & "000000000000", 12) Return StringSplit($sBinString, "", 2) EndFunc ;==>_Buttons
Skruge Posted April 1, 2009 Posted April 1, 2009 Button 1: 1 Button 2: 2 Button 3: 4 Button 4: 8 Button 5: 16 etc...Yep, that clarifies things greatly. It was exactly what I meant by bit-encoded, but I had a bug in my original post. (0-$ix instead of 1-$ix) Try this function:#include <Array.au3> $aButton = _Buttons(9) _ArrayDisplay($aButton) Func _ShowPressedButtons($iValue) Local $a_Buttons[12] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] For $iX = 1 To 11 If BitAND($iValue, BitShift(1, 1 - $iX)) Then $a_Buttons[$iX] = 1 Next Return $a_Buttons EndFunc ;==>_ShowPressedButtons I made the return a 1-based instead of zero-based array, so you could reference "Button 3" as $aButton[3], ignoring zero. [font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]
psynegy Posted April 3, 2009 Author Posted April 3, 2009 That's awesome, thanks so much. Note to self: don't attempt stupid maths.
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