Advertisement
Guest User

rd-switch spark core

a guest
Dec 26th, 2013
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.07 KB | None | 0 0
  1. /* ======================= .h =============================== */
  2.  
  3. /*
  4.   RCSwitch - Arduino libary for remote control outlet switches
  5.   Copyright (c) 2011 Suat Özgür.  All right reserved.
  6.  
  7.   Contributors:
  8.   - Andre Koehler / info(at)tomate-online(dot)de
  9.   - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com
  10.   - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46
  11.   - Dominik Fischer / dom_fischer(at)web(dot)de
  12.   - Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com
  13.  
  14.   Project home: http://code.google.com/p/rc-switch/
  15.  
  16.   This library is free software; you can redistribute it and/or
  17.   modify it under the terms of the GNU Lesser General Public
  18.   License as published by the Free Software Foundation; either
  19.   version 2.1 of the License, or (at your option) any later version.
  20.  
  21.   This library is distributed in the hope that it will be useful,
  22.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  24.   Lesser General Public License for more details.
  25.  
  26.   You should have received a copy of the GNU Lesser General Public
  27.   License along with this library; if not, write to the Free Software
  28.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  29. */
  30. #ifndef _RCSwitch_h
  31. #define _RCSwitch_h
  32.  
  33.  
  34. // https://community.sparkdevices.com/t/fix-for-include-arduino-h/953
  35.  
  36.     #define ARDUINO_H
  37.     #include <stdint.h>
  38.     #include <stddef.h>
  39.     #include <stdlib.h>
  40.    
  41. // to make it compile (by Frido)
  42.  
  43.     #define boolean bool
  44.     #pragma GCC diagnostic ignored "-Wwrite-strings"
  45.     #pragma GCC diagnostic ignored "-Wconversion-null"
  46.     #pragma GCC diagnostic ignored "-Wpointer-arith"
  47.     #pragma GCC diagnostic ignored "-Wsign-compare"
  48.     #pragma GCC diagnostic ignored "-Wreturn-type"
  49.    
  50. /*
  51. #if defined(ARDUINO) && ARDUINO >= 100
  52.     #include "Arduino.h"
  53.    
  54. #elif defined(ENERGIA) // LaunchPad, FraunchPad and StellarPad specific
  55.     #include "Energia.h"   
  56. #else
  57.     #include "WProgram.h"
  58. #endif
  59.  
  60.  
  61.  
  62. // At least for the ATTiny X4/X5, receiving has to be disabled due to
  63. // missing libm depencies (udivmodhi4)
  64. #if defined( __AVR_ATtinyX5__ ) or defined ( __AVR_ATtinyX4__ )
  65. #define RCSwitchDisableReceiving
  66. #endif
  67.  
  68. */
  69.  
  70. // Number of maximum High/Low changes per packet.
  71. // We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync
  72. #define RCSWITCH_MAX_CHANGES 67
  73.  
  74. #define PROTOCOL3_SYNC_FACTOR   71
  75. #define PROTOCOL3_0_HIGH_CYCLES  4
  76. #define PROTOCOL3_0_LOW_CYCLES  11
  77. #define PROTOCOL3_1_HIGH_CYCLES  9
  78. #define PROTOCOL3_1_LOW_CYCLES   6
  79.  
  80. class RCSwitch {
  81.  
  82.   public:
  83.     RCSwitch();
  84.    
  85.     void switchOn(int nGroupNumber, int nSwitchNumber);
  86.     void switchOff(int nGroupNumber, int nSwitchNumber);
  87.     void switchOn(char* sGroup, int nSwitchNumber);
  88.     void switchOff(char* sGroup, int nSwitchNumber);
  89.     void switchOn(char sFamily, int nGroup, int nDevice);
  90.     void switchOff(char sFamily, int nGroup, int nDevice);
  91.     void switchOn(char* sGroup, char* sDevice);
  92.     void switchOff(char* sGroup, char* sDevice);
  93.     void switchOn(char sGroup, int nDevice);
  94.     void switchOff(char sGroup, int nDevice);
  95.  
  96.     void sendTriState(char* Code);
  97.     void send(unsigned long Code, unsigned int length);
  98.     void send(char* Code);
  99.    
  100.     #if not defined( RCSwitchDisableReceiving )
  101.     void enableReceive(int interrupt);
  102.     void enableReceive();
  103.     void disableReceive();
  104.     bool available();
  105.     void resetAvailable();
  106.    
  107.     unsigned long getReceivedValue();
  108.     unsigned int getReceivedBitlength();
  109.     unsigned int getReceivedDelay();
  110.     unsigned int getReceivedProtocol();
  111.     unsigned int* getReceivedRawdata();
  112.     #endif
  113.  
  114.     void enableTransmit(int nTransmitterPin);
  115.     void disableTransmit();
  116.     void setPulseLength(int nPulseLength);
  117.     void setRepeatTransmit(int nRepeatTransmit);
  118.     #if not defined( RCSwitchDisableReceiving )
  119.     void setReceiveTolerance(int nPercent);
  120.     #endif
  121.     void setProtocol(int nProtocol);
  122.     void setProtocol(int nProtocol, int nPulseLength);
  123.  
  124.   private:
  125.     char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);
  126.     char* getCodeWordA(char* sGroup, int nSwitchNumber, boolean bStatus);
  127.     char* getCodeWordA(char* sGroup, char* sDevice, boolean bStatus);
  128.     char* getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus);
  129.     char* getCodeWordD(char group, int nDevice, boolean bStatus);
  130.     void sendT0();
  131.     void sendT1();
  132.     void sendTF();
  133.     void send0();
  134.     void send1();
  135.     void sendSync();
  136.     void transmit(int nHighPulses, int nLowPulses);
  137.  
  138.     static char* dec2binWzerofill(unsigned long dec, unsigned int length);
  139.     static char* dec2binWcharfill(unsigned long dec, unsigned int length, char fill);
  140.    
  141.     #if not defined( RCSwitchDisableReceiving )
  142.     static void handleInterrupt();
  143.     static bool receiveProtocol1(unsigned int changeCount);
  144.     static bool receiveProtocol2(unsigned int changeCount);
  145.     static bool receiveProtocol3(unsigned int changeCount);
  146.     int nReceiverInterrupt;
  147.     #endif
  148.     int nTransmitterPin;
  149.     int nPulseLength;
  150.     int nRepeatTransmit;
  151.     char nProtocol;
  152.  
  153.     #if not defined( RCSwitchDisableReceiving )
  154.     static int nReceiveTolerance;
  155.     static unsigned long nReceivedValue;
  156.     static unsigned int nReceivedBitlength;
  157.     static unsigned int nReceivedDelay;
  158.     static unsigned int nReceivedProtocol;
  159.     #endif
  160.     /*
  161.      * timings[0] contains sync timing, followed by a number of bits
  162.      */
  163.     static unsigned int timings[RCSWITCH_MAX_CHANGES];
  164.  
  165.    
  166. };
  167.  
  168. #endif
  169.  
  170. /* ========================= .cpp ================================= */
  171.  
  172. /*
  173.   RCSwitch - Arduino libary for remote control outlet switches
  174.   Copyright (c) 2011 Suat Özgür.  All right reserved.
  175.  
  176.   Contributors:
  177.   - Andre Koehler / info(at)tomate-online(dot)de
  178.   - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com
  179.   - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46
  180.   - Dominik Fischer / dom_fischer(at)web(dot)de
  181.   - Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com
  182.   - Andreas Steinel / A.<lastname>(at)gmail(dot)com
  183.  
  184.   Project home: http://code.google.com/p/rc-switch/
  185.  
  186.   This library is free software; you can redistribute it and/or
  187.   modify it under the terms of the GNU Lesser General Public
  188.   License as published by the Free Software Foundation; either
  189.   version 2.1 of the License, or (at your option) any later version.
  190.  
  191.   This library is distributed in the hope that it will be useful,
  192.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  193.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  194.   Lesser General Public License for more details.
  195.  
  196.   You should have received a copy of the GNU Lesser General Public
  197.   License along with this library; if not, write to the Free Software
  198.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  199. */
  200.  
  201. // #include "RCSwitch.h"
  202.  
  203. #if not defined( RCSwitchDisableReceiving )
  204. unsigned long RCSwitch::nReceivedValue = NULL;
  205. unsigned int RCSwitch::nReceivedBitlength = 0;
  206. unsigned int RCSwitch::nReceivedDelay = 0;
  207. unsigned int RCSwitch::nReceivedProtocol = 0;
  208. int RCSwitch::nReceiveTolerance = 60;
  209. #endif
  210. unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES];
  211.  
  212. RCSwitch::RCSwitch() {
  213.   this->nTransmitterPin = -1;
  214.   this->setPulseLength(350);
  215.   this->setRepeatTransmit(10);
  216.   this->setProtocol(1);
  217.   #if not defined( RCSwitchDisableReceiving )
  218.   this->nReceiverInterrupt = -1;
  219.   this->setReceiveTolerance(60);
  220.   RCSwitch::nReceivedValue = NULL;
  221.   #endif
  222. }
  223.  
  224. /**
  225.   * Sets the protocol to send.
  226.   */
  227. void RCSwitch::setProtocol(int nProtocol) {
  228.   this->nProtocol = nProtocol;
  229.   if (nProtocol == 1){
  230.     this->setPulseLength(350);
  231.   }
  232.   else if (nProtocol == 2) {
  233.     this->setPulseLength(650);
  234.   }
  235.   else if (nProtocol == 3) {
  236.     this->setPulseLength(100);
  237.   }
  238. }
  239.  
  240. /**
  241.   * Sets the protocol to send with pulse length in microseconds.
  242.   */
  243. void RCSwitch::setProtocol(int nProtocol, int nPulseLength) {
  244.   this->nProtocol = nProtocol;
  245.   this->setPulseLength(nPulseLength);
  246. }
  247.  
  248.  
  249. /**
  250.   * Sets pulse length in microseconds
  251.   */
  252. void RCSwitch::setPulseLength(int nPulseLength) {
  253.   this->nPulseLength = nPulseLength;
  254. }
  255.  
  256. /**
  257.  * Sets Repeat Transmits
  258.  */
  259. void RCSwitch::setRepeatTransmit(int nRepeatTransmit) {
  260.   this->nRepeatTransmit = nRepeatTransmit;
  261. }
  262.  
  263. /**
  264.  * Set Receiving Tolerance
  265.  */
  266. #if not defined( RCSwitchDisableReceiving )
  267. void RCSwitch::setReceiveTolerance(int nPercent) {
  268.   RCSwitch::nReceiveTolerance = nPercent;
  269. }
  270. #endif
  271.  
  272.  
  273. /**
  274.  * Enable transmissions
  275.  *
  276.  * @param nTransmitterPin    Arduino Pin to which the sender is connected to
  277.  */
  278. void RCSwitch::enableTransmit(int nTransmitterPin) {
  279.   this->nTransmitterPin = nTransmitterPin;
  280.   pinMode(this->nTransmitterPin, OUTPUT);
  281. }
  282.  
  283. /**
  284.   * Disable transmissions
  285.   */
  286. void RCSwitch::disableTransmit() {
  287.   this->nTransmitterPin = -1;
  288. }
  289.  
  290. /**
  291.  * Switch a remote switch on (Type D REV)
  292.  *
  293.  * @param sGroup        Code of the switch group (A,B,C,D)
  294.  * @param nDevice       Number of the switch itself (1..3)
  295.  */
  296. void RCSwitch::switchOn(char sGroup, int nDevice) {
  297.   this->sendTriState( this->getCodeWordD(sGroup, nDevice, true) );
  298. }
  299.  
  300. /**
  301.  * Switch a remote switch off (Type D REV)
  302.  *
  303.  * @param sGroup        Code of the switch group (A,B,C,D)
  304.  * @param nDevice       Number of the switch itself (1..3)
  305.  */
  306. void RCSwitch::switchOff(char sGroup, int nDevice) {
  307.   this->sendTriState( this->getCodeWordD(sGroup, nDevice, false) );
  308. }
  309.  
  310. /**
  311.  * Switch a remote switch on (Type C Intertechno)
  312.  *
  313.  * @param sFamily  Familycode (a..f)
  314.  * @param nGroup   Number of group (1..4)
  315.  * @param nDevice  Number of device (1..4)
  316.   */
  317. void RCSwitch::switchOn(char sFamily, int nGroup, int nDevice) {
  318.   this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, true) );
  319. }
  320.  
  321. /**
  322.  * Switch a remote switch off (Type C Intertechno)
  323.  *
  324.  * @param sFamily  Familycode (a..f)
  325.  * @param nGroup   Number of group (1..4)
  326.  * @param nDevice  Number of device (1..4)
  327.  */
  328. void RCSwitch::switchOff(char sFamily, int nGroup, int nDevice) {
  329.   this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, false) );
  330. }
  331.  
  332. /**
  333.  * Switch a remote switch on (Type B with two rotary/sliding switches)
  334.  *
  335.  * @param nAddressCode  Number of the switch group (1..4)
  336.  * @param nChannelCode  Number of the switch itself (1..4)
  337.  */
  338. void RCSwitch::switchOn(int nAddressCode, int nChannelCode) {
  339.   this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, true) );
  340. }
  341.  
  342. /**
  343.  * Switch a remote switch off (Type B with two rotary/sliding switches)
  344.  *
  345.  * @param nAddressCode  Number of the switch group (1..4)
  346.  * @param nChannelCode  Number of the switch itself (1..4)
  347.  */
  348. void RCSwitch::switchOff(int nAddressCode, int nChannelCode) {
  349.   this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, false) );
  350. }
  351.  
  352. /**
  353.  * Deprecated, use switchOn(char* sGroup, char* sDevice) instead!
  354.  * Switch a remote switch on (Type A with 10 pole DIP switches)
  355.  *
  356.  * @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")
  357.  * @param nChannelCode  Number of the switch itself (1..5)
  358.  */
  359. void RCSwitch::switchOn(char* sGroup, int nChannel) {
  360.   char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };
  361.   this->switchOn(sGroup, code[nChannel]);
  362. }
  363.  
  364. /**
  365.  * Deprecated, use switchOff(char* sGroup, char* sDevice) instead!
  366.  * Switch a remote switch off (Type A with 10 pole DIP switches)
  367.  *
  368.  * @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")
  369.  * @param nChannelCode  Number of the switch itself (1..5)
  370.  */
  371. void RCSwitch::switchOff(char* sGroup, int nChannel) {
  372.   char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" };
  373.   this->switchOff(sGroup, code[nChannel]);
  374. }
  375.  
  376. /**
  377.  * Switch a remote switch on (Type A with 10 pole DIP switches)
  378.  *
  379.  * @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")
  380.  * @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")
  381.  */
  382. void RCSwitch::switchOn(char* sGroup, char* sDevice) {
  383.     this->sendTriState( this->getCodeWordA(sGroup, sDevice, true) );
  384. }
  385.  
  386. /**
  387.  * Switch a remote switch off (Type A with 10 pole DIP switches)
  388.  *
  389.  * @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")
  390.  * @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")
  391.  */
  392. void RCSwitch::switchOff(char* sGroup, char* sDevice) {
  393.     this->sendTriState( this->getCodeWordA(sGroup, sDevice, false) );
  394. }
  395.  
  396. /**
  397.  * Returns a char[13], representing the Code Word to be send.
  398.  * 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.
  399.  * A Code Bit can have 4 different states: "F" (floating), "0" (low), "1" (high), "S" (synchronous bit)
  400.  *
  401.  * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+
  402.  * | 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 |
  403.  * | 1=0FFF 2=F0FF 3=FF0F 4=FFF0   | 1=0FFF 2=F0FF 3=FF0F 4=FFF0    | F                                       | F                                       | on=FF off=F0         | S          |
  404.  * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+
  405.  *
  406.  * @param nAddressCode  Number of the switch group (1..4)
  407.  * @param nChannelCode  Number of the switch itself (1..4)
  408.  * @param bStatus       Wether to switch on (true) or off (false)
  409.  *
  410.  * @return char[13]
  411.  */
  412. char* RCSwitch::getCodeWordB(int nAddressCode, int nChannelCode, boolean bStatus) {
  413.    int nReturnPos = 0;
  414.    static char sReturn[13];
  415.    
  416.    char* code[5] = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" };
  417.    if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) {
  418.     return '\0';
  419.    }
  420.    for (int i = 0; i<4; i++) {
  421.      sReturn[nReturnPos++] = code[nAddressCode][i];
  422.    }
  423.  
  424.    for (int i = 0; i<4; i++) {
  425.      sReturn[nReturnPos++] = code[nChannelCode][i];
  426.    }
  427.    
  428.    sReturn[nReturnPos++] = 'F';
  429.    sReturn[nReturnPos++] = 'F';
  430.    sReturn[nReturnPos++] = 'F';
  431.    
  432.    if (bStatus) {
  433.       sReturn[nReturnPos++] = 'F';
  434.    } else {
  435.       sReturn[nReturnPos++] = '0';
  436.    }
  437.    
  438.    sReturn[nReturnPos] = '\0';
  439.    
  440.    return sReturn;
  441. }
  442.  
  443. /**
  444.  * Returns a char[13], representing the Code Word to be send.
  445.  *
  446.  * getCodeWordA(char*, char*)
  447.  *
  448.  */
  449. char* RCSwitch::getCodeWordA(char* sGroup, char* sDevice, boolean bOn) {
  450.     static char sDipSwitches[13];
  451.     int i = 0;
  452.     int j = 0;
  453.    
  454.     for (i=0; i < 5; i++) {
  455.         if (sGroup[i] == '0') {
  456.             sDipSwitches[j++] = 'F';
  457.         } else {
  458.             sDipSwitches[j++] = '0';
  459.         }
  460.     }
  461.  
  462.     for (i=0; i < 5; i++) {
  463.         if (sDevice[i] == '0') {
  464.             sDipSwitches[j++] = 'F';
  465.         } else {
  466.             sDipSwitches[j++] = '0';
  467.         }
  468.     }
  469.  
  470.     if ( bOn ) {
  471.         sDipSwitches[j++] = '0';
  472.         sDipSwitches[j++] = 'F';
  473.     } else {
  474.         sDipSwitches[j++] = 'F';
  475.         sDipSwitches[j++] = '0';
  476.     }
  477.  
  478.     sDipSwitches[j] = '\0';
  479.  
  480.     return sDipSwitches;
  481. }
  482.  
  483. /**
  484.  * Like getCodeWord (Type C = Intertechno)
  485.  */
  486. char* RCSwitch::getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus) {
  487.   static char sReturn[13];
  488.   int nReturnPos = 0;
  489.  
  490.   if ( (byte)sFamily < 97 || (byte)sFamily > 112 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) {
  491.     return '\0';
  492.   }
  493.  
  494.   char* sDeviceGroupCode =  dec2binWzerofill(  (nDevice-1) + (nGroup-1)*4, 4  );
  495.   char familycode[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" };
  496.   for (int i = 0; i<4; i++) {
  497.     sReturn[nReturnPos++] = familycode[ (int)sFamily - 97 ][i];
  498.   }
  499.   for (int i = 0; i<4; i++) {
  500.     sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0');
  501.   }
  502.   sReturn[nReturnPos++] = '0';
  503.   sReturn[nReturnPos++] = 'F';
  504.   sReturn[nReturnPos++] = 'F';
  505.   if (bStatus) {
  506.     sReturn[nReturnPos++] = 'F';
  507.   } else {
  508.     sReturn[nReturnPos++] = '0';
  509.   }
  510.   sReturn[nReturnPos] = '\0';
  511.   return sReturn;
  512. }
  513.  
  514. /**
  515.  * Decoding for the REV Switch Type
  516.  *
  517.  * Returns a char[13], representing the Tristate to be send.
  518.  * A Code Word consists of 7 address bits and 5 command data bits.
  519.  * A Code Bit can have 3 different states: "F" (floating), "0" (low), "1" (high)
  520.  *
  521.  * +-------------------------------+--------------------------------+-----------------------+
  522.  * | 4 bits address (switch group) | 3 bits address (device number) | 5 bits (command data) |
  523.  * | A=1FFF B=F1FF C=FF1F D=FFF1   | 1=0FFF 2=F0FF 3=FF0F 4=FFF0    | on=00010 off=00001    |
  524.  * +-------------------------------+--------------------------------+-----------------------+
  525.  *
  526.  * Source: http://www.the-intruder.net/funksteckdosen-von-rev-uber-arduino-ansteuern/
  527.  *
  528.  * @param sGroup        Name of the switch group (A..D, resp. a..d)
  529.  * @param nDevice       Number of the switch itself (1..3)
  530.  * @param bStatus       Wether to switch on (true) or off (false)
  531.  *
  532.  * @return char[13]
  533.  */
  534.  
  535. char* RCSwitch::getCodeWordD(char sGroup, int nDevice, boolean bStatus){
  536.     static char sReturn[13];
  537.     int nReturnPos = 0;
  538.  
  539.     // Building 4 bits address
  540.     // (Potential problem if dec2binWcharfill not returning correct string)
  541.     char *sGroupCode;
  542.     switch(sGroup){
  543.         case 'a':
  544.         case 'A':
  545.             sGroupCode = dec2binWcharfill(8, 4, 'F'); break;
  546.         case 'b':
  547.         case 'B':
  548.             sGroupCode = dec2binWcharfill(4, 4, 'F'); break;
  549.         case 'c':
  550.         case 'C':
  551.             sGroupCode = dec2binWcharfill(2, 4, 'F'); break;
  552.         case 'd':
  553.         case 'D':
  554.             sGroupCode = dec2binWcharfill(1, 4, 'F'); break;
  555.         default:
  556.             return '\0';
  557.     }
  558.    
  559.     for (int i = 0; i<4; i++)
  560.     {
  561.         sReturn[nReturnPos++] = sGroupCode[i];
  562.     }
  563.  
  564.  
  565.     // Building 3 bits address
  566.     // (Potential problem if dec2binWcharfill not returning correct string)
  567.     char *sDevice;
  568.     switch(nDevice) {
  569.         case 1:
  570.             sDevice = dec2binWcharfill(4, 3, 'F'); break;
  571.         case 2:
  572.             sDevice = dec2binWcharfill(2, 3, 'F'); break;
  573.         case 3:
  574.             sDevice = dec2binWcharfill(1, 3, 'F'); break;
  575.         default:
  576.             return '\0';
  577.     }
  578.  
  579.     for (int i = 0; i<3; i++)
  580.         sReturn[nReturnPos++] = sDevice[i];
  581.  
  582.     // fill up rest with zeros
  583.     for (int i = 0; i<5; i++)
  584.         sReturn[nReturnPos++] = '0';
  585.  
  586.     // encode on or off
  587.     if (bStatus)
  588.         sReturn[10] = '1';
  589.     else
  590.         sReturn[11] = '1';
  591.  
  592.     // last position terminate string
  593.     sReturn[12] = '\0';
  594.     return sReturn;
  595.  
  596. }
  597.  
  598. /**
  599.  * @param sCodeWord   /^[10FS]*$/  -> see getCodeWord
  600.  */
  601. void RCSwitch::sendTriState(char* sCodeWord) {
  602.   for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {
  603.     int i = 0;
  604.     while (sCodeWord[i] != '\0') {
  605.       switch(sCodeWord[i]) {
  606.         case '0':
  607.           this->sendT0();
  608.         break;
  609.         case 'F':
  610.           this->sendTF();
  611.         break;
  612.         case '1':
  613.           this->sendT1();
  614.         break;
  615.       }
  616.       i++;
  617.     }
  618.     this->sendSync();    
  619.   }
  620. }
  621.  
  622. void RCSwitch::send(unsigned long Code, unsigned int length) {
  623.   this->send( this->dec2binWzerofill(Code, length) );
  624. }
  625.  
  626. void RCSwitch::send(char* sCodeWord) {
  627.   for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) {
  628.     int i = 0;
  629.     while (sCodeWord[i] != '\0') {
  630.       switch(sCodeWord[i]) {
  631.         case '0':
  632.           this->send0();
  633.         break;
  634.         case '1':
  635.           this->send1();
  636.         break;
  637.       }
  638.       i++;
  639.     }
  640.     this->sendSync();
  641.   }
  642. }
  643.  
  644. void RCSwitch::transmit(int nHighPulses, int nLowPulses) {
  645.     #if not defined ( RCSwitchDisableReceiving )
  646.     boolean disabled_Receive = false;
  647.     int nReceiverInterrupt_backup = nReceiverInterrupt;
  648.     #endif
  649.     if (this->nTransmitterPin != -1) {
  650.         #if not defined( RCSwitchDisableReceiving )
  651.         if (this->nReceiverInterrupt != -1) {
  652.             this->disableReceive();
  653.             disabled_Receive = true;
  654.         }
  655.         #endif
  656.         digitalWrite(this->nTransmitterPin, HIGH);
  657.         delayMicroseconds( this->nPulseLength * nHighPulses);
  658.         digitalWrite(this->nTransmitterPin, LOW);
  659.         delayMicroseconds( this->nPulseLength * nLowPulses);
  660.        
  661.         #if not defined( RCSwitchDisableReceiving )
  662.         if(disabled_Receive){
  663.             this->enableReceive(nReceiverInterrupt_backup);
  664.         }
  665.         #endif
  666.     }
  667. }
  668. /**
  669.  * Sends a "0" Bit
  670.  *                       _    
  671.  * Waveform Protocol 1: | |___
  672.  *                       _  
  673.  * Waveform Protocol 2: | |__
  674.  */
  675. void RCSwitch::send0() {
  676.     if (this->nProtocol == 1){
  677.         this->transmit(1,3);
  678.     }
  679.     else if (this->nProtocol == 2) {
  680.         this->transmit(1,2);
  681.     }
  682.     else if (this->nProtocol == 3) {
  683.         this->transmit(4,11);
  684.     }
  685. }
  686.  
  687. /**
  688.  * Sends a "1" Bit
  689.  *                       ___  
  690.  * Waveform Protocol 1: |   |_
  691.  *                       __  
  692.  * Waveform Protocol 2: |  |_
  693.  */
  694. void RCSwitch::send1() {
  695.       if (this->nProtocol == 1){
  696.         this->transmit(3,1);
  697.     }
  698.     else if (this->nProtocol == 2) {
  699.         this->transmit(2,1);
  700.     }
  701.     else if (this->nProtocol == 3) {
  702.         this->transmit(9,6);
  703.     }
  704. }
  705.  
  706.  
  707. /**
  708.  * Sends a Tri-State "0" Bit
  709.  *            _     _
  710.  * Waveform: | |___| |___
  711.  */
  712. void RCSwitch::sendT0() {
  713.   this->transmit(1,3);
  714.   this->transmit(1,3);
  715. }
  716.  
  717. /**
  718.  * Sends a Tri-State "1" Bit
  719.  *            ___   ___
  720.  * Waveform: |   |_|   |_
  721.  */
  722. void RCSwitch::sendT1() {
  723.   this->transmit(3,1);
  724.   this->transmit(3,1);
  725. }
  726.  
  727. /**
  728.  * Sends a Tri-State "F" Bit
  729.  *            _     ___
  730.  * Waveform: | |___|   |_
  731.  */
  732. void RCSwitch::sendTF() {
  733.   this->transmit(1,3);
  734.   this->transmit(3,1);
  735. }
  736.  
  737. /**
  738.  * Sends a "Sync" Bit
  739.  *                       _
  740.  * Waveform Protocol 1: | |_______________________________
  741.  *                       _
  742.  * Waveform Protocol 2: | |__________
  743.  */
  744. void RCSwitch::sendSync() {
  745.  
  746.     if (this->nProtocol == 1){
  747.         this->transmit(1,31);
  748.     }
  749.     else if (this->nProtocol == 2) {
  750.         this->transmit(1,10);
  751.     }
  752.     else if (this->nProtocol == 3) {
  753.         this->transmit(1,71);
  754.     }
  755. }
  756.  
  757. #if not defined( RCSwitchDisableReceiving )
  758. /**
  759.  * Enable receiving data
  760.  */
  761. void RCSwitch::enableReceive(int interrupt) {
  762.   this->nReceiverInterrupt = interrupt;
  763.   this->enableReceive();
  764. }
  765.  
  766. void RCSwitch::enableReceive() {
  767.   if (this->nReceiverInterrupt != -1) {
  768.     RCSwitch::nReceivedValue = NULL;
  769.     RCSwitch::nReceivedBitlength = NULL;
  770.     attachInterrupt(this->nReceiverInterrupt, handleInterrupt, CHANGE);
  771.   }
  772. }
  773.  
  774. /**
  775.  * Disable receiving data
  776.  */
  777. void RCSwitch::disableReceive() {
  778.   detachInterrupt(this->nReceiverInterrupt);
  779.   this->nReceiverInterrupt = -1;
  780. }
  781.  
  782. bool RCSwitch::available() {
  783.   return RCSwitch::nReceivedValue != NULL;
  784. }
  785.  
  786. void RCSwitch::resetAvailable() {
  787.   RCSwitch::nReceivedValue = NULL;
  788. }
  789.  
  790. unsigned long RCSwitch::getReceivedValue() {
  791.     return RCSwitch::nReceivedValue;
  792. }
  793.  
  794. unsigned int RCSwitch::getReceivedBitlength() {
  795.   return RCSwitch::nReceivedBitlength;
  796. }
  797.  
  798. unsigned int RCSwitch::getReceivedDelay() {
  799.   return RCSwitch::nReceivedDelay;
  800. }
  801.  
  802. unsigned int RCSwitch::getReceivedProtocol() {
  803.   return RCSwitch::nReceivedProtocol;
  804. }
  805.  
  806. unsigned int* RCSwitch::getReceivedRawdata() {
  807.     return RCSwitch::timings;
  808. }
  809.  
  810. /**
  811.  *
  812.  */
  813. bool RCSwitch::receiveProtocol1(unsigned int changeCount){
  814.    
  815.       unsigned long code = 0;
  816.       unsigned long delay = RCSwitch::timings[0] / 31;
  817.       unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01;    
  818.  
  819.       for (int i = 1; i<changeCount ; i=i+2) {
  820.      
  821.           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) {
  822.             code = code << 1;
  823.           } 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) {
  824.             code+=1;
  825.             code = code << 1;
  826.           } else {
  827.             // Failed
  828.             i = changeCount;
  829.             code = 0;
  830.           }
  831.       }      
  832.       code = code >> 1;
  833.     if (changeCount > 6) {    // ignore < 4bit values as there are no devices sending 4bit values => noise
  834.       RCSwitch::nReceivedValue = code;
  835.       RCSwitch::nReceivedBitlength = changeCount / 2;
  836.       RCSwitch::nReceivedDelay = delay;
  837.       RCSwitch::nReceivedProtocol = 1;
  838.     }
  839.  
  840.     if (code == 0){
  841.         return false;
  842.     }else if (code != 0){
  843.         return true;
  844.     }
  845.    
  846.  
  847. }
  848.  
  849. bool RCSwitch::receiveProtocol2(unsigned int changeCount){
  850.    
  851.       unsigned long code = 0;
  852.       unsigned long delay = RCSwitch::timings[0] / 10;
  853.       unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01;    
  854.  
  855.       for (int i = 1; i<changeCount ; i=i+2) {
  856.      
  857.           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) {
  858.             code = code << 1;
  859.           } 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) {
  860.             code+=1;
  861.             code = code << 1;
  862.           } else {
  863.             // Failed
  864.             i = changeCount;
  865.             code = 0;
  866.           }
  867.       }      
  868.       code = code >> 1;
  869.     if (changeCount > 6) {    // ignore < 4bit values as there are no devices sending 4bit values => noise
  870.       RCSwitch::nReceivedValue = code;
  871.       RCSwitch::nReceivedBitlength = changeCount / 2;
  872.       RCSwitch::nReceivedDelay = delay;
  873.       RCSwitch::nReceivedProtocol = 2;
  874.     }
  875.  
  876.     if (code == 0){
  877.         return false;
  878.     }else if (code != 0){
  879.         return true;
  880.     }
  881.  
  882. }
  883.  
  884. /** Protocol 3 is used by BL35P02.
  885.  *
  886.  */
  887. bool RCSwitch::receiveProtocol3(unsigned int changeCount){
  888.    
  889.       unsigned long code = 0;
  890.       unsigned long delay = RCSwitch::timings[0] / PROTOCOL3_SYNC_FACTOR;
  891.       unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01;    
  892.  
  893.       for (int i = 1; i<changeCount ; i=i+2) {
  894.      
  895.           if  (RCSwitch::timings[i]   > delay*PROTOCOL3_0_HIGH_CYCLES - delayTolerance
  896.             && RCSwitch::timings[i]   < delay*PROTOCOL3_0_HIGH_CYCLES + delayTolerance
  897.             && RCSwitch::timings[i+1] > delay*PROTOCOL3_0_LOW_CYCLES  - delayTolerance
  898.             && RCSwitch::timings[i+1] < delay*PROTOCOL3_0_LOW_CYCLES  + delayTolerance) {
  899.             code = code << 1;
  900.           } else if (RCSwitch::timings[i]   > delay*PROTOCOL3_1_HIGH_CYCLES - delayTolerance
  901.                   && RCSwitch::timings[i]   < delay*PROTOCOL3_1_HIGH_CYCLES + delayTolerance
  902.                   && RCSwitch::timings[i+1] > delay*PROTOCOL3_1_LOW_CYCLES  - delayTolerance
  903.                   && RCSwitch::timings[i+1] < delay*PROTOCOL3_1_LOW_CYCLES  + delayTolerance) {
  904.             code+=1;
  905.             code = code << 1;
  906.           } else {
  907.             // Failed
  908.             i = changeCount;
  909.             code = 0;
  910.           }
  911.       }      
  912.       code = code >> 1;
  913.       if (changeCount > 6) {    // ignore < 4bit values as there are no devices sending 4bit values => noise
  914.         RCSwitch::nReceivedValue = code;
  915.         RCSwitch::nReceivedBitlength = changeCount / 2;
  916.         RCSwitch::nReceivedDelay = delay;
  917.         RCSwitch::nReceivedProtocol = 3;
  918.       }
  919.  
  920.       if (code == 0){
  921.         return false;
  922.       }else if (code != 0){
  923.         return true;
  924.       }
  925. }
  926.  
  927. void RCSwitch::handleInterrupt() {
  928.  
  929.   static unsigned int duration;
  930.   static unsigned int changeCount;
  931.   static unsigned long lastTime;
  932.   static unsigned int repeatCount;
  933.  
  934.  
  935.   long time = micros();
  936.   duration = time - lastTime;
  937.  
  938.   if (duration > 5000 && duration > RCSwitch::timings[0] - 200 && duration < RCSwitch::timings[0] + 200) {
  939.     repeatCount++;
  940.     changeCount--;
  941.     if (repeatCount == 2) {
  942.       if (receiveProtocol1(changeCount) == false){
  943.         if (receiveProtocol2(changeCount) == false){
  944.           if (receiveProtocol3(changeCount) == false){
  945.             //failed
  946.           }
  947.         }
  948.       }
  949.       repeatCount = 0;
  950.     }
  951.     changeCount = 0;
  952.   } else if (duration > 5000) {
  953.     changeCount = 0;
  954.   }
  955.  
  956.   if (changeCount >= RCSWITCH_MAX_CHANGES) {
  957.     changeCount = 0;
  958.     repeatCount = 0;
  959.   }
  960.   RCSwitch::timings[changeCount++] = duration;
  961.   lastTime = time;  
  962. }
  963.  
  964. /**
  965.   * Turns a decimal value to its binary representation
  966.   */
  967. char* RCSwitch::dec2binWzerofill(unsigned long Dec, unsigned int bitLength){
  968.     return dec2binWcharfill(Dec, bitLength, '0');
  969. }
  970.  
  971. char* RCSwitch::dec2binWcharfill(unsigned long Dec, unsigned int bitLength, char fill){
  972.   static char bin[64];
  973.   unsigned int i=0;
  974.  
  975.   while (Dec > 0) {
  976.     bin[32+i++] = ((Dec & 1) > 0) ? '1' : fill;
  977.     Dec = Dec >> 1;
  978.   }
  979.  
  980.   for (unsigned int j = 0; j< bitLength; j++) {
  981.     if (j >= bitLength - i) {
  982.       bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
  983.     }else {
  984.       bin[j] = fill;
  985.     }
  986.   }
  987.   bin[bitLength] = '\0';
  988.  
  989.   return bin;
  990. }
  991.  
  992. #endif
  993.  
  994.  
  995.  
  996. /* ========================     MAIN      =========================== */
  997.  
  998. /*
  999.   Example for outlets which are configured with a 10 pole DIP switch.
  1000.  
  1001.   http://code.google.com/p/rc-switch/
  1002.  
  1003.   Need help? http://forum.ardumote.com
  1004. */
  1005.  
  1006.  
  1007. RCSwitch mySwitch = RCSwitch();
  1008.  
  1009. void setup() {
  1010.  
  1011.   // Transmitter is connected to Arduino Pin #10  
  1012.   mySwitch.enableTransmit(D7);
  1013.  
  1014.   // Optional set pulse length.
  1015.   // mySwitch.setPulseLength(320);
  1016.  
  1017. }
  1018.  
  1019. void loop() {
  1020.  
  1021.   // Switch on:
  1022.   // The first parameter represents the setting of the first 5 DIP switches.
  1023.   // In this example it's ON-ON-OFF-OFF-ON.
  1024.   //
  1025.   // The second parameter represents the setting of the last 5 DIP switches.
  1026.   // In this example the last 5 DIP switches are OFF-ON-OFF-ON-OFF.  
  1027.   mySwitch.switchOn("11111", "00010");
  1028.  
  1029.   // Wait a second
  1030.   delay(1000);
  1031.  
  1032.   // Switch off
  1033.   mySwitch.switchOff("11111", "00010");
  1034.  
  1035.   // Wait another second
  1036.   delay(1000);
  1037.  
  1038. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement