forked from r10r/rcswitch-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.cpp
131 lines (119 loc) · 3.4 KB
/
send.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Usage: see printUsage()
*/
#define RPI
#include "RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
void printUsage()
{
std::cout << " Usage: sudo ./send <groupCode> <switchNumber> <command>\n";
std::cout << " e.g. sudo ./send 01011 3 1\n";
std::cout << " sudo ./send <groupNumber> <switchNumber> <command>\n";
std::cout << " e.g. sudo ./send 4 3 0\n";
std::cout << " sudo ./send <familyCharacter> <groupNumber> <switchNumber> <command>\n";
std::cout << " e.g. sudo ./send c 2 3 1\n";
std::cout << " sudo ./send <dipSwitchGroup> <dipSwitchUnit> <command>\n";
std::cout << " e.g. sudo ./send 11100 00001 1\n";
std::cout << " sudo ./send <protocol> <command>\n";
std::cout << " e.g. sudo ./send 3 000000000001010100010001\n";
std::cout << "\n";
std::cout << " Command is 0 for OFF and 1 for ON\n";
std::cout << "\n";
std::cout << " See http://code.google.com/p/rc-switch/wiki/HowTo_OperateLowCostOutlets for more information about supported switches\n";
}
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 == 3) {
int sProtocol = atoi(argv[1]);
char* command = argv[2];
mySwitch.setProtocol(sProtocol);
mySwitch.send(command);
} else if(argc == 4)
{
char* sGroup = argv[1];
char* sSwitch = argv[2];
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[%s] command[%i]\n", sGroup, sSwitch, command);
switch(command) {
case 1:
if (strlen(sSwitch) > 2) {
mySwitch.switchOn(sGroup, sSwitch);
} else {
mySwitch.switchOn(sGroup, nSwitchNumber);
}
break;
case 0:
if (strlen(sSwitch) > 2) {
mySwitch.switchOff(sGroup, sSwitch);
} else {
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;
}