Jump to content
Sign in to follow this  
psynegy

Simplification of math function

Recommended Posts

psynegy

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

Share this post


Link to post
Share on other sites
Malkey

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.

;
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 by Malkey

Share this post


Link to post
Share on other sites
Skruge

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]

Share this post


Link to post
Share on other sites
psynegy

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

Button 2: 2

Button 3: 4

Button 4: 8

Button 5: 16

etc...

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 by psynegy

Share this post


Link to post
Share on other sites
WideBoyDixon

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 ÀÌØíÙ±Õ°ÀÌØí¹Õ´¤¼ÀÌØí¹Õ

Share this post


Link to post
Share on other sites
picaxe

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

Share this post


Link to post
Share on other sites
Skruge

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

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]

Share this post


Link to post
Share on other sites
psynegy

That's awesome, thanks so much. Note to self: don't attempt stupid maths.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×