#!/usr/bin/env python

import threading
import time
from ctypes import *
#	load BCA_Lib.dll
bca = cdll.LoadLibrary("BCA_Lib.dll")
# function
CMPFUNC = CFUNCTYPE(None, c_void_p, c_ubyte, c_ubyte, c_void_p, c_uint, c_void_p)
#	receive function
def recv_func(contx, channel, event, data, length, cookie):
	dat = cast(data, POINTER(c_ubyte))
	print "receive channel:", channel, \
													"data:", \
													hex(dat[0]).upper(),\
													hex(dat[1]).upper(),\
													hex(dat[2]).upper(),\
													hex(dat[3]).upper(),\
													hex(dat[4]).upper(),\
													hex(dat[5]).upper(),\
													hex(dat[6]).upper(),\
													hex(dat[7]).upper()
	return
#
class thread(threading.Thread):
		def __init__(self):
				threading.Thread.__init__(self)

		def run(self):
				while loop:
					time.sleep(1)
#
func = CMPFUNC(recv_func)
#
loop = 1
#
if __name__ == "__main__":
#	open device
	print "OpenDevice"
	context = bca.BCA_OpenDevice(0)
#	regist callback function
	print "RegisterReceiveFunc"
	bca.BCA_RegisterReceiveFunc(context, func, context)
#	open channel
	print "OpenChannel"
	res = bca.BCA_OpenChannel(context,\
														0,\
														16,\
														0,\
														0,\
														0,\
														0,\
														66,\
														c_double(4.0))
#
	app = thread()
	app.start()
	app.join()
#	close channel
	print "CloseChannel"
	res = bca.BCA_CloseChannel(context, 0)
#	close device
	print "CloseDevice"
	res = bca.BCA_CloseDevice(context)
