source: ETALON/CLIO/20170221_savedata.c

Last change on this file was 755, checked in by delerue, 6 years ago

Importing savedata

File size: 10.5 KB
Line 
1// $URL: http://subversion:8080/svn/gsc/trunk/drivers/LINUX/18AI32SSC1M/savedata/savedata.c $
2// $Rev: 35161 $
3// $Date: 2016-06-06 16:28:07 -0500 (Mon, 06 Jun 2016) $
4
5#include <ctype.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <time.h>
10#include <unistd.h>
11
12#include "main.h"
13
14#define MAX_INFO_FILE_LENGTH 500
15
16
17// #defines *******************************************************************
18
19#define _1M             (1024L * 1024L)
20
21
22
23// variables *******************************************************************
24
25static  u32     _buffer[_1M];
26
27
28
29//*****************************************************************************
30static int _channels(int fd, s32* chans)
31{
32        s32             active  = -1;
33        char    buf[64];
34        int             errs    = 0;
35        s32             first   = -1;
36        s32             last    = -1;
37        s32             single  = -1;
38
39        gsc_label("Input Channels");
40
41        active  = -1;
42        errs    += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_ACTIVE, &active );
43        errs    += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_FIRST,  &first  );
44        errs    += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_LAST,   &last   );
45        errs    += ai32ssc1m_dsl_ioctl(fd, AI32SSC1M_IOCTL_CHAN_SINGLE, &single );
46
47        if (errs == 0)
48        {
49                switch (active)
50                {
51                        default:
52
53                                errs++;
54                                printf("FAIL <---  (invalid ACTIVE selection: %ld)\n", (long) active);
55                                break;
56
57                        case AI32SSC1M_CHAN_ACTIVE_SINGLE:
58
59                                chans[0]        = 1;
60                                sprintf(buf, "# %ld", (long) single);
61                                break;
62
63                        case AI32SSC1M_CHAN_ACTIVE_0_1:
64
65                                chans[0]        = 2;
66                                strcpy(buf, "#s 0-1");
67                                break;
68
69                        case AI32SSC1M_CHAN_ACTIVE_0_3:
70
71                                chans[0]        = 4;
72                                strcpy(buf, "#s 0-3");
73                                break;
74
75                        case AI32SSC1M_CHAN_ACTIVE_0_7:
76
77                                chans[0]        = 8;
78                                strcpy(buf, "#s 0-7");
79                                break;
80
81                        case AI32SSC1M_CHAN_ACTIVE_0_15:
82
83                                chans[0]        = 16;
84                                strcpy(buf, "#s 0-15");
85                                break;
86
87                        case AI32SSC1M_CHAN_ACTIVE_0_31:
88
89                                chans[0]        = 32;
90                                strcpy(buf, "#s 0-31");
91                                break;
92
93                        case AI32SSC1M_CHAN_ACTIVE_RANGE:
94
95                                chans[0]        = last - first + 1;
96
97                                if (first == last)
98                                        sprintf(buf, "# %ld", (long) first);
99                                else
100                                        sprintf(buf, "#s %ld-%ld", (long) first, (long) last);
101
102                                break;
103                }
104
105                if (errs == 0)
106                {
107                        printf( "%ld Channel%s  (%s)\n",
108                                        (long) chans[0],
109                                        (chans[0] == 1) ? "" : "s",
110                                        buf);
111                }
112        }
113
114        return(errs);
115}
116
117
118
119//*****************************************************************************
120static int _read_data(int fd)
121{
122        int             errs;
123        long    get             = sizeof(_buffer) / 4;
124        int             got;
125
126        gsc_label("Reading");
127        got     = ai32ssc1m_dsl_read(fd, _buffer, get);
128
129        if (got < 0)
130        {
131                errs    = 1;
132        }
133        else if (got != get)
134        {
135                errs    = 1;
136                printf( "FAIL <---  (got %ld samples, requested %ld)\n",
137                                (long) got,
138                                (long) get);
139        }
140        else
141        {
142                errs    = 0;
143                printf( "PASS  (%ld samples)\n",
144                                (long) get);
145        }
146
147        return(errs);
148}
149
150
151
152
153//*****************************************************************************
154static int _save_data_with_datename(int fd, int chan, int acquisition_rate, int errs)
155{
156  //  char      name_hex[50];
157  //  char      name_dec[50];
158        char    name[50];
159
160        s32 first_channel=-1;
161        s32 last_channel=-1;
162        s32 amplitude=-1;
163        FILE*           file;
164        int                     i;
165        long            l;
166        //      const char*     name    = "data.txt";
167        long            samples = sizeof(_buffer) / 4;
168 
169        time_t t = time(NULL);
170        struct tm tm = *localtime(&t);
171
172        sprintf(name,"data_CLIO_%d-%02d-%02d_%02d-%02d-%02d.txt", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
173
174        errs    += ai32ssc1m_chan_first                 (fd, -1, -1, &first_channel);
175        errs    += ai32ssc1m_chan_last                  (fd, -1, -1, &last_channel);
176                errs    += ai32ssc1m_ain_range(fd, -1, -1, &amplitude);
177        gsc_label("Filename");
178        printf("%s \n",name);
179
180        gsc_label("Saving");
181
182        for (;;)
183        {
184                if (errs)
185                {
186                        printf("SKIPPED  (errors)\n");
187                        errs    = 0;
188                        break;
189                }
190
191                file    = fopen(name, "w+b");
192
193                if (file == NULL)
194                {
195                        printf("FAIL <---  (unable to create %s)\n", name);
196                        errs    = 1;
197                        break;
198                }
199
200                fprintf(file,"#date= %d-%02d-%02d_%02d-%02d-%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
201                fprintf(file,"#first= %d\n", first_channel);
202                fprintf(file,"#last= %d\n", last_channel);
203                fprintf(file,"#acquisition_rate= %d\n", acquisition_rate);
204                fprintf(file,"#amplitude= %d\n", amplitude);
205
206
207                //              printf("SAMPLES %d \n",samples);
208                for (l = 0; l < samples; l++)
209                {
210                        i       = fprintf(file, "%08lX  ", (long) _buffer[l]);
211
212                        if (i != 10)
213                        {
214                                printf("FAIL <---  (fprintf() failure to %s)\n", name);
215                                errs    = 1;
216                                break;
217                        }
218                        //                      if ((l) && (_buffer[l] & 0x80000000))
219                        if (l%32==31)
220                        {
221                                i       = fprintf(file, "\r\n");
222
223                                if (i != 2)
224                                {
225                                        printf("FAIL <---  (fprintf() failure to %s)\n", name);;
226                                        errs    = 1;
227                                        break;
228                                }
229                        }
230                       
231                }
232
233                fprintf(file,"\r\n");
234                fclose(file);
235
236                if (errs == 0)
237                        printf("PASS  (%s)\n", name);
238
239                break;
240        }
241
242        return(errs);
243
244} // _save_data_with_name
245
246//*****************************************************************************
247static int _save_data(int fd, int chan, int errs)
248{
249        FILE*           file;
250        int                     i;
251        long            l;
252        const char*     name    = "data.txt";
253        long            samples = sizeof(_buffer) / 4;
254
255        gsc_label("Saving");
256
257        for (;;)
258        {
259                if (errs)
260                {
261                        printf("SKIPPED  (errors)\n");
262                        errs    = 0;
263                        break;
264                }
265
266                file    = fopen(name, "w+b");
267
268                if (file == NULL)
269                {
270                        printf("FAIL <---  (unable to create %s)\n", name);
271                        errs    = 1;
272                        break;
273                }
274
275                for (l = 0; l < samples; l++)
276                {
277                        if ((l) && (_buffer[l] & 0x80000000))
278                        {
279                                i       = fprintf(file, "\r\n");
280
281                                if (i != 2)
282                                {
283                                        printf("FAIL <---  (fprintf() failure to %s)\n", name);;
284                                        errs    = 1;
285                                        break;
286                                }
287                        }
288
289                        i       = fprintf(file, "  %08lX", (long) _buffer[l]);
290
291                        if (i != 10)
292                        {
293                                printf("FAIL <---  (fprintf() failure to %s)\n", name);
294                                errs    = 1;
295                                break;
296                        }
297                }
298
299                fclose(file);
300
301                if (errs == 0)
302                        printf("PASS  (%s)\n", name);
303
304                break;
305        }
306
307        return(errs);
308}
309
310
311
312//*****************************************************************************
313int save_data(int fd, s32 io_mode)
314{
315        s32     chans   = AI32SSC1M_CHAN_ACTIVE_0_31;
316        //      s32     chans   = AI32SSC1M_CHAN_ACTIVE_0_15;
317        int     errs    = 0;
318        s32     timeout = 20;
319        s32     acquisition_rate    = 36; // 36=1Ms/s min: 549s/s
320        s32     previous_acquisition_rate=0;
321        gsc_label("Capture & Save");
322        printf("\n");
323        gsc_label_level_inc();
324
325        //Acquisition rate     
326        errs    += ai32ssc1m_config_ai(fd, -1, acquisition_rate);
327        previous_acquisition_rate=acquisition_rate;
328        //      errs    += ai32ssc1m_config_ai(fd, -1, 1440);
329
330        //Trigger:
331        //AI32SSC1M_BURST_SYNC_EXT => external trigger
332        //AI32SSC1M_BURST_SYNC_DISABLE => Internal trigger
333                errs    += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_DISABLE, NULL); // Internal trigger
334        //      errs    += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_EXT, NULL); // External trigger
335        //errs    += ai32ssc1m_burst_sync(fd, -1, AI32SSC1M_BURST_SYNC_RBG, NULL); // External trigger
336
337                        //      errs    += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL);
338                //                      errs    += ai32ssc1m_aux_sync_mode(fd,-1,AI32SSC1M_AUX_SYNC_MODE_INPUT,NULL);
339                                //errs  += ai32ssc1m_aux_sync_mode(fd,-1,AI32SSC1M_AUX_SYNC_MODE_DISABLE,NULL);
340        errs    += ai32ssc1m_aux_clk_mode(fd,-1,AI32SSC1M_AUX_CLK_MODE_DISABLE,NULL);
341        errs    += ai32ssc1m_burst_size(fd, -1, 10240, NULL);
342
343                //              errs    += ai32ssc1m_burst_size(fd, -1, 16, NULL); // Burst size
344               
345        //Timeout (duration set above)
346        errs    += ai32ssc1m_rx_io_timeout(fd,-1, -1, timeout, NULL);
347       
348        errs    += ai32ssc1m_rx_io_mode(fd, -1, io_mode, NULL);
349        errs    += _channels(fd, &chans);
350
351        //Channels range
352        errs    += ai32ssc1m_chan_active                (fd, -1, AI32SSC1M_CHAN_ACTIVE_RANGE,  NULL);
353        errs    += ai32ssc1m_chan_first                 (fd, -1, 0, NULL);
354        errs    += ai32ssc1m_chan_last                  (fd, -1, 31,    NULL);
355        errs    += ai32ssc1m_ain_range(fd, -1, AI32SSC1M_AIN_RANGE_5V, NULL);
356        //      errs    += ai32ssc1m_ain_mode(fd, -1, -1, &timeout);
357        errs    += ai32ssc1m_ain_mode(fd, -1, AI32SSC1M_AIN_MODE_DIFF, NULL);
358        //      errs    += ai32ssc1m_ain_mode(fd, -1, AI32SSC1M_AIN_MODE_VREF, NULL);
359        gsc_label("1 second before data taking...");
360        sleep(1);
361        printf("Now\n");
362        gsc_label_level_inc();
363        for(;;) {
364          gsc_label("Acquiring");
365          errs  += ai32ssc1m_ain_buf_clear(fd, -1);
366          errs  += _read_data(fd);
367          errs  += ai32ssc1m_ain_buf_overflow(fd, -1, -1, NULL);
368          errs  += ai32ssc1m_ain_buf_underflow(fd, -1, -1, NULL);
369        //      errs    += ai32ssc1m_ain_buf_thr_lvl(fd, -1, -1, &timeout);
370        //printf("thr lvl %d \n",timeout);
371          errs  += _save_data_with_datename(fd, chans, acquisition_rate, errs);
372          printf("Done\n");
373       
374          char fileinfo[]="/home/etalon-admin/acquisition_data/acquisition_info.txt";
375          FILE* fid;
376          char line[MAX_INFO_FILE_LENGTH];
377          int pause_between_acquisition_seconds=1;
378          fid  = fopen(fileinfo, "r");
379          if (fid == NULL) {
380              printf("FAIL <---  (unable to read %s)\n", fileinfo);
381          } else {         
382            fread(line,sizeof(char),MAX_INFO_FILE_LENGTH,fid);
383            printf("Line read: %s\n",line); 
384            int iline=0;
385            while(line[iline]!='\0'){
386              int char_found=0;
387              if (strncmp("PAUSE",&(line[iline]),5)==0) {
388                iline+=6;
389                pause_between_acquisition_seconds=0;
390                while(((line[iline]-'0')>=0)&&((line[iline]-'0')<='9')) {
391                  pause_between_acquisition_seconds*=10;
392                  pause_between_acquisition_seconds+=(line[iline]-'0');
393                  iline++;
394                }
395                char_found=1;
396              } //line is pause
397              if (strncmp("RATE",&(line[iline]),4)==0) {
398                acquisition_rate=0;
399                iline+=5;
400                while(((line[iline]-'0')>=0)&&((line[iline]-'0')<='9')) {
401                  //              printf("val= %d %c %d %d\n",iline, line[iline], line[iline]-'0', acquisition_rate);
402                  acquisition_rate*=10;
403                  acquisition_rate+=(line[iline]-'0');
404                  iline++;
405                }
406                if (acquisition_rate!=previous_acquisition_rate) {
407                  errs  += ai32ssc1m_config_ai(fd, -1, acquisition_rate);
408                  previous_acquisition_rate=acquisition_rate;
409                }
410                char_found=1;
411              }
412              if ((line[iline]=='\f')||(line[iline]=='\n')){
413                iline++;
414                char_found=1;
415              } //special caracters
416              //printf("Reading... %d %d %d %s\n", iline, line[iline],char_found,&(line[iline]));
417              if(char_found==0){
418                iline++;
419              }
420            } //while reading line
421            fclose(fid);
422          }
423          printf("Sleep %d\n",pause_between_acquisition_seconds);
424          sleep(pause_between_acquisition_seconds);
425         
426          char filename[]="/home/etalon-admin/acquisition_data/pause_acquisition";
427          int isleep=0;
428          while( access( filename, F_OK ) != -1 ) {
429            // file exists
430            if (isleep==0) {
431              printf("File %s exists\nPause...\n",filename);
432              isleep=1;
433            }
434            printf(".");
435            sleep(10);
436          }
437          char filename_stop[]="/home/etalon-admin/acquisition_data/stop_acquisition";
438          if( access( filename_stop, F_OK ) != -1 ) {
439            printf("File %s is present, exiting...\n",filename_stop);
440            // file exists
441            break; //we leave the for loop
442          }
443        }
444        gsc_label_level_dec();
445        gsc_label_level_dec();
446        return(errs);
447}
448
449
450
Note: See TracBrowser for help on using the repository browser.