RCSwitch.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. RCSwitch - Arduino libary for remote control outlet switches
  3. Copyright (c) 2011 Suat Özgür. All right reserved.
  4. Contributors:
  5. - Andre Koehler / info(at)tomate-online(dot)de
  6. - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com
  7. - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46
  8. - Dominik Fischer / dom_fischer(at)web(dot)de
  9. - Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com
  10. - Max Horn / max(at)quendi(dot)de
  11. - Robert ter Vehn / <first name>.<last name>(at)gmail(dot)com
  12. Project home: https://github.com/sui77/rc-switch/
  13. This library is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Lesser General Public
  15. License as published by the Free Software Foundation; either
  16. version 2.1 of the License, or (at your option) any later version.
  17. This library is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. Lesser General Public License for more details.
  21. You should have received a copy of the GNU Lesser General Public
  22. License along with this library; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #ifndef _RCSwitch_h
  26. #define _RCSwitch_h
  27. #if defined(ARDUINO) && ARDUINO >= 100
  28. #include "Arduino.h"
  29. #elif defined(ENERGIA) // LaunchPad, FraunchPad and StellarPad specific
  30. #include "Energia.h"
  31. #elif defined(RPI) // Raspberry Pi
  32. #define RaspberryPi
  33. // Include libraries for RPi:
  34. #include <string.h> /* memcpy */
  35. #include <stdlib.h> /* abs */
  36. #include <wiringPi.h>
  37. #elif defined(SPARK)
  38. #include "application.h"
  39. #else
  40. #include "WProgram.h"
  41. #endif
  42. #include <stdint.h>
  43. // At least for the ATTiny X4/X5, receiving has to be disabled due to
  44. // missing libm depencies (udivmodhi4)
  45. #if defined( __AVR_ATtinyX5__ ) or defined ( __AVR_ATtinyX4__ )
  46. #define RCSwitchDisableReceiving
  47. #endif
  48. // Number of maximum high/Low changes per packet.
  49. // We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync
  50. #define RCSWITCH_MAX_CHANGES 67
  51. class RCSwitch {
  52. public:
  53. RCSwitch();
  54. void switchOn(int nGroupNumber, int nSwitchNumber);
  55. void switchOff(int nGroupNumber, int nSwitchNumber);
  56. void switchOn(const char* sGroup, int nSwitchNumber);
  57. void switchOff(const char* sGroup, int nSwitchNumber);
  58. void switchOn(char sFamily, int nGroup, int nDevice);
  59. void switchOff(char sFamily, int nGroup, int nDevice);
  60. void switchOn(const char* sGroup, const char* sDevice);
  61. void switchOff(const char* sGroup, const char* sDevice);
  62. void switchOn(char sGroup, int nDevice);
  63. void switchOff(char sGroup, int nDevice);
  64. void sendTriState(const char* sCodeWord);
  65. void send(unsigned long code, unsigned int length);
  66. void send(const char* sCodeWord);
  67. #if not defined( RCSwitchDisableReceiving )
  68. void enableReceive(int interrupt);
  69. void enableReceive();
  70. void disableReceive();
  71. bool available();
  72. void resetAvailable();
  73. unsigned long getReceivedValue();
  74. unsigned int getReceivedBitlength();
  75. unsigned int getReceivedDelay();
  76. unsigned int getReceivedProtocol();
  77. unsigned int* getReceivedRawdata();
  78. #endif
  79. void enableTransmit(int nTransmitterPin);
  80. void disableTransmit();
  81. void setPulseLength(int nPulseLength);
  82. void setRepeatTransmit(int nRepeatTransmit);
  83. #if not defined( RCSwitchDisableReceiving )
  84. void setReceiveTolerance(int nPercent);
  85. #endif
  86. /**
  87. * Description of a single pule, which consists of a high signal
  88. * whose duration is "high" times the base pulse length, followed
  89. * by a low signal lasting "low" times the base pulse length.
  90. * Thus, the pulse overall lasts (high+low)*pulseLength
  91. */
  92. struct HighLow {
  93. uint8_t high;
  94. uint8_t low;
  95. };
  96. /**
  97. * A "protocol" describes how zero and one bits are encoded into high/low
  98. * pulses.
  99. */
  100. struct Protocol {
  101. /** base pulse length in microseconds, e.g. 350 */
  102. uint16_t pulseLength;
  103. HighLow syncFactor;
  104. HighLow zero;
  105. HighLow one;
  106. /**
  107. * If true, interchange high and low logic levels in all transmissions.
  108. *
  109. * By default, RCSwitch assumes that any signals it sends or receives
  110. * can be broken down into pulses which start with a high signal level,
  111. * followed by a a low signal level. This is e.g. the case for the
  112. * popular PT 2260 encoder chip, and thus many switches out there.
  113. *
  114. * But some devices do it the other way around, and start with a low
  115. * signal level, followed by a high signal level, e.g. the HT6P20B. To
  116. * accommodate this, one can set invertedSignal to true, which causes
  117. * RCSwitch to change how it interprets any HighLow struct FOO: It will
  118. * then assume transmissions start with a low signal lasting
  119. * FOO.high*pulseLength microseconds, followed by a high signal lasting
  120. * FOO.low*pulseLength microseconds.
  121. */
  122. bool invertedSignal;
  123. };
  124. void setProtocol(Protocol protocol);
  125. void setProtocol(int nProtocol);
  126. void setProtocol(int nProtocol, int nPulseLength);
  127. private:
  128. char* getCodeWordA(const char* sGroup, const char* sDevice, bool bStatus);
  129. char* getCodeWordB(int nGroupNumber, int nSwitchNumber, bool bStatus);
  130. char* getCodeWordC(char sFamily, int nGroup, int nDevice, bool bStatus);
  131. char* getCodeWordD(char group, int nDevice, bool bStatus);
  132. void transmit(HighLow pulses);
  133. #if not defined( RCSwitchDisableReceiving )
  134. static void handleInterrupt();
  135. static bool receiveProtocol(const int p, unsigned int changeCount);
  136. int nReceiverInterrupt;
  137. #endif
  138. int nTransmitterPin;
  139. int nRepeatTransmit;
  140. Protocol protocol;
  141. #if not defined( RCSwitchDisableReceiving )
  142. static int nReceiveTolerance;
  143. volatile static unsigned long nReceivedValue;
  144. volatile static unsigned int nReceivedBitlength;
  145. volatile static unsigned int nReceivedDelay;
  146. volatile static unsigned int nReceivedProtocol;
  147. const static unsigned int nSeparationLimit;
  148. /*
  149. * timings[0] contains sync timing, followed by a number of bits
  150. */
  151. static unsigned int timings[RCSWITCH_MAX_CHANGES];
  152. #endif
  153. };
  154. #endif