#!/usr/bin/env python

import threading
import time
from ctypes import *
#
count = 0
loop = 1
# load BCA_Lib.dll
bca = cdll.LoadLibrary("BCA_Lib.dll")
#	function
CMPFUNC = CFUNCTYPE(None, c_void_p, c_ubyte, c_void_p)
#	send callback function
def send_func(contx, channel, cookie):
	print "send_func"
	global count
	data = (c_ubyte * 8)()
	data[0] = 1
	data[1] = 2
	data[2] = 3
	data[3] = 4
	data[4] = 5
	data[5] = 6
	data[6] = 7
	data[7] = count
	count+=1
	# data send
	bca.BCA_SendData(contx, channel, data, 8);

	return
#
func = CMPFUNC(send_func)
#
class thread(threading.Thread):
		def __init__(self):
				threading.Thread.__init__(self)

		def run(self):
				while loop:
					time.sleep(1)
#
if __name__ == "__main__":
	#	open device
	print "OpenDevice"
	context = bca.BCA_OpenDevice(0)
	#	regist callback function
	print "RegisterSendFunc"
	bca.BCA_RegisterSendFunc(context, func, context)
	#	open channel
	print "OpenChannel"
	res = bca.BCA_OpenChannel(context,\
														0,\
														0,\
														0,\
														33,\
														1,\
														1,\
														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)
