]> git.zerfleddert.de Git - fs20pcs/commitdiff
parse time in extensionbyte
authorMichael Gernoth <michael@gernoth.net>
Sun, 19 May 2013 10:29:39 +0000 (10:29 +0000)
committerMichael Gernoth <michael@gernoth.net>
Sun, 19 May 2013 10:29:39 +0000 (10:29 +0000)
Makefile
fs20pcs.c

index 14f37d53caa66e2cce2164085026f8e9f20b7f76..fbbbf8b90ddc1bd118b8ece0ebefc728f66c0e84 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CFLAGS=-O2 -Wall -I/opt/local/include -g
 CFLAGS=-O2 -Wall -I/opt/local/include -g
-LDFLAGS=-L/opt/local/lib -lusb-1.0
+LDFLAGS=-L/opt/local/lib -lusb-1.0 -lm
 CC=gcc
 
 all: fs20pcs
 CC=gcc
 
 all: fs20pcs
index d74d5b0976ae32b62b6e2d60d750f51f1aa851b8..3f64a4123ef53046e62f440474dcbc9870e23033 100644 (file)
--- a/fs20pcs.c
+++ b/fs20pcs.c
@@ -3,6 +3,7 @@
 #include <stdint.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <math.h>
 #include <libusb-1.0/libusb.h>
 
 #define USB_TIMEOUT            10000
 #include <libusb-1.0/libusb.h>
 
 #define USB_TIMEOUT            10000
@@ -10,6 +11,8 @@
 #define ID_VENDOR      0x18ef
 #define ID_PRODUCT     0xe015
 
 #define ID_VENDOR      0x18ef
 #define ID_PRODUCT     0xe015
 
+#define EXT_TO_SEC(ext) ((1 << ((ext & 0xf0) >> 4)) * (ext & 0x0f) * 0.25)
+
 extern char *optarg;
 
 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
 extern char *optarg;
 
 /* Not in all libusb-1.0 versions, so we have to roll our own :-( */
@@ -108,13 +111,13 @@ int fs20pcs_send(libusb_device_handle *devh, unsigned char* send_data)
        int i;
        int ret;
 
        int i;
        int ret;
 
-       err = libusb_interrupt_transfer(devh, 0x01, send_data, 11, &cnt, 5000);
+       err = libusb_interrupt_transfer(devh, 0x01, send_data, 11, &cnt, USB_TIMEOUT);
        if (err) {
                fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
                return 0;
        }
 
        if (err) {
                fprintf(stderr, "Can't send data: %s\n", usb_strerror(err));
                return 0;
        }
 
-       err = libusb_interrupt_transfer(devh, 0x81, recv_data, sizeof(recv_data), &cnt, 5000);
+       err = libusb_interrupt_transfer(devh, 0x81, recv_data, sizeof(recv_data), &cnt, USB_TIMEOUT);
        if (err) {
                fprintf(stderr, "Can't receive data: %s\n", usb_strerror(err));
                return 0;
        if (err) {
                fprintf(stderr, "Can't receive data: %s\n", usb_strerror(err));
                return 0;
@@ -303,6 +306,99 @@ int parse_command(char *cmd)
        return command;
 }
 
        return command;
 }
 
