#!/usr/bin/env python

# Defines the entry point for the application.


#  Take ADAM-5017 for example
#  Slot : 0
#  Get/Set Integration time
#  
#  Note: You have mark the module definition or change slot index

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

iSlot = 0
myprocHandle = c_int(0)

def kbhit():                                   
    fd = sys.stdin.fileno()                    
    r = select.select([sys.stdin],[],[],0.01)  
    rcode = ''                                 
    if len(r[0]) >0:                           
        rcode  = sys.stdin.read(1)             
    return rcode                  

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


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

				
def showIntegrationTime():
	global iSlot
	IntegrationTime = c_int(0)
	ret = bdaqadamio.AI_GetRangeIntegrationTime(myprocHandle, iSlot, IntegrationTime)
	if ret == 0:
		if IntegrationTime.value == 1 :
			print("AI_GetRangeIntegrationTime: \t\t%s"%("50 Hz"))
		else :
			print("AI_GetRangeIntegrationTime: \t\t%s"%("60 Hz"))
	else:
		print("AI_GetRangeIntegrationTime failed.ret = 0x%X"%ret)



def setIntegrationTime():
	global iSlot
	IntegrationTime = int(raw_input("Enter IntegrationTime[0:60Hz,1:50Hz]:"))

	ret = bdaqadamio.AI_SetRangeIntegrationTime(myprocHandle, iSlot, IntegrationTime)
	if ret== 0:
		print("AI_SetRangeIntegrationTime done.")
	else :
		print("AI_SetRangeIntegrationTime failed.ret = 0x%X"%ret)


def main():
	global iSlot
 	print("/*** Intgration Time sample ***/")
	iSlot = int(raw_input("Select slotid: "))

	iRet = OpenLib()
	if iRet == 0:
		showIntegrationTime()
		setIntegrationTime()
		showIntegrationTime()
	else:
		print("OpenLib(%d) failed."%iRet)

	CloseLib()
	print("/***        END         ***/")
	
if __name__ == "__main__":
	main()

