pce.c
changeset 18 f85465640dd4
parent 17 021968512187
child 19 3ed36b0e41bd
equal deleted inserted replaced
17:021968512187 18:f85465640dd4
    29 usb_dev_handle *handle;
    29 usb_dev_handle *handle;
    30 struct usb_device *device;
    30 struct usb_device *device;
    31 
    31 
    32 int ep_in = 1;
    32 int ep_in = 1;
    33 int ep_out = 2;
    33 int ep_out = 2;
       
    34 
       
    35 
       
    36 /* Downloading, the base timestamp */
       
    37 time_t timestamp;
       
    38 int time_per_sample;
    34 
    39 
    35 enum
    40 enum
    36 {
    41 {
    37     HELP,
    42     HELP,
    38     SET,
    43     SET,
   155     assert(res == 1);
   160     assert(res == 1);
   156 }
   161 }
   157 
   162 
   158 void parse_data(char *data)
   163 void parse_data(char *data)
   159 {
   164 {
   160     fprintf(outfile, "# Start: %hi-%hhi-%hhi %hhi:%hhi:%hhi\n",
   165     time_t now;
   161             *((short int *) (data + 16)),
   166     struct tm *tm_now;
   162             *((unsigned char *) (data + 28))+1,
   167     struct tm tm;
   163             *((unsigned char *) (data + 29)),
   168 
   164             *((unsigned char *) (data + 30)),
   169     now = time(0);
   165             *((unsigned char *) (data + 31)),
   170 
   166             *((unsigned char *) (data + 32)));
   171     tm_now = localtime(&now);
       
   172     memset(&tm, 0, sizeof(tm));
       
   173 
       
   174     tm.tm_isdst = tm_now->tm_isdst;
       
   175     tm.tm_year = *((short int *) (data + 16)) - 1900;
       
   176     tm.tm_mon = *((unsigned char *) (data + 28));
       
   177     tm.tm_mday = *((unsigned char *) (data + 29));
       
   178     tm.tm_hour = *((unsigned char *) (data + 30));
       
   179     tm.tm_min = *((unsigned char *) (data + 31));
       
   180     tm.tm_sec = *((unsigned char *) (data + 32));
       
   181 
       
   182     timestamp = mktime(&tm);
       
   183 
       
   184     fprintf(outfile, "# Start: %i-%i-%i %i:%i:%i @%llu\n",
       
   185             tm.tm_year + 1900,
       
   186             tm.tm_mon+1,
       
   187             tm.tm_mday,
       
   188             tm.tm_hour,
       
   189             tm.tm_min,
       
   190             tm.tm_sec,
       
   191             (unsigned long long) timestamp);
       
   192 
   167     fprintf(outfile, "# Num Samples: %hi\n",
   193     fprintf(outfile, "# Num Samples: %hi\n",
   168             *((short int *) (data + 8))
   194             *((short int *) (data + 8))
   169             );
   195             );
   170     fprintf(outfile, "# Time between samples: %hi s\n",
   196 
   171             *((short int *) (data + 12))
   197     time_per_sample = *((short int *) (data + 12));
   172             );
   198 
       
   199     fprintf(outfile, "# Time between samples: %i s\n",
       
   200             time_per_sample);
   173 }
   201 }
   174 
   202 
   175 int
   203 int
   176 start_download()
   204 start_download()
   177 {
   205 {
   202         res = usb_bulk_read(handle, ep_in, data, 64, 1000);
   230         res = usb_bulk_read(handle, ep_in, data, 64, 1000);
   203         assert(res == 64);
   231         assert(res == 64);
   204 
   232 
   205         parse_data(data);
   233         parse_data(data);
   206 
   234 
   207         int sec = 0;
   235         int sample = 0;
   208         int block = 0;
   236         int block = 0;
   209         do
   237         do
   210         {
   238         {
   211             char frame[3];
   239             char frame[3];
   212             int step_frames = num_frames >= 0x40 ? 0x40 : num_frames;
   240             int step_frames = num_frames >= 0x40 ? 0x40 : num_frames;
   232                 int j;
   260                 int j;
   233                 for(j=0; j<take; j+=4)
   261                 for(j=0; j<take; j+=4)
   234                 {
   262                 {
   235                     float temp = *((short int *) (data + j)) / 10.;
   263                     float temp = *((short int *) (data + j)) / 10.;
   236                     float hum = *((short int *) (data + j + 2)) / 10.;
   264                     float hum = *((short int *) (data + j + 2)) / 10.;
   237                     fprintf(outfile, "%i %g %g\n", sec, temp, hum);
   265 
   238                     sec++;
   266                     time_t sampletime = timestamp + time_per_sample * sample;
       
   267 
       
   268                     fprintf(outfile, "%i %llu %g %g\n", sample,
       
   269                             (unsigned long long) sampletime,
       
   270                             temp, hum);
       
   271                     sample++;
   239                 }
   272                 }
   240                 num -= 64;
   273                 num -= 64;
   241                 num_frames -= 1;
   274                 num_frames -= 1;
   242             }
   275             }
   243             ++block;
   276             ++block;