+int parse_extension(char *ext)
+{
+       int extension = -1;
+       char *ep;
+
+       if (ext == NULL)
+               return -1;
+
+       if ((ext[strlen(ext)-1] == 's') ||
+           (ext[strlen(ext)-1] == 'S')) {
+               double t;
+               double diff = 1;
+               uint8_t high = 0;
+               uint8_t low = 0;
+               uint8_t i;
+
+               t = strtoul(ext, &ep, 10);
+               if ((*ep != 's') && (*ep != 'S')) {
+                       fprintf(stderr, "Not a valid time in seconds: %s\n", ext);
+                       return -1;
+               }
+
+               /* time = 2^(high nibble) * (low nibble) * 0.25 , high may only be 12 */
+               t /= 0.25;
+#ifdef DEBUG
+               printf("t: %f\n", t);
+#endif
+
+               for(i = 1; i <= 0xf; i++) {
+                       double h;
+                       double l;
+                       uint8_t i_l;
+                       double d;
+
+#ifdef DEBUG
+                       printf("i: 0x%01x\n", i);
+#endif
+
+                       h = t / i;
+#ifdef DEBUG
+                       printf("h: %f\n", h);
+#endif
+
+                       l = log2(h);
+#ifdef DEBUG
+                       printf("l: %f\n", l);
+#endif
+
+                       i_l = l;
+                       if (i_l > 12)
+                               i_l = 12;
+
+                       d = l - i_l;
+
+#ifdef DEBUG
+                       printf("d: %f\n", d);
+#endif
+
+                       if (d < diff) {
+#ifdef DEBUG
+                               printf("Current best match!\n");
+#endif
+                               diff = d;
+                               high = l;
+                               low = i;
+                       }
+               }
+
+#ifdef DEBUG
+               printf("Got: %f, high: %01x, low: %01x\n", t, high, low);
+#endif
+
+               if ((high == 0) && (low == 0)) {
+                       fprintf(stderr, "Can't find extension byte for %s!\n", ext);
+                       return -1;
+               }
+
+               extension = ((high & 0xf) << 4) | (low & 0xf);
+       } else {
+               unsigned long val;
+
+               val = strtoul(ext, &ep, 16);
+               if (*ep != '\0') {
+                       fprintf(stderr, "Not a 1 byte hexstring or time: %s\n", ext);
+                       return -1;
+               }
+
+               extension = val & 0xff;
+       }
+
+       return extension;
+}
+
 void syntax(char *prog)
 {
        fprintf(stderr, "Syntax: %s options\n\n", prog);
 void syntax(char *prog)
 {
        fprintf(stderr, "Syntax: %s options\n\n", prog);
@@ -313,7 +409,7 @@ void syntax(char *prog)
        fprintf(stderr, "The following options need an housecode:\n");
        fprintf(stderr, "\t-a [ELV|hex]\tdestination address\n");
        fprintf(stderr, "\t-s [hex|alias]\tsend command byte\n");
        fprintf(stderr, "The following options need an housecode:\n");
        fprintf(stderr, "\t-a [ELV|hex]\tdestination address\n");
        fprintf(stderr, "\t-s [hex|alias]\tsend command byte\n");
-       fprintf(stderr, "\t-e hex\t\textension byte for command\n");
+       fprintf(stderr, "\t-e [hex|time]\textension byte for command or time in seconds (e.g. 10s)\n");
        fprintf(stderr, "\t-r n\t\trepeat sending n times\n");
        fprintf(stderr, "\nCommand bytes (without extension byte):\n");
        fprintf(stderr, "hex\t\talias\tdescription\n");
        fprintf(stderr, "\t-r n\t\trepeat sending n times\n");
        fprintf(stderr, "\nCommand bytes (without extension byte):\n");
        fprintf(stderr, "hex\t\talias\tdescription\n");
@@ -372,7 +468,7 @@ int main(int argc, char **argv)
        int opt;
        int action = NO_ACTION;
        int command = -1;
        int opt;
        int action = NO_ACTION;
        int command = -1;
-       uint8_t extension = 0x00;
+       int extension = 0;
        uint8_t repeat = 0;
        int addr = -1;
 
        uint8_t repeat = 0;
        int addr = -1;
 
@@ -434,8 +530,8 @@ int main(int argc, char **argv)
 #endif
                                break;
                        case 'e':
 #endif
                                break;
                        case 'e':
-                               extension = strtoul(optarg, &ep, 16);
-                               if (*ep != '\0') {
+                               extension = parse_extension(optarg);
+                               if (extension == -1) {
                                        fprintf(stderr, "Can't parse extension!\n");
                                        exit(EXIT_FAILURE);
                                }
                                        fprintf(stderr, "Can't parse extension!\n");
                                        exit(EXIT_FAILURE);
                                }
@@ -479,15 +575,15 @@ int main(int argc, char **argv)
                                send_data[1] = 0x07;
                                send_data[2] = 0xf2;
                                send_data[8] = repeat;
                                send_data[1] = 0x07;
                                send_data[2] = 0xf2;
                                send_data[8] = repeat;
-                               printf("Sending 0x%02x 0x%02x to address %d (hc: 0x%02x%02x) (repeated %d times)\n",
-                                       command, extension, addr,
+                               printf("Sending 0x%02x 0x%02x (%.2fs) to address %d (hc: 0x%02x%02x) (repeated %d times)\n",
+                                       command, extension, EXT_TO_SEC(extension), addr,
                                        housecode[0], housecode[1],
                                        repeat);
                        } else {
                                send_data[1] = 0x06;
                                send_data[2] = 0xf1;
                                        housecode[0], housecode[1],
                                        repeat);
                        } else {
                                send_data[1] = 0x06;
                                send_data[2] = 0xf1;
-                               printf("Sending 0x%02x 0x%02x to address %d (hc: 0x%02x%02x)\n",
-                                       command, extension, addr,
+                               printf("Sending 0x%02x 0x%02x (%.2fs) to address %d (hc: 0x%02x%02x)\n",
+                                       command, extension, EXT_TO_SEC(extension), addr,
                                        housecode[0], housecode[1]);
                        }
                        send_data[3] = housecode[0];
                                        housecode[0], housecode[1]);
                        }
                        send_data[3] = housecode[0];
Impressum, Datenschutz