function AiData=contec_ad(interval,data_num,AiChannels)
%  contec_ad.m
%  The calling syntax is:
% AiData = contec_ad(interval,data_num,AiChannels);
%  Kazuyuki Kobayashi   Feb 21, 2005
%  Copyright (c) 2005 Gerox(c)
%  All Rights Reserved
%  for Windows版高機能アナログ入出力ドライバ API-AIO(WDM)
%これを使用する際には、コンテック社のドライバに含まれるcaio.hが必要です。
%  ADA16-8/2(CB)L ADI12-8(USB)GY AD128PM でテスト済み
% Windows版高機能アナログ入出力ドライバ API-AIO(WDM)がサポートしているカードで動作可
% ただし、入力レンジなどの設定をチューニングする必要あり、
if nargin ~=3
		fprintf('   Contec AD card driver for MATLAB Version 6.5 later \n');
		fprintf('                       Gerox(c) 2005 All Rights Reserved\n');
		fprintf('                                    http://www.gerox.com\n');
		fprintf('   In order to use this function,please install         \n');
		fprintf('   API-AIO(WDM) (Contec web site )  and copy caio.h in matlab current working directory\n');
		fprintf('                  default DeviceName is AIO000\n\n');
		fprintf('   Usage: [analog_in] = contec_ad(interval,data_num,channel)\n\n');
		fprintf('   Example:  data = contec_ad(interval,data_num,channel)            \n');
		fprintf('   Inerval ranges and data_nums, channles are depending on hardware');
		fprintf('   Following parameters are for AD128PM ad/da card');
		fprintf('   interval range   :%f - %f Sec\n',1048576.0/1e7,100./1e7);
		fprintf('   data_num*channel :1-16384\n');
		fprintf('   channel          :1-8\n\n');
end	
if exist('caio.h')~= 2;
	disp('これを使用する際には、コンテック社のドライバAPI-AIO(WDM)に含まれるcaio.hが必要です。');
	return;
end
if ~libisloaded('caio');loadlibrary('caio.dll','caio.h');end
Id=0;DeviceName='AIO000';
AiData=[];
[Ret,DeviceName,Id]=calllib('caio','AioInit',DeviceName, Id);
if Ret ~= 0
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioInit = %d : %s\n',Ret,ErrorString);disp('error');
	fprintf('AioInit = %d \n',Ret);
	Ret = calllib('caio','AioExit',Id );
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioResetDevice',Id);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioResetDevice = %d : %s\n',Ret,ErrorString);
	fprintf('AioResetDevice = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
%	±10V			: 0	|0～10V			: 50
%	±5V			: 1	|0～5V			: 51
%	±2.5V		: 2	|0～4.095V	: 52
%	±1.25V		: 3	|0～2.5V		: 53
%	±1V			: 4	|0～1.25V		: 54
%	±0.625V	: 5	|0～1V			: 55
%	±0.5V		: 6	|0～0.5V		: 56
%	±0.3125V	: 7	|0～0.25V		: 57
%	±0.25V		: 8	|0～0.1V		: 58
%	±0.125V	: 9	|0～0.05V		: 59
%	±0.1V		:10	|0～0.025V	: 60
%	±0.05V		:11	|0～0.0125V	: 61
%	±0.025V	:12	|0～20mA		:100
%	±0.0125V	:13	|4～20mA		:101
%	1～5V			:150
PM10=0;%±10V
Ret=calllib('caio','AioSetAiRangeAll',Id,PM10);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetAiRangeAll = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetAiRangeAll = %d\n',Ret);
	Ret = calllib('caio','AioExit',Id );
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiChannels',Id,AiChannels);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AiSetAiChannels = %d : %s\n',Ret,ErrorString);
	fprintf('AiSetAiChannels = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiMemoryType',Id,0);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetAiMemoryType = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetAiMemoryType = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiClockType',Id,0);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetAiClockType = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetAiClockType = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiSamplingClock',Id,(interval*1000000.));
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetAiSamplingClock = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetAiSamplingClock = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiStartTrigger',Id,0);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetAiStartTrigger = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetAiStartTrigger = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiStopTrigger',Id,0);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioAiStopTrigger = %d : %s\n',Ret,ErrorString);
	fprintf('AioAiStopTrigger = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioSetAiStopTimes',Id,data_num);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetStopTimes = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetStopTimes = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioResetAiMemory',Id);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioResetAiMemory = %d : %s\n',Ret,ErrorString);
	fprintf('AioResetAiMemory = %d\n',Ret);
	Ret = calllib('caio','AioExit',Id );
	unloadlibrary('caio');
	return;
end
Ret=calllib('caio','AioStartAi',Id);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioStartAi = %d : %s\n',Ret,ErrorString);
	fprintf('AioStartAi = %d\n',Ret);
	Ret = calllib('caio','AioExit',Id );
	unloadlibrary('caio');
	return;
end
AIS_BUSY=00000001;
AiStatus = 1;AiSamplingCount=0;
while	bitand(double(AiStatus),AIS_BUSY)==AIS_BUSY;
	[Ret,AiStatus]=calllib('caio','AioGetAiStatus',Id,AiStatus);
	if Ret~=0;
%		[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%		fprintf('AioGetAiStatus = %d : %s\n',Ret,ErrorString);
		fprintf('AioGetAiStatus = %d\n',Ret);
		Ret = calllib('caio','AioExit',Id );
		unloadlibrary('caio');
		return;
	end
	[Ret,AiSamplingCount]= calllib('caio','AioGetAiSamplingCount',Id, AiSamplingCount);
	if Ret~=0;
%		[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%		fprintf('AioGetAiSamplingCount = %d : %s\n',Ret,ErrorString);
		fprintf('AioGetAiSamplingCount = %d \n',Ret);
		Ret = calllib('caio','AioExit',Id );
		unloadlibrary('caio');
		return;
	end
end
AiData=libpointer('singlePtr',zeros(1,data_num*AiChannels));
AiSamplingTimes = data_num;
[Ret,AiSamplingTimes,AiData]=calllib('caio','AioGetAiSamplingDataEx',Id, AiSamplingTimes, AiData);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioGetAiSamplingDataEx = %d : %s\n',Ret,ErrorString);
	fprintf('AioGetAiSamplingDataEx = %d\n',Ret);
	Ret = calllib('caio','AioExit',Id );
	unloadlibrary('caio');
	return;
end
AiData=reshape(AiData,AiChannels,data_num)';
Ret = calllib('caio','AioExit',Id );
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioExit = %d : %s\n',Ret,ErrorString);
	fprintf('AioExit = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
if libisloaded('caio');unloadlibrary('caio');end
