예제 #1
0
int main(int argc, char *argv[]) {
    
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
//    int PIN = 121;
    char*  systemCode = argv[1];
    int unitCode = atoi(argv[2]);
    int command  = atoi(argv[3]);
    int timer = atoi(argv[4]);
    int pin = atoi(argv[5]);
    int repeat = atoi(argv[6]);
//    if (wiringPiSetup () == -1) return 1;
	printf("sending systemCode[%s] unitCode[%i] command[%i] timer[%i] pin[%i] repeat[%i]\n", systemCode, unitCode, command, timer, pin, repeat);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(pin);
        mySwitch.setPulseLength(timer);
        mySwitch.setRepeatTransmit(repeat);
    switch(command) {
        case 1:
            mySwitch.switchOn(systemCode, unitCode);
            break;
        case 0:
            mySwitch.switchOff(systemCode,unitCode);
            break;
        default:
            printf("command[%i] is unsupported\n", command);
            return -1;
    }
	return 0;
}
예제 #2
0
int main(int argc, char *argv[]) {
    
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 17;
    int outlet = atoi(argv[1]);
    int command  = atoi(argv[2]);
    
    if (wiringPiSetupSys () == -1) return 1;
    RCSwitch mySwitch = RCSwitch();
    mySwitch.enableTransmit(PIN);
   
    

    switch(command) {
    case 1:
      mySwitch.switchOn(outlet, 4);
      break;
    case 0:
      mySwitch.switchOff(outlet, 4);
      break;
    default:
      printf("command[%i] is unsupported\n", command);
      return -1;
    }
    return 0;
}
예제 #3
0
파일: send.cpp 프로젝트: vchlum/433Utils
int main(int argc, char *argv[]) {
    
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 0;
    char* systemCode = argv[1];
    int unitCode = atoi(argv[2]);
    int command  = atoi(argv[3]);
    
    if (wiringPiSetup () == -1) return 1;
	printf("sending systemCode[%s] unitCode[%i] command[%i]\n", systemCode, unitCode, command);
	RCSwitch mySwitch = RCSwitch();
	if (argv[4] != NULL) mySwitch.setPulseLength(atoi(argv[4]));
	mySwitch.setProtocol(2);
	mySwitch.enableTransmit(PIN);
    
    switch(command) {
        case 1:
            mySwitch.switchOn(systemCode, unitCode);
            break;
        case 0:
            mySwitch.switchOff(systemCode, unitCode);
            break;
        default:
            printf("command[%i] is unsupported\n", command);
            return -1;
    }
	return 0;
}
예제 #4
0
int main(int argc, char *argv[]) {
    
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 0;
    char* sGroup = argv[1];
    int nDevice = atoi(argv[2]);
    int command  = atoi(argv[3]);
    
    if (wiringPiSetup () == -1) return 1;
	printf("sending sGroup[%s] nDevice[%i] command[%i]\n", sGroup, nDevice, command);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(PIN);
    
    switch(command) {
        case 1:
            mySwitch.switchOn(sGroup[0], nDevice);
            break;
        case 0:
            mySwitch.switchOff(sGroup[0], nDevice);
            break;
        default:
            printf("command[%i] is unsupported\n", command);
            return -1;
    }
	return 0;
}
JNIEXPORT void JNICALL Java_com_opitz_jni_NativeRCSwitchAdapter_switchOff(JNIEnv * env, jobject obj, jstring jsGroup, jstring jsChannel ){
    if (wiringPiSetup () == -1) {
            printf("noWiringPiSetup");
    }

    const char *csGroup = env->GetStringUTFChars(jsGroup, 0);
    const char *csChannel = env->GetStringUTFChars(jsChannel, 0);

    char sGroup[6];
    char sChannel[6];

    for (int i = 0; i<5; i++) {
        sGroup[i] = csGroup[i];
        sChannel[i] = csChannel[i];
    }
    sGroup[5] = '\0';
    sChannel[5] = '\0';

    cout<<"OFF with "<<sGroup<<" and "<< sChannel <<endl;


    piHiPri(20);


    //for testing purposes set to the ELRO Power Plugs
    mySwitch.setPulseLength(300);
    mySwitch.enableTransmit(0);
    mySwitch.setRepeatTransmit(3);

    mySwitch.switchOff(sGroup, sChannel);

}
예제 #6
0
/**
 * @brief arduino setup function
 */
