int main(int argc, char** argv) { unsigned char maxval = 0x40; // Brightest value unsigned int delay = 250; // Milliseconds if(geteuid() != 0) { std::cout << "Need root access" << std::endl; return 1; } if(argc > 1) { USBLamp lamp = USBLamp(); lamp.open(); if (lamp.isConnected()) { lamp.init(); for (int i=1; i<argc; ++i) { Color color = getColor(argv[i], maxval); lamp.setColor(color.red, color.green, color.blue); if (i+1<argc) { Color nextColor = getColor(argv[i+1], maxval); // Do fading for (int j=0; j<maxval; ++j) { usleep(delay*1000/maxval+1); Color c; c.red = color.red + (nextColor.red-color.red)*j/maxval; c.green = color.green + (nextColor.green-color.green)*j/maxval; c.blue = color.blue + (nextColor.blue-color.blue)*j/maxval; lamp.setColor(c.red, c.green, c.blue); } } } lamp.close(); } else { std::cout << "no lamp found" << std::endl; } } else { std::cout << "Usage: usblamp color [color...]" << std::endl; std::cout << "where colors include:" << std::endl; std::cout << " red" << std::endl; std::cout << " blue" << std::endl; std::cout << " green" << std::endl; std::cout << " white" << std::endl; std::cout << " magenta" << std::endl; std::cout << " cyan" << std::endl; std::cout << " yellow" << std::endl; std::cout << " #rrggbb (hex)" << std::endl; std::cout << " off" << std::endl; std::cout << "Website: https://github.com/daniel-git/usblamp" << std::endl; } return 0; }
int main(int argc, char** argv) { unsigned int delay = 250; // Milliseconds // Check root access if(geteuid() != 0) { std::cout << "Need root access" << std::endl; return 1; } if(argc < 2) { print_help(); return 0; } USBLamp lamp = USBLamp(); lamp.open(); if (lamp.isConnected()) { lamp.init(); bool nextArgDelay = false; for (int i=1; i<argc; ++i) { if (strlen(argv[i]) > 4 && argv[i][0] == '-' && argv[i][1] == 'd') { delay = atoi(&argv[i][2]); if (DEBUG) { std::cout << "Delay: " << delay << "ms" << std::endl; } continue; } if (strlen(argv[i]) == 2 && argv[i][0] == '-' && argv[i][1] == 'd') { nextArgDelay = true; continue; } if (nextArgDelay) { delay = atoi(argv[i]); nextArgDelay = false; if (DEBUG) { std::cout << "Delay: " << delay << "ms" << std::endl; } continue; } Color color = getColor(argv[i], Color::maxval); lamp.setColor(color); if (i+1<argc) { Color nextColor = getColor(argv[i+1], Color::maxval); lamp.fading(delay, nextColor); } } lamp.close(); } else { std::cout << "no lamp found" << std::endl; } return 0; }
void listen(USBLamp lamp, int port) { struct sockaddr_in server; struct sockaddr_in from; unsigned char buf[1024]; int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { std::cout << "Can't open socket " << std::endl; exit(1); } socklen_t length = sizeof(server); memset(&server, 0, (int)length); server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(port); if (bind(sock, (struct sockaddr *)&server, length) < 0) { std::cout << "Can't bind to port " << port << std::endl; exit(1); } int n; int maxval = Color::maxval; do { n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&from, &length); if (n > 2) { int red = std::max(0, std::min(255, buf[0]*maxval/255)); int green = std::max(0, std::min(255, buf[1]*maxval/255)); int blue = std::max(0, std::min(255, buf[2]*maxval/255)); Color color = Color(red, green, blue); Color lastcolor = lamp.getColor(); lamp.setColor(color); buf[0] = lastcolor.red*255/maxval; buf[1] = lastcolor.green*255/maxval; buf[2] = lastcolor.blue*255/maxval; n = sendto(sock, buf, 3, 0, (struct sockaddr *)&from, length); } } while (n >= 0); }
int main(int argc, char** argv) { unsigned int delay = 250; // Milliseconds unsigned int port = 0; // UDP datagram port unsigned short repeat = 0; unsigned int repeat_wait = 0; char opt_help = 0; char opt_switch = 0; int opt; // Check root access if (geteuid() != 0) { std::cout << "Need root access" << std::endl; return 1; } while ((opt = getopt(argc, argv, "hsr:d:p:")) != -1) { // for each option... char *p1; switch ( opt ) { case 'h': opt_help++; break; case 's': opt_switch++; break; case 'r': repeat = atoi(optarg); if ((p1 = strchr(optarg,','))) { repeat_wait = atoi(p1+1); } break; case 'd': delay = atoi(optarg); if (DEBUG) { std::cout << "Delay: " << delay << "ms" << std::endl; } break; case 'p': port = atoi(optarg); if (DEBUG) { std::cout << "Listening on UDP port " << port << std::endl; } break; case '?': // unknown option... opt_help++; break; } } repeat++; optind--; argc -= optind; argv += optind; if ((!port && argc < 2) || opt_help) { print_help(); return 0; } USBLamp lamp = USBLamp(); lamp.open(); if (lamp.isConnected()) { unsigned int wait = delay; char first = 1; lamp.init(); lamp.switchOff(); while (repeat--) { for (int i=1; i<argc; i++) { char *p1 = argv[i]; Color color = getColor(p1, Color::maxval); if (first) { lamp.setColor(color); first = 0; } else { if (opt_switch) { usleep(wait*1000); lamp.setColor(color); } else { lamp.fading(wait,color); } } if ((p1 = strchr(p1,','))) wait = atoi(p1+1); else wait = delay; } if (repeat && repeat_wait) usleep(repeat_wait * 1000); } if (port) listen(lamp, port); lamp.close(); } else { std::cout << "no lamp found" << std::endl; } return 0; }