diff --git a/lw12ctl.c b/lw12ctl.c index 0da6221..2b64b4b 100644 --- a/lw12ctl.c +++ b/lw12ctl.c @@ -25,10 +25,13 @@ struct sockaddr_in server_addr; static int lw12_cmd_light(int argc, char *argv[]) { if (argc == 0) { printf("Not enough parameters, choices are:\n"); - printf(" on - turn the LED stripe on\n"); - printf(" off - turn the LED stripe off\n"); - printf(" set " - " - set a color value (e.g. 'light set red 244')\n"); + printf(" on\n"); + printf(" off\n"); + printf(" set \n\n"); + printf("Color:\n------\nred\ngreen\nblue\nrgb\n\n" + "The colors red, green and blue require one additional\n" + "parameter as number between 0 - 255. The `rgb` value\n" + "has to be given in #RRGGBB hex format.\n"); } if (strncasecmp(argv[0], "on", strlen(argv[0])) == 0) { lw12_sendcmd(sockfd, &server_addr, (char *)LIGHTS_ON); @@ -37,32 +40,52 @@ static int lw12_cmd_light(int argc, char *argv[]) { lw12_sendcmd(sockfd, &server_addr, (char *)LIGHTS_OFF); } else if (strncasecmp(argv[0], "set", strlen(argv[0])) == 0) { if (argc < 3) { - printf("Not enough parameters, usage:\n"); - printf("set " - " - set a color value (e.g. 'light set red 244')\n"); + printf("Not enough parameters, usage:\n" + " - set \n\b"); return 1; } // todo set color - char cmd[LW12_CMD_LENGTH]; + unsigned char cmd[LW12_CMD_LENGTH]; // last char gets not copied?! - strncpy(cmd, LIGHT_COLOR, LW12_CMD_LENGTH); + strncpy((char *)cmd, LIGHT_COLOR, LW12_CMD_LENGTH); - int pos; - if (strncasecmp(argv[1], "red", strlen(argv[1])) == 0) - pos = 0; - else if (strncasecmp(argv[1], "green", strlen(argv[1])) == 0) - pos = 1; - else if (strncasecmp(argv[1], "blue", strlen(argv[1])) == 0) - pos = 2; - else { + if (strncasecmp(argv[1], "red", strlen(argv[1])) == 0) { + uint8_t colorvalue = (uint8_t)atoi(argv[2]); + cmd[4] = (char)colorvalue; + } else if (strncasecmp(argv[1], "green", strlen(argv[1])) == 0) { + uint8_t colorvalue = (uint8_t)atoi(argv[2]); + cmd[5] = (char)colorvalue; + } else if (strncasecmp(argv[1], "blue", strlen(argv[1])) == 0) { + uint8_t colorvalue = (uint8_t)atoi(argv[2]); + cmd[6] = (char)colorvalue; + } else if (strncasecmp(argv[1], "rgb", strlen(argv[1])) == 0) { + if (strlen(argv[2]) != 7) { + printf("Invalid RGB value. Example: #ff138a\n"); + return 1; + } + char *pos = argv[2]; + if (*pos == '#') + pos++; + int colorvalue = (int)strtol(pos, 0, 16); + // exchange byte order to match RR GG BB + colorvalue = (colorvalue & 0x000000ff) << 16 + | (colorvalue & 0x00ff0000) >> 16 + | (colorvalue & 0x0000ff00); + memcpy(cmd+4, &colorvalue, 4); + } else { printf("Unknown color selected, use: red, green or blue\n"); return 1; } - uint8_t colorvalue = (uint8_t)atoi(argv[2]); - cmd[4 + pos] = (char)colorvalue; // last char gets not copied?! + for (int x = 0; x < 9; x++) + printf("%02x", cmd[x]); + printf("\n"); cmd[LW12_CMD_LENGTH-1] = '\xef'; + + for (int x = 0; x < 9; x++) + printf("%02x", cmd[x]); + printf("\n"); lw12_sendcmd(sockfd, &server_addr, (char *)cmd); } return 0;