[UDF] Additional360Features - Vibration und Batterie Level des Xbox 360 Controllers auslesen

  • Hallo zusammen,

    inzwischen ist es morgen und ich veröffentliche gleich mal meine 2. UDF. Manche mögen denken dass ich kein Leben habe, aber wenn schon mal Wochenende ist, muss das ja auch genutzt werden :rolleyes:

    Mit dieser UDF ist es möglich den Batteriestand des 360 Controllers aus zu lesen und ihn Vibrieren zu lasen. Beide Funktionen gab es mehr oder weniger schon vorher in anderer Form. Ich habe sie lediglich etwas aufgearbeitet und in einer schönen UDF zusammen gefasst. Für die kompletten Credits, schaut bitte in die jeweilige Beschreibung der Funktion :)

    _additional360Features.au3

    Spoiler anzeigen
    [autoit]


    #include-once

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name ..........: _360Vibrate
    ; Description ...:
    ; Syntax ........: _360Vibrate($Dll, $LMotor, $RMotor, $ControllerIndex)
    ; Parameters ....: $Dll - Handle to the xinput9_1_0.dll
    ; $LMotor - The speed of the left motor. 0 = Stop; 100 = Full Power
    ; $RMotor - The speed of the right motor. 0 = Stop; 100 = Full Power
    ; $ControllerIndex - Number of controller whitch sould vibrate. 0 - 3 (0 is player 1, 1 is player 2 and so on)
    ; Return values .: None
    ; Author ........: All Credits go to Oxin8 from this thread: http://www.autoitscript.com/forum/topic/21…360-controller/
    ; i steal the function from his _XInput.au3 and modified it a bit for multi controller support and an easier usage
    ; Modified ......: ErrorKid @ autoit.de - Added multi controller support and optimise it for an easier usage
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......: Yes. See Additional360FeaturesExample.au3
    ; ===============================================================================================================================
    Func _360Vibrate($Dll, $LMotor, $RMotor, $ControllerIndex)
    $xinputvibration = DllStructCreate("short;short")
    DllStructSetData($xinputvibration, 1, Round(($LMotor / 100) * 32767))
    DllStructSetData($xinputvibration, 2, Round(($RMotor / 100) * 32767))
    DllCall($Dll, "long", "XInputSetState", "long", 0, "ptr", DllStructGetPtr($xinputvibration))
    EndFunc ;==>_360Vibrate

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name ..........: _Get360BatteryLevel
    ; Description ...:
    ; Syntax ........: _Get360BatteryLevel($Dll, $ControllerIndex)
    ; Parameters ....: $Dll - Handle to the xinput1_3.dll
    ; $ControllerIndex - Number of controller where the batteryinfos should be read. 0 - 3 (0 is player 1, 1 is player 2 and so on)
    ; Return values .: Return an array.
    ; Index 0 is the type of the battery:
    ; 0 = Battery is disconnected
    ; 1 = Controller is wired
    ; 2 = Batterytype is Alkaline
    ; 3 = Batterytype is NIMH
    ; 255= Batterytype is unknown
    ; Index 1 is the state of the battery:
    ; 0 = Battery is emptry
    ; 1 = Batterylevel is low
    ; 2 = Batterylevel is medium
    ; 3 = Batterylevel is full
    ; Author ........: Credits go to monoscout999 from this thread: http://www.autoitscript.com/forum/topic/131788-xbox-dll-call/
    ; He provided me the necessary information to wrote this function
    ; Modified ......: ErrorKid @ autoit.de - Made a runnable function out of the informations from monoscout999 and the MSDN-Documentation
    ; Remarks .......:
    ; Related .......:
    ; Link ..........:
    ; Example .......: Yes. See Additional360FeaturesExample.au3
    ; ===============================================================================================================================
    Func _Get360BatteryLevel($Dll, $ControllerIndex)
    $return = ""
    Dim $return[2]
    $struct = DllStructCreate("byte type;byte level")
    $pointer = DllStructGetPtr($struct)
    DllCall($Dll, "dword", "XInputGetBatteryInformation", "dword", 0, "byte", 0x00, "ptr", $pointer)
    $level = DllStructGetData($struct, "level")
    $type = DllStructGetData($struct, "type")
    $return[0] = $type
    $return[1] = $level
    Return $return
    EndFunc ;==>_Get360BatteryLevel

    [/autoit]

    Additional360FeaturesExample.au3

    Spoiler anzeigen
    [autoit]


    #include <_additional360Features.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $BatteryDll = DllOpen("xinput1_3.dll")
    $VibrationDll = DllOpen("xinput9_1_0.dll")

    [/autoit] [autoit][/autoit] [autoit]

    ;; Example for the battery level
    While 1
    $batteryinfo = _Get360BatteryLevel($BatteryDll, 0)
    ToolTip("Current batterytype: " & $batteryinfo[0] & @CRLF & _
    "Current batterystate: " & $batteryinfo[1] & @CRLF & _
    "Look at the returnvalues in the functiondescription for the meaning!")
    Sleep(10)
    ExitLoop
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;; Comment the first while-Loop out to get here!

    [/autoit] [autoit][/autoit] [autoit]

    ;; Example for the vibration
    _360Vibrate($VibrationDll, 100, 100, 0)
    Sleep(1000)
    _360Vibrate($VibrationDll, 0, 0, 1)

    [/autoit]

    Falls es Fragen oder Verbesserungsvorschläge gibt, meldet euch bitte. Ich bin offen für Verbesserungsvorschläge.
    Auch hier gilt wieder: Eigentlich habe ich die Funktionen so gut wie möglich Dokumentiert. Falls noch irgendwas absolut unverständlich ist kann ich gerne noch den Beispiel Code mit ein paar mehr Kommentaren versehen.

    So long, keep on rocking :rock:
    ErrorKid

  • Ich brauch die UDF zwar nicht, aber du solltest noch für eine saubere Verwendung auch DllClose benutzen. Und DllOpen und DllClose noch in Funktionen von der UDF packen. Z.B. _XBox360Com_StartUp und _XBox360Com_Shutdown.