#include<Array.au3>
#include-once

Global $bK8055Debug = False
#cs ----------------------------------------------------------------------------

	AutoIt Version: 3.3.0.0
	Author:         myName

	Script Function:
	handles the k8055d.dll        2009.10.19

#ce ----------------------------------------------------------------------------
Func USB_DLLOpen()
	If $bK8055Debug Then ConsoleWrite("Dll wird geöffnet" & @CRLF)

	DllOpen(@ScriptDir & "\" & "k8055D_multi.dll") ; öffnen der DLL

EndFunc   ;==>USB_DLLOpen

;----------------------------------------------------------------------------

Func initial_board($address)
	If $bK8055Debug Then ConsoleWrite("Board wird initialisiert" & @CRLF)
	$begin = TimerInit()
	Do
		$result = USBOpenDevice($address)
		Sleep(250)

		$dif = TimerDiff($begin)

		If $dif > "4000" Then ExitLoop

	Until $result = 1

	ClearAllDigital($address)

	Sleep(250)

;~     SetDigitalChannel($address, "4")

	If $result = 1 Then
		If $bK8055Debug Then ConsoleWrite("Board wurde initialisiert" & @CRLF)
		Return "1"

	Else
		MsgBox(0, "ERROR", "fehler bei initialisierung des USB board's" & $address)
		MsgBox(0, "ERROR", "Programm wird beendet! mit " & $result)
		Exit
		Return "0"

	EndIf


EndFunc   ;==>initial_board


;----------------------------------------------------------------------------

Func USBOpenDevice($address)
	If $bK8055Debug Then ConsoleWrite("USBDevice " & $address & " wird geöffnet" & @CRLF)

	$result = DllCall("k8055D_multi.dll", "long", "OpenDevice", "long", $address)
	If Not @error Then

		If $result[0] = $address Then
			Return "1"

		Else
			If $bK8055Debug Then ConsoleWrite("Öffnen von USBDevice " & $address & " nicht erfolgreich!" & @CRLF)
			Exit
		EndIf
	Else
		If $bK8055Debug Then ConsoleWrite("Öffnen von USBDevice " & $address & " nicht erfolgreich!" & @CRLF)
		Exit
	EndIf
EndFunc   ;==>USBOpenDevice

;---------------------------------------------------------------------------

Func CloseAllDevice()
	If $bK8055Debug Then ConsoleWrite("entlade DLL " & @CRLF)
	$res = DllCall("k8055D_multi.dll", "none", "CloseDevice")
	DllClose(@ScriptDir & "\" & "k8055D_multi.dll") ; öffnen der DLL

EndFunc   ;==>CloseAllDevice

;---------------------------------------------------------------------------

Func SearchDevices()

	$result = DllCall("k8055D_multi.dll", "long", "SearchDevices")

	Return $result[0]

EndFunc   ;==>SearchDevices

;---------------------------------------------------------------------------

Func USBCloseDevice($address)
	SetCurrentDevice($address)
	$res = DllCall("k8055D_multi.dll", "none", "CloseDevice")
	;Return ;;$result[0]
EndFunc   ;==>USBCloseDevice

;---------------------------------------------------------------------------

Func ClearAllDigital($address)
	SetCurrentDevice($address)
	$res = DllCall("k8055D_multi.dll", "int", "ClearAllDigital")
EndFunc   ;==>ClearAllDigital

;----------------------------------------------------------------------------

Func SetCurrentDevice($address)

	$begin = TimerInit()
	Do

		$result = DllCall("k8055D_multi.dll", "long", "OpenDevice", "int", $address)

		;ToolTip($address&"="&$result[0],0,0)
		$dif = TimerDiff($begin)

		If $dif > "4000" Then ExitLoop

	Until $address = $result[0]

	;ToolTip("",0,0)

	If $address = $result[0] Then
		;nix
	Else
		MsgBox(0, "", "adresse = " & $address & "  result = " & $result[0])

	EndIf

EndFunc   ;==>SetCurrentDevice


;----------------------------------------------------------------------------

Func SetDigitalChannel($address, $channel)
	SetCurrentDevice($address)
	DllCall("k8055D_multi.dll", "none", "SetDigitalChannel", "int", $channel)
EndFunc   ;==>SetDigitalChannel

;---------------------------------------------------------------------------

Func ClearDigitalChannel($address, $channel)
	SetCurrentDevice($address)
	DllCall("k8055D_multi.dll", "none", "ClearDigitalChannel", "int", $channel)
EndFunc   ;==>ClearDigitalChannel

;---------------------------------------------------------------------------

Func ReadAnalogChannel($address, $channel)
	SetCurrentDevice($address)
	$result = DllCall("k8055D_multi.dll", "int", "ReadAnalogChannel", "int", $channel)
	Return $result[0]
EndFunc   ;==>ReadAnalogChannel

;---------------------------------------------------------------------------

Func ReadAllDigital($address)
	SetCurrentDevice($address)
	$alldigit = DllCall("k8055D_multi.dll", "int", "ReadAllDigital")
	Return $alldigit[0]
EndFunc   ;==>ReadAllDigital
;---------------------------------------------------------------------------


Func ReadDigitalChannel($address, $sellect)
	Dim $channel[6]
	$channel[1] = 0
	$channel[2] = 0
	$channel[3] = 0
	$channel[4] = 0
	$channel[5] = 0



	$wert = ReadAllDigital($address)

	;MsgBox(0,"",$alldigit[0] & "  " & $wert)
	Select

		Case $wert = 0
			$channel[1] = 0
			$channel[2] = 0
			$channel[3] = 0
			$channel[4] = 0
			$channel[5] = 0

		Case $wert = 1
			;I1
			$channel[1] = 1

		Case $wert = 2
			;I2
			$channel[2] = 1

		Case $wert = 3
			;I1 + I2
			$channel[1] = 1
			$channel[2] = 1

		Case $wert = 4
			;I3
			$channel[3] = 1

		Case $wert = 5
			;I3 + I1
			$channel[1] = 1
			$channel[3] = 1

		Case $wert = 6
			;I3 + I2
			$channel[2] = 1
			$channel[3] = 1

		Case $wert = 7
			;I3 + I2 + I1
			$channel[1] = 1
			$channel[2] = 1
			$channel[3] = 1

		Case $wert = 8
			;I4
			$channel[4] = 1

		Case $wert = 9
			;I4 + I1
			$channel[1] = 1
			$channel[4] = 1

		Case $wert = 10
			;I4 + I2
			$channel[2] = 1
			$channel[4] = 1

		Case $wert = 11
			;I4 + I2 + I1
			$channel[1] = 1
			$channel[2] = 1
			$channel[4] = 1

		Case $wert = 12
			;I4 + I3
			$channel[3] = 1
			$channel[4] = 1

		Case $wert = 13
			;I4 + I3 + I1
			$channel[1] = 1
			$channel[3] = 1
			$channel[4] = 1

		Case $wert = 14
			;I4 + I3 + I2
			$channel[2] = 1
			$channel[3] = 1
			$channel[4] = 1

		Case $wert = 15
			;I4 + I3 * I2 + I1
			$channel[1] = 1
			$channel[2] = 1
			$channel[3] = 1
			$channel[4] = 1

		Case $wert = 16
			;I5
			$channel[5] = 1

		Case $wert = 17
			;I1
			$channel[1] = 1
			$channel[5] = 1

		Case $wert = 18
			;I2
			$channel[2] = 1
			$channel[5] = 1

		Case $wert = 19
			;I1 + I2
			$channel[1] = 1
			$channel[2] = 1
			$channel[5] = 1

		Case $wert = 20
			;I3
			$channel[3] = 1
			$channel[5] = 1

		Case $wert = 21
			;I3 + I1
			$channel[1] = 1
			$channel[3] = 1
			$channel[5] = 1

		Case $wert = 22
			;I3 + I2
			$channel[2] = 1
			$channel[3] = 1
			$channel[5] = 1

		Case $wert = 23
			;I3 + I2 + I1
			$channel[1] = 1
			$channel[2] = 1
			$channel[3] = 1
			$channel[5] = 1

		Case $wert = 24
			;I4
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 25
			;I4 + I1
			$channel[1] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 26
			;I4 + I2
			$channel[2] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 27
			;I4 + I2 + I1
			$channel[1] = 1
			$channel[2] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 28
			;I4 + I3
			$channel[3] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 29
			;I4 + I3 + I1
			$channel[1] = 1
			$channel[3] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 30
			;I4 + I3 + I2
			$channel[2] = 1
			$channel[3] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case $wert = 31
			;I4 + I3 * I2 + I1
			$channel[1] = 1
			$channel[2] = 1
			$channel[3] = 1
			$channel[4] = 1
			$channel[5] = 1

		Case Else

			$channel[1] = 0
			$channel[2] = 0
			$channel[3] = 0
			$channel[4] = 0
			$channel[5] = 0

	EndSelect

	If $channel[$sellect] = 1 Then
		;    MsgBox(0,"","return 1")
		Return 1
	Else
		;    MsgBox(0,"","return 0")
		Return 0

	EndIf

EndFunc   ;==>ReadDigitalChannel
;--------------------------