First working a bit version, with usage()
authorviric <viriketo@gmail.com>
Sun, 24 Oct 2010 03:54:16 +0200
changeset 2 884635f4a24f
parent 1 fdce143cc82c
child 3 5925ce9887c2
First working a bit version, with usage()
pce.c
protocol
--- a/pce.c	Sun Oct 24 02:21:02 2010 +0200
+++ b/pce.c	Sun Oct 24 03:54:16 2010 +0200
@@ -1,6 +1,10 @@
 #include <usb.h>
 #include <stdio.h>
 #include <assert.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
 
 const int vendor=0x10c4;
 const int product=0x0003;
@@ -11,6 +15,21 @@
 int ep_in = 1;
 int ep_out = 2;
 
+enum
+{
+    HELP,
+    SET,
+    DOWNLOAD
+} action;
+
+struct
+{
+    int nsamples;
+    int time_per_sample;
+    char *download_file;
+} config;
+
+FILE *outfile;
 
 int connect()
 {
@@ -50,6 +69,10 @@
 {
     int res;
     char buf[64];
+    struct tm *t;
+    time_t tmp_time;
+    tmp_time = time(NULL);
+    t = localtime(&tmp_time);
 
     buf[0] = '\xce';
     buf[1] = 0;
@@ -57,36 +80,36 @@
     buf[3] = 0;
 
     // Samples
-    *( (short int *) &buf[4]) = 16000;
+    *( (int *) &buf[4]) = config.nsamples;
     // Samples got
     *( (int *) &buf[8]) = 0;
     // Sample rate
-    *( (int *) &buf[12]) = 2;
+    *( (int *) &buf[12]) = config.time_per_sample;
     // Year
-    *( (int *) &buf[16]) = 2010;
+    *( (int *) &buf[16]) = t->tm_year;
     // Mintemp
     buf[20] = 0;
     buf[21] = 0;
-    *( (short int *) &buf[22]) = 16000;
+    *( (short int *) &buf[22]) = 0x4120;
     // Maxtemp
     buf[24] = 0;
     buf[25] = 0;
-    *( (short int *) &buf[26]) = 17000;
+    *( (short int *) &buf[26]) = 0x41a0;
 
     // Month
-    buf[28] = 10;
+    buf[28] = t->tm_mon;
     // Day
-    buf[29] = 10;
+    buf[29] = t->tm_mday;
     // hour
-    buf[30] = 0;
+    buf[30] = t->tm_hour;
     // Minute
-    buf[31] = 0;
+    buf[31] = t->tm_min;
     // second
-    buf[32] = 0;
+    buf[32] = t->tm_sec;
     // Celsius (1 F)
     buf[33] = 0;
-    // Pause between leds
-    buf[34] = 0x7e;
+    // Pause between leds (300s)
+    buf[34] = 0x1e;
     // Name
     buf[35] = 'A';
     buf[36] = '0';
@@ -96,11 +119,11 @@
     // Minhum
     buf[52] = 0;
     buf[53] = 0;
-    *( (short int *) &buf[54]) = 16000;
+    *( (short int *) &buf[54]) = 0x410f;
     // Maxhum
     buf[56] = 0;
     buf[57] = 0;
-    *( (short int *) &buf[58]) = 17000;
+    *( (short int *) &buf[58]) = 0x4296;
     // final
     buf[60] = 0xce;
     buf[61] = 0x0;
@@ -114,6 +137,23 @@
     assert(res == 1);
 }
 
+void parse_data(char *data)
+{
+    fprintf(outfile, "# Start: %hi-%hhi-%hhi %hhi:%hhi:%hhi\n",
+            *((short int *) (data + 16)),
+            *((unsigned char *) (data + 28)),
+            *((unsigned char *) (data + 29)),
+            *((unsigned char *) (data + 30)),
+            *((unsigned char *) (data + 31)),
+            *((unsigned char *) (data + 32)));
+    fprintf(outfile, "# Num Samples: %hi\n",
+            *((short int *) (data + 8))
+            );
+    fprintf(outfile, "# Time between samples: %hi s\n",
+            *((short int *) (data + 12))
+            );
+}
+
 int
 start_download()
 {
@@ -125,22 +165,100 @@
     char data[100];
     res = usb_bulk_read(handle, ep_in, data, 3, 1000);
     assert(res == 3);
-    printf("%02hhx%02hhx%02hhx\n", data[0], data[1], data[2]);
+    fprintf(stderr, "%02hhx%02hhx%02hhx\n", data[0], data[1], data[2]);
+
+    int num = *((short int*) &data[1]);
+    fprintf(stderr, "Found %i bytes\n", num);
+
+    if (num > 0)
+    {
+        int num_frames = (num + 63) / 64;
+        fprintf(stderr, "%i frames\n", num_frames);
 
-    res = usb_bulk_write(handle, ep_out, "\x00\x00\x01", 3, 1000);
-    assert(res == 3);
+        /*
+        res = usb_bulk_write(handle, ep_out, "\x00\x00\x01", 3, 1000);
+        assert(res == 3);
+        */
+
+        res = usb_bulk_read(handle, ep_in, data, 64, 1000);
+        assert(res == 64);
+
+        parse_data(data);
+
+        char frame[3];
+        frame[0] = 0;
+        frame[1] = 0;
+        frame[2] = num_frames;
 
-    res = usb_bulk_read(handle, ep_in, data, 3, 1000);
-    assert(res == 3);
-    printf("%02hhx%02hhx%02hhx\n", data[0], data[1], data[2]);
+        res = usb_bulk_write(handle, ep_out, frame, 3, 1000);
+        assert(res == 3);
+
+        res = usb_bulk_read(handle, ep_in, data, 3, 1000);
+        assert(res == 3);
+        fprintf(stderr, "%02hhx%02hhx%02hhx\n", data[0], data[1], data[2]);
 
-    res = usb_bulk_read(handle, ep_in, data, 64, 1000);
-    assert(res == 64);
+        int i;
+        int sec = 0;
+        for(i=0; i < num_frames; ++i)
+        {
+            res = usb_bulk_read(handle, ep_in, data, 64, 1000);
+            assert(res == 64);
+            int take = (num >= 64) ? 64 : num;
+
+            int j;
+            for(j=0; j<take; j+=4)
+            {
+                float temp = *((short int *) (data + j)) / 10.;
+                float hum = *((short int *) (data + j + 2)) / 10.;
+                fprintf(outfile, "%i %g %g\n", sec, temp, hum);
+                sec++;
+            }
+            num -= 64;
+        }
+    }
 }
 
-int main()
+void
+usage()
+{
+    fprintf(stderr, "usage: pce [-n nsamples] [-t timepersample] < -d downloadfile | -s >\n");
+}
+
+int main(int argc, char **argv)
 {
     int res;
+    int opt;
+
+    config.nsamples = 16000;
+    config.time_per_sample = 2;
+    config.download_file = 0;
+    action = HELP;
+    while  ((opt = getopt(argc, argv, "n:d:t:s")) != -1) {
+        switch (opt) {
+            case 'n':
+                config.nsamples = atoi(optarg);
+                break;
+            case 't':
+                config.time_per_sample = atoi(optarg);
+                break;
+            case 'd':
+                config.download_file = optarg;
+                action = DOWNLOAD;
+                break;
+            case 's':
+                action = SET;
+                break;
+            default:
+                usage();
+                return 1;
+        }
+    }
+
+    if (action == HELP)
+    {
+        usage();
+        return 0;
+    }
 
     usb_init();
 
@@ -154,24 +272,28 @@
         return -1;
     }
 
-    res = usb_set_configuration(handle,
-        device->config->bConfigurationValue);
-    assert(res == 0 && "set_configuration");
-
     res = usb_claim_interface(handle,
         device->config->interface->altsetting->bInterfaceNumber);
     assert(res == 0 && "claim_interface");
 
-    res = usb_set_altinterface(handle,
-        device->config->interface->altsetting->bAlternateSetting);
-    assert(res == 0 && "claim_interface");
-
-    send_program();
-    send_config();
-
-    /*
-    start_download();
-    */
+    if (config.download_file == 0)
+    {
+        send_program();
+        send_config();
+    }
+    else
+    {
+        outfile = fopen(config.download_file, "w");
+        if (outfile != NULL)
+        {
+            start_download();
+            fclose(outfile);
+        }
+        else
+        {
+            fprintf(stderr, "Error opening file: %s", strerror(errno));
+        }
+    }
 
     res = usb_release_interface(handle,
         device->config->interface->altsetting->bInterfaceNumber);
--- a/protocol	Sun Oct 24 02:21:02 2010 +0200
+++ b/protocol	Sun Oct 24 03:54:16 2010 +0200
@@ -11,7 +11,7 @@
 17 <= minut
 18 <= segon
 00 <= en cas de celsius, 01 en cas de farenheit
-7e <= pausa entre leds: 30s = 7E, 20s = 54, ... el bit més alt vol dir "leds flash for high and low alarm" (fe, en aquest cas)
+7e <= pausa entre leds: 30s = 7E, 20s = 54, ... el bit més alt vol dir "leds flash for high and low alarm" (fe, en aquest cas):  1e = 30s, 14 = 20.   9e/94 = flash.
 16 bytes seguents <= nom donat, 00 al final.
 01 <= 1 manual, 2 instant
 00 00 0c 42 <= mínim humitat (f041 = 30, 0c42 = 35, 2042 = 40, 3442 = 45)