From: Michael Gernoth Date: Sun, 19 May 2013 09:19:14 +0000 (+0000) Subject: add humamn readable command-aliases X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/fs20pcs/commitdiff_plain/4636e355dcb8fae161dcc26341638eebae1ad8f0?ds=sidebyside add humamn readable command-aliases --- diff --git a/fs20pcs.c b/fs20pcs.c index 10f9718..d74d5b0 100644 --- a/fs20pcs.c +++ b/fs20pcs.c @@ -269,6 +269,40 @@ int parse_addr(char *ad) return addr; } +int parse_command(char *cmd) +{ + int command = -1; + char *ep; + unsigned long val; + + if (cmd == NULL) + return -1; + + if (!strcasecmp(cmd, "off")) { + command = 0x00; + } else if (!strcasecmp(cmd, "on")) { + command = 0x11; + } else if (!strcasecmp(cmd, "toggle")) { + command = 0x12; + } else if (!strcasecmp(cmd, "dimup")) { + command = 0x13; + } else if (!strcasecmp(cmd, "dimdown")) { + command = 0x14; + } else if (!strcasecmp(cmd, "on-for-timer")) { + command = 0x39; + } else { + val = strtoul(cmd, &ep, 16); + if (*ep != '\0') { + fprintf(stderr, "Not a 1 byte hexstring or alias: %s\n", cmd); + return -1; + } + + command = val & 0xff; + } + + return command; +} + void syntax(char *prog) { fprintf(stderr, "Syntax: %s options\n\n", prog); @@ -277,42 +311,44 @@ void syntax(char *prog) fprintf(stderr, "\t-x\t\tabort sending long press\n"); fprintf(stderr, "\t-h [ELV|hex]\thousecode in ELV- or hex-notation\n"); fprintf(stderr, "The following options need an housecode:\n"); - fprintf(stderr, "\t-a [ELV|hex]\tsend commands to device at specified address\n"); - fprintf(stderr, "\t-s hex\t\tsend command byte\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-r n\t\trepeat sending n times\n"); fprintf(stderr, "\nCommand bytes (without extension byte):\n"); - fprintf(stderr, "0x00\t\tswitch off\n"); - fprintf(stderr, "0x01-0x10\tswitch on dimmed (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%)\n"); - fprintf(stderr, "0x11\t\tswitch on\n"); - fprintf(stderr, "0x12\t\ttoggle\n"); - fprintf(stderr, "0x13\t\tdimup\n"); - fprintf(stderr, "0x14\t\tdimdown\n"); - fprintf(stderr, "0x15\t\tdimup, pause, dimdown, pause, ...\n"); - fprintf(stderr, "0x16\t\tstart/stop programming internal timer\n"); - fprintf(stderr, "0x17\t\tlearn housecode/address\n"); - fprintf(stderr, "0x18\t\toff for internal timer, then back on with current level\n"); - fprintf(stderr, "0x19\t\ton (100%%) for internal timer, then off\n"); - fprintf(stderr, "0x1a\t\ton (old level) for internal timer, then off\n"); - fprintf(stderr, "0x1b\t\tfactory reset\n"); - fprintf(stderr, "0x1e\t\ton (100%%) for internal timer, then old level\n"); - fprintf(stderr, "0x1f\t\ton (old level) for internal timer, then old state\n"); + fprintf(stderr, "hex\t\talias\tdescription\n"); + fprintf(stderr, "0x00\t\toff\tswitch off\n"); + fprintf(stderr, "0x01-0x10\t\tswitch on dimmed (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%)\n"); + fprintf(stderr, "0x11\t\ton\tswitch on\n"); + fprintf(stderr, "0x12\t\ttoggle\ttoggle\n"); + fprintf(stderr, "0x13\t\tdimup\tdimup\n"); + fprintf(stderr, "0x14\t\tdimdown\tdimdown\n"); + fprintf(stderr, "0x15\t\t\tdimup, pause, dimdown, pause, ...\n"); + fprintf(stderr, "0x16\t\t\tstart/stop programming internal timer\n"); + fprintf(stderr, "0x17\t\t\tlearn housecode/address\n"); + fprintf(stderr, "0x18\t\t\toff for internal timer, then back on with current level\n"); + fprintf(stderr, "0x19\t\t\ton (100%%) for internal timer, then off\n"); + fprintf(stderr, "0x1a\t\t\ton (old level) for internal timer, then off\n"); + fprintf(stderr, "0x1b\t\t\tfactory reset\n"); + fprintf(stderr, "0x1e\t\t\ton (100%%) for internal timer, then old level\n"); + fprintf(stderr, "0x1f\t\t\ton (old level) for internal timer, then old state\n"); fprintf(stderr, "\nCommand bytes (with timer as extension byte):\n"); - fprintf(stderr, "0x20\t\tdim down to off while timer is running\n"); - fprintf(stderr, "0x21-0x30\tdim to (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%) while timer is running\n"); - fprintf(stderr, "0x31\t\tdim to last level while timer is running\n"); - fprintf(stderr, "0x32\t\tdim to off, then after timer dim to off\n"); - fprintf(stderr, "0x33\t\tdimup now and switch off after timer\n"); - fprintf(stderr, "0x34\t\tdimdown now and switch off after timer\n"); - fprintf(stderr, "0x35\t\tdimup or dimdown (toggle) and switch off after timer\n"); - fprintf(stderr, "0x36\t\tprogram internal timer\n"); - fprintf(stderr, "0x38\t\toff for timer, then back on with current level\n"); - fprintf(stderr, "0x39\t\ton (100%%) for timer, then off\n"); - fprintf(stderr, "0x3a\t\ton (old level) for timer, then off\n"); - fprintf(stderr, "0x3c\t\tprogram internal ramp-up-time\n"); - fprintf(stderr, "0x3d\t\tprogram internal ramp-down-time\n"); - fprintf(stderr, "0x3e\t\ton (100%%) for timer, then old level\n"); - fprintf(stderr, "0x3f\t\ton (old level) for timer, then old state\n"); + fprintf(stderr, "hex\t\talias\tdescription\n"); + fprintf(stderr, "0x20\t\t\tdim down to off while timer is running\n"); + fprintf(stderr, "0x21-0x30\t\tdim to (0x01: 6.25%%, 0x02: 12.5%%, ..., 0x10: 100%%) while timer is running\n"); + fprintf(stderr, "0x31\t\t\tdim to last level while timer is running\n"); + fprintf(stderr, "0x32\t\t\tdim to off, then after timer dim to off\n"); + fprintf(stderr, "0x33\t\t\tdimup now and switch off after timer\n"); + fprintf(stderr, "0x34\t\t\tdimdown now and switch off after timer\n"); + fprintf(stderr, "0x35\t\t\tdimup or dimdown (toggle) and switch off after timer\n"); + fprintf(stderr, "0x36\t\t\tprogram internal timer\n"); + fprintf(stderr, "0x38\t\t\toff for timer, then back on with current level\n"); + fprintf(stderr, "0x39\ton-for-timer\ton (100%%) for timer, then off\n"); + fprintf(stderr, "0x3a\t\t\ton (old level) for timer, then off\n"); + fprintf(stderr, "0x3c\t\t\tprogram internal ramp-up-time\n"); + fprintf(stderr, "0x3d\t\t\tprogram internal ramp-down-time\n"); + fprintf(stderr, "0x3e\t\t\ton (100%%) for timer, then old level\n"); + fprintf(stderr, "0x3f\t\t\ton (old level) for timer, then old state\n"); }; enum { @@ -335,7 +371,7 @@ int main(int argc, char **argv) int err; int opt; int action = NO_ACTION; - uint8_t command = 0x00; + int command = -1; uint8_t extension = 0x00; uint8_t repeat = 0; int addr = -1; @@ -358,8 +394,8 @@ int main(int argc, char **argv) DUPLICATE_ACTION; action = SEND_COMMAND; - command = strtoul(optarg, &ep, 16); - if (*ep != '\0') { + command = parse_command(optarg); + if (command == -1) { fprintf(stderr, "Can't parse command!\n"); exit(EXIT_FAILURE); }