コード例 #1
0
ファイル: main.cpp プロジェクト: pSub/usblamp
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;
}
コード例 #2
0
ファイル: main.cpp プロジェクト: bradpe/usblamp
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;
}