--- /dev/null
+static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength){
+ static char bin[64];
+ unsigned int i=0;
+
+ while (Dec > 0) {
+ bin[32+i++] = (Dec & 1 > 0) ? '1' : '0';
+ Dec = Dec >> 1;
+ }
+
+ for (unsigned int j = 0; j< bitLength; j++) {
+ if (j >= bitLength - i) {
+ bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
+ }else {
+ bin[j] = '0';
+ }
+ }
+ bin[bitLength] = '\0';
+
+ return bin;
+}\r
--- /dev/null
+/*
+ A simple RCSwitch/Ethernet/Webserver demo
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <SPI.h>
+#include <Ethernet.h>
+#include <RCSwitch.h>
+
+// Ethernet configuration
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
+byte ip[] = { 192,168,0, 2 }; // IP Address
+EthernetServer server(80); // Server Port 80
+
+// RCSwitch configuration
+RCSwitch mySwitch = RCSwitch();
+int RCTransmissionPin = 7;
+
+// More to do...
+// You should also modify the processCommand() and
+// httpResponseHome() functions to fit your needs.
+
+
+
+/**
+ * Setup
+ */
+void setup() {
+ Ethernet.begin(mac, ip);
+ server.begin();
+ mySwitch.enableTransmit( RCTransmissionPin );
+}
+
+/**
+ * Loop
+ */
+void loop() {
+ char* command = httpServer();
+}
+
+/**
+ * Command dispatcher
+ */
+void processCommand(char* command) {
+ if (strcmp(command, "1-on") == 0) {
+ mySwitch.switchOn(1,1);
+ } else if (strcmp(command, "1-off") == 0) {
+ mySwitch.switchOff(1,1);
+ } else if (strcmp(command, "2-on") == 0) {
+ mySwitch.switchOn(1,2);
+ } else if (strcmp(command, "2-off") == 0) {
+ mySwitch.switchOff(1,2);
+ }
+}
+
+/**
+ * HTTP Response with homepage
+ */
+void httpResponseHome(EthernetClient c) {
+ c.println("HTTP/1.1 200 OK");
+ c.println("Content-Type: text/html");
+ c.println();
+ c.println("<html>");
+ c.println("<head>");
+ c.println( "<title>RCSwitch Webserver Demo</title>");
+ c.println( "<style>");
+ c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
+ c.println( "</style>");
+ c.println("</head>");
+ c.println("<body>");
+ c.println( "<h1>RCSwitch Webserver Demo</h1>");
+ c.println( "<ul>");
+ c.println( "<li><a href=\"./?1-on\">Switch #1 on</a></li>");
+ c.println( "<li><a href=\"./?1-off\">Switch #1 off</a></li>");
+ c.println( "</ul>");
+ c.println( "<ul>");
+ c.println( "<li><a href=\"./?2-on\">Switch #2 on</a></li>");
+ c.println( "<li><a href=\"./?2-off\">Switch #2 off</a></li>");
+ c.println( "</ul>");
+ c.println( "<hr>");
+ c.println( "<a href=\"http://code.google.com/p/rc-switch/\">http://code.google.com/p/rc-switch/</a>");
+ c.println("</body>");
+ c.println("</html>");
+}
+
+/**
+ * HTTP Redirect to homepage
+ */
+void httpResponseRedirect(EthernetClient c) {
+ c.println("HTTP/1.1 301 Found");
+ c.println("Location: /");
+ c.println();
+}
+
+/**
+ * HTTP Response 414 error
+ * Command must not be longer than 30 characters
+ **/
+void httpResponse414(EthernetClient c) {
+ c.println("HTTP/1.1 414 Request URI too long");
+ c.println("Content-Type: text/plain");
+ c.println();
+ c.println("414 Request URI too long");
+}
+
+/**
+ * Process HTTP requests, parse first request header line and
+ * call processCommand with GET query string (everything after
+ * the ? question mark in the URL).
+ */
+char* httpServer() {
+ EthernetClient client = server.available();
+ if (client) {
+ char sReturnCommand[32];
+ int nCommandPos=-1;
+ sReturnCommand[0] = '\0';
+ while (client.connected()) {
+ if (client.available()) {
+ char c = client.read();
+ if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
+ sReturnCommand[nCommandPos] = '\0';
+ if (strcmp(sReturnCommand, "\0") == 0) {
+ httpResponseHome(client);
+ } else {
+ processCommand(sReturnCommand);
+ httpResponseRedirect(client);
+ }
+ break;
+ }
+ if (nCommandPos>-1) {
+ sReturnCommand[nCommandPos++] = c;
+ }
+ if (c == '?' && nCommandPos == -1) {
+ nCommandPos = 0;
+ }
+ }
+ if (nCommandPos > 30) {
+ httpResponse414(client);
+ sReturnCommand[0] = '\0';
+ break;
+ }
+ }
+ if (nCommandPos!=-1) {
+ sReturnCommand[nCommandPos] = '\0';
+ }
+ // give the web browser time to receive the data
+ delay(1);
+ client.stop();
+
+ return sReturnCommand;
+ }
+ return '\0';
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ Example for Intertechno outlets\r
+ \r
+ http://code.google.com/p/rc-switch/\r
+*/\r
+\r
+#include <RCSwitch.h>\r
+\r
+RCSwitch mySwitch = RCSwitch();\r
+\r
+void setup() {\r
+\r
+ // Transmitter is connected to Arduino Pin #10 \r
+ mySwitch.enableTransmit(10);\r
+ \r
+ // Optional set pulse length.\r
+ // mySwitch.setPulseLength(320);\r
+ \r
+}\r
+\r
+void loop() {\r
+\r
+ // Switch on:\r
+ // The first parameter represents the familycode (a, b, c, ... f)\r
+ // The second parameter represents the group number\r
+ // The third parameter represents the device number\r
+ // \r
+ // In this example it's family 'b', group #3, device #2 \r
+ mySwitch.switchOn('b', 3, 2);\r
+\r
+ // Wait a second\r
+ delay(1000);\r
+ \r
+ // Switch off\r
+ mySwitch.switchOff('b', 3, 2);\r
+ \r
+ // Wait another second\r
+ delay(1000);\r
+ \r
+}
\ No newline at end of file
--- /dev/null
+/*
+ Example for receiving
+
+ http://code.google.com/p/rc-switch/
+
+ If you want to visualize a telegram copy the raw data and
+ paste it into http://test.sui.li/oszi/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+ Serial.begin(9600);
+ mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
+}
+
+void loop() {
+ if (mySwitch.available()) {
+ output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
+ mySwitch.resetAvailable();
+ }
+}\r
--- /dev/null
+/*\r
+ RCSwitch - Arduino libary for remote control outlet switches\r
+ Copyright (c) 2011 Suat Özgür. All right reserved.\r
+ \r
+ Contributors:\r
+ - Andre Koehler / info(at)tomate-online(dot)de\r
+ - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com\r
+ - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46\r
+ - Dominik Fischer / dom_fischer(at)web(dot)de\r
+ \r
+ Project home: http://code.google.com/p/rc-switch/\r
+\r
+ This library is free software; you can redistribute it and/or\r
+ modify it under the terms of the GNU Lesser General Public\r
+ License as published by the Free Software Foundation; either\r
+ version 2.1 of the License, or (at your option) any later version.\r
+\r
+ This library is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ Lesser General Public License for more details.\r
+\r
+ You should have received a copy of the GNU Lesser General Public\r
+ License along with this library; if not, write to the Free Software\r
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
+*/\r
+\r
+#include "RCSwitch.h"\r
+\r
+unsigned long RCSwitch::nReceivedValue = NULL;\r
+unsigned int RCSwitch::nReceivedBitlength = 0;\r
+unsigned int RCSwitch::nReceivedDelay = 0;\r
+unsigned int RCSwitch::nReceivedProtocol = 0;\r
+unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES];\r
+int RCSwitch::nReceiveTolerance = 60;\r
+\r
+RCSwitch::RCSwitch() {\r
+ this->nReceiverInterrupt = -1;\r
+ this->nTransmitterPin = -1;\r
+ RCSwitch::nReceivedValue = NULL;\r
+ this->setPulseLength(350);\r
+ this->setRepeatTransmit(10);\r
+ this->setReceiveTolerance(60);\r
+ this->setProtocol(1);\r
+}\r
+\r
+/**\r
+ * Sets the protocol to send.\r
+ */\r
+void RCSwitch::setProtocol(int nProtocol) {\r
+ this->nProtocol = nProtocol;\r
+ if (nProtocol == 1){\r
+ this->setPulseLength(350);\r
+ }\r
+ else if (nProtocol == 2) {\r
+ this->setPulseLength(650);\r
+ }\r
+}\r
+\r
+/**\r
+ * Sets the protocol to send with pulse length in microseconds.\r
+ */\r
+void RCSwitch::setProtocol(int nProtocol, int nPulseLength) {\r
+ this->nProtocol = nProtocol;\r
+ if (nProtocol == 1){\r
+ this->setPulseLength(nPulseLength);\r
+ }\r
+ else if (nProtocol == 2) {\r
+ this->setPulseLength(nPulseLength);\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ * Sets pulse length in microseconds\r
+ */\r
+void RCSwitch::setPulseLength(int nPulseLength) {\r
+ this->nPulseLength = nPulseLength;\r
+}\r
+\r
+/**\r
+ * Sets Repeat Transmits\r
+ */\r
+void RCSwitch::setRepeatTransmit(int nRepeatTransmit) {\r
+ this->nRepeatTransmit = nRepeatTransmit;\r
+}\r
+\r
+/**\r
+ * Set Receiving Tolerance\r
+ */\r
+void RCSwitch::setReceiveTolerance(int nPercent) {\r
+ RCSwitch::nReceiveTolerance = nPercent;\r
+}\r
+ \r
+\r
+/**\r
+ * Enable transmissions\r
+ *\r
+ * @param nTransmitterPin Arduino Pin to which the sender is connected to\r
+ */\r
+void RCSwitch::enableTransmit(int nTransmitterPin) {\r
+ this->nTransmitterPin = nTransmitterPin;\r
+ pinMode(this->nTransmitterPin, OUTPUT);\r
+}\r
+\r
+/**\r
+ * Disable transmissions\r
+ */\r
+void RCSwitch::disableTransmit() {\r
+ this->nTransmitterPin = -1;\r
+}\r
+\r
+/**\r
+ * Switch a remote switch on (Type C Intertechno)\r
+ *\r
+ * @param sFamily Familycode (a..f)\r
+ * @param nGroup Number of group (1..4)\r
+ * @param nDevice Number of device (1..4)\r
+ */\r
+void RCSwitch::switchOn(char sFamily, int nGroup, int nDevice) {\r
+ this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, true) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch off (Type C Intertechno)\r
+ *\r
+ * @param sFamily Familycode (a..f)\r
+ * @param nGroup Number of group (1..4)\r
+ * @param nDevice Number of device (1..4)\r
+ */\r
+void RCSwitch::switchOff(char sFamily, int nGroup, int nDevice) {\r
+ this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, false) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch on (Type B with two rotary/sliding switches)\r
+ *\r
+ * @param nAddressCode Number of the switch group (1..4)\r
+ * @param nChannelCode Number of the switch itself (1..4)\r
+ */\r
+void RCSwitch::switchOn(int nAddressCode, int nChannelCode) {\r
+ this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, true) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch off (Type B with two rotary/sliding switches)\r
+ *\r
+ * @param nAddressCode Number of the switch group (1..4)\r
+ * @param nChannelCode Number of the switch itself (1..4)\r
+ */\r
+void RCSwitch::switchOff(int nAddressCode, int nChannelCode) {\r
+ this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, false) );\r
+}\r
+\r
+/**\r
+ * Deprecated, use switchOn(char* sGroup, char* sDevice) instead!\r
+ * Switch a remote switch on (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param nChannelCode Number of the switch itself (1..5)\r
+ */\r
+void RCSwitch::switchOn(char* sGroup, int nChannel) {\r
+ char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };\r
+ this->switchOn(sGroup, code[nChannel]);\r
+}\r
+\r
+/**\r
+ * Deprecated, use switchOff(char* sGroup, char* sDevice) instead!\r
+ * Switch a remote switch off (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param nChannelCode Number of the switch itself (1..5)\r
+ */\r
+void RCSwitch::switchOff(char* sGroup, int nChannel) {\r
+ char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };\r
+ this->switchOff(sGroup, code[nChannel]);\r
+}\r
+\r
+/**\r
+ * Switch a remote switch on (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param sDevice Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ */\r
+void RCSwitch::switchOn(char* sGroup, char* sDevice) {\r
+ this->sendTriState( this->getCodeWordA(sGroup, sDevice, true) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch off (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param sDevice Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ */\r
+void RCSwitch::switchOff(char* sGroup, char* sDevice) {\r
+ this->sendTriState( this->getCodeWordA(sGroup, sDevice, false) );\r
+}\r
+\r
+/**\r
+ * Returns a char[13], representing the Code Word to be send.\r
+ * A Code Word consists of 9 address bits, 3 data bits and one sync bit but in our case only the first 8 address bits and the last 2 data bits were used.\r
+ * A Code Bit can have 4 different states: "F" (floating), "0" (low), "1" (high), "S" (synchronous bit)\r
+ *\r
+ * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+\r
+ * | 4 bits address (switch group) | 4 bits address (switch number) | 1 bit address (not used, so never mind) | 1 bit address (not used, so never mind) | 2 data bits (on|off) | 1 sync bit |\r
+ * | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | F | F | on=FF off=F0 | S |\r
+ * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+\r
+ *\r
+ * @param nAddressCode Number of the switch group (1..4)\r
+ * @param nChannelCode Number of the switch itself (1..4)\r
+ * @param bStatus Wether to switch on (true) or off (false)\r
+ *\r
+ * @return char[13]\r
+ */\r
+char* RCSwitch::getCodeWordB(int nAddressCode, int nChannelCode, boolean bStatus) {\r
+ int nReturnPos = 0;\r
+ static char sReturn[13];\r
+ \r
+ char* code[5] = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" };\r
+ if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) {\r
+ return '\0';\r
+ }\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = code[nAddressCode][i];\r
+ }\r
+\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = code[nChannelCode][i];\r
+ }\r
+ \r
+ sReturn[nReturnPos++] = 'F';\r
+ sReturn[nReturnPos++] = 'F';\r
+ sReturn[nReturnPos++] = 'F';\r
+ \r
+ if (bStatus) {\r
+ sReturn[nReturnPos++] = 'F';\r
+ } else {\r
+ sReturn[nReturnPos++] = '0';\r
+ }\r
+ \r
+ sReturn[nReturnPos] = '\0';\r
+ \r
+ return sReturn;\r
+}\r
+\r
+/**\r
+ * Returns a char[13], representing the Code Word to be send.\r
+ *\r
+ * getCodeWordA(char*, char*)\r
+ *\r
+ */\r
+char* RCSwitch::getCodeWordA(char* sGroup, char* sDevice, boolean bOn) {\r
+ static char sDipSwitches[13];\r
+ int i = 0;\r
+ int j = 0;\r
+ \r
+ for (i=0; i < 5; i++) {\r
+ if (sGroup[i] == '0') {\r
+ sDipSwitches[j++] = 'F';\r
+ } else {\r
+ sDipSwitches[j++] = '0';\r
+ }\r
+ }\r
+\r
+ for (i=0; i < 5; i++) {\r
+ if (sDevice[i] == '0') {\r
+ sDipSwitches[j++] = 'F';\r
+ } else {\r
+ sDipSwitches[j++] = '0';\r
+ }\r
+ }\r
+\r
+ if ( bOn ) {\r
+ sDipSwitches[j++] = '0';\r
+ sDipSwitches[j++] = 'F';\r
+ } else {\r
+ sDipSwitches[j++] = 'F';\r
+ sDipSwitches[j++] = '0';\r
+ }\r
+\r
+ sDipSwitches[j] = '\0';\r
+\r
+ return sDipSwitches;\r
+}\r
+\r
+/**\r
+ * Like getCodeWord (Type C = Intertechno)\r
+ */\r
+char* RCSwitch::getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus) {\r
+ static char sReturn[13];\r
+ int nReturnPos = 0;\r
+ \r
+ if ( (byte)sFamily < 97 || (byte)sFamily > 112 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {\r
+ return '\0';\r
+ }\r
+ \r
+ char* sDeviceGroupCode = dec2binWzerofill( (nDevice-1) + (nGroup-1)*4, 4 );\r
+ char familycode[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = familycode[ (int)sFamily - 97 ][i];\r
+ }\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0');\r
+ }\r
+ sReturn[nReturnPos++] = '0';\r
+ sReturn[nReturnPos++] = 'F';\r
+ sReturn[nReturnPos++] = 'F';\r
+ if (bStatus) {\r
+ sReturn[nReturnPos++] = 'F';\r
+ } else {\r
+ sReturn[nReturnPos++] = '0';\r
+ }\r
+ sReturn[nReturnPos] = '\0';\r
+ return sReturn;\r
+}\r
+\r
+/**\r
+ * Sends a Code Word\r
+ * @param sCodeWord /^[10FS]*$/ -> see getCodeWord\r
+ */\r
+void RCSwitch::sendTriState(char* sCodeWord) {\r
+ for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {\r
+ int i = 0;\r
+ while (sCodeWord[i] != '\0') {\r
+ switch(sCodeWord[i]) {\r
+ case '0':\r
+ this->sendT0();\r
+ break;\r
+ case 'F':\r
+ this->sendTF();\r
+ break;\r
+ case '1':\r
+ this->sendT1();\r
+ break;\r
+ }\r
+ i++;\r
+ }\r
+ this->sendSync(); \r
+ }\r
+}\r
+\r
+void RCSwitch::send(unsigned long Code, unsigned int length) {\r
+ this->send( this->dec2binWzerofill(Code, length) );\r
+}\r
+\r
+void RCSwitch::send(char* sCodeWord) {\r
+ for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {\r
+ int i = 0;\r
+ while (sCodeWord[i] != '\0') {\r
+ switch(sCodeWord[i]) {\r
+ case '0':\r
+ this->send0();\r
+ break;\r
+ case '1':\r
+ this->send1();\r
+ break;\r
+ }\r
+ i++;\r
+ }\r
+ this->sendSync();\r
+ }\r
+}\r
+\r
+void RCSwitch::transmit(int nHighPulses, int nLowPulses) {\r
+ boolean disabled_Receive = false;\r
+ int nReceiverInterrupt_backup = nReceiverInterrupt;\r
+ if (this->nTransmitterPin != -1) {\r
+ if (this->nReceiverInterrupt != -1) {\r
+ this->disableReceive();\r
+ disabled_Receive = true;\r
+ }\r
+ digitalWrite(this->nTransmitterPin, HIGH);\r
+ delayMicroseconds( this->nPulseLength * nHighPulses);\r
+ digitalWrite(this->nTransmitterPin, LOW);\r
+ delayMicroseconds( this->nPulseLength * nLowPulses);\r
+ if(disabled_Receive){\r
+ this->enableReceive(nReceiverInterrupt_backup);\r
+ }\r
+ }\r
+}\r
+/**\r
+ * Sends a "0" Bit\r
+ * _ \r
+ * Waveform Protocol 1: | |___\r
+ * _ \r
+ * Waveform Protocol 2: | |__\r
+ */\r
+void RCSwitch::send0() {\r
+ if (this->nProtocol == 1){\r
+ this->transmit(1,3);\r
+ }\r
+ else if (this->nProtocol == 2) {\r
+ this->transmit(1,2);\r
+ }\r
+}\r
+\r
+/**\r
+ * Sends a "1" Bit\r
+ * ___ \r
+ * Waveform Protocol 1: | |_\r
+ * __ \r
+ * Waveform Protocol 2: | |_\r
+ */\r
+void RCSwitch::send1() {\r
+ if (this->nProtocol == 1){\r
+ this->transmit(3,1);\r
+ }\r
+ else if (this->nProtocol == 2) {\r
+ this->transmit(2,1);\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ * Sends a Tri-State "0" Bit\r
+ * _ _\r
+ * Waveform: | |___| |___\r
+ */\r
+void RCSwitch::sendT0() {\r
+ this->transmit(1,3);\r
+ this->transmit(1,3);\r
+}\r
+\r
+/**\r
+ * Sends a Tri-State "1" Bit\r
+ * ___ ___\r
+ * Waveform: | |_| |_\r
+ */\r
+void RCSwitch::sendT1() {\r
+ this->transmit(3,1);\r
+ this->transmit(3,1);\r
+}\r
+\r
+/**\r
+ * Sends a Tri-State "F" Bit\r
+ * _ ___\r
+ * Waveform: | |___| |_\r
+ */\r
+void RCSwitch::sendTF() {\r
+ this->transmit(1,3);\r
+ this->transmit(3,1);\r
+}\r
+\r
+/**\r
+ * Sends a "Sync" Bit\r
+ * _\r
+ * Waveform Protocol 1: | |_______________________________\r
+ * _\r
+ * Waveform Protocol 2: | |__________\r
+ */\r
+void RCSwitch::sendSync() {\r
+\r
+ if (this->nProtocol == 1){\r
+ this->transmit(1,31);\r
+ }\r
+ else if (this->nProtocol == 2) {\r
+ this->transmit(1,10);\r
+ }\r
+}\r
+\r
+/**\r
+ * Enable receiving data\r
+ */\r
+void RCSwitch::enableReceive(int interrupt) {\r
+ this->nReceiverInterrupt = interrupt;\r
+ this->enableReceive();\r
+}\r
+\r
+void RCSwitch::enableReceive() {\r
+ if (this->nReceiverInterrupt != -1) {\r
+ RCSwitch::nReceivedValue = NULL;\r
+ RCSwitch::nReceivedBitlength = NULL;\r
+ attachInterrupt(this->nReceiverInterrupt, handleInterrupt, CHANGE);\r
+ }\r
+}\r
+\r
+/**\r
+ * Disable receiving data\r
+ */\r
+void RCSwitch::disableReceive() {\r
+ detachInterrupt(this->nReceiverInterrupt);\r
+ this->nReceiverInterrupt = -1;\r
+}\r
+\r
+bool RCSwitch::available() {\r
+ return RCSwitch::nReceivedValue != NULL;\r
+}\r
+\r
+void RCSwitch::resetAvailable() {\r
+ RCSwitch::nReceivedValue = NULL;\r
+}\r
+\r
+unsigned long RCSwitch::getReceivedValue() {\r
+ return RCSwitch::nReceivedValue;\r
+}\r
+\r
+unsigned int RCSwitch::getReceivedBitlength() {\r
+ return RCSwitch::nReceivedBitlength;\r
+}\r
+\r
+unsigned int RCSwitch::getReceivedDelay() {\r
+ return RCSwitch::nReceivedDelay;\r
+}\r
+\r
+unsigned int RCSwitch::getReceivedProtocol() {\r
+ return RCSwitch::nReceivedProtocol;\r
+}\r
+\r
+unsigned int* RCSwitch::getReceivedRawdata() {\r
+ return RCSwitch::timings;\r
+}\r
+\r
+/**\r
+ *\r
+ */\r
+bool RCSwitch::receiveProtocol1(unsigned int changeCount){\r
+ \r
+ unsigned long code = 0;\r
+ unsigned long delay = RCSwitch::timings[0] / 31;\r
+ unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; \r
+\r
+ for (int i = 1; i<changeCount ; i=i+2) {\r
+ \r
+ if (RCSwitch::timings[i] > delay-delayTolerance && RCSwitch::timings[i] < delay+delayTolerance && RCSwitch::timings[i+1] > delay*3-delayTolerance && RCSwitch::timings[i+1] < delay*3+delayTolerance) {\r
+ code = code << 1;\r
+ } else if (RCSwitch::timings[i] > delay*3-delayTolerance && RCSwitch::timings[i] < delay*3+delayTolerance && RCSwitch::timings[i+1] > delay-delayTolerance && RCSwitch::timings[i+1] < delay+delayTolerance) {\r
+ code+=1;\r
+ code = code << 1;\r
+ } else {\r
+ // Failed\r
+ i = changeCount;\r
+ code = 0;\r
+ }\r
+ } \r
+ code = code >> 1;\r
+ if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise\r
+ RCSwitch::nReceivedValue = code;\r
+ RCSwitch::nReceivedBitlength = changeCount / 2;\r
+ RCSwitch::nReceivedDelay = delay;\r
+ RCSwitch::nReceivedProtocol = 1;\r
+ }\r
+\r
+ if (code == 0){\r
+ return false;\r
+ }else if (code != 0){\r
+ return true;\r
+ }\r
+ \r
+\r
+}\r
+\r
+bool RCSwitch::receiveProtocol2(unsigned int changeCount){\r
+ \r
+ unsigned long code = 0;\r
+ unsigned long delay = RCSwitch::timings[0] / 10;\r
+ unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; \r
+\r
+ for (int i = 1; i<changeCount ; i=i+2) {\r
+ \r
+ if (RCSwitch::timings[i] > delay-delayTolerance && RCSwitch::timings[i] < delay+delayTolerance && RCSwitch::timings[i+1] > delay*2-delayTolerance && RCSwitch::timings[i+1] < delay*2+delayTolerance) {\r
+ code = code << 1;\r
+ } else if (RCSwitch::timings[i] > delay*2-delayTolerance && RCSwitch::timings[i] < delay*2+delayTolerance && RCSwitch::timings[i+1] > delay-delayTolerance && RCSwitch::timings[i+1] < delay+delayTolerance) {\r
+ code+=1;\r
+ code = code << 1;\r
+ } else {\r
+ // Failed\r
+ i = changeCount;\r
+ code = 0;\r
+ }\r
+ } \r
+ code = code >> 1;\r
+ if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise\r
+ RCSwitch::nReceivedValue = code;\r
+ RCSwitch::nReceivedBitlength = changeCount / 2;\r
+ RCSwitch::nReceivedDelay = delay;\r
+ RCSwitch::nReceivedProtocol = 2;\r
+ }\r
+\r
+ if (code == 0){\r
+ return false;\r
+ }else if (code != 0){\r
+ return true;\r
+ }\r
+\r
+}\r
+void RCSwitch::handleInterrupt() {\r
+\r
+ static unsigned int duration;\r
+ static unsigned int changeCount;\r
+ static unsigned long lastTime;\r
+ static unsigned int repeatCount;\r
+ \r
+\r
+ long time = micros();\r
+ duration = time - lastTime;\r
+ \r
+ if (duration > 5000 && duration > RCSwitch::timings[0] - 200 && duration < RCSwitch::timings[0] + 200) {\r
+ repeatCount++;\r
+ changeCount--;\r
+ if (repeatCount == 2) {\r
+ if (receiveProtocol1(changeCount) == false){\r
+ if (receiveProtocol2(changeCount) == false){\r
+ //failed\r
+ }\r
+ }\r
+ repeatCount = 0;\r
+ }\r
+ changeCount = 0;\r
+ } else if (duration > 5000) {\r
+ changeCount = 0;\r
+ }\r
+ \r
+ if (changeCount >= RCSWITCH_MAX_CHANGES) {\r
+ changeCount = 0;\r
+ repeatCount = 0;\r
+ }\r
+ RCSwitch::timings[changeCount++] = duration;\r
+ lastTime = time; \r
+}\r
+\r
+/**\r
+ * Turns a decimal value to its binary representation\r
+ */\r
+char* RCSwitch::dec2binWzerofill(unsigned long Dec, unsigned int bitLength){\r
+ static char bin[64];\r
+ unsigned int i=0;\r
+\r
+ while (Dec > 0) {\r
+ bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';\r
+ Dec = Dec >> 1;\r
+ }\r
+\r
+ for (unsigned int j = 0; j< bitLength; j++) {\r
+ if (j >= bitLength - i) {\r
+ bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];\r
+ }else {\r
+ bin[j] = '0';\r
+ }\r
+ }\r
+ bin[bitLength] = '\0';\r
+ \r
+ return bin;\r
+}\r
+\r
--- /dev/null
+/*
+ Example for outlets which are configured with two rotary/sliding switches.
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+
+ // Transmitter is connected to Arduino Pin #10
+ mySwitch.enableTransmit(10);
+
+ // Optional set pulse length.
+ // mySwitch.setPulseLength(320);
+
+}
+
+void loop() {
+
+ // Switch on:
+ // The first parameter represents the setting of the first rotary switch.
+ // In this example it's switched to "1" or "A" or "I".
+ //
+ // The second parameter represents the setting of the second rotary switch.
+ // In this example it's switched to "4" or "D" or "IV".
+ mySwitch.switchOn(1, 4);
+
+ // Wait a second
+ delay(1000);
+
+ // Switch off
+ mySwitch.switchOff(1, 4);
+
+ // Wait another second
+ delay(1000);
+
+}
--- /dev/null
+/*
+ Example for different sending methods
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+
+ Serial.begin(9600);
+
+ // Transmitter is connected to Arduino Pin #10
+ mySwitch.enableTransmit(10);
+
+ // Optional set pulse length.
+ // mySwitch.setPulseLength(320);
+
+ // Optional set protocol (default is 1, will work for most outlets)
+ // mySwitch.setProtocol(2);
+
+ // Optional set number of transmission repetitions.
+ // mySwitch.setRepeatTransmit(15);
+
+}
+
+void loop() {
+
+ /* See Example: TypeA_WithDIPSwitches */
+ mySwitch.switchOn("11111", "00010")
+ delay(1000);
+ mySwitch.switchOn("11111", "00010")
+ deleay(1000);
+
+ /* Same switch as above, but using decimal code */
+ mySwitch.send(5393, 24);
+ delay(1000);
+ mySwitch.send(5396, 24);
+ delay(1000);
+
+ /* Same switch as above, but using binary code */
+ mySwitch.send("000000000001010100010001");
+ delay(1000);
+ mySwitch.send("000000000001010100010100");
+ delay(1000);
+
+ /* Same switch as above, but tri-state code */
+ mySwitch.sendTriState("00000FFF0F0F");
+ delay(1000);
+ mySwitch.sendTriState("00000FFF0FF0");
+ delay(1000);
+
+ delay(20000);
+}
--- /dev/null
+void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
+
+ if (decimal == 0) {
+ Serial.print("Unknown encoding.");
+ } else {
+ char* b = dec2binWzerofill(decimal, length);
+ Serial.print("Decimal: ");
+ Serial.print(decimal);
+ Serial.print(" (");
+ Serial.print( length );
+ Serial.print("Bit) Binary: ");
+ Serial.print( b );
+ Serial.print(" Tri-State: ");
+ Serial.print( bin2tristate( b) );
+ Serial.print(" PulseLength: ");
+ Serial.print(delay);
+ Serial.print(" microseconds");
+ Serial.print(" Protocol: ");
+ Serial.println(protocol);
+ }
+
+ Serial.print("Raw data: ");
+ for (int i=0; i<= length*2; i++) {
+ Serial.print(raw[i]);
+ Serial.print(",");
+ }
+ Serial.println();
+ Serial.println();
+}
+
+
+static char* bin2tristate(char* bin) {
+ char returnValue[50];
+ int pos = 0;
+ int pos2 = 0;
+ while (bin[pos]!='\0' && bin[pos+1]!='\0') {
+ if (bin[pos]=='0' && bin[pos+1]=='0') {
+ returnValue[pos2] = '0';
+ } else if (bin[pos]=='1' && bin[pos+1]=='1') {
+ returnValue[pos2] = '1';
+ } else if (bin[pos]=='0' && bin[pos+1]=='1') {
+ returnValue[pos2] = 'F';
+ } else {
+ return "not applicable";
+ }
+ pos = pos+2;
+ pos2++;
+ }
+ returnValue[pos2] = '\0';
+ return returnValue;
+}
+\r
--- /dev/null
+/*
+ Simple example for receiving
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+ Serial.begin(9600);
+ mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
+}
+
+void loop() {
+ if (mySwitch.available()) {
+
+ int value = mySwitch.getReceivedValue();
+
+ if (value == 0) {
+ Serial.print("Unknown encoding");
+ } else {
+ Serial.print("Received ");
+ Serial.print( mySwitch.getReceivedValue() );
+ Serial.print(" / ");
+ Serial.print( mySwitch.getReceivedBitlength() );
+ Serial.print("bit ");
+ Serial.print("Protocol: ");
+ Serial.println( mySwitch.getReceivedProtocol() );
+ }
+
+ mySwitch.resetAvailable();
+ }
+}\r
--- /dev/null
+#######################################\r
+# Syntax Coloring Map For RCSwitch\r
+#######################################\r
+\r
+#######################################\r
+# Datatypes (KEYWORD1)\r
+#######################################\r
+\r
+RCSwitch KEYWORD1\r
+\r
+#######################################\r
+# Methods and Functions (KEYWORD2)\r
+#######################################\r
+\r
+##########\r
+#SENDS Begin\r
+##########\r
+switchOn KEYWORD2\r
+switchOff KEYWORD2\r
+sendTriState KEYWORD2\r
+send KEYWORD2\r
+##########\r
+#SENDS End\r
+##########\r
+\r
+##########\r
+#RECEIVE Begin\r
+##########\r
+enableReceive KEYWORD2\r
+disableReceive KEYWORD2\r
+available KEYWORD2 \r
+resetAvailable KEYWORD2\r
+setReceiveTolerance KEYWORD2\r
+getReceivedValue KEYWORD2\r
+getReceivedBitlength KEYWORD2\r
+getReceivedDelay KEYWORD2\r
+getReceivedProtocol KEYWORD2\r
+getReceivedRawdata KEYWORD2\r
+##########\r
+#RECEIVE End\r
+##########\r
+\r
+##########\r
+#OTHERS Begin\r
+##########\r
+enableTransmit KEYWORD2\r
+disableTransmit KEYWORD2\r
+setPulseLength KEYWORD2\r
+setProtocol KEYWORD2\r
+setRepeatTransmit KEYWORD2\r
+##########\r
+#OTHERS End\r
+##########\r
+\r
+#######################################\r
+# Constants (LITERAL1)\r
+#######################################\r
--- /dev/null
+/*
+ Example for outlets which are configured with a 10 pole DIP switch.
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+
+ // Transmitter is connected to Arduino Pin #10
+ mySwitch.enableTransmit(10);
+
+ // Optional set pulse length.
+ // mySwitch.setPulseLength(320);
+
+}
+
+void loop() {
+
+ // Switch on:
+ // The first parameter represents the setting of the first 5 DIP switches.
+ // In this example it's ON-ON-OFF-OFF-ON.
+ //
+ // The second parameter represents the setting of the last 5 DIP switches.
+ // In this example the last 5 DIP switches are OFF-ON-OFF-ON-OFF.
+ mySwitch.switchOn("11001", "01010");
+
+ // Wait a second
+ delay(1000);
+
+ // Switch off
+ mySwitch.switchOff("11001", "01010");
+
+ // Wait another second
+ delay(1000);
+
+}\r
--- /dev/null
+/*\r
+ RCSwitch - Arduino libary for remote control outlet switches\r
+ Copyright (c) 2011 Suat Özgür. All right reserved.\r
+\r
+ Contributors:\r
+ - Andre Koehler / info(at)tomate-online(dot)de\r
+ - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com\r
+ - Dominik Fischer / dom_fischer(at)web(dot)de\r
+ \r
+ Project home: http://code.google.com/p/rc-switch/\r
+\r
+ This library is free software; you can redistribute it and/or\r
+ modify it under the terms of the GNU Lesser General Public\r
+ License as published by the Free Software Foundation; either\r
+ version 2.1 of the License, or (at your option) any later version.\r
+\r
+ This library is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ Lesser General Public License for more details.\r
+\r
+ You should have received a copy of the GNU Lesser General Public\r
+ License along with this library; if not, write to the Free Software\r
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
+*/\r
+#ifndef _RCSwitch_h\r
+#define _RCSwitch_h\r
+\r
+#if defined(ARDUINO) && ARDUINO >= 100\r
+ #include "Arduino.h"\r
+#else\r
+ #include "WProgram.h"\r
+#endif\r
+\r
+// Number of maximum High/Low changes per packet.\r
+// We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync\r
+#define RCSWITCH_MAX_CHANGES 67\r
+\r
+\r
+class RCSwitch {\r
+\r
+ public:\r
+ RCSwitch();\r
+ \r
+ void switchOn(int nGroupNumber, int nSwitchNumber);\r
+ void switchOff(int nGroupNumber, int nSwitchNumber);\r
+ void switchOn(char* sGroup, int nSwitchNumber);\r
+ void switchOff(char* sGroup, int nSwitchNumber);\r
+ void switchOn(char sFamily, int nGroup, int nDevice);\r
+ void switchOff(char sFamily, int nGroup, int nDevice);\r
+ void switchOn(char* sGroup, char* sDevice);\r
+ void switchOff(char* sGroup, char* sDevice);\r
+\r
+ void sendTriState(char* Code);\r
+ void send(unsigned long Code, unsigned int length);\r
+ void send(char* Code);\r
+ \r
+ void enableReceive(int interrupt);\r
+ void enableReceive();\r
+ void disableReceive();\r
+ bool available();\r
+ void resetAvailable();\r
+ \r
+ unsigned long getReceivedValue();\r
+ unsigned int getReceivedBitlength();\r
+ unsigned int getReceivedDelay();\r
+ unsigned int getReceivedProtocol();\r
+ unsigned int* getReceivedRawdata();\r
+ \r
+ void enableTransmit(int nTransmitterPin);\r
+ void disableTransmit();\r
+ void setPulseLength(int nPulseLength);\r
+ void setRepeatTransmit(int nRepeatTransmit);\r
+ void setReceiveTolerance(int nPercent);\r
+ void setProtocol(int nProtocol);\r
+ void setProtocol(int nProtocol, int nPulseLength);\r
+ \r
+ private:\r
+ char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);\r
+ char* getCodeWordA(char* sGroup, int nSwitchNumber, boolean bStatus);\r
+ char* getCodeWordA(char* sGroup, char* sDevice, boolean bStatus);\r
+ char* getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus);\r
+ void sendT0();\r
+ void sendT1();\r
+ void sendTF();\r
+ void send0();\r
+ void send1();\r
+ void sendSync();\r
+ void transmit(int nHighPulses, int nLowPulses);\r
+\r
+ static char* dec2binWzerofill(unsigned long dec, unsigned int length);\r
+ \r
+ static void handleInterrupt();\r
+ static bool receiveProtocol1(unsigned int changeCount);\r
+ static bool receiveProtocol2(unsigned int changeCount);\r
+ int nReceiverInterrupt;\r
+ int nTransmitterPin;\r
+ int nPulseLength;\r
+ int nRepeatTransmit;\r
+ char nProtocol;\r
+\r
+ static int nReceiveTolerance;\r
+ static unsigned long nReceivedValue;\r
+ static unsigned int nReceivedBitlength;\r
+ static unsigned int nReceivedDelay;\r
+ static unsigned int nReceivedProtocol;\r
+ static unsigned int timings[RCSWITCH_MAX_CHANGES];\r
+\r
+ \r
+};\r
+\r
+#endif\r
--- /dev/null
+/*\r
+ RCSwitch - Arduino libary for remote control outlet switches\r
+ Copyright (c) 2011 Suat Özgür. All right reserved.\r
+ \r
+ Contributors:\r
+ - Andre Koehler / info(at)tomate-online(dot)de\r
+ - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com\r
+ - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46\r
+ - Dominik Fischer / dom_fischer(at)web(dot)de\r
+ \r
+ Project home: http://code.google.com/p/rc-switch/\r
+\r
+ This library is free software; you can redistribute it and/or\r
+ modify it under the terms of the GNU Lesser General Public\r
+ License as published by the Free Software Foundation; either\r
+ version 2.1 of the License, or (at your option) any later version.\r
+\r
+ This library is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ Lesser General Public License for more details.\r
+\r
+ You should have received a copy of the GNU Lesser General Public\r
+ License along with this library; if not, write to the Free Software\r
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
+*/\r
+\r
+#include "RCSwitch.h"\r
+\r
+unsigned long RCSwitch::nReceivedValue = NULL;\r
+unsigned int RCSwitch::nReceivedBitlength = 0;\r
+unsigned int RCSwitch::nReceivedDelay = 0;\r
+unsigned int RCSwitch::nReceivedProtocol = 0;\r
+unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES];\r
+int RCSwitch::nReceiveTolerance = 60;\r
+\r
+RCSwitch::RCSwitch() {\r
+ this->nReceiverInterrupt = -1;\r
+ this->nTransmitterPin = -1;\r
+ RCSwitch::nReceivedValue = NULL;\r
+ this->setPulseLength(350);\r
+ this->setRepeatTransmit(10);\r
+ this->setReceiveTolerance(60);\r
+ this->setProtocol(1);\r
+}\r
+\r
+/**\r
+ * Sets the protocol to send.\r
+ */\r
+void RCSwitch::setProtocol(int nProtocol) {\r
+ this->nProtocol = nProtocol;\r
+ if (nProtocol == 1){\r
+ this->setPulseLength(350);\r
+ }\r
+ else if (nProtocol == 2) {\r
+ this->setPulseLength(650);\r
+ }\r
+}\r
+\r
+/**\r
+ * Sets the protocol to send with pulse length in microseconds.\r
+ */\r
+void RCSwitch::setProtocol(int nProtocol, int nPulseLength) {\r
+ this->nProtocol = nProtocol;\r
+ if (nProtocol == 1){\r
+ this->setPulseLength(nPulseLength);\r
+ }\r
+ else if (nProtocol == 2) {\r
+ this->setPulseLength(nPulseLength);\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ * Sets pulse length in microseconds\r
+ */\r
+void RCSwitch::setPulseLength(int nPulseLength) {\r
+ this->nPulseLength = nPulseLength;\r
+}\r
+\r
+/**\r
+ * Sets Repeat Transmits\r
+ */\r
+void RCSwitch::setRepeatTransmit(int nRepeatTransmit) {\r
+ this->nRepeatTransmit = nRepeatTransmit;\r
+}\r
+\r
+/**\r
+ * Set Receiving Tolerance\r
+ */\r
+void RCSwitch::setReceiveTolerance(int nPercent) {\r
+ RCSwitch::nReceiveTolerance = nPercent;\r
+}\r
+ \r
+\r
+/**\r
+ * Enable transmissions\r
+ *\r
+ * @param nTransmitterPin Arduino Pin to which the sender is connected to\r
+ */\r
+void RCSwitch::enableTransmit(int nTransmitterPin) {\r
+ this->nTransmitterPin = nTransmitterPin;\r
+ pinMode(this->nTransmitterPin, OUTPUT);\r
+}\r
+\r
+/**\r
+ * Disable transmissions\r
+ */\r
+void RCSwitch::disableTransmit() {\r
+ this->nTransmitterPin = -1;\r
+}\r
+\r
+/**\r
+ * Switch a remote switch on (Type C Intertechno)\r
+ *\r
+ * @param sFamily Familycode (a..f)\r
+ * @param nGroup Number of group (1..4)\r
+ * @param nDevice Number of device (1..4)\r
+ */\r
+void RCSwitch::switchOn(char sFamily, int nGroup, int nDevice) {\r
+ this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, true) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch off (Type C Intertechno)\r
+ *\r
+ * @param sFamily Familycode (a..f)\r
+ * @param nGroup Number of group (1..4)\r
+ * @param nDevice Number of device (1..4)\r
+ */\r
+void RCSwitch::switchOff(char sFamily, int nGroup, int nDevice) {\r
+ this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, false) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch on (Type B with two rotary/sliding switches)\r
+ *\r
+ * @param nAddressCode Number of the switch group (1..4)\r
+ * @param nChannelCode Number of the switch itself (1..4)\r
+ */\r
+void RCSwitch::switchOn(int nAddressCode, int nChannelCode) {\r
+ this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, true) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch off (Type B with two rotary/sliding switches)\r
+ *\r
+ * @param nAddressCode Number of the switch group (1..4)\r
+ * @param nChannelCode Number of the switch itself (1..4)\r
+ */\r
+void RCSwitch::switchOff(int nAddressCode, int nChannelCode) {\r
+ this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, false) );\r
+}\r
+\r
+/**\r
+ * Deprecated, use switchOn(char* sGroup, char* sDevice) instead!\r
+ * Switch a remote switch on (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param nChannelCode Number of the switch itself (1..5)\r
+ */\r
+void RCSwitch::switchOn(char* sGroup, int nChannel) {\r
+ char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };\r
+ this->switchOn(sGroup, code[nChannel]);\r
+}\r
+\r
+/**\r
+ * Deprecated, use switchOff(char* sGroup, char* sDevice) instead!\r
+ * Switch a remote switch off (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param nChannelCode Number of the switch itself (1..5)\r
+ */\r
+void RCSwitch::switchOff(char* sGroup, int nChannel) {\r
+ char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };\r
+ this->switchOff(sGroup, code[nChannel]);\r
+}\r
+\r
+/**\r
+ * Switch a remote switch on (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param sDevice Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ */\r
+void RCSwitch::switchOn(char* sGroup, char* sDevice) {\r
+ this->sendTriState( this->getCodeWordA(sGroup, sDevice, true) );\r
+}\r
+\r
+/**\r
+ * Switch a remote switch off (Type A with 10 pole DIP switches)\r
+ *\r
+ * @param sGroup Code of the switch group (refers to DIP switches 1..5 where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ * @param sDevice Code of the switch device (refers to DIP switches 6..10 (A..E) where "1" = on and "0" = off, if all DIP switches are on it's "11111")\r
+ */\r
+void RCSwitch::switchOff(char* sGroup, char* sDevice) {\r
+ this->sendTriState( this->getCodeWordA(sGroup, sDevice, false) );\r
+}\r
+\r
+/**\r
+ * Returns a char[13], representing the Code Word to be send.\r
+ * A Code Word consists of 9 address bits, 3 data bits and one sync bit but in our case only the first 8 address bits and the last 2 data bits were used.\r
+ * A Code Bit can have 4 different states: "F" (floating), "0" (low), "1" (high), "S" (synchronous bit)\r
+ *\r
+ * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+\r
+ * | 4 bits address (switch group) | 4 bits address (switch number) | 1 bit address (not used, so never mind) | 1 bit address (not used, so never mind) | 2 data bits (on|off) | 1 sync bit |\r
+ * | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | F | F | on=FF off=F0 | S |\r
+ * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+\r
+ *\r
+ * @param nAddressCode Number of the switch group (1..4)\r
+ * @param nChannelCode Number of the switch itself (1..4)\r
+ * @param bStatus Wether to switch on (true) or off (false)\r
+ *\r
+ * @return char[13]\r
+ */\r
+char* RCSwitch::getCodeWordB(int nAddressCode, int nChannelCode, boolean bStatus) {\r
+ int nReturnPos = 0;\r
+ static char sReturn[13];\r
+ \r
+ char* code[5] = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" };\r
+ if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) {\r
+ return '\0';\r
+ }\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = code[nAddressCode][i];\r
+ }\r
+\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = code[nChannelCode][i];\r
+ }\r
+ \r
+ sReturn[nReturnPos++] = 'F';\r
+ sReturn[nReturnPos++] = 'F';\r
+ sReturn[nReturnPos++] = 'F';\r
+ \r
+ if (bStatus) {\r
+ sReturn[nReturnPos++] = 'F';\r
+ } else {\r
+ sReturn[nReturnPos++] = '0';\r
+ }\r
+ \r
+ sReturn[nReturnPos] = '\0';\r
+ \r
+ return sReturn;\r
+}\r
+\r
+/**\r
+ * Returns a char[13], representing the Code Word to be send.\r
+ *\r
+ * getCodeWordA(char*, char*)\r
+ *\r
+ */\r
+char* RCSwitch::getCodeWordA(char* sGroup, char* sDevice, boolean bOn) {\r
+ static char sDipSwitches[13];\r
+ int i = 0;\r
+ int j = 0;\r
+ \r
+ for (i=0; i < 5; i++) {\r
+ if (sGroup[i] == '0') {\r
+ sDipSwitches[j++] = 'F';\r
+ } else {\r
+ sDipSwitches[j++] = '0';\r
+ }\r
+ }\r
+\r
+ for (i=0; i < 5; i++) {\r
+ if (sDevice[i] == '0') {\r
+ sDipSwitches[j++] = 'F';\r
+ } else {\r
+ sDipSwitches[j++] = '0';\r
+ }\r
+ }\r
+\r
+ if ( bOn ) {\r
+ sDipSwitches[j++] = '0';\r
+ sDipSwitches[j++] = 'F';\r
+ } else {\r
+ sDipSwitches[j++] = 'F';\r
+ sDipSwitches[j++] = '0';\r
+ }\r
+\r
+ sDipSwitches[j] = '\0';\r
+\r
+ return sDipSwitches;\r
+}\r
+\r
+/**\r
+ * Like getCodeWord (Type C = Intertechno)\r
+ */\r
+char* RCSwitch::getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus) {\r
+ static char sReturn[13];\r
+ int nReturnPos = 0;\r
+ \r
+ if ( (byte)sFamily < 97 || (byte)sFamily > 112 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {\r
+ return '\0';\r
+ }\r
+ \r
+ char* sDeviceGroupCode = dec2binWzerofill( (nDevice-1) + (nGroup-1)*4, 4 );\r
+ char familycode[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = familycode[ (int)sFamily - 97 ][i];\r
+ }\r
+ for (int i = 0; i<4; i++) {\r
+ sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0');\r
+ }\r
+ sReturn[nReturnPos++] = '0';\r
+ sReturn[nReturnPos++] = 'F';\r
+ sReturn[nReturnPos++] = 'F';\r
+ if (bStatus) {\r
+ sReturn[nReturnPos++] = 'F';\r
+ } else {\r
+ sReturn[nReturnPos++] = '0';\r
+ }\r
+ sReturn[nReturnPos] = '\0';\r
+ return sReturn;\r
+}\r
+\r
+/**\r
+ * Sends a Code Word\r
+ * @param sCodeWord /^[10FS]*$/ -> see getCodeWord\r
+ */\r
+void RCSwitch::sendTriState(char* sCodeWord) {\r
+ for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {\r
+ int i = 0;\r
+ while (sCodeWord[i] != '\0') {\r
+ switch(sCodeWord[i]) {\r
+ case '0':\r
+ this->sendT0();\r
+ break;\r
+ case 'F':\r
+ this->sendTF();\r
+ break;\r
+ case '1':\r
+ this->sendT1();\r
+ break;\r
+ }\r
+ i++;\r
+ }\r
+ this->sendSync(); \r
+ }\r
+}\r
+\r
+void RCSwitch::send(unsigned long Code, unsigned int length) {\r
+ this->send( this->dec2binWzerofill(Code, length) );\r
+}\r
+\r
+void RCSwitch::send(char* sCodeWord) {\r
+ for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {\r
+ int i = 0;\r
+ while (sCodeWord[i] != '\0') {\r
+ switch(sCodeWord[i]) {\r
+ case '0':\r
+ this->send0();\r
+ break;\r
+ case '1':\r
+ this->send1();\r
+ break;\r
+ }\r
+ i++;\r
+ }\r
+ this->sendSync();\r
+ }\r
+}\r
+\r
+void RCSwitch::transmit(int nHighPulses, int nLowPulses) {\r
+ boolean disabled_Receive = false;\r
+ int nReceiverInterrupt_backup = nReceiverInterrupt;\r
+ if (this->nTransmitterPin != -1) {\r
+ if (this->nReceiverInterrupt != -1) {\r
+ this->disableReceive();\r
+ disabled_Receive = true;\r
+ }\r
+ digitalWrite(this->nTransmitterPin, HIGH);\r
+ delayMicroseconds( this->nPulseLength * nHighPulses);\r
+ digitalWrite(this->nTransmitterPin, LOW);\r
+ delayMicroseconds( this->nPulseLength * nLowPulses);\r
+ if(disabled_Receive){\r
+ this->enableReceive(nReceiverInterrupt_backup);\r
+ }\r
+ }\r
+}\r
+/**\r
+ * Sends a "0" Bit\r
+ * _ \r
+ * Waveform Protocol 1: | |___\r
+ * _ \r
+ * Waveform Protocol 2: | |__\r
+ */\r
+void RCSwitch::send0() {\r
+ if (this->nProtocol == 1){\r
+ this->transmit(1,3);\r
+ }\r
+ else if (this->nProtocol == 2) {\r
+ this->transmit(1,2);\r
+ }\r
+}\r
+\r
+/**\r
+ * Sends a "1" Bit\r
+ * ___ \r
+ * Waveform Protocol 1: | |_\r
+ * __ \r
+ * Waveform Protocol 2: | |_\r
+ */\r
+void RCSwitch::send1() {\r
+ if (this->nProtocol == 1){\r
+ this->transmit(3,1);\r
+ }\r
+ else if (this->nProtocol == 2) {\r
+ this->transmit(2,1);\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ * Sends a Tri-State "0" Bit\r
+ * _ _\r
+ * Waveform: | |___| |___\r
+ */\r
+void RCSwitch::sendT0() {\r
+ this->transmit(1,3);\r
+ this->transmit(1,3);\r
+}\r
+\r
+/**\r
+ * Sends a Tri-State "1" Bit\r
+ * ___ ___\r
+ * Waveform: | |_| |_\r
+ */\r
+void RCSwitch::sendT1() {\r
+ this->transmit(3,1);\r
+ this->transmit(3,1);\r
+}\r
+\r
+/**\r
+ * Sends a Tri-State "F" Bit\r
+ * _ ___\r
+ * Waveform: | |___| |_\r
+ */\r
+void RCSwitch::sendTF() {\r
+ this->transmit(1,3);\r
+ this->transmit(3,1);\r
+}\r
+\r
+/**\r
+ * Sends a "Sync" Bit\r
+ * _\r
+ * Waveform Protocol 1: | |_______________________________\r
+ * _\r
+ * Waveform Protocol 2: | |__________\r
+ */\r
+void RCSwitch::sendSync() {\r
+\r
+ if (this->nProtocol == 1){\r
+ this->transmit(1,31);\r
+ }\r
+ else if (this->nProtocol == 2) {\r
+ this->transmit(1,10);\r
+ }\r
+}\r
+\r
+/**\r
+ * Enable receiving data\r
+ */\r
+void RCSwitch::enableReceive(int interrupt) {\r
+ this->nReceiverInterrupt = interrupt;\r
+ this->enableReceive();\r
+}\r
+\r
+void RCSwitch::enableReceive() {\r
+ if (this->nReceiverInterrupt != -1) {\r
+ RCSwitch::nReceivedValue = NULL;\r
+ RCSwitch::nReceivedBitlength = NULL;\r
+ attachInterrupt(this->nReceiverInterrupt, handleInterrupt, CHANGE);\r
+ }\r
+}\r
+\r
+/**\r
+ * Disable receiving data\r
+ */\r
+void RCSwitch::disableReceive() {\r
+ detachInterrupt(this->nReceiverInterrupt);\r
+ this->nReceiverInterrupt = -1;\r
+}\r
+\r
+bool RCSwitch::available() {\r
+ return RCSwitch::nReceivedValue != NULL;\r
+}\r
+\r
+void RCSwitch::resetAvailable() {\r
+ RCSwitch::nReceivedValue = NULL;\r
+}\r
+\r
+unsigned long RCSwitch::getReceivedValue() {\r
+ return RCSwitch::nReceivedValue;\r
+}\r
+\r
+unsigned int RCSwitch::getReceivedBitlength() {\r
+ return RCSwitch::nReceivedBitlength;\r
+}\r
+\r
+unsigned int RCSwitch::getReceivedDelay() {\r
+ return RCSwitch::nReceivedDelay;\r
+}\r
+\r
+unsigned int RCSwitch::getReceivedProtocol() {\r
+ return RCSwitch::nReceivedProtocol;\r
+}\r
+\r
+unsigned int* RCSwitch::getReceivedRawdata() {\r
+ return RCSwitch::timings;\r
+}\r
+\r
+/**\r
+ *\r
+ */\r
+bool RCSwitch::receiveProtocol1(unsigned int changeCount){\r
+ \r
+ unsigned long code = 0;\r
+ unsigned long delay = RCSwitch::timings[0] / 31;\r
+ unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; \r
+\r
+ for (int i = 1; i<changeCount ; i=i+2) {\r
+ \r
+ if (RCSwitch::timings[i] > delay-delayTolerance && RCSwitch::timings[i] < delay+delayTolerance && RCSwitch::timings[i+1] > delay*3-delayTolerance && RCSwitch::timings[i+1] < delay*3+delayTolerance) {\r
+ code = code << 1;\r
+ } else if (RCSwitch::timings[i] > delay*3-delayTolerance && RCSwitch::timings[i] < delay*3+delayTolerance && RCSwitch::timings[i+1] > delay-delayTolerance && RCSwitch::timings[i+1] < delay+delayTolerance) {\r
+ code+=1;\r
+ code = code << 1;\r
+ } else {\r
+ // Failed\r
+ i = changeCount;\r
+ code = 0;\r
+ }\r
+ } \r
+ code = code >> 1;\r
+ if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise\r
+ RCSwitch::nReceivedValue = code;\r
+ RCSwitch::nReceivedBitlength = changeCount / 2;\r
+ RCSwitch::nReceivedDelay = delay;\r
+ RCSwitch::nReceivedProtocol = 1;\r
+ }\r
+\r
+ if (code == 0){\r
+ return false;\r
+ }else if (code != 0){\r
+ return true;\r
+ }\r
+ \r
+\r
+}\r
+\r
+bool RCSwitch::receiveProtocol2(unsigned int changeCount){\r
+ \r
+ unsigned long code = 0;\r
+ unsigned long delay = RCSwitch::timings[0] / 10;\r
+ unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; \r
+\r
+ for (int i = 1; i<changeCount ; i=i+2) {\r
+ \r
+ if (RCSwitch::timings[i] > delay-delayTolerance && RCSwitch::timings[i] < delay+delayTolerance && RCSwitch::timings[i+1] > delay*2-delayTolerance && RCSwitch::timings[i+1] < delay*2+delayTolerance) {\r
+ code = code << 1;\r
+ } else if (RCSwitch::timings[i] > delay*2-delayTolerance && RCSwitch::timings[i] < delay*2+delayTolerance && RCSwitch::timings[i+1] > delay-delayTolerance && RCSwitch::timings[i+1] < delay+delayTolerance) {\r
+ code+=1;\r
+ code = code << 1;\r
+ } else {\r
+ // Failed\r
+ i = changeCount;\r
+ code = 0;\r
+ }\r
+ } \r
+ code = code >> 1;\r
+ if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise\r
+ RCSwitch::nReceivedValue = code;\r
+ RCSwitch::nReceivedBitlength = changeCount / 2;\r
+ RCSwitch::nReceivedDelay = delay;\r
+ RCSwitch::nReceivedProtocol = 2;\r
+ }\r
+\r
+ if (code == 0){\r
+ return false;\r
+ }else if (code != 0){\r
+ return true;\r
+ }\r
+\r
+}\r
+void RCSwitch::handleInterrupt() {\r
+\r
+ static unsigned int duration;\r
+ static unsigned int changeCount;\r
+ static unsigned long lastTime;\r
+ static unsigned int repeatCount;\r
+ \r
+\r
+ long time = micros();\r
+ duration = time - lastTime;\r
+ \r
+ if (duration > 5000 && duration > RCSwitch::timings[0] - 200 && duration < RCSwitch::timings[0] + 200) {\r
+ repeatCount++;\r
+ changeCount--;\r
+ if (repeatCount == 2) {\r
+ if (receiveProtocol1(changeCount) == false){\r
+ if (receiveProtocol2(changeCount) == false){\r
+ //failed\r
+ }\r
+ }\r
+ repeatCount = 0;\r
+ }\r
+ changeCount = 0;\r
+ } else if (duration > 5000) {\r
+ changeCount = 0;\r
+ }\r
+ \r
+ if (changeCount >= RCSWITCH_MAX_CHANGES) {\r
+ changeCount = 0;\r
+ repeatCount = 0;\r
+ }\r
+ RCSwitch::timings[changeCount++] = duration;\r
+ lastTime = time; \r
+}\r
+\r
+/**\r
+ * Turns a decimal value to its binary representation\r
+ */\r
+char* RCSwitch::dec2binWzerofill(unsigned long Dec, unsigned int bitLength){\r
+ static char bin[64];\r
+ unsigned int i=0;\r
+\r
+ while (Dec > 0) {\r
+ bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';\r
+ Dec = Dec >> 1;\r
+ }\r
+\r
+ for (unsigned int j = 0; j< bitLength; j++) {\r
+ if (j >= bitLength - i) {\r
+ bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];\r
+ }else {\r
+ bin[j] = '0';\r
+ }\r
+ }\r
+ bin[bitLength] = '\0';\r
+ \r
+ return bin;\r
+}\r
+\r
--- /dev/null
+/*\r
+ RCSwitch - Arduino libary for remote control outlet switches\r
+ Copyright (c) 2011 Suat Özgür. All right reserved.\r
+\r
+ Contributors:\r
+ - Andre Koehler / info(at)tomate-online(dot)de\r
+ - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com\r
+ - Dominik Fischer / dom_fischer(at)web(dot)de\r
+ \r
+ Project home: http://code.google.com/p/rc-switch/\r
+\r
+ This library is free software; you can redistribute it and/or\r
+ modify it under the terms of the GNU Lesser General Public\r
+ License as published by the Free Software Foundation; either\r
+ version 2.1 of the License, or (at your option) any later version.\r
+\r
+ This library is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ Lesser General Public License for more details.\r
+\r
+ You should have received a copy of the GNU Lesser General Public\r
+ License along with this library; if not, write to the Free Software\r
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
+*/\r
+#ifndef _RCSwitch_h\r
+#define _RCSwitch_h\r
+\r
+#if defined(ARDUINO) && ARDUINO >= 100\r
+ #include "Arduino.h"\r
+#else\r
+ #include "WProgram.h"\r
+#endif\r
+\r
+// Number of maximum High/Low changes per packet.\r
+// We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync\r
+#define RCSWITCH_MAX_CHANGES 67\r
+\r
+\r
+class RCSwitch {\r
+\r
+ public:\r
+ RCSwitch();\r
+ \r
+ void switchOn(int nGroupNumber, int nSwitchNumber);\r
+ void switchOff(int nGroupNumber, int nSwitchNumber);\r
+ void switchOn(char* sGroup, int nSwitchNumber);\r
+ void switchOff(char* sGroup, int nSwitchNumber);\r
+ void switchOn(char sFamily, int nGroup, int nDevice);\r
+ void switchOff(char sFamily, int nGroup, int nDevice);\r
+ void switchOn(char* sGroup, char* sDevice);\r
+ void switchOff(char* sGroup, char* sDevice);\r
+\r
+ void sendTriState(char* Code);\r
+ void send(unsigned long Code, unsigned int length);\r
+ void send(char* Code);\r
+ \r
+ void enableReceive(int interrupt);\r
+ void enableReceive();\r
+ void disableReceive();\r
+ bool available();\r
+ void resetAvailable();\r
+ \r
+ unsigned long getReceivedValue();\r
+ unsigned int getReceivedBitlength();\r
+ unsigned int getReceivedDelay();\r
+ unsigned int getReceivedProtocol();\r
+ unsigned int* getReceivedRawdata();\r
+ \r
+ void enableTransmit(int nTransmitterPin);\r
+ void disableTransmit();\r
+ void setPulseLength(int nPulseLength);\r
+ void setRepeatTransmit(int nRepeatTransmit);\r
+ void setReceiveTolerance(int nPercent);\r
+ void setProtocol(int nProtocol);\r
+ void setProtocol(int nProtocol, int nPulseLength);\r
+ \r
+ private:\r
+ char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);\r
+ char* getCodeWordA(char* sGroup, int nSwitchNumber, boolean bStatus);\r
+ char* getCodeWordA(char* sGroup, char* sDevice, boolean bStatus);\r
+ char* getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus);\r
+ void sendT0();\r
+ void sendT1();\r
+ void sendTF();\r
+ void send0();\r
+ void send1();\r
+ void sendSync();\r
+ void transmit(int nHighPulses, int nLowPulses);\r
+\r
+ static char* dec2binWzerofill(unsigned long dec, unsigned int length);\r
+ \r
+ static void handleInterrupt();\r
+ static bool receiveProtocol1(unsigned int changeCount);\r
+ static bool receiveProtocol2(unsigned int changeCount);\r
+ int nReceiverInterrupt;\r
+ int nTransmitterPin;\r
+ int nPulseLength;\r
+ int nRepeatTransmit;\r
+ char nProtocol;\r
+\r
+ static int nReceiveTolerance;\r
+ static unsigned long nReceivedValue;\r
+ static unsigned int nReceivedBitlength;\r
+ static unsigned int nReceivedDelay;\r
+ static unsigned int nReceivedProtocol;\r
+ static unsigned int timings[RCSWITCH_MAX_CHANGES];\r
+\r
+ \r
+};\r
+\r
+#endif\r
--- /dev/null
+/*
+ Example for receiving
+
+ http://code.google.com/p/rc-switch/
+
+ If you want to visualize a telegram copy the raw data and
+ paste it into http://test.sui.li/oszi/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+ Serial.begin(9600);
+ mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
+}
+
+void loop() {
+ if (mySwitch.available()) {
+ output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
+ mySwitch.resetAvailable();
+ }
+}\r
--- /dev/null
+static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength){
+ static char bin[64];
+ unsigned int i=0;
+
+ while (Dec > 0) {
+ bin[32+i++] = (Dec & 1 > 0) ? '1' : '0';
+ Dec = Dec >> 1;
+ }
+
+ for (unsigned int j = 0; j< bitLength; j++) {
+ if (j >= bitLength - i) {
+ bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
+ }else {
+ bin[j] = '0';
+ }
+ }
+ bin[bitLength] = '\0';
+
+ return bin;
+}\r
--- /dev/null
+void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
+
+ if (decimal == 0) {
+ Serial.print("Unknown encoding.");
+ } else {
+ char* b = dec2binWzerofill(decimal, length);
+ Serial.print("Decimal: ");
+ Serial.print(decimal);
+ Serial.print(" (");
+ Serial.print( length );
+ Serial.print("Bit) Binary: ");
+ Serial.print( b );
+ Serial.print(" Tri-State: ");
+ Serial.print( bin2tristate( b) );
+ Serial.print(" PulseLength: ");
+ Serial.print(delay);
+ Serial.print(" microseconds");
+ Serial.print(" Protocol: ");
+ Serial.println(protocol);
+ }
+
+ Serial.print("Raw data: ");
+ for (int i=0; i<= length*2; i++) {
+ Serial.print(raw[i]);
+ Serial.print(",");
+ }
+ Serial.println();
+ Serial.println();
+}
+
+
+static char* bin2tristate(char* bin) {
+ char returnValue[50];
+ int pos = 0;
+ int pos2 = 0;
+ while (bin[pos]!='\0' && bin[pos+1]!='\0') {
+ if (bin[pos]=='0' && bin[pos+1]=='0') {
+ returnValue[pos2] = '0';
+ } else if (bin[pos]=='1' && bin[pos+1]=='1') {
+ returnValue[pos2] = '1';
+ } else if (bin[pos]=='0' && bin[pos+1]=='1') {
+ returnValue[pos2] = 'F';
+ } else {
+ return "not applicable";
+ }
+ pos = pos+2;
+ pos2++;
+ }
+ returnValue[pos2] = '\0';
+ return returnValue;
+}
+\r
--- /dev/null
+/*
+ Simple example for receiving
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+ Serial.begin(9600);
+ mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
+}
+
+void loop() {
+ if (mySwitch.available()) {
+
+ int value = mySwitch.getReceivedValue();
+
+ if (value == 0) {
+ Serial.print("Unknown encoding");
+ } else {
+ Serial.print("Received ");
+ Serial.print( mySwitch.getReceivedValue() );
+ Serial.print(" / ");
+ Serial.print( mySwitch.getReceivedBitlength() );
+ Serial.print("bit ");
+ Serial.print("Protocol: ");
+ Serial.println( mySwitch.getReceivedProtocol() );
+ }
+
+ mySwitch.resetAvailable();
+ }
+}\r
--- /dev/null
+/*
+ Example for different sending methods
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+
+ Serial.begin(9600);
+
+ // Transmitter is connected to Arduino Pin #10
+ mySwitch.enableTransmit(10);
+
+ // Optional set pulse length.
+ // mySwitch.setPulseLength(320);
+
+ // Optional set protocol (default is 1, will work for most outlets)
+ // mySwitch.setProtocol(2);
+
+ // Optional set number of transmission repetitions.
+ // mySwitch.setRepeatTransmit(15);
+
+}
+
+void loop() {
+
+ /* See Example: TypeA_WithDIPSwitches */
+ mySwitch.switchOn("11111", "00010")
+ delay(1000);
+ mySwitch.switchOn("11111", "00010")
+ deleay(1000);
+
+ /* Same switch as above, but using decimal code */
+ mySwitch.send(5393, 24);
+ delay(1000);
+ mySwitch.send(5396, 24);
+ delay(1000);
+
+ /* Same switch as above, but using binary code */
+ mySwitch.send("000000000001010100010001");
+ delay(1000);
+ mySwitch.send("000000000001010100010100");
+ delay(1000);
+
+ /* Same switch as above, but tri-state code */
+ mySwitch.sendTriState("00000FFF0F0F");
+ delay(1000);
+ mySwitch.sendTriState("00000FFF0FF0");
+ delay(1000);
+
+ delay(20000);
+}
--- /dev/null
+/*
+ Example for outlets which are configured with a 10 pole DIP switch.
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+
+ // Transmitter is connected to Arduino Pin #10
+ mySwitch.enableTransmit(10);
+
+ // Optional set pulse length.
+ // mySwitch.setPulseLength(320);
+
+}
+
+void loop() {
+
+ // Switch on:
+ // The first parameter represents the setting of the first 5 DIP switches.
+ // In this example it's ON-ON-OFF-OFF-ON.
+ //
+ // The second parameter represents the setting of the last 5 DIP switches.
+ // In this example the last 5 DIP switches are OFF-ON-OFF-ON-OFF.
+ mySwitch.switchOn("11001", "01010");
+
+ // Wait a second
+ delay(1000);
+
+ // Switch off
+ mySwitch.switchOff("11001", "01010");
+
+ // Wait another second
+ delay(1000);
+
+}\r
--- /dev/null
+/*
+ Example for outlets which are configured with two rotary/sliding switches.
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <RCSwitch.h>
+
+RCSwitch mySwitch = RCSwitch();
+
+void setup() {
+
+ // Transmitter is connected to Arduino Pin #10
+ mySwitch.enableTransmit(10);
+
+ // Optional set pulse length.
+ // mySwitch.setPulseLength(320);
+
+}
+
+void loop() {
+
+ // Switch on:
+ // The first parameter represents the setting of the first rotary switch.
+ // In this example it's switched to "1" or "A" or "I".
+ //
+ // The second parameter represents the setting of the second rotary switch.
+ // In this example it's switched to "4" or "D" or "IV".
+ mySwitch.switchOn(1, 4);
+
+ // Wait a second
+ delay(1000);
+
+ // Switch off
+ mySwitch.switchOff(1, 4);
+
+ // Wait another second
+ delay(1000);
+
+}
--- /dev/null
+/*\r
+ Example for Intertechno outlets\r
+ \r
+ http://code.google.com/p/rc-switch/\r
+*/\r
+\r
+#include <RCSwitch.h>\r
+\r
+RCSwitch mySwitch = RCSwitch();\r
+\r
+void setup() {\r
+\r
+ // Transmitter is connected to Arduino Pin #10 \r
+ mySwitch.enableTransmit(10);\r
+ \r
+ // Optional set pulse length.\r
+ // mySwitch.setPulseLength(320);\r
+ \r
+}\r
+\r
+void loop() {\r
+\r
+ // Switch on:\r
+ // The first parameter represents the familycode (a, b, c, ... f)\r
+ // The second parameter represents the group number\r
+ // The third parameter represents the device number\r
+ // \r
+ // In this example it's family 'b', group #3, device #2 \r
+ mySwitch.switchOn('b', 3, 2);\r
+\r
+ // Wait a second\r
+ delay(1000);\r
+ \r
+ // Switch off\r
+ mySwitch.switchOff('b', 3, 2);\r
+ \r
+ // Wait another second\r
+ delay(1000);\r
+ \r
+}
\ No newline at end of file
--- /dev/null
+/*
+ A simple RCSwitch/Ethernet/Webserver demo
+
+ http://code.google.com/p/rc-switch/
+*/
+
+#include <SPI.h>
+#include <Ethernet.h>
+#include <RCSwitch.h>
+
+// Ethernet configuration
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
+byte ip[] = { 192,168,0, 2 }; // IP Address
+EthernetServer server(80); // Server Port 80
+
+// RCSwitch configuration
+RCSwitch mySwitch = RCSwitch();
+int RCTransmissionPin = 7;
+
+// More to do...
+// You should also modify the processCommand() and
+// httpResponseHome() functions to fit your needs.
+
+
+
+/**
+ * Setup
+ */
+void setup() {
+ Ethernet.begin(mac, ip);
+ server.begin();
+ mySwitch.enableTransmit( RCTransmissionPin );
+}
+
+/**
+ * Loop
+ */
+void loop() {
+ char* command = httpServer();
+}
+
+/**
+ * Command dispatcher
+ */
+void processCommand(char* command) {
+ if (strcmp(command, "1-on") == 0) {
+ mySwitch.switchOn(1,1);
+ } else if (strcmp(command, "1-off") == 0) {
+ mySwitch.switchOff(1,1);
+ } else if (strcmp(command, "2-on") == 0) {
+ mySwitch.switchOn(1,2);
+ } else if (strcmp(command, "2-off") == 0) {
+ mySwitch.switchOff(1,2);
+ }
+}
+
+/**
+ * HTTP Response with homepage
+ */
+void httpResponseHome(EthernetClient c) {
+ c.println("HTTP/1.1 200 OK");
+ c.println("Content-Type: text/html");
+ c.println();
+ c.println("<html>");
+ c.println("<head>");
+ c.println( "<title>RCSwitch Webserver Demo</title>");
+ c.println( "<style>");
+ c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
+ c.println( "</style>");
+ c.println("</head>");
+ c.println("<body>");
+ c.println( "<h1>RCSwitch Webserver Demo</h1>");
+ c.println( "<ul>");
+ c.println( "<li><a href=\"./?1-on\">Switch #1 on</a></li>");
+ c.println( "<li><a href=\"./?1-off\">Switch #1 off</a></li>");
+ c.println( "</ul>");
+ c.println( "<ul>");
+ c.println( "<li><a href=\"./?2-on\">Switch #2 on</a></li>");
+ c.println( "<li><a href=\"./?2-off\">Switch #2 off</a></li>");
+ c.println( "</ul>");
+ c.println( "<hr>");
+ c.println( "<a href=\"http://code.google.com/p/rc-switch/\">http://code.google.com/p/rc-switch/</a>");
+ c.println("</body>");
+ c.println("</html>");
+}
+
+/**
+ * HTTP Redirect to homepage
+ */
+void httpResponseRedirect(EthernetClient c) {
+ c.println("HTTP/1.1 301 Found");
+ c.println("Location: /");
+ c.println();
+}
+
+/**
+ * HTTP Response 414 error
+ * Command must not be longer than 30 characters
+ **/
+void httpResponse414(EthernetClient c) {
+ c.println("HTTP/1.1 414 Request URI too long");
+ c.println("Content-Type: text/plain");
+ c.println();
+ c.println("414 Request URI too long");
+}
+
+/**
+ * Process HTTP requests, parse first request header line and
+ * call processCommand with GET query string (everything after
+ * the ? question mark in the URL).
+ */
+char* httpServer() {
+ EthernetClient client = server.available();
+ if (client) {
+ char sReturnCommand[32];
+ int nCommandPos=-1;
+ sReturnCommand[0] = '\0';
+ while (client.connected()) {
+ if (client.available()) {
+ char c = client.read();
+ if ((c == '\n') || (c == ' ' && nCommandPos>-1)) {
+ sReturnCommand[nCommandPos] = '\0';
+ if (strcmp(sReturnCommand, "\0") == 0) {
+ httpResponseHome(client);
+ } else {
+ processCommand(sReturnCommand);
+ httpResponseRedirect(client);
+ }
+ break;
+ }
+ if (nCommandPos>-1) {
+ sReturnCommand[nCommandPos++] = c;
+ }
+ if (c == '?' && nCommandPos == -1) {
+ nCommandPos = 0;
+ }
+ }
+ if (nCommandPos > 30) {
+ httpResponse414(client);
+ sReturnCommand[0] = '\0';
+ break;
+ }
+ }
+ if (nCommandPos!=-1) {
+ sReturnCommand[nCommandPos] = '\0';
+ }
+ // give the web browser time to receive the data
+ delay(1);
+ client.stop();
+
+ return sReturnCommand;
+ }
+ return '\0';
+}
\ No newline at end of file
--- /dev/null
+#######################################\r
+# Syntax Coloring Map For RCSwitch\r
+#######################################\r
+\r
+#######################################\r
+# Datatypes (KEYWORD1)\r
+#######################################\r
+\r
+RCSwitch KEYWORD1\r
+\r
+#######################################\r
+# Methods and Functions (KEYWORD2)\r
+#######################################\r
+\r
+##########\r
+#SENDS Begin\r
+##########\r
+switchOn KEYWORD2\r
+switchOff KEYWORD2\r
+sendTriState KEYWORD2\r
+send KEYWORD2\r
+##########\r
+#SENDS End\r
+##########\r
+\r
+##########\r
+#RECEIVE Begin\r
+##########\r
+enableReceive KEYWORD2\r
+disableReceive KEYWORD2\r
+available KEYWORD2 \r
+resetAvailable KEYWORD2\r
+setReceiveTolerance KEYWORD2\r
+getReceivedValue KEYWORD2\r
+getReceivedBitlength KEYWORD2\r
+getReceivedDelay KEYWORD2\r
+getReceivedProtocol KEYWORD2\r
+getReceivedRawdata KEYWORD2\r
+##########\r
+#RECEIVE End\r
+##########\r
+\r
+##########\r
+#OTHERS Begin\r
+##########\r
+enableTransmit KEYWORD2\r
+disableTransmit KEYWORD2\r
+setPulseLength KEYWORD2\r
+setProtocol KEYWORD2\r
+setRepeatTransmit KEYWORD2\r
+##########\r
+#OTHERS End\r
+##########\r
+\r
+#######################################\r
+# Constants (LITERAL1)\r
+#######################################\r