#!/usr/bin/env python
# AdamDI.cpp : Defines the entry point for the application.


# Take ADAM-5050 for example
# Slot : 0
# Reverse channel 0 value 

import sys
import ctypes
from ctypes import cdll
from ctypes import *
import time
import os
import select
import bdaqadamio

iSlot = 0
iChannel = 0

myprocHandle = c_int(0)

def OpenLib() :
	ret = bdaqadamio.ADAMDrvOpen(myprocHandle)
	if ret == 0:
		return 0
	else :
		return -99

def CloseLib() :
	if myprocHandle != 0 :
		bdaqadamio.ADAMDrvClose(myprocHandle)

def main():
	dVal = c_int(0)
	dmode = c_int(0)

 	print("/*** Adam50XXDI Sample ***/")
	iSlot = int(raw_input("Select slotid: "))
	iChannel = int(raw_input("Select channel: "))
	iRet = OpenLib()
	if iRet == 0:
		# Get dio module values
		if bdaqadamio.DIO_GetUniversalStatus(myprocHandle, iSlot, dmode) == 0:
			print("GetModes: %lx"%dmode.value)

		if bdaqadamio.DI_GetValues(myprocHandle, iSlot, dVal) == 0:
			print("GetValues: %lx"%dVal.value)
			bValue = (dVal.value) & (0x00000001<<iChannel)
			if bValue > 0 :
				print("Ch%d:1"%iChannel)
			else:
				print("Ch%d:0"%iChannel)
			dVal.value = 0
	else:
		print("OpenLib(%d) failed."%iRet)

	CloseLib()
	print("/***        END         ***/\n")

if __name__ == "__main__":
    main()      
