1 /***************************************************************************
2 * Copyright (C) 2004 by Nicolas Sutre *
3 * nicolas.sutre@free.fr *
4 * *
5 * Copyright (C) 2005 by Bob Dempsey *
6 * bdempsey_64@msn.com *
7 * *
8 * Copyright (C) 2005 by Julien Etelain and Edward Nys *
9 * Converted to C *
10 * Commented and improved by Julien Etelain <julien.etelain@utbm.fr> *
11 * Edward Nys <edward.ny@utbm.fr> *
12 * *
13 * Copyleft (L) 2005 by Sven Lindberg *
14 * k8055@k8055.mine.nu *
15 * Give it up already :) Simplified (read improved..) and included *
16 * with k8055 lib and with a functional man page *
17 * *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
22 * *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
27 * *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the *
30 * Free Software Foundation, Inc., *
31 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
32 ***************************************************************************/
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <usb.h>
37 #include <assert.h>
38 #include <sys/time.h>
39 #include "k8055.h"
40
41 #define STR_BUFF 256
42 #define false 0
43 #define true 1
44
45 extern int DEBUG;
46
47 int ia1 = -1;
48 int ia2 = -1;
49 int id8 = -1;
50 int ipid = 0;
51
52 int numread = 1;
53
54 int debug = 0;
55
56 int dbt1 = -1; // (-1 => not to set)
57 int dbt2 = -1; // (-1 => not to set)
58
59 int resetcnt1 = false;
60 int resetcnt2 = false;
61
62 int delay = 0;
63
64 /*
65 Convert a string on n chars to an integer
66 Return 1 on sucess
67 0 on failure (non number)
68 */
Convert_StringToInt(char * text,int * i)69 int Convert_StringToInt(char *text, int *i)
70 {
71 return sscanf(text, "%d", i);
72 }
73
74 /*
75 Write help to standart output
76 */
display_help(char * params[])77 void display_help ( char *params[] ) {
78
79 printf("K8055 version 0.3 MrBrain Build\n");
80 printf("Copyright (C) 2004 by Nicolas Sutre\n");
81 printf("Copyright (C) 2005 by Bob Dempsey\n");
82 printf("Copyright (C) 2005 by Julien Etelain and Edward Nys\n");
83 printf("Copyleft (L) 2005 by Sven Lindberg\n");
84 printf("\n");
85 printf("Syntax : %s [-p:(number)] [-d:(value)] [-a1:(value)] [-a2:(value)]\n",params[0]);
86 printf(" [-num:(number) [-delay:(number)] [-dbt1:(value)]\n");
87 printf(" [-dbt2:(value)] [-reset1] [-reset2] [-debug]\n");
88 printf(" -p:(number) Set board number\n");
89 printf(" -d:(value) Set digital output value (8 bits in decimal)\n");
90 printf(" -a1:(value) Set analog output 1 value (0-255)\n");
91 printf(" -a2:(value) Set analog output 2 value (0-255)\n");
92 printf(" -num:(number) Set number of measures\n");
93 printf(" -delay:(number) Set delay between two measure (in msec)\n");
94 printf(" -dbt1:(value) Set debounce time for counter 1 (in msec)\n");
95 printf(" -dbt2:(value) Set debounce time for counter 2 (in msec)\n");
96 printf(" -reset1 Reset counter 1\n");
97 printf(" -reset2 Reset counter 2\n");
98 printf(" -debug Activate debug mode\n");
99 printf("Example : %s -p:1 -d:147 -a1:25 -a2:203\n",params[0]);
100 printf("\n");
101 printf("Output : (timestamp);(digital);(analog 1);(analog 2);(counter 1);(counter 2)\n");
102 printf("Note : timestamp is the number of msec when data is read since program start\n");
103 printf("Example : 499;16;128;230;9;8\n");
104 printf("499 : Measure done 499 msec after program start\n");
105
106 }
107
108
109 /*
110 Read arguments, and store values
111 Return true if arguments are valid
112 else return false
113 */
read_param(int argc,char * params[])114 int read_param(int argc,char *params[])
115 {
116 int erreurParam = false;
117 int i;
118
119 ipid = 0;
120
121 for (i=1; i<argc;i++)
122 {
123 if ( !strncmp(params[i],"-p:",3) &&
124 !Convert_StringToInt(params[i]+3,&ipid) ) erreurParam = true;
125 else
126 if ( !strncmp(params[i],"-a1:",4) &&
127 !Convert_StringToInt(params[i]+4,&ia1) ) erreurParam = true;
128 else
129 if ( !strncmp(params[i],"-a2:",4) &&
130 !Convert_StringToInt(params[i]+4,&ia2) ) erreurParam = true;
131 else
132 if ( !strncmp(params[i],"-d:",3) &&
133 !Convert_StringToInt(params[i]+3,&id8) ) erreurParam = true;
134 else
135 if ( !strncmp(params[i],"-num:",5) &&
136 !Convert_StringToInt(params[i]+5,&numread) ) erreurParam = true;
137 else
138 if ( !strncmp(params[i],"-delay:",7) &&
139 !Convert_StringToInt(params[i]+7,&delay) ) erreurParam = true;
140 else
141 if ( !strncmp(params[i],"-dbt1:",6) &&
142 !Convert_StringToInt(params[i]+6,&dbt1) ) erreurParam = true;
143 else
144 if ( !strncmp(params[i],"-dbt2:",6) &&
145 !Convert_StringToInt(params[i]+6,&dbt2) ) erreurParam = true;
146 else
147 if ( !strcmp(params[i],"-debug") ){
148 debug = true;
149 DEBUG = true;
150 }
151 else
152 if ( !strcmp(params[i],"-reset1") ) resetcnt1 = true;
153 else
154 if ( !strcmp(params[i],"-reset2") ) resetcnt2 = true;
155 else
156 if ( !strcmp(params[i],"--help") ) {
157 display_help(params);
158 return false;
159 }
160
161 }
162
163
164 /*
165 Send parameters to standart error
166 */
167 if ( debug )
168 fprintf(stderr,"Parameters : Card=%d Analog1=%d Analog2=%d Digital=%d\n",ipid,ia1,ia2,id8);
169
170 if (ipid<0 || ipid>3){
171 printf("Invalid board address!\n");
172 return -1;
173 }
174
175
176 if (erreurParam)
177 {
178
179 printf("Invalid or incomplete options\n");
180
181 display_help(params);
182 return false;
183 }
184
185
186 return true;
187 }
188
189 /*
190 Give current timestamp in miliseconds
191 */
time_msec(void)192 inline unsigned long int time_msec ( void ) {
193 struct timeval t; struct timezone tz;
194 gettimeofday (&t,&tz);
195 return (1000*t.tv_sec)+(t.tv_usec/1000);
196 }
197
198
main(int argc,char * params[])199 int main (int argc,char *params[])
200 {
201 int i,result[3];
202 unsigned char d=0;
203 long a1=0,a2=0;
204 unsigned short c1=0, c2=0;
205 unsigned long int start,mstart=0,lastcall=0;
206
207 start = time_msec();
208
209 /*
210 Load parameters
211 If parameters are valid continue
212 */
213
214 if (read_param(argc,params))
215 {
216 /*
217 Initialise USB system
218 and enable debug mode
219
220 if ( debug )
221 usb_set_debug(2);
222 */
223 /*
224 Search the device
225 */
226 if ( OpenDevice(ipid)<0 ) {
227 printf("Could not open the k8055 (port:%d)\nPlease ensure that the device is correctly connected.\n",ipid);
228 return (-1);
229
230 } else {
231
232 if ( resetcnt1 )
233 ResetCounter(1);
234 if ( resetcnt2 )
235 ResetCounter(2);
236 if ( dbt1 != -1 )
237 SetCounterDebounceTime(1,dbt1);
238
239 if ( dbt2 != -1 )
240 SetCounterDebounceTime(2,dbt1);
241
242 mstart = time_msec(); // Measure start
243 for (i=0; i<numread; i++) {
244
245 if ( delay ) {
246 // Wait until next measure
247 while ( time_msec()-mstart < i*delay );
248 }
249 ReadAllAnalog(&a1,&a2);
250 d=ReadAllDigital();
251 c1=ReadCounter(1);
252 c2=ReadCounter(2);
253 lastcall = time_msec();
254 printf("%d;%d;%d;%d;%d;%d\n", (int)(lastcall-start),d, (int)a1, (int)a2,c1,c2 );
255 }
256
257 if (debug && ((ia1!=-1)||(ia2!=-1)||(id8!=-1))) printf("Set ");
258 if (ia1!=-1){ result[0]=OutputAnalogChannel(1,ia1);
259 if (debug) printf("analog1:%d ",result[0]);}
260 if (ia2!=-1){ result[1]=OutputAnalogChannel(2,ia2);
261 if (debug) printf("analog2:%d ",result[1]);}
262 if (id8!=-1){ result[2]=WriteAllDigital((long)id8);
263 if (debug) printf("digital:%d",result[2]);}
264 if (debug) printf("\n");
265 CloseDevice();
266 }
267 }
268 return 0;
269 }
270