void setup() {
  Serial.begin(9600);

  Display::Init();

  if (RTC.get() == 0) {
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.set(hhmmss());
  }

  setSyncProvider(RTC.get); // the function to get the time from the RTC

  // Transmitter is connected to Arduino Pin #10
  mySwitch.enableTransmit(7);

  dallastemp.begin(); // Inizialisieren der Dallas Temperature library
  dallastemp.setWaitForConversion(false);

  aqua.begin();

  pinMode(BUTTON_PIN_1, INPUT_PULLUP);
  pinMode(BUTTON_PIN_2, INPUT_PULLUP);
//  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
//  pinMode(BUTTON_PIN_4, INPUT_PULLUP);
}
예제 #7
0
int main(int argc, char *argv[]) {
    
    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
    int PIN = 0;
    
    // Parse the firt parameter to this command as an integer
    int code = atoi(argv[1]);
    
    if (wiringPiSetup () == -1) return 1;
	printf("sending code[%i]\n", code);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(PIN);

    // Optional set pulse length.
       mySwitch.setPulseLength(500);
  
    // Optional set protocol (default is 1, will work for most outlets)
    // mySwitch.setProtocol(2);
  
    // Optional set number of transmission repetitions.
    // mySwitch.setRepeatTransmit(15);
  
    
    mySwitch.send(code, 24);
    
	return 0;

}
예제 #8
0
int main(int argc, char* argv[]) {
    
  Cgicc formData;
  /*
  output rcDataPin is hardcoded for testing purposes
  see https://projects.drogon.net/raspberry-pi/wiringpi/rcDataPins/
  for rcDataPin maprcDataPing of the raspberry pi GPIO connector
  */
  int rcDataPin = 0;
  int ledPin = 1;
    
    

   

   // form_iterator fi = formData.getElement("device");  
   string foo = formData("cmd");
   // if( !fi->isEmpty() && fi != (*formData).end()) {  
      // cout << "selected device: " << fi->getValue() << endl;  
   // }else{
   //    cout << "No text entered for first name" << endl;  
   // }



  int mode = atoi(formData("cmd").c_str());
  int groupId = atoi(formData("group").c_str());
  int DeviceId  = atoi(formData("device").c_str());

   // int mode = 1;
   // int groupId = 1;
   // int DeviceId = 1;

    
  if (wiringPiSetup() == -1) return -1;
    
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(rcDataPin);

  pinMode(ledPin, OUTPUT);
    
  switch(mode) {
    case 1:
      digitalWrite(ledPin, HIGH);
      mySwitch.switchOn('a',groupId, DeviceId);
      break;
    case 0:
      digitalWrite(ledPin, LOW);
      mySwitch.switchOff('a',groupId, DeviceId);
      break;
    default:
      cout << "unsuported mode[" << mode << "]" << endl;
      return -1;
    }

  cout << "Location:http://raspberrypi.local/\n\n";

	return 0;
}
예제 #9
0
int main(int argc, char *argv[]) {

  /*
   * output PIN is hardcoded for testing purposes
   * see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
   * for pin mapping of the raspberry pi GPIO connector
   */
  int PIN = 0;
  string unitcode = argv[1];
  string state  = argv[2];

  if (wiringPiSetup () == -1) return 1;
  piHiPri(20);
  /* printf("sending unitcode[%i] state[%i]\n", unitcode, state); */
  RCSwitch mySwitch = RCSwitch();
  mySwitch.setPulseLength(300);
  mySwitch.enableTransmit(PIN);
  
  std::cout<< "unitcode: " << unitcode << std::endl;
  std::cout<< "Laenge: " << unitcode.length() << std::endl;

  string::size_type pos = 0;
  
while ((pos = unitcode.find("1", pos)) != string::npos) {
   string s2 = "x";
  unitcode.replace(pos, 1, s2, 0, s2.length());
}
std::cout<< "spos  " << pos << std::endl;
std::cout<< "find  " << unitcode.find("0", pos) << std::endl;
pos=0;
while ((pos = unitcode.find("0", pos)) != string::npos) {
   string s1 = "1";
  unitcode.replace(pos, 1, s1, 0, s1.length());
}
pos=0;
while ((pos = unitcode.find("x", pos)) != string::npos) {
   string s2 = "0";
  unitcode.replace(pos, 1, s2, 0, s2.length());
}  
  string execute = unitcode + state;
  
  std::cout<< "seeeeeeeeeeeeeeeeeeeeeeeeeeeeeee: " << execute << std::endl;
  string exe = execute;
  for(int i=0;i < 24;i+=2){
	
	execute = exe.insert(i, 1, '0');
  }

  char * buffer = new char[execute.length()];
  char *kacka=strcpy(buffer,execute.c_str());
    
  //char *array="000000000001000000000000";
  mySwitch.send(kacka);  
				
  std::cout<< "state: " << state << std::endl;
  std::cout<< "execute: " << execute << std::endl;
  return 0;
}
예제 #10
0
int main()
{
 int code=5,i=0;
 double duration,duration1,pos_arr[3][2], tmp1=0, tmp2=0, tmp3=0; 
 struct timeval start,end,start1;
 RCSwitch mySwitch = RCSwitch();
 mySwitch.enableTransmit(PIN);
 receiver receiver1(1),receiver2(2),receiver3(3);
 receiver1.init();
 receiver2.init();
 receiver3.init();
 ofstream data;
 data.open ("data.txt");
 printf("activating emitter\n");
 mySwitch.send(code,24);
// delay(3);
 while (true)
 {
 gettimeofday(&start1,NULL);
 std::thread t1(&receiver::dst,&receiver1);
 std::thread t2(&receiver::dst,&receiver2);
 std::thread t3(&receiver::dst,&receiver3);
 t1.join();
 t2.join();
 t3.join();
// std::cout<<receiver1.distance<<"  "<<receiver2.distance<<"  "<<receiver3.distance<<"  "<<i<<std::endl;
 pos_arr[0][0]=tmp1;
 pos_arr[1][0]=tmp2;
 pos_arr[2][0]=tmp3;
 pos_arr[0][1]=receiver1.distance;
 pos_arr[1][1]=receiver2.distance;
 pos_arr[2][1]=receiver3.distance;
 tmp1= pos_arr[0][1];
 tmp2= pos_arr[1][1];
 tmp3= pos_arr[2][1];
 gettimeofday(&start,NULL);
 kalman(pos_arr);
 gettimeofday(&end,NULL);
 duration1=(abs(end.tv_usec-start1.tv_usec)/1000.0);
 duration=(abs(end.tv_usec-start.tv_usec)/1000.0);

//std::cout<<"receiver1"<<receiver1.distance <<"receiver2"<<receiver2.distance<< "receiver3"<<receiver3.distance  <<      std::endl;

 std::cout<<"time for Kalman / calculations "<<duration<< "  " << duration1<< std::endl;
 delay(50-duration1);

// gettimeofday(&end1,NULL);
// duration1=((dend1.tv_usec-start1.tv_usec)*1000+(end1.tv_sec-start1.tv_sec)/1000)+0.5;
// std::cout<<"time for Kalman "<<duration<<std::endl;
// delay(50-duration1/1000000);
 i++;
 }
data.close();
return 0;
}
예제 #11
0
파일: rc.cpp 프로젝트: ZigFisher/Glutinium
RCSwitch initTx(int pin, int protocol, int repetition, int delay)
{
    pinMode(pin, OUTPUT);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.setRepeatTransmit(repetition);
	mySwitch.enableTransmit(pin);
    mySwitch.setProtocol(protocol);
	if (delay > 0) {
		mySwitch.setPulseLength(delay);
	}
	return mySwitch;
}
int main(int argc, char *argv[]) {
    
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */



    int PIN = 0;
    char* systemCode = argv[1];
    int unitCode = atoi(argv[2]);
    int command  = atoi(argv[3]);

     /*
     Added the possibility to send binaries, instead of switchOn/switchOff
     */
     int code = atoi(argv[1]);
     int pulse = atoi(argv[2]);
     int bit = atoi(argv[3]);

    
    if (wiringPiSetup () == -1) return 1;
	printf("sending systemCode[%s] unitCode[%i] command[%i]\n", systemCode, unitCode, command);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(PIN);
    
    switch(command) {
        case 1:
            mySwitch.switchOn(systemCode, unitCode);
            break;
        case 0:
            mySwitch.switchOff(systemCode, unitCode);
            break;
	case 2:
             mySwitch.setPulseLength(pulse);
             mySwitch.send(systemCode);
            break;
	default:
             mySwitch.setPulseLength(pulse);
             mySwitch.send(code, bit);
            break;

    }
	return 0;
}
예제 #13
0
int main()
{
 int code=5,i=0;
 timespec tsleep{0};
 long long duration2=0,duration1=0;
 RCSwitch mySwitch = RCSwitch();
 mySwitch.enableTransmit(PIN);
 receiver receiver1(1),receiver2(2),receiver3(3);
 receiver1.init();
 receiver2.init();
 receiver3.init();
 printf("activating emitter\n");
 mySwitch.send(code,24);
 std::ofstream data;
 data.open ("data.txt");
// delay(3);

 while (true)
 {
 auto begin = std::chrono::high_resolution_clock::now();
////////////////////////////////////////////////////////////////////////////////////
// mySwitch.send(code,24);
 receiver1.dst();
// receiver2.dst();
// receiver3.dst();
 data<<receiver1.distance<<"  "<<receiver2.distance<<"  "<<receiver3.distance<<std::endl;
////////////////////////////////////////////////////////////////////////////////
 auto end = std::chrono::high_resolution_clock::now(); 

// data<<end.tv_usec<<"\n";
 duration2 = std::chrono::duration_cast <std::chrono::nanoseconds>(end-begin).count();
 tsleep.tv_nsec=50000000-duration2;
 nanosleep(&tsleep,NULL);
// i++;
// if (i==20)
// {
// mySwitch.send(code,24);
// i=0;
// }
// auto end2 = std::chrono::high_resolution_clock::now();
// duration1 = std::chrono::duration_cast <std::chrono::nanoseconds>(end2-begin).count();
// data<<duration1<<"\n";
 }
return 0;
}
예제 #14
0
int main(int argc, char *argv[]) {
    
    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
    int PIN = 29;
    
    // Parse the firt parameter to this command as an integer
    int code = atoi(argv[1]);
    if (wiringPiSetup () == -1) return 1;
	printf("sending code[%i]\n", code);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(PIN);
    
    mySwitch.send(code, 24);
    
	return 0;
}
int main(int argc, char *argv[]) {
    
    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 0;
    char* tristate = argv[1];
    
    if (wiringPiSetup () == -1) return 1;
	printf("sending tristate[%s] \n", tristate);
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(PIN);
    mySwitch.setPulseLength(360);

    mySwitch.sendTriState(tristate);

	return 0;
}
int main(int argc, char *argv[]) {

    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
    int PIN = 0;

    // Parse the firt parameter to this command as an integer
    int code = atoi(argv[1]);

    if (wiringPiSetup () == -1) return 1;
	printf("sending code[%i]\n", code);
	RCSwitch mySwitch = RCSwitch();
      //Pulse length depends on the RF outlets you are using. Use RFSniffer to see what pulse length your device uses.
	mySwitch.setPulseLength(185);
	mySwitch.enableTransmit(PIN);
    	mySwitch.send(code, 24);
	return 0;

     }
예제 #17
0
int main(int argc, char *argv[]) {

    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */

    int PIN = 0;
    unsigned long RCcode = atol(argv[1]);
    unsigned int RClength = atoi(argv[2]);

    if (wiringPiSetup () == -1) return 1;
    printf("Sending RCcode[%lu] RClength[%i]\n", RCcode, RClength);
    RCSwitch mySwitch = RCSwitch();
    mySwitch.enableTransmit(PIN);
    mySwitch.send(RCcode, RClength);

    return 0;
}
예제 #18
0
int main(int argc, char *argv[]) {
    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
    int PIN = 0;

    // Parse the first tow parameters to this command as integers
    int count = argc - 1;
    int timings[count];
    for(int i = 0; i < count; i++) {
        timings[i] = atoi(argv[i + 1]);
    }

    if (wiringPiSetup () == -1) return 1;

    RCSwitch mySwitch = RCSwitch();
    mySwitch.enableTransmit(PIN);

    mySwitch.sendRaw(count, timings);

    return 0;
}
예제 #19
0
int main(int argc, char *argv[])
{
	//!!!!!!!!!!!!!!! Remove comments below for easy debugging (override command line parameters)  !!!!!!!!
	//argv = dummy_args;
	//argc = sizeof(dummy_args) / sizeof(dummy_args[0]) - 1;
	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	
	if (wiringPiSetup() == -1) {
		cout << "Could not set up wiringPi\r\n" << endl;
	}
	
	unsigned long code = 0;
	uint  repeats = 0;
	RCSwitch rc = RCSwitch(); 
	
	if (argc > 1) { code = strtol(argv[1], NULL, 16); }
	if (argc == 3) { repeats = atoi(argv[2]); }

	

	if (argc == 3)
	{
		pinMode(RF_TX_POWER_PIN, OUTPUT);      // Set the Transmitter power pin to output mode
		digitalWrite(RF_TX_POWER_PIN, HIGH);   // Turn on power to the RF Transmitter 
		rc.setRepeatTransmit(repeats);         // Set the number of repeats
		rc.enableTransmit(RF_TX_DATA_PIN);     // Set Transmitter to the correct IO pin
		rc.send(code, 24);                     // Send the actual code
		digitalWrite(RF_TX_POWER_PIN, LOW);    // Turn off power to the RF Transmitter 
		cout << "sent " << std::hex << code << endl; 
	}	
	else {
		cout << "Usage   : transmit <code> <repeats>" << endl;
		cout << "          Where is <code> is the code to send" << endl; 
		cout << "          Where is <repeats> is how many times the code is repeated" << endl; 
		cout << "Example : transmit abcdef 10" << endl;
	}
	
	return 0;
}
예제 #20
0
int main(int argc, char *argv[]) {
    
    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
    int PIN = 0;
    
    // Parse the first parameter to this command as an integer
    int protocol = 0; // A value of 0 will use rc-switch's default value
    int pulseLength = 0;

    // If no command line argument is given, print the help text
    if (argc == 1) {
        printf("Usage: %s decimalcode [protocol] [pulselength]\n", argv[0]);
        printf("decimalcode\t- As decoded by RFSniffer\n");
        printf("protocol\t- According to rc-switch definitions\n");
        printf("pulselength\t- pulselength in microseconds\n");
        return -1;
    }

    // Change protocol and pulse length accroding to parameters
    int code = atoi(argv[1]);
    if (argc >= 3) protocol = atoi(argv[2]);
    if (argc >= 4) pulseLength = atoi(argv[3]);
    
    if (wiringPiSetup () == -1) return 1;
    printf("sending code[%i]\n", code);
    RCSwitch mySwitch = RCSwitch();
    if (protocol != 0) mySwitch.setProtocol(protocol);
    if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
    mySwitch.enableTransmit(PIN);
    
    mySwitch.send(code, 24);
    
    return 0;

}
예제 #21
0
int main(int argc, char *argv[]) {
  setenv("WIRINGPI_GPIOMEM", "1", 0);

  if (wiringPiSetup () == -1) return EXIT_FAILURE;
  piHiPri(99);

  RCSwitch rc = RCSwitch();
  rc.enableTransmit(PIN);

  int opt;
  while ((opt = getopt(argc,argv,"p:")) != EOF)
    switch(opt) {
      case 'p': rc.setPulseLength(atoi(optarg)); break;
      case '?': usage(); return EXIT_FAILURE;
      default: abort();
    }

  if(argc - optind == 3) { // There should be 3 arguments left after getopt
    char* systemcode = argv[optind];
    char* deviceStr  = argv[optind+1];
    char* command    = argv[optind+2];
    int device;
    if( isSwitches(systemcode,5) &&
        isSwitches(command,   1) &&
        (device = getDevice(deviceStr)) != -1) {
      switch(atoi(command)) {
        case 0: rc.switchOff(systemcode, device); return EXIT_SUCCESS;
        case 1: rc.switchOn(systemcode, device);  return EXIT_SUCCESS;
        default: break; // Shouldn't get here as command was validated with isSwitches
      }
    }
  }

  usage(); // Failed somewhere above. Print out the usage
  return EXIT_FAILURE;
}
예제 #22
0
파일: sendCS.cpp 프로젝트: GSmurf/tools
int main (int argc, char** argv)
{
	if (setuid(0))
	{
		perror("setuid");
		return 1;
	}

	piHiPri(99); // Put process in real-time mode

	log("Demarrage du programme");
	pin = atoi(argv[1]);
	sender = atoi(argv[2]);
	if (strcmp(argv[3],"portal")==0) {
		interruptor = -1;
	} else {
		if (argv[3][0]=='a')
			group=42;
		else if (argv[3][0]=='b')
			group=138;
		else if (argv[3][0]=='c')
			group=162;
		else if (argv[3][0]=='d')
			group=168;
		else {
			group=-1;
			interruptor = atoi(argv[3]);
		}
		if (group != -1) {
			if (argv[3][1]=='1')
				interruptor=10;
			else if (argv[3][1]=='2')
				interruptor=34;
			else if (argv[3][1]=='3')
				interruptor=40;
		}
	}
	onoff = argv[4];
	pulse =(argc==6)? atoi(argv[5]) : 0;

	//Si on ne trouve pas la librairie wiringPI, on arrête l'execution
    if(wiringPiSetup() == -1)
    {
        log("Librairie Wiring PI introuvable, veuillez lier cette librairie...");
        return -1;
    }
	
	if (interruptor == -1) {
		log("Lancement en mode RCSwitch ...");
		RCSwitch mySwitch = RCSwitch();
		mySwitch.enableTransmit(pin);
		mySwitch.setProtocol(5);
		mySwitch.setRepeatTransmit(10);
		mySwitch.send(const_cast<char*>(onoff.c_str()));
		if (pulse > 0) {
			delay(pulse);
			mySwitch.send(const_cast<char*>(onoff.c_str()));
		}
	} else if (group != -1) {
		log("Lancement en mode Casto ...");
                RCSwitch mySwitch = RCSwitch();
                mySwitch.enableTransmit(pin);
                mySwitch.setProtocol(1);
                mySwitch.setRepeatTransmit(10);
		group = group << 15;
		interruptor = interruptor << 9;
		int separator = 42;
		separator = separator << 3;
		long localCode = group + interruptor + separator;
		if(onoff=="on"){
			localCode += 7;
		} else if (onoff=="off") {
			localCode += 4;
		} else
			log("Mode not implemented.");
                mySwitch.send(localCode,24);
	} else {
		log("Lancement en mode DIO ...");
		pinMode(pin, OUTPUT);
		log("Pin GPIO configure en sortie");
		itob(sender,26);
		itobInterruptor(interruptor,4);
		if(onoff=="on"){
			action(true);
		} else if (onoff=="off"){
			action(false);	 
		} else {
			action(true);
			delay(pulse);
			action(false);
		}
	}
	log("Fin du programme");    // execution terminée.

}
예제 #23
0
int main(int argc, char *argv[]) {
  
     // This pin is not the first pin on the RPi GPIO header!
     // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     // for more information.
     int PIN = 2;
     int OPENDOOR = 3;
     int CLOSEDOOR = 4;
     time_t now1,now2;
     bool flag=false;
     
     if(wiringPiSetup() == -1) {
       printf("wiringPiSetup failed, exiting...");
       return 0;
     }

     int pulseLength = 0;
     if (argv[1] != NULL) pulseLength = atoi(argv[1]);

     mySwitch = RCSwitch();
     ledSwitch = RCSwitch();
     closeDoorLed = RCSwitch();
	 if (pulseLength != 0) mySwitch.setPulseLength(pulseLength);
     mySwitch.enableReceive(PIN);  // Receiver on interrupt 0 => that is pin #2
     ledSwitch.enableTransmit(OPENDOOR);
     closeDoorLed.enableTransmit(CLOSEDOOR);
    
     while(1) {
  
      if (mySwitch.available()) {
    
        int value = mySwitch.getReceivedValue();
    
        if (value == 0) {
          printf("Unknown encoding\n");
        } else {    
          if(value==5396){
             if(!flag){
                time(&now1); 
	     }
             flag = true;
             printf("Door is Open\n");
             ledSwitch.send(15,24);
             
          } else {
              if(flag){
                 time(&now2);
                 flag = false;
                 printf("Door was open for: %.f seconds\n",difftime(now2,now1));
              }
              printf("Door is Closed\n");
              closeDoorLed.send(15,24);
          }
        }
        mySwitch.resetAvailable();
    
      }
      
  
  }

  exit(0);


}
/* ---------------------------------------------------------------- */
int main(int argc, char *argv[]) 
{  
    parseCommandLineArgs(argc, argv);
	
	ifstream jsonFile;
	string rfJsonFilePath = "./radioFrequenceSignalConfig.json";

	int repeat				= 10;	
	int protocol			= 1;	
	int pin					= 0;
	double pulseLength 		= 0;
	long signalBitNumber	= 0;

   	int decimalcode			= 0;
   	int nbbit				= 24;
   	
   	string remote	 		= "";
   	bool isManchesterCoded	= false;
   	string binarycode		= "";	
    	
	std::map<std::string,std::string>::iterator it;

	it = commandArgs.find("lev");
	if( commandArgs.find("lev") != commandArgs.end()) loglevel=atoi(it->second.c_str());

	if( commandArgs.find("listremotes") == commandArgs.end() && commandArgs.find("getremotebuttons") == commandArgs.end() )
		displayCommandLineArgs();
	
	it = commandArgs.find("jsonconf");
	if(it != commandArgs.end())		rfJsonFilePath=commandArgs["jsonconf"].c_str();
	// chargement de la conf (fichier json contenant toutes les infos des télécommandes radiofréquence)
	jsonFile.open(rfJsonFilePath.c_str());		
	bool parsedResult = jsonReader.parse(jsonFile, jsonRoot, false);
	jsonFile.close();
	if(!parsedResult) // Report failures and their locations in the document.
	{
		string str = "Echec d'analyse du fichier " + rfJsonFilePath + " : \n" + jsonReader.getFormatedErrorMessages() + "\n";
		trace(3,str.c_str());		
		return 1;
	}

	it = commandArgs.find("listremotes");
	if( commandArgs.find("listremotes") != commandArgs.end()) {diplayRemotes(); return 0;}

	it = commandArgs.find("repeat");
	if(it != commandArgs.end())		repeat=atoi(commandArgs["repeat"].c_str());
	
	it = commandArgs.find("remote");
	if(it != commandArgs.end())		remote=commandArgs["remote"];
	else
	{
		trace(3,"Parameter remote must be set (-remote=<value>) with one of the following values");
		diplayRemotes();
		return 1;
	}

    // Parse the firt parameter to this command as an integer
    if (wiringPiSetup () == -1) return 1;
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(pin);    
    mySwitch.setRepeatTransmit(repeat);
    mySwitch.setProtocol(protocol);
    
    //-------- Getting protocol signal information from json document ---------//
    Json::Value remoteNode = jsonRoot[remote];    
    if (remoteNode.isNull())
    {
		trace(3,"Unknown remote");
		diplayRemotes();
		return 2;
	}
    
    Json::Value protocolNode 		= remoteNode["protocol"];
    if (protocolNode.isNull() || !protocolNode.isInt())
    {
		string str = INVALIDE_REMOTE_PROP + remote + " 'protocol' is not set or is not an integer";
		trace(3, str.c_str());
		return 3;
	}
    else  {protocol 		= protocolNode.asInt();}
    
    Json::Value pulseLengthNode 	= remoteNode["pulseLength"];
    if (pulseLengthNode.isNull()) 	
    {
		string str = INVALIDE_REMOTE_PROP + remote + " 'pulseLength' is not set or is not an integer";
		trace(3, str.c_str());
		return 4;
	}
    else 							pulseLength 	= pulseLengthNode.asInt();
    
    Json::Value signalBitNumberNode = remoteNode["SignalBitNumber"];
    if (signalBitNumberNode.isNull()) 	{}
    else 							signalBitNumber = signalBitNumberNode.asInt();

    Json::Value signalsNode = remoteNode["signals"];
    if (signalsNode.isNull()) 		
    {
		string str = INVALIDE_REMOTE_PROP + remote + " 'signals' is not or not correctly set";
		trace(3, str.c_str());
		return 5;
	}
    else 							
    {
		Value::Members membersOfSignals = signalsNode.getMemberNames();		
		it = commandArgs.find("getremotebuttons");
		if( commandArgs.find("getremotebuttons") != commandArgs.end() )		
			{diplayButtonsForRemote(membersOfSignals); return 0;}
		
		it = commandArgs.find("btn");
		if(it == commandArgs.end())	
		{
			trace(3, "Parameter 'btn' must be set (-btn=<value>) with one of the following values");
			diplayButtonsForRemote(membersOfSignals);
			return 6;
		}
		else
		{
			string btnName = commandArgs["btn"];
			jsonSignal = signalsNode[btnName];
			if (jsonSignal.isNull())									
			{
				string str = INVALIDE_REMOTE_PROP + remote + " button:" + btnName + " Signal code is not set";
				trace(3, str.c_str());
				return 7;
			}
			else if (!jsonSignal.isArray() && (!jsonSignal.isInt())) 	
			{
				string str = INVALIDE_REMOTE_PROP + remote + " button:" + btnName + " Signal code is neither an array nor an integer";
				trace(3, str.c_str());
				return 8;
			}
		}
	}


	// Assertion sur signalBitNumberNode!=0 ou jsonSignal.isArray()
	if (signalBitNumber == 0 && !jsonSignal.isArray() )
	{
		trace(3, "Propeties of remote are invalid : either 'signalBitNumberNode' must be set or each button signal must be an array of bit values (0,1) ");
		return 9;
	}

	//------------------------ Getting protocole info for this remote -----------------//
	string protocole = "protocole" + patch::to_string(protocol);
	Json::Value protocoleInfoNode = jsonRoot[protocole];
	if (protocoleInfoNode.isNull())
	{
		string str = INVALIDE_REMOTE_PROP + remote + " : remote protocol '" + protocole	+ "' doesn't exist in declared protocols ";
		trace(3, str.c_str());
		return 10;
	}

	
	Json::Value  presendSignalCodeTransformationNode = remoteNode["PresendSignalCodeTransformation"];
	if (!presendSignalCodeTransformationNode.isNull())
	{
		string presendSignalCodeTransformationValue = presendSignalCodeTransformationNode.asString();
		if (presendSignalCodeTransformationValue.find("MANCHESTER") != string::npos)
		{isManchesterCoded = true;}
	}
	else
	{
		Json::Value  protocol_presendSignalCodeTransformationNode 	= protocoleInfoNode["PresendSignalCodeTransformation"];
		if (!protocol_presendSignalCodeTransformationNode.isNull() && protocol_presendSignalCodeTransformationNode.asString().find("MANCHESTER") != string::npos)
		{isManchesterCoded = true;}
	}

	//------------------ Getting Start lock HIGH-->LOW pulses ---------//
	Json::Value  startlock_highlowNode 	= remoteNode["startlock_highlow"];
	if (!startlock_highlowNode.isNull()) 	// startlock_highlow est précisé dans les info du button de la remote
	{
		if (!startlock_highlowNode.isArray() || startlock_highlowNode.size() != 2)
		{
			string str = INVALIDE_REMOTE_PROP + remote + "' : startlock_highlow is not an array or is not well set";
			trace(3, str.c_str());
			return 10;
		}
		else
		{
			startlock_highlow[0] = startlock_highlowNode[0].asInt();
			startlock_highlow[1] = startlock_highlowNode[1].asInt();
		}
	}
	else  									// Retrouver le noeud protocole et en déduire startlock_highlow
	{		
		Json::Value startlock_highlowNode =  protocoleInfoNode["startlock_low_NbPulseLength"];
		if (startlock_highlowNode.isNull()) 	
		{
			string str = INVALIDE_REMOTE_PROP + remote + "' : startlock_highlow array is neither set in btn, nor in protocol";
			trace(3, str.c_str());
			return 11;
		}
		else if (startlock_highlowNode.isDouble())
		{
			startlock_highlow[0] = pulseLength;
			startlock_highlow[1] = startlock_highlowNode.asDouble() * pulseLength;
		}
		else // must be an integer
		{
			string str = INVALIDE_REMOTE_PROP + remote + "' : startlock_highlow must be an integer";
			trace(3, str.c_str());
			return 12;
		}
	}

	//------------------ Getting End lock HIGH-->LOW pulses ---------//
	Json::Value  endlock_highlowNode 	= remoteNode["endlock_highlow"];
	if (!endlock_highlowNode.isNull())
	{
		if (!endlock_highlowNode.isArray() || endlock_highlowNode.size() != 2)
		{
			string str = INVALIDE_REMOTE_PROP + remote + "' : endlock_highlow is not an array or is not well set";
			trace(3, str.c_str());
			return 13;
		}
		else
		{
			endlock_highlow[0] = endlock_highlowNode[0].asInt();
			endlock_highlow[1] = endlock_highlowNode[1].asInt();
		}
	}
	else  									// Retrouver le noeud protocole et en déduire endlock_highlow
	{		
		Json::Value endlock_highlowNode =  protocoleInfoNode["endlock_low_NbPulseLength"];
		if (endlock_highlowNode.isNull()) 	
		{
			string str = INVALIDE_REMOTE_PROP + remote + "' : endlock_highlow array is neither set in btn, nor in protocol";
			trace(3, str.c_str());
			return 14;
		}
		else if (endlock_highlowNode.isDouble())
		{
			endlock_highlow[0] = pulseLength;
			endlock_highlow[1] = endlock_highlowNode.asDouble() * pulseLength;
		}
		else // must be an integer
		{
			string str = INVALIDE_REMOTE_PROP + remote + "' : endlock_highlow must be an integer";
			trace(3, str.c_str());
			return 15;
		}
	}	

	//------------------ Getting zeroEncodingInNbPulse HIGH-->LOW pulses ---------//
	Json::Value  protocol_zeroEncodingInNbPulseNode = protocoleInfoNode["ZeroEncoding_highlow_NbPulseLength"];
	Json::Value  zeroEncodingInNbPulseNode 			= remoteNode["ZeroEncodingInNbPulse"];
	if (!zeroEncodingInNbPulseNode.isNull() && zeroEncodingInNbPulseNode.isArray())
	{
		zeroEncoding_highlow[0] = zeroEncodingInNbPulseNode[0].asDouble();
		zeroEncoding_highlow[1] = zeroEncodingInNbPulseNode[1].asDouble();
	}
	else if (!protocol_zeroEncodingInNbPulseNode.isNull() && protocol_zeroEncodingInNbPulseNode.isArray())
	{
		zeroEncoding_highlow[0] = protocol_zeroEncodingInNbPulseNode[0].asDouble() * pulseLength;
		zeroEncoding_highlow[1] = protocol_zeroEncodingInNbPulseNode[1].asDouble() * pulseLength;
	}
	else
	{
		string str = INVALIDE_REMOTE_PROP + remote + " : 'zeroEncoding_highlow' parameter must be an array and must be set either in 'remote node' or in 'protocol' node";
		trace(3, str.c_str());
		return 16;
	}
	
	//------------------ Getting oneEncodingInNbPulse HIGH-->LOW pulses ---------//
	Json::Value  protocol_oneEncodingInNbPulseNode 	= protocoleInfoNode["OneEncoding_highlow_NbPulseLength"];
	Json::Value  oneEncodingInNbPulseNode 			= remoteNode["OneEncodingInNbPulse"];
	if (!oneEncodingInNbPulseNode.isNull() && oneEncodingInNbPulseNode.isArray())
	{
		oneEncoding_highlow[0] = oneEncodingInNbPulseNode[0].asDouble();
		oneEncoding_highlow[1] = oneEncodingInNbPulseNode[1].asDouble();
	}
	else if (!protocol_oneEncodingInNbPulseNode.isNull() && protocol_oneEncodingInNbPulseNode.isArray())
	{
		oneEncoding_highlow[0] = protocol_oneEncodingInNbPulseNode[0].asDouble() * pulseLength;
		oneEncoding_highlow[1] = protocol_oneEncodingInNbPulseNode[1].asDouble() * pulseLength;
	}
	else
	{
		string str = INVALIDE_REMOTE_PROP + remote + " : 'oneEncoding_highlow' parameter must be an array and must be set in 'remote node' or in 'protocol' node";
		trace(3, str.c_str());
		return 17;
	}

	//---------------------------------------------- Traitement d'envoi du signal --------------------------------------------------//
	if (jsonSignal.isArray())
	{
		remotecodestring = "";
		for(unsigned int index=0; index<jsonSignal.size(); ++index)  
			remotecodestring.append(jsonSignal[index].asString());	
		strcpy(remotecode,remotecodestring.c_str());
	}
	else // c'est un entier
	{
		decimalcode = jsonSignal.asInt();
		strcpy(remotecode,dec2binWzerofill(decimalcode,signalBitNumber));
	}

	if (isManchesterCoded) // Codage manchester si nécessaire
	{
		string binarycode = "";
    	for(int i=0;i<strlen(remotecode);i++)
    	{
    		if (remotecode[i]=='0')	    		binarycode.append("01");
	    	else if (remotecode[i]=='1')		binarycode.append("10");
	    	else 
	    	{
				string str = " Invalid manchester coded signal : ";  str += remotecode;
	    		trace(3, str.c_str());
	    		return 18;
	    	}
    	}
    	strcpy(remotecode,binarycode.c_str());
	}

	// positionner les paramètres de mySwitch : pulseLength, protocole, etc ...
	mySwitch.setRepeatTransmit(repeat);
	mySwitch.setPulseLength(pulseLength);
	mySwitch.setProtocol(protocol);
	mySwitch.setSendZeroEncodingInNbPulse(zeroEncoding_highlow[0]/pulseLength, zeroEncoding_highlow[1]/pulseLength);
	mySwitch.setSendOneEncodingInNbPulse(oneEncoding_highlow[0]/pulseLength, oneEncoding_highlow[1]/pulseLength);

	mySwitch.setSendStartLockHighLowInNbPulse(startlock_highlow[0]/pulseLength, startlock_highlow[1]/pulseLength);
	mySwitch.setSendEndLockHighLowInNbPulse(endlock_highlow[0]/pulseLength, endlock_highlow[1]/pulseLength);

	cout << "> Sending raw code = '"<< remotecode << "' > " << endl;
	if (loglevel <= 1)
	{
		cout << "  Number of bit    = " << strlen(remotecode) << endl;
		cout << "  Manchester       = " << isManchesterCoded << endl;
		cout << "  Protocole        = " << protocol	<< endl;
		cout << "  ZeroEncoding     = " << zeroEncoding_highlow[0] 	<< "," << zeroEncoding_highlow[1] 	<< endl;
		cout << "  OneEncoding      = " << oneEncoding_highlow[0] 	<< "," << oneEncoding_highlow[1] 	<< endl;
		cout << "  StartLock        = " << startlock_highlow[0] 	<< "," << startlock_highlow[1] 		<< endl;
		cout << "  EndLock          = " << endlock_highlow[0] 		<< "," << endlock_highlow[1] 		<< endl;
		cout << "  Repeat           = " << repeat 		<< endl;
	}
	if (loglevel <= 0)
		cout << mySwitch.computeAndDisplaySignalToSend(remotecode);
	
	
	mySwitch.send(remotecode);
 	cout << "< Sent <" << endl;
	   
	return 0;
}
예제 #25
0
/* ---------------------------------------------------------------- */
int main(int argc, char *argv[]) 
{  
    parseCommandLineArgs(argc, argv);
	displayCommandLineArgs();
	
	ifstream jsonFile;
	string rfJsonFilePath = "./radioFrequenceSignalConfig.json";
	jsonFile.open(rfJsonFilePath.c_str());
		
	//bool parsedSuccess = reader.parse(json_example, root, false);
	bool parsedResult = jsonReader.parse(jsonFile, jsonRoot, false);
	jsonFile.close();
	
	if(!parsedResult) // Report failures and their locations in the document.
	{
		cout<<"Echec d'analyse du fichier " << rfJsonFilePath << " : " << endl << jsonReader.getFormatedErrorMessages() << endl;
		return 1;
	}
	
	int repeat						= 10;	
	int protocol					= 1;	
	int pin							= 0;	

   	int decimalcode			= 0;
   	int nbbit						= 24;
   	
   	string manchbin 			= "";
   	string binarycode		= "";	
    	
    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
	
	std::map<std::string,std::string>::iterator it;
	it = commandArgs.find("repeat");
	if(it != commandArgs.end())		repeat=atoi(commandArgs["repeat"].c_str());
	
	it = commandArgs.find("protocol");
	if(it != commandArgs.end())		protocol=atoi(commandArgs["protocol"].c_str());
	
	it = commandArgs.find("pin");
	if(it != commandArgs.end())		pin=atoi(commandArgs["pin"].c_str());
	
	it = commandArgs.find("dec");
	if(it != commandArgs.end())		decimalcode=atoi(commandArgs["dec"].c_str());

	it = commandArgs.find("nbbit");
	if(it != commandArgs.end())		nbbit=atoi(commandArgs["nbbit"].c_str());
	
	it = commandArgs.find("bin");
	if(it != commandArgs.end())		binarycode=commandArgs["bin"];

	it = commandArgs.find("manchbin");
	if(it != commandArgs.end())		manchbin=commandArgs["manchbin"];
	
	// Assertion arguments bien positionnés
	int isSet = 0;
	if (decimalcode !=0) 			isSet++;
	if (binarycode.length() !=0)  	isSet++;
	if (manchbin.length() !=0) 		isSet++;
	if (isSet != 1)
	{
		cout << " One and only one of the following argument must be set : dec (with nbbit), bin,  manchbin" << endl;
		return 2;
	}
	// Assertion si décimal code set, alors nbbit doit l'etre
	if (decimalcode !=0 && nbbit==0)
	{
		cout << " dec is set without nbbit ; nbbit must be set too ..." << endl;
		return 3;
	}
	
	// Assertion protocol
	if (protocol < 1 || protocol > 3)
	{
		cout << " protocol must set between 1 and 3" << endl;
		return 4;
	}
	
    // Parse the firt parameter to this command as an integer
    if (wiringPiSetup () == -1) return 1;
	RCSwitch mySwitch = RCSwitch();
	mySwitch.enableTransmit(pin);    
    mySwitch.setRepeatTransmit(repeat);
    mySwitch.setProtocol(protocol);
    
    // Get protocol signal from json document
    //jsonRoot[];
    
    
    
    if (decimalcode != 0)
    {
		cout << "sending decimal code='" << decimalcode << "'  ...";
   	 	mySwitch.send(decimalcode, nbbit);
   	 	cout << " sent" << endl;
    }
    else if (binarycode.length() != 0)
    {
		cout << "sending binary code " << binarycode << "...";
    	mySwitch.send((char*)binarycode.c_str());
   	 	cout << " sent" << endl;
    }
    else // manchesterCode
    {
    	binarycode = "";
    	const char * manchesterCode = manchbin.c_str();
    	for(int i=0;i<manchbin.length();i++)
    	{
    		if (manchesterCode[i]=='0')	    		binarycode.append("01");
	    	else if (manchesterCode[i]=='1')		binarycode.append("10");
	    	else 
	    	{
	    		cout << " Invalid manchester coded signal : " << manchbin << endl;
	    		return 4;
	    	}
    	}
		cout << "sending manchester binary code=" << manchbin << " true code is=" << binarycode << "...";
    	mySwitch.send((char*)binarycode.c_str());
   	 	cout << " sent" << endl;
    }
	return 0;
}
예제 #26
0
int main(int argc, char *argv[]) {

    /*
     output PIN is hardcoded for testing purposes
     see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
     for pin mapping of the raspberry pi GPIO connector
     */
    int PIN = 0;
    if (wiringPiSetup () == -1) return 1;
    RCSwitch mySwitch = RCSwitch();
    mySwitch.enableTransmit(PIN);

    if(argc == 4)
    {
        char* sGroup = argv[1];
        int nSwitchNumber = atoi(argv[2]);

        int command  = atoi(argv[3]);

        if(strlen(sGroup) > 2)
        {
            //Type A: 10 pole DIP switches
            printf("sending [Type A] groupCode[%s] switchNumber[%i] command[%i]\n", sGroup, nSwitchNumber, command);

            switch(command) {
            case 1:
                mySwitch.switchOn(sGroup, nSwitchNumber);
                break;
            case 0:
                mySwitch.switchOff(sGroup, nSwitchNumber);
                break;
            default:
                printf("command[%i] is unsupported\n", command);
                printUsage();
                return -1;
            }
            return 0;
        } else {
            //Type B: Two rotary/sliding switches
            int nGroupNumber = atoi(sGroup);
            printf("sending [Type B] groupNumber[%i] switchNumber[%i] command[%i]\n", nGroupNumber, nSwitchNumber, command);
            switch(command) {
            case 1:
                mySwitch.switchOn(nGroupNumber, nSwitchNumber);
                break;
            case 0:
                mySwitch.switchOff(nGroupNumber, nSwitchNumber);
                break;
            default:
                printf("command[%i] is unsupported\n", command);
                printUsage();
                return -1;
            }
            return 0;
        }
    }
    else if(argc == 5)
    {
        //Type C: Intertechno
        char* sFamily = argv[1];
        int nGroup = atoi(argv[2]);
        int nDevice = atoi(argv[3]);

        int command = atoi(argv[4]);

        printf("sending [Type C] family[%s] groupNumber[%i] switchNumber[%i] command[%i]\n", sFamily, nGroup, nDevice, command);

        switch(command) {
        case 1:
            mySwitch.switchOn(sFamily[0], nGroup, nDevice);
            break;
        case 0:
            mySwitch.switchOff(sFamily[0], nGroup, nDevice);
            break;
        default:
            printf("command[%i] is unsupported\n", command);
            printUsage();
            return -1;
        }
        return 0;
    }
    else
    {
        printUsage();
    }
    return 1;
}
예제 #27
0
int main(int argc, char *argv[]) {
  /**
   * output PIN is hardcoded for testing purposes
   * see https://projects.drogon.net/raspberry-pi/wiringpi/pins/
   * for pin mapping of the raspberry pi GPIO connector
   */
  int PIN = 0;
  /**
   * using old numbering mode by default,
   * see RCSwitch.cpp for differences
   */
  bool binaryMode = false;
  char* systemCode;
  int unitCode;
  int command;

  if (argc < 4) {
    /**
     * no matter, which mode is used, at least 4 arguments are required:
     * 0: command name (send)
     * 1: systemCode
     * 2: unitCode
     * 3: command
     * if there are less arguments, the help should be printed
     * and the application should terminate.
     */
    printUsage();
    return 1;
  }

  /**
   * This needs to stand after the check of how many arguments are passed,
   * because if there is only 1 argument passed, argv[1] is NULL, which
   * will result in an error, because a std::string can't be constructed
   * by a NULL value. Therefore it is important to terminate the application
   * if there are less than 2 arguments (in this case: less than 4 arguments)
   * passed.
   */
  std::string firstArgument = argv[1];

  if (firstArgument == "-b" or firstArgument == "--binary") {
    if (argc < 5) {
    /**
     * in binaryMode, 5 arguments are required:
     * 0: command name ('send')
     * 1: binary operator ('-b')
     * 2: systemCode
     * 3: unitCode
     * 4: command
     * if there are less arguments, the help should be printed,
     * and the application should terminate.
     */
      printUsage();
      return 1;
    }

    printf("operating in binary mode...\n");
    binaryMode = true;
    //position of data in input is now shifted by 1 because of '-b'-flag
    systemCode = argv[2];
    unitCode = atoi(argv[3]);
    command  = atoi(argv[4]);
  } else if (firstArgument == "-h" or firstArgument == "--help" or firstArgument == "-?") {
    printUsage();
  } else {
    //no binary mode, therefore using normal mode with old numbering
    systemCode = argv[1];
    unitCode = atoi(argv[2]);
    command  = atoi(argv[3]);
  }

  if (wiringPiSetup () == -1) return 1;
  piHiPri(20);
  printf("sending systemCode[%s] unitCode[%i] command[%i]\n", systemCode, unitCode, command);
  RCSwitch mySwitch = RCSwitch();
  mySwitch.setPulseLength(300);
  mySwitch.enableTransmit(PIN);

  if (binaryMode) {
    switch (command){
      case 1:
        mySwitch.switchOnBinary(systemCode, unitCode);
        break;
      case 0:
        mySwitch.switchOffBinary(systemCode, unitCode);
        break;
      default:
        printf("command[%i] is unsupported\n", command);
        printUsage();
        return -1;
    }
    return 0;
  } else {
    switch(command) {
      case 1:
        mySwitch.switchOn(systemCode, unitCode);
        break;
      case 0:
        mySwitch.switchOff(systemCode, unitCode);
        break;
      case 2:
        // 00001 2 on binary coded
        mySwitch.send("010101010001000101010001");
        break;
      case 3:
        // 00001 2 on as TriState
        mySwitch.sendTriState("FFFF0F0FFF0F");
        break;
      default:
        printf("command[%i] is unsupported\n", command);
        printUsage();
        return -1;
    }
    return 0;
  }
}