NewRemoteTransmitter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * NewRemoteSwitch library v1.2.0 (20140128) made by Randy Simons http://randysimons.nl/
  3. * See NewRemoteTransmitter.h for details.
  4. *
  5. * License: GPLv3. See license.txt
  6. */
  7. #include "NewRemoteTransmitter.h"
  8. #ifdef ESP8266
  9. // interrupt handler and related code must be in RAM on ESP8266,
  10. #define RECEIVE_ATTR ICACHE_RAM_ATTR
  11. #else
  12. #define RECEIVE_ATTR
  13. #endif
  14. NewRemoteTransmitter::NewRemoteTransmitter(unsigned long address, byte pin, unsigned int periodusec, byte repeats) {
  15. _address = address;
  16. _pin = pin;
  17. _periodusec = periodusec;
  18. _repeats = (1 << repeats) - 1; // I.e. _repeats = 2^repeats - 1
  19. pinMode(_pin, OUTPUT);
  20. }
  21. void NewRemoteTransmitter::sendGroup(boolean switchOn) {
  22. for (int8_t i = _repeats; i >= 0; i--) {
  23. _sendStartPulse();
  24. _sendAddress();
  25. // Do send group bit
  26. _sendBit(true);
  27. // Switch on | off
  28. _sendBit(switchOn);
  29. // No unit. Is this actually ignored?..
  30. _sendUnit(0);
  31. _sendStopPulse();
  32. }
  33. }
  34. void NewRemoteTransmitter::sendUnit(byte unit, boolean switchOn) {
  35. for (int8_t i = _repeats; i >= 0; i--) {
  36. _sendStartPulse();
  37. _sendAddress();
  38. // No group bit
  39. _sendBit(false);
  40. // Switch on | off
  41. _sendBit(switchOn);
  42. _sendUnit(unit);
  43. _sendStopPulse();
  44. }
  45. }
  46. void NewRemoteTransmitter::sendDim(byte unit, byte dimLevel) {
  47. for (int8_t i = _repeats; i >= 0; i--) {
  48. _sendStartPulse();
  49. _sendAddress();
  50. // No group bit
  51. _sendBit(false);
  52. // Switch type 'dim'
  53. digitalWrite(_pin, HIGH);
  54. delayMicroseconds(_periodusec);
  55. digitalWrite(_pin, LOW);
  56. delayMicroseconds(_periodusec);
  57. digitalWrite(_pin, HIGH);
  58. delayMicroseconds(_periodusec);
  59. digitalWrite(_pin, LOW);
  60. delayMicroseconds(_periodusec);
  61. _sendUnit(unit);
  62. for (int8_t j=3; j>=0; j--) {
  63. _sendBit(dimLevel & 1<<j);
  64. }
  65. _sendStopPulse();
  66. }
  67. }
  68. void NewRemoteTransmitter::sendGroupDim(byte dimLevel) {
  69. for (int8_t i = _repeats; i >= 0; i--) {
  70. _sendStartPulse();
  71. _sendAddress();
  72. // No group bit
  73. _sendBit(true);
  74. // Switch type 'dim'
  75. digitalWrite(_pin, HIGH);
  76. delayMicroseconds(_periodusec);
  77. digitalWrite(_pin, LOW);
  78. delayMicroseconds(_periodusec);
  79. digitalWrite(_pin, HIGH);
  80. delayMicroseconds(_periodusec);
  81. digitalWrite(_pin, LOW);
  82. delayMicroseconds(_periodusec);
  83. _sendUnit(0);
  84. for (int8_t j=3; j>=0; j--) {
  85. _sendBit(dimLevel & 1<<j);
  86. }
  87. _sendStopPulse();
  88. }
  89. }
  90. void NewRemoteTransmitter::_sendStartPulse(){
  91. digitalWrite(_pin, HIGH);
  92. delayMicroseconds(_periodusec);
  93. digitalWrite(_pin, LOW);
  94. delayMicroseconds(_periodusec * 10 + (_periodusec >> 1)); // Actually 10.5T insteat of 10.44T. Close enough.
  95. }
  96. void NewRemoteTransmitter::_sendAddress() {
  97. for (int8_t i=25; i>=0; i--) {
  98. _sendBit((_address >> i) & 1);
  99. }
  100. }
  101. void NewRemoteTransmitter::_sendUnit(byte unit) {
  102. for (int8_t i=3; i>=0; i--) {
  103. _sendBit(unit & 1<<i);
  104. }
  105. }
  106. void NewRemoteTransmitter::_sendStopPulse() {
  107. digitalWrite(_pin, HIGH);
  108. delayMicroseconds(_periodusec);
  109. digitalWrite(_pin, LOW);
  110. delayMicroseconds(_periodusec * 40);
  111. }
  112. void NewRemoteTransmitter::_sendBit(boolean isBitOne) {
  113. if (isBitOne) {
  114. // Send '1'
  115. digitalWrite(_pin, HIGH);
  116. delayMicroseconds(_periodusec);
  117. digitalWrite(_pin, LOW);
  118. delayMicroseconds(_periodusec * 5);
  119. digitalWrite(_pin, HIGH);
  120. delayMicroseconds(_periodusec);
  121. digitalWrite(_pin, LOW);
  122. delayMicroseconds(_periodusec);
  123. } else {
  124. // Send '0'
  125. digitalWrite(_pin, HIGH);
  126. delayMicroseconds(_periodusec);
  127. digitalWrite(_pin, LOW);
  128. delayMicroseconds(_periodusec);
  129. digitalWrite(_pin, HIGH);
  130. delayMicroseconds(_periodusec);
  131. digitalWrite(_pin, LOW);
  132. delayMicroseconds(_periodusec * 5);
  133. }
  134. }