Library to switch 433MHz remote controlled sockets.
RCSwitch.cpp@0:6f4be1a7962c, 2014-10-12 (annotated)
- Committer:
- TheChrisyd
- Date:
- Sun Oct 12 09:57:12 2014 +0000
- Revision:
- 0:6f4be1a7962c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TheChrisyd | 0:6f4be1a7962c | 1 | /* |
TheChrisyd | 0:6f4be1a7962c | 2 | RCSwitch - Ported from the Arduino libary for remote control outlet switches |
TheChrisyd | 0:6f4be1a7962c | 3 | Copyright (c) 2011 Suat Özgür. All right reserved. |
TheChrisyd | 0:6f4be1a7962c | 4 | |
TheChrisyd | 0:6f4be1a7962c | 5 | Contributors: |
TheChrisyd | 0:6f4be1a7962c | 6 | - Andre Koehler / info(at)tomate-online(dot)de |
TheChrisyd | 0:6f4be1a7962c | 7 | - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com |
TheChrisyd | 0:6f4be1a7962c | 8 | - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46 |
TheChrisyd | 0:6f4be1a7962c | 9 | - Dominik Fischer / dom_fischer(at)web(dot)de |
TheChrisyd | 0:6f4be1a7962c | 10 | - Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com |
TheChrisyd | 0:6f4be1a7962c | 11 | - Chris Dick / Porting to mbed |
TheChrisyd | 0:6f4be1a7962c | 12 | |
TheChrisyd | 0:6f4be1a7962c | 13 | Project home: http://code.google.com/p/rc-switch/ |
TheChrisyd | 0:6f4be1a7962c | 14 | |
TheChrisyd | 0:6f4be1a7962c | 15 | This library is free software; you can redistribute it and/or |
TheChrisyd | 0:6f4be1a7962c | 16 | modify it under the terms of the GNU Lesser General Public |
TheChrisyd | 0:6f4be1a7962c | 17 | License as published by the Free Software Foundation; either |
TheChrisyd | 0:6f4be1a7962c | 18 | version 2.1 of the License, or (at your option) any later version. |
TheChrisyd | 0:6f4be1a7962c | 19 | |
TheChrisyd | 0:6f4be1a7962c | 20 | This library is distributed in the hope that it will be useful, |
TheChrisyd | 0:6f4be1a7962c | 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
TheChrisyd | 0:6f4be1a7962c | 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
TheChrisyd | 0:6f4be1a7962c | 23 | Lesser General Public License for more details. |
TheChrisyd | 0:6f4be1a7962c | 24 | |
TheChrisyd | 0:6f4be1a7962c | 25 | You should have received a copy of the GNU Lesser General Public |
TheChrisyd | 0:6f4be1a7962c | 26 | License along with this library; if not, write to the Free Software |
TheChrisyd | 0:6f4be1a7962c | 27 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
TheChrisyd | 0:6f4be1a7962c | 28 | */ |
TheChrisyd | 0:6f4be1a7962c | 29 | #include "RCSwitch.h" |
TheChrisyd | 0:6f4be1a7962c | 30 | |
TheChrisyd | 0:6f4be1a7962c | 31 | unsigned long RCSwitch::nReceivedValue = NULL; |
TheChrisyd | 0:6f4be1a7962c | 32 | unsigned int RCSwitch::nReceivedBitlength = 0; |
TheChrisyd | 0:6f4be1a7962c | 33 | unsigned int RCSwitch::nReceivedDelay = 0; |
TheChrisyd | 0:6f4be1a7962c | 34 | unsigned int RCSwitch::nReceivedProtocol = 0; |
TheChrisyd | 0:6f4be1a7962c | 35 | int RCSwitch::nReceiveTolerance = 60; |
TheChrisyd | 0:6f4be1a7962c | 36 | unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES]; |
TheChrisyd | 0:6f4be1a7962c | 37 | bool RCSwitch::TransmitEnablePin = false; |
TheChrisyd | 0:6f4be1a7962c | 38 | bool RCSwitch::ReceiveEnabled = true; |
TheChrisyd | 0:6f4be1a7962c | 39 | bool RCSwitch::TransmitEnable = true; |
TheChrisyd | 0:6f4be1a7962c | 40 | |
TheChrisyd | 0:6f4be1a7962c | 41 | /** Class constructor. |
TheChrisyd | 0:6f4be1a7962c | 42 | * The constructor assigns the specified pinout, attatches |
TheChrisyd | 0:6f4be1a7962c | 43 | * an Interrupt to the receive pin. for the LPC1768 this must not |
TheChrisyd | 0:6f4be1a7962c | 44 | * be pins 19 and 20. For the KL25Z, the pin must be on ports A or C |
TheChrisyd | 0:6f4be1a7962c | 45 | * @param tx Transmitter pin of the RF module. |
TheChrisyd | 0:6f4be1a7962c | 46 | * @param rx Receiver pin of the RF module. |
TheChrisyd | 0:6f4be1a7962c | 47 | */ |
TheChrisyd | 0:6f4be1a7962c | 48 | RCSwitch::RCSwitch(PinName tx, PinName rx) |
TheChrisyd | 0:6f4be1a7962c | 49 | : |
TheChrisyd | 0:6f4be1a7962c | 50 | _tx(DigitalOut(tx)), |
TheChrisyd | 0:6f4be1a7962c | 51 | _rx(InterruptIn(rx)), |
TheChrisyd | 0:6f4be1a7962c | 52 | _tx_en(NC) |
TheChrisyd | 0:6f4be1a7962c | 53 | { |
TheChrisyd | 0:6f4be1a7962c | 54 | nReceivedDelay = 0; |
TheChrisyd | 0:6f4be1a7962c | 55 | RCSwitch::nReceivedValue = NULL; |
TheChrisyd | 0:6f4be1a7962c | 56 | nReceivedBitlength = 0; |
TheChrisyd | 0:6f4be1a7962c | 57 | ReceiveEnabled = true; |
TheChrisyd | 0:6f4be1a7962c | 58 | TransmitEnable = true; |
TheChrisyd | 0:6f4be1a7962c | 59 | TransmitEnablePin = false; |
TheChrisyd | 0:6f4be1a7962c | 60 | setRepeatTransmit(15); |
TheChrisyd | 0:6f4be1a7962c | 61 | setProtocol(1); |
TheChrisyd | 0:6f4be1a7962c | 62 | setReceiveTolerance(60); |
TheChrisyd | 0:6f4be1a7962c | 63 | _rx.rise(this, &RCSwitch::RCSwitchRxPinChange); |
TheChrisyd | 0:6f4be1a7962c | 64 | _rx.fall(this, &RCSwitch::RCSwitchRxPinChange); |
TheChrisyd | 0:6f4be1a7962c | 65 | timer.start(); |
TheChrisyd | 0:6f4be1a7962c | 66 | } |
TheChrisyd | 0:6f4be1a7962c | 67 | |
TheChrisyd | 0:6f4be1a7962c | 68 | /** Class constructor. |
TheChrisyd | 0:6f4be1a7962c | 69 | * The constructor assigns the specified pinout, attatches |
TheChrisyd | 0:6f4be1a7962c | 70 | * an Interrupt to the receive pin. for the LPC1768 this must not |
TheChrisyd | 0:6f4be1a7962c | 71 | * be pins 19 and 20. For the KL25Z, the pin must be on ports A or C |
TheChrisyd | 0:6f4be1a7962c | 72 | * @param tx Transmitter pin of the RF module. |
TheChrisyd | 0:6f4be1a7962c | 73 | * @param rx Receiver pin of the RF module. |
TheChrisyd | 0:6f4be1a7962c | 74 | * @param tx_en Enable pin of the transmitter |
TheChrisyd | 0:6f4be1a7962c | 75 | */ |
TheChrisyd | 0:6f4be1a7962c | 76 | RCSwitch::RCSwitch(PinName tx, PinName rx, PinName tx_en) |
TheChrisyd | 0:6f4be1a7962c | 77 | : |
TheChrisyd | 0:6f4be1a7962c | 78 | _tx(DigitalOut(tx)), |
TheChrisyd | 0:6f4be1a7962c | 79 | _rx(InterruptIn(rx)), |
TheChrisyd | 0:6f4be1a7962c | 80 | _tx_en(DigitalOut(tx_en)) |
TheChrisyd | 0:6f4be1a7962c | 81 | { |
TheChrisyd | 0:6f4be1a7962c | 82 | nReceivedDelay = 0; |
TheChrisyd | 0:6f4be1a7962c | 83 | RCSwitch::nReceivedValue = NULL; |
TheChrisyd | 0:6f4be1a7962c | 84 | nReceivedBitlength = 0; |
TheChrisyd | 0:6f4be1a7962c | 85 | ReceiveEnabled = true; |
TheChrisyd | 0:6f4be1a7962c | 86 | TransmitEnable = false; |
TheChrisyd | 0:6f4be1a7962c | 87 | TransmitEnablePin = true; |
TheChrisyd | 0:6f4be1a7962c | 88 | setRepeatTransmit(15); |
TheChrisyd | 0:6f4be1a7962c | 89 | setProtocol(1); |
TheChrisyd | 0:6f4be1a7962c | 90 | setReceiveTolerance(60); |
TheChrisyd | 0:6f4be1a7962c | 91 | _rx.rise(this, &RCSwitch::RCSwitchRxPinChange); |
TheChrisyd | 0:6f4be1a7962c | 92 | _rx.fall(this, &RCSwitch::RCSwitchRxPinChange); |
TheChrisyd | 0:6f4be1a7962c | 93 | timer.start(); |
TheChrisyd | 0:6f4be1a7962c | 94 | |
TheChrisyd | 0:6f4be1a7962c | 95 | } |
TheChrisyd | 0:6f4be1a7962c | 96 | |
TheChrisyd | 0:6f4be1a7962c | 97 | /** |
TheChrisyd | 0:6f4be1a7962c | 98 | * Set protocol to be used in transmission |
TheChrisyd | 0:6f4be1a7962c | 99 | * @param nProtocol Protocol type ot transmit |
TheChrisyd | 0:6f4be1a7962c | 100 | */ |
TheChrisyd | 0:6f4be1a7962c | 101 | void RCSwitch::setProtocol(int nProtocol) { |
TheChrisyd | 0:6f4be1a7962c | 102 | this->nProtocol = nProtocol; |
TheChrisyd | 0:6f4be1a7962c | 103 | if (nProtocol == 1){ |
TheChrisyd | 0:6f4be1a7962c | 104 | this->setPulseLength(350); |
TheChrisyd | 0:6f4be1a7962c | 105 | } |
TheChrisyd | 0:6f4be1a7962c | 106 | else if (nProtocol == 2) { |
TheChrisyd | 0:6f4be1a7962c | 107 | this->setPulseLength(650); |
TheChrisyd | 0:6f4be1a7962c | 108 | } |
TheChrisyd | 0:6f4be1a7962c | 109 | else if (nProtocol == 3) { |
TheChrisyd | 0:6f4be1a7962c | 110 | this->setPulseLength(100); |
TheChrisyd | 0:6f4be1a7962c | 111 | } |
TheChrisyd | 0:6f4be1a7962c | 112 | } |
TheChrisyd | 0:6f4be1a7962c | 113 | |
TheChrisyd | 0:6f4be1a7962c | 114 | /** |
TheChrisyd | 0:6f4be1a7962c | 115 | * Set protocol to be used in transmission |
TheChrisyd | 0:6f4be1a7962c | 116 | * @param nProtocol Protocol type ot transmit |
TheChrisyd | 0:6f4be1a7962c | 117 | * @param nPulseLength Length of each pulse |
TheChrisyd | 0:6f4be1a7962c | 118 | */ |
TheChrisyd | 0:6f4be1a7962c | 119 | void RCSwitch::setProtocol(int nProtocol, int nPulseLength) { |
TheChrisyd | 0:6f4be1a7962c | 120 | this->nProtocol = nProtocol; |
TheChrisyd | 0:6f4be1a7962c | 121 | this->setPulseLength(nPulseLength); |
TheChrisyd | 0:6f4be1a7962c | 122 | } |
TheChrisyd | 0:6f4be1a7962c | 123 | |
TheChrisyd | 0:6f4be1a7962c | 124 | |
TheChrisyd | 0:6f4be1a7962c | 125 | /** |
TheChrisyd | 0:6f4be1a7962c | 126 | * Set pulse length in micro seconds |
TheChrisyd | 0:6f4be1a7962c | 127 | * @param nPulseLength the Length of the pulse |
TheChrisyd | 0:6f4be1a7962c | 128 | */ |
TheChrisyd | 0:6f4be1a7962c | 129 | void RCSwitch::setPulseLength(int nPulseLength) { |
TheChrisyd | 0:6f4be1a7962c | 130 | this->nPulseLength = nPulseLength; |
TheChrisyd | 0:6f4be1a7962c | 131 | } |
TheChrisyd | 0:6f4be1a7962c | 132 | |
TheChrisyd | 0:6f4be1a7962c | 133 | /** |
TheChrisyd | 0:6f4be1a7962c | 134 | * Set number of times to repeat transmission |
TheChrisyd | 0:6f4be1a7962c | 135 | * @param nRepeat Number of repeats |
TheChrisyd | 0:6f4be1a7962c | 136 | */ |
TheChrisyd | 0:6f4be1a7962c | 137 | void RCSwitch::setRepeatTransmit(int nRepeatTransmit) { |
TheChrisyd | 0:6f4be1a7962c | 138 | this->nRepeatTransmit = nRepeatTransmit; |
TheChrisyd | 0:6f4be1a7962c | 139 | } |
TheChrisyd | 0:6f4be1a7962c | 140 | |
TheChrisyd | 0:6f4be1a7962c | 141 | /** |
TheChrisyd | 0:6f4be1a7962c | 142 | * Set receive tolerance |
TheChrisyd | 0:6f4be1a7962c | 143 | * @param nPercent Percentage tolerance of the receiver |
TheChrisyd | 0:6f4be1a7962c | 144 | */ |
TheChrisyd | 0:6f4be1a7962c | 145 | void RCSwitch::setReceiveTolerance(int nPercent) { |
TheChrisyd | 0:6f4be1a7962c | 146 | RCSwitch::nReceiveTolerance = nPercent; |
TheChrisyd | 0:6f4be1a7962c | 147 | } |
TheChrisyd | 0:6f4be1a7962c | 148 | |
TheChrisyd | 0:6f4be1a7962c | 149 | |
TheChrisyd | 0:6f4be1a7962c | 150 | /** |
TheChrisyd | 0:6f4be1a7962c | 151 | * Enable the transmitter |
TheChrisyd | 0:6f4be1a7962c | 152 | */ |
TheChrisyd | 0:6f4be1a7962c | 153 | void RCSwitch::enableTransmit() { |
TheChrisyd | 0:6f4be1a7962c | 154 | if (TransmitEnablePin == true) |
TheChrisyd | 0:6f4be1a7962c | 155 | { |
TheChrisyd | 0:6f4be1a7962c | 156 | _tx_en = true; |
TheChrisyd | 0:6f4be1a7962c | 157 | } |
TheChrisyd | 0:6f4be1a7962c | 158 | TransmitEnable = true; |
TheChrisyd | 0:6f4be1a7962c | 159 | } |
TheChrisyd | 0:6f4be1a7962c | 160 | |
TheChrisyd | 0:6f4be1a7962c | 161 | /** |
TheChrisyd | 0:6f4be1a7962c | 162 | * Disable the transmitter |
TheChrisyd | 0:6f4be1a7962c | 163 | */ |
TheChrisyd | 0:6f4be1a7962c | 164 | void RCSwitch::disableTransmit() { |
TheChrisyd | 0:6f4be1a7962c | 165 | if (TransmitEnablePin == true) |
TheChrisyd | 0:6f4be1a7962c | 166 | { |
TheChrisyd | 0:6f4be1a7962c | 167 | _tx_en = false; |
TheChrisyd | 0:6f4be1a7962c | 168 | } |
TheChrisyd | 0:6f4be1a7962c | 169 | TransmitEnable = false; |
TheChrisyd | 0:6f4be1a7962c | 170 | } |
TheChrisyd | 0:6f4be1a7962c | 171 | |
TheChrisyd | 0:6f4be1a7962c | 172 | /** |
TheChrisyd | 0:6f4be1a7962c | 173 | * Switch a remote switch on (Type D REV) |
TheChrisyd | 0:6f4be1a7962c | 174 | * |
TheChrisyd | 0:6f4be1a7962c | 175 | * @param sGroup Code of the switch group (A,B,C,D) |
TheChrisyd | 0:6f4be1a7962c | 176 | * @param nDevice Number of the switch itself (1..3) |
TheChrisyd | 0:6f4be1a7962c | 177 | */ |
TheChrisyd | 0:6f4be1a7962c | 178 | void RCSwitch::switchOn(char sGroup, int nDevice) { |
TheChrisyd | 0:6f4be1a7962c | 179 | this->sendTriState( this->getCodeWordD(sGroup, nDevice, true) ); |
TheChrisyd | 0:6f4be1a7962c | 180 | } |
TheChrisyd | 0:6f4be1a7962c | 181 | |
TheChrisyd | 0:6f4be1a7962c | 182 | /** |
TheChrisyd | 0:6f4be1a7962c | 183 | * Switch a remote switch off (Type D REV) |
TheChrisyd | 0:6f4be1a7962c | 184 | * |
TheChrisyd | 0:6f4be1a7962c | 185 | * @param sGroup Code of the switch group (A,B,C,D) |
TheChrisyd | 0:6f4be1a7962c | 186 | * @param nDevice Number of the switch itself (1..3) |
TheChrisyd | 0:6f4be1a7962c | 187 | */ |
TheChrisyd | 0:6f4be1a7962c | 188 | void RCSwitch::switchOff(char sGroup, int nDevice) { |
TheChrisyd | 0:6f4be1a7962c | 189 | this->sendTriState( this->getCodeWordD(sGroup, nDevice, false) ); |
TheChrisyd | 0:6f4be1a7962c | 190 | } |
TheChrisyd | 0:6f4be1a7962c | 191 | |
TheChrisyd | 0:6f4be1a7962c | 192 | /** |
TheChrisyd | 0:6f4be1a7962c | 193 | * Switch a remote switch on (Type C Intertechno) |
TheChrisyd | 0:6f4be1a7962c | 194 | * |
TheChrisyd | 0:6f4be1a7962c | 195 | * @param sFamily Familycode (a..f) |
TheChrisyd | 0:6f4be1a7962c | 196 | * @param nGroup Number of group (1..4) |
TheChrisyd | 0:6f4be1a7962c | 197 | * @param nDevice Number of device (1..4) |
TheChrisyd | 0:6f4be1a7962c | 198 | */ |
TheChrisyd | 0:6f4be1a7962c | 199 | void RCSwitch::switchOn(char sFamily, int nGroup, int nDevice) { |
TheChrisyd | 0:6f4be1a7962c | 200 | this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, true) ); |
TheChrisyd | 0:6f4be1a7962c | 201 | } |
TheChrisyd | 0:6f4be1a7962c | 202 | |
TheChrisyd | 0:6f4be1a7962c | 203 | /** |
TheChrisyd | 0:6f4be1a7962c | 204 | * Switch a remote switch off (Type C Intertechno) |
TheChrisyd | 0:6f4be1a7962c | 205 | * |
TheChrisyd | 0:6f4be1a7962c | 206 | * @param sFamily Familycode (a..f) |
TheChrisyd | 0:6f4be1a7962c | 207 | * @param nGroup Number of group (1..4) |
TheChrisyd | 0:6f4be1a7962c | 208 | * @param nDevice Number of device (1..4) |
TheChrisyd | 0:6f4be1a7962c | 209 | */ |
TheChrisyd | 0:6f4be1a7962c | 210 | void RCSwitch::switchOff(char sFamily, int nGroup, int nDevice) { |
TheChrisyd | 0:6f4be1a7962c | 211 | this->sendTriState( this->getCodeWordC(sFamily, nGroup, nDevice, false) ); |
TheChrisyd | 0:6f4be1a7962c | 212 | } |
TheChrisyd | 0:6f4be1a7962c | 213 | |
TheChrisyd | 0:6f4be1a7962c | 214 | /** |
TheChrisyd | 0:6f4be1a7962c | 215 | * Switch a remote switch on (Type B with two rotary/sliding switches) |
TheChrisyd | 0:6f4be1a7962c | 216 | * |
TheChrisyd | 0:6f4be1a7962c | 217 | * @param nAddressCode Number of the switch group (1..4) |
TheChrisyd | 0:6f4be1a7962c | 218 | * @param nChannelCode Number of the switch itself (1..4) |
TheChrisyd | 0:6f4be1a7962c | 219 | */ |
TheChrisyd | 0:6f4be1a7962c | 220 | void RCSwitch::switchOn(int nAddressCode, int nChannelCode) { |
TheChrisyd | 0:6f4be1a7962c | 221 | this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, true) ); |
TheChrisyd | 0:6f4be1a7962c | 222 | } |
TheChrisyd | 0:6f4be1a7962c | 223 | |
TheChrisyd | 0:6f4be1a7962c | 224 | /** |
TheChrisyd | 0:6f4be1a7962c | 225 | * Switch a remote switch off (Type B with two rotary/sliding switches) |
TheChrisyd | 0:6f4be1a7962c | 226 | * |
TheChrisyd | 0:6f4be1a7962c | 227 | * @param nAddressCode Number of the switch group (1..4) |
TheChrisyd | 0:6f4be1a7962c | 228 | * @param nChannelCode Number of the switch itself (1..4) |
TheChrisyd | 0:6f4be1a7962c | 229 | */ |
TheChrisyd | 0:6f4be1a7962c | 230 | void RCSwitch::switchOff(int nAddressCode, int nChannelCode) { |
TheChrisyd | 0:6f4be1a7962c | 231 | this->sendTriState( this->getCodeWordB(nAddressCode, nChannelCode, false) ); |
TheChrisyd | 0:6f4be1a7962c | 232 | } |
TheChrisyd | 0:6f4be1a7962c | 233 | |
TheChrisyd | 0:6f4be1a7962c | 234 | /** |
TheChrisyd | 0:6f4be1a7962c | 235 | * Deprecated, use switchOn(char* sGroup, char* sDevice) instead! |
TheChrisyd | 0:6f4be1a7962c | 236 | * Switch a remote switch on (Type A with 10 pole DIP switches) |
TheChrisyd | 0:6f4be1a7962c | 237 | * |
TheChrisyd | 0:6f4be1a7962c | 238 | * @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") |
TheChrisyd | 0:6f4be1a7962c | 239 | * @param nChannelCode Number of the switch itself (1..5) |
TheChrisyd | 0:6f4be1a7962c | 240 | */ |
TheChrisyd | 0:6f4be1a7962c | 241 | void RCSwitch::switchOn(char* sGroup, int nChannel) { |
TheChrisyd | 0:6f4be1a7962c | 242 | char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" }; |
TheChrisyd | 0:6f4be1a7962c | 243 | this->switchOn(sGroup, code[nChannel]); |
TheChrisyd | 0:6f4be1a7962c | 244 | } |
TheChrisyd | 0:6f4be1a7962c | 245 | |
TheChrisyd | 0:6f4be1a7962c | 246 | /** |
TheChrisyd | 0:6f4be1a7962c | 247 | * Deprecated, use switchOff(char* sGroup, char* sDevice) instead! |
TheChrisyd | 0:6f4be1a7962c | 248 | * Switch a remote switch off (Type A with 10 pole DIP switches) |
TheChrisyd | 0:6f4be1a7962c | 249 | * |
TheChrisyd | 0:6f4be1a7962c | 250 | * @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") |
TheChrisyd | 0:6f4be1a7962c | 251 | * @param nChannelCode Number of the switch itself (1..5) |
TheChrisyd | 0:6f4be1a7962c | 252 | */ |
TheChrisyd | 0:6f4be1a7962c | 253 | void RCSwitch::switchOff(char* sGroup, int nChannel) { |
TheChrisyd | 0:6f4be1a7962c | 254 | char* code[6] = { "00000", "10000", "01000", "00100", "00010", "00001" }; |
TheChrisyd | 0:6f4be1a7962c | 255 | this->switchOff(sGroup, code[nChannel]); |
TheChrisyd | 0:6f4be1a7962c | 256 | } |
TheChrisyd | 0:6f4be1a7962c | 257 | |
TheChrisyd | 0:6f4be1a7962c | 258 | /** |
TheChrisyd | 0:6f4be1a7962c | 259 | * Switch a remote switch on (Type A with 10 pole DIP switches) |
TheChrisyd | 0:6f4be1a7962c | 260 | * |
TheChrisyd | 0:6f4be1a7962c | 261 | * @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") |
TheChrisyd | 0:6f4be1a7962c | 262 | * @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") |
TheChrisyd | 0:6f4be1a7962c | 263 | */ |
TheChrisyd | 0:6f4be1a7962c | 264 | void RCSwitch::switchOn(char* sGroup, char* sDevice) { |
TheChrisyd | 0:6f4be1a7962c | 265 | this->sendTriState( this->getCodeWordA(sGroup, sDevice, true) ); |
TheChrisyd | 0:6f4be1a7962c | 266 | } |
TheChrisyd | 0:6f4be1a7962c | 267 | |
TheChrisyd | 0:6f4be1a7962c | 268 | /** |
TheChrisyd | 0:6f4be1a7962c | 269 | * Switch a remote switch off (Type A with 10 pole DIP switches) |
TheChrisyd | 0:6f4be1a7962c | 270 | * |
TheChrisyd | 0:6f4be1a7962c | 271 | * @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") |
TheChrisyd | 0:6f4be1a7962c | 272 | * @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") |
TheChrisyd | 0:6f4be1a7962c | 273 | */ |
TheChrisyd | 0:6f4be1a7962c | 274 | void RCSwitch::switchOff(char* sGroup, char* sDevice) { |
TheChrisyd | 0:6f4be1a7962c | 275 | this->sendTriState( this->getCodeWordA(sGroup, sDevice, false) ); |
TheChrisyd | 0:6f4be1a7962c | 276 | } |
TheChrisyd | 0:6f4be1a7962c | 277 | |
TheChrisyd | 0:6f4be1a7962c | 278 | /** |
TheChrisyd | 0:6f4be1a7962c | 279 | * Returns a char[13], representing the Code Word to be send. |
TheChrisyd | 0:6f4be1a7962c | 280 | * 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. |
TheChrisyd | 0:6f4be1a7962c | 281 | * A Code Bit can have 4 different states: "F" (floating), "0" (low), "1" (high), "S" (synchronous bit) |
TheChrisyd | 0:6f4be1a7962c | 282 | * |
TheChrisyd | 0:6f4be1a7962c | 283 | * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+ |
TheChrisyd | 0:6f4be1a7962c | 284 | * | 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 | |
TheChrisyd | 0:6f4be1a7962c | 285 | * | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | F | F | on=FF off=F0 | S | |
TheChrisyd | 0:6f4be1a7962c | 286 | * +-------------------------------+--------------------------------+-----------------------------------------+-----------------------------------------+----------------------+------------+ |
TheChrisyd | 0:6f4be1a7962c | 287 | * |
TheChrisyd | 0:6f4be1a7962c | 288 | * @param nAddressCode Number of the switch group (1..4) |
TheChrisyd | 0:6f4be1a7962c | 289 | * @param nChannelCode Number of the switch itself (1..4) |
TheChrisyd | 0:6f4be1a7962c | 290 | * @param bStatus Wether to switch on (true) or off (false) |
TheChrisyd | 0:6f4be1a7962c | 291 | * |
TheChrisyd | 0:6f4be1a7962c | 292 | * @return char[13] |
TheChrisyd | 0:6f4be1a7962c | 293 | */ |
TheChrisyd | 0:6f4be1a7962c | 294 | char* RCSwitch::getCodeWordB(int nAddressCode, int nChannelCode, bool bStatus) { |
TheChrisyd | 0:6f4be1a7962c | 295 | int nReturnPos = 0; |
TheChrisyd | 0:6f4be1a7962c | 296 | static char sReturn[13]; |
TheChrisyd | 0:6f4be1a7962c | 297 | |
TheChrisyd | 0:6f4be1a7962c | 298 | char* code[5] = { "FFFF", "0FFF", "F0FF", "FF0F", "FFF0" }; |
TheChrisyd | 0:6f4be1a7962c | 299 | if (nAddressCode < 1 || nAddressCode > 4 || nChannelCode < 1 || nChannelCode > 4) { |
TheChrisyd | 0:6f4be1a7962c | 300 | return '\0'; |
TheChrisyd | 0:6f4be1a7962c | 301 | } |
TheChrisyd | 0:6f4be1a7962c | 302 | for (int i = 0; i<4; i++) { |
TheChrisyd | 0:6f4be1a7962c | 303 | sReturn[nReturnPos++] = code[nAddressCode][i]; |
TheChrisyd | 0:6f4be1a7962c | 304 | } |
TheChrisyd | 0:6f4be1a7962c | 305 | |
TheChrisyd | 0:6f4be1a7962c | 306 | for (int i = 0; i<4; i++) { |
TheChrisyd | 0:6f4be1a7962c | 307 | sReturn[nReturnPos++] = code[nChannelCode][i]; |
TheChrisyd | 0:6f4be1a7962c | 308 | } |
TheChrisyd | 0:6f4be1a7962c | 309 | |
TheChrisyd | 0:6f4be1a7962c | 310 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 311 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 312 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 313 | |
TheChrisyd | 0:6f4be1a7962c | 314 | if (bStatus) { |
TheChrisyd | 0:6f4be1a7962c | 315 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 316 | } else { |
TheChrisyd | 0:6f4be1a7962c | 317 | sReturn[nReturnPos++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 318 | } |
TheChrisyd | 0:6f4be1a7962c | 319 | |
TheChrisyd | 0:6f4be1a7962c | 320 | sReturn[nReturnPos] = '\0'; |
TheChrisyd | 0:6f4be1a7962c | 321 | |
TheChrisyd | 0:6f4be1a7962c | 322 | return sReturn; |
TheChrisyd | 0:6f4be1a7962c | 323 | } |
TheChrisyd | 0:6f4be1a7962c | 324 | |
TheChrisyd | 0:6f4be1a7962c | 325 | /** |
TheChrisyd | 0:6f4be1a7962c | 326 | * Returns a char[13], representing the Code Word to be send. |
TheChrisyd | 0:6f4be1a7962c | 327 | * |
TheChrisyd | 0:6f4be1a7962c | 328 | * getCodeWordA(char*, char*) |
TheChrisyd | 0:6f4be1a7962c | 329 | * |
TheChrisyd | 0:6f4be1a7962c | 330 | */ |
TheChrisyd | 0:6f4be1a7962c | 331 | char* RCSwitch::getCodeWordA(char* sGroup, char* sDevice, bool bOn) { |
TheChrisyd | 0:6f4be1a7962c | 332 | static char sDipSwitches[13]; |
TheChrisyd | 0:6f4be1a7962c | 333 | int i = 0; |
TheChrisyd | 0:6f4be1a7962c | 334 | int j = 0; |
TheChrisyd | 0:6f4be1a7962c | 335 | |
TheChrisyd | 0:6f4be1a7962c | 336 | for (i=0; i < 5; i++) { |
TheChrisyd | 0:6f4be1a7962c | 337 | if (sGroup[i] == '0') { |
TheChrisyd | 0:6f4be1a7962c | 338 | sDipSwitches[j++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 339 | } else { |
TheChrisyd | 0:6f4be1a7962c | 340 | sDipSwitches[j++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 341 | } |
TheChrisyd | 0:6f4be1a7962c | 342 | } |
TheChrisyd | 0:6f4be1a7962c | 343 | |
TheChrisyd | 0:6f4be1a7962c | 344 | for (i=0; i < 5; i++) { |
TheChrisyd | 0:6f4be1a7962c | 345 | if (sDevice[i] == '0') { |
TheChrisyd | 0:6f4be1a7962c | 346 | sDipSwitches[j++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 347 | } else { |
TheChrisyd | 0:6f4be1a7962c | 348 | sDipSwitches[j++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 349 | } |
TheChrisyd | 0:6f4be1a7962c | 350 | } |
TheChrisyd | 0:6f4be1a7962c | 351 | |
TheChrisyd | 0:6f4be1a7962c | 352 | if ( bOn ) { |
TheChrisyd | 0:6f4be1a7962c | 353 | sDipSwitches[j++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 354 | sDipSwitches[j++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 355 | } else { |
TheChrisyd | 0:6f4be1a7962c | 356 | sDipSwitches[j++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 357 | sDipSwitches[j++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 358 | } |
TheChrisyd | 0:6f4be1a7962c | 359 | |
TheChrisyd | 0:6f4be1a7962c | 360 | sDipSwitches[j] = '\0'; |
TheChrisyd | 0:6f4be1a7962c | 361 | |
TheChrisyd | 0:6f4be1a7962c | 362 | return sDipSwitches; |
TheChrisyd | 0:6f4be1a7962c | 363 | } |
TheChrisyd | 0:6f4be1a7962c | 364 | |
TheChrisyd | 0:6f4be1a7962c | 365 | /** |
TheChrisyd | 0:6f4be1a7962c | 366 | * Like getCodeWord (Type C = Intertechno) |
TheChrisyd | 0:6f4be1a7962c | 367 | */ |
TheChrisyd | 0:6f4be1a7962c | 368 | char* RCSwitch::getCodeWordC(char sFamily, int nGroup, int nDevice, bool bStatus) { |
TheChrisyd | 0:6f4be1a7962c | 369 | static char sReturn[13]; |
TheChrisyd | 0:6f4be1a7962c | 370 | int nReturnPos = 0; |
TheChrisyd | 0:6f4be1a7962c | 371 | |
TheChrisyd | 0:6f4be1a7962c | 372 | if ( (char)sFamily < 97 || (char)sFamily > 112 || nGroup < 1 || nGroup > 4 || nDevice < 1 || nDevice > 4) { |
TheChrisyd | 0:6f4be1a7962c | 373 | return '\0'; |
TheChrisyd | 0:6f4be1a7962c | 374 | } |
TheChrisyd | 0:6f4be1a7962c | 375 | |
TheChrisyd | 0:6f4be1a7962c | 376 | char* sDeviceGroupCode = dec2binWzerofill( (nDevice-1) + (nGroup-1)*4, 4 ); |
TheChrisyd | 0:6f4be1a7962c | 377 | char familycode[16][5] = { "0000", "F000", "0F00", "FF00", "00F0", "F0F0", "0FF0", "FFF0", "000F", "F00F", "0F0F", "FF0F", "00FF", "F0FF", "0FFF", "FFFF" }; |
TheChrisyd | 0:6f4be1a7962c | 378 | for (int i = 0; i<4; i++) { |
TheChrisyd | 0:6f4be1a7962c | 379 | sReturn[nReturnPos++] = familycode[ (int)sFamily - 97 ][i]; |
TheChrisyd | 0:6f4be1a7962c | 380 | } |
TheChrisyd | 0:6f4be1a7962c | 381 | for (int i = 0; i<4; i++) { |
TheChrisyd | 0:6f4be1a7962c | 382 | sReturn[nReturnPos++] = (sDeviceGroupCode[3-i] == '1' ? 'F' : '0'); |
TheChrisyd | 0:6f4be1a7962c | 383 | } |
TheChrisyd | 0:6f4be1a7962c | 384 | sReturn[nReturnPos++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 385 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 386 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 387 | if (bStatus) { |
TheChrisyd | 0:6f4be1a7962c | 388 | sReturn[nReturnPos++] = 'F'; |
TheChrisyd | 0:6f4be1a7962c | 389 | } else { |
TheChrisyd | 0:6f4be1a7962c | 390 | sReturn[nReturnPos++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 391 | } |
TheChrisyd | 0:6f4be1a7962c | 392 | sReturn[nReturnPos] = '\0'; |
TheChrisyd | 0:6f4be1a7962c | 393 | return sReturn; |
TheChrisyd | 0:6f4be1a7962c | 394 | } |
TheChrisyd | 0:6f4be1a7962c | 395 | |
TheChrisyd | 0:6f4be1a7962c | 396 | /** |
TheChrisyd | 0:6f4be1a7962c | 397 | * Decoding for the REV Switch Type |
TheChrisyd | 0:6f4be1a7962c | 398 | * |
TheChrisyd | 0:6f4be1a7962c | 399 | * Returns a char[13], representing the Tristate to be send. |
TheChrisyd | 0:6f4be1a7962c | 400 | * A Code Word consists of 7 address bits and 5 command data bits. |
TheChrisyd | 0:6f4be1a7962c | 401 | * A Code Bit can have 3 different states: "F" (floating), "0" (low), "1" (high) |
TheChrisyd | 0:6f4be1a7962c | 402 | * |
TheChrisyd | 0:6f4be1a7962c | 403 | * +-------------------------------+--------------------------------+-----------------------+ |
TheChrisyd | 0:6f4be1a7962c | 404 | * | 4 bits address (switch group) | 3 bits address (device number) | 5 bits (command data) | |
TheChrisyd | 0:6f4be1a7962c | 405 | * | A=1FFF B=F1FF C=FF1F D=FFF1 | 1=0FFF 2=F0FF 3=FF0F 4=FFF0 | on=00010 off=00001 | |
TheChrisyd | 0:6f4be1a7962c | 406 | * +-------------------------------+--------------------------------+-----------------------+ |
TheChrisyd | 0:6f4be1a7962c | 407 | * |
TheChrisyd | 0:6f4be1a7962c | 408 | * Source: http://www.the-intruder.net/funksteckdosen-von-rev-uber-arduino-ansteuern/ |
TheChrisyd | 0:6f4be1a7962c | 409 | * |
TheChrisyd | 0:6f4be1a7962c | 410 | * @param sGroup Name of the switch group (A..D, resp. a..d) |
TheChrisyd | 0:6f4be1a7962c | 411 | * @param nDevice Number of the switch itself (1..3) |
TheChrisyd | 0:6f4be1a7962c | 412 | * @param bStatus Wether to switch on (true) or off (false) |
TheChrisyd | 0:6f4be1a7962c | 413 | * |
TheChrisyd | 0:6f4be1a7962c | 414 | * @return char[13] |
TheChrisyd | 0:6f4be1a7962c | 415 | */ |
TheChrisyd | 0:6f4be1a7962c | 416 | |
TheChrisyd | 0:6f4be1a7962c | 417 | char* RCSwitch::getCodeWordD(char sGroup, int nDevice, bool bStatus){ |
TheChrisyd | 0:6f4be1a7962c | 418 | static char sReturn[13]; |
TheChrisyd | 0:6f4be1a7962c | 419 | int nReturnPos = 0; |
TheChrisyd | 0:6f4be1a7962c | 420 | |
TheChrisyd | 0:6f4be1a7962c | 421 | // Building 4 bits address |
TheChrisyd | 0:6f4be1a7962c | 422 | // (Potential problem if dec2binWcharfill not returning correct string) |
TheChrisyd | 0:6f4be1a7962c | 423 | char *sGroupCode; |
TheChrisyd | 0:6f4be1a7962c | 424 | switch(sGroup){ |
TheChrisyd | 0:6f4be1a7962c | 425 | case 'a': |
TheChrisyd | 0:6f4be1a7962c | 426 | case 'A': |
TheChrisyd | 0:6f4be1a7962c | 427 | sGroupCode = dec2binWcharfill(8, 4, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 428 | case 'b': |
TheChrisyd | 0:6f4be1a7962c | 429 | case 'B': |
TheChrisyd | 0:6f4be1a7962c | 430 | sGroupCode = dec2binWcharfill(4, 4, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 431 | case 'c': |
TheChrisyd | 0:6f4be1a7962c | 432 | case 'C': |
TheChrisyd | 0:6f4be1a7962c | 433 | sGroupCode = dec2binWcharfill(2, 4, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 434 | case 'd': |
TheChrisyd | 0:6f4be1a7962c | 435 | case 'D': |
TheChrisyd | 0:6f4be1a7962c | 436 | sGroupCode = dec2binWcharfill(1, 4, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 437 | default: |
TheChrisyd | 0:6f4be1a7962c | 438 | return '\0'; |
TheChrisyd | 0:6f4be1a7962c | 439 | } |
TheChrisyd | 0:6f4be1a7962c | 440 | |
TheChrisyd | 0:6f4be1a7962c | 441 | for (int i = 0; i<4; i++) |
TheChrisyd | 0:6f4be1a7962c | 442 | { |
TheChrisyd | 0:6f4be1a7962c | 443 | sReturn[nReturnPos++] = sGroupCode[i]; |
TheChrisyd | 0:6f4be1a7962c | 444 | } |
TheChrisyd | 0:6f4be1a7962c | 445 | |
TheChrisyd | 0:6f4be1a7962c | 446 | |
TheChrisyd | 0:6f4be1a7962c | 447 | // Building 3 bits address |
TheChrisyd | 0:6f4be1a7962c | 448 | // (Potential problem if dec2binWcharfill not returning correct string) |
TheChrisyd | 0:6f4be1a7962c | 449 | char *sDevice; |
TheChrisyd | 0:6f4be1a7962c | 450 | switch(nDevice) { |
TheChrisyd | 0:6f4be1a7962c | 451 | case 1: |
TheChrisyd | 0:6f4be1a7962c | 452 | sDevice = dec2binWcharfill(4, 3, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 453 | case 2: |
TheChrisyd | 0:6f4be1a7962c | 454 | sDevice = dec2binWcharfill(2, 3, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 455 | case 3: |
TheChrisyd | 0:6f4be1a7962c | 456 | sDevice = dec2binWcharfill(1, 3, 'F'); break; |
TheChrisyd | 0:6f4be1a7962c | 457 | default: |
TheChrisyd | 0:6f4be1a7962c | 458 | return '\0'; |
TheChrisyd | 0:6f4be1a7962c | 459 | } |
TheChrisyd | 0:6f4be1a7962c | 460 | |
TheChrisyd | 0:6f4be1a7962c | 461 | for (int i = 0; i<3; i++) |
TheChrisyd | 0:6f4be1a7962c | 462 | sReturn[nReturnPos++] = sDevice[i]; |
TheChrisyd | 0:6f4be1a7962c | 463 | |
TheChrisyd | 0:6f4be1a7962c | 464 | // fill up rest with zeros |
TheChrisyd | 0:6f4be1a7962c | 465 | for (int i = 0; i<5; i++) |
TheChrisyd | 0:6f4be1a7962c | 466 | sReturn[nReturnPos++] = '0'; |
TheChrisyd | 0:6f4be1a7962c | 467 | |
TheChrisyd | 0:6f4be1a7962c | 468 | // encode on or off |
TheChrisyd | 0:6f4be1a7962c | 469 | if (bStatus) |
TheChrisyd | 0:6f4be1a7962c | 470 | sReturn[10] = '1'; |
TheChrisyd | 0:6f4be1a7962c | 471 | else |
TheChrisyd | 0:6f4be1a7962c | 472 | sReturn[11] = '1'; |
TheChrisyd | 0:6f4be1a7962c | 473 | |
TheChrisyd | 0:6f4be1a7962c | 474 | // last position terminate string |
TheChrisyd | 0:6f4be1a7962c | 475 | sReturn[12] = '\0'; |
TheChrisyd | 0:6f4be1a7962c | 476 | return sReturn; |
TheChrisyd | 0:6f4be1a7962c | 477 | |
TheChrisyd | 0:6f4be1a7962c | 478 | } |
TheChrisyd | 0:6f4be1a7962c | 479 | |
TheChrisyd | 0:6f4be1a7962c | 480 | /** |
TheChrisyd | 0:6f4be1a7962c | 481 | * Sends a codeword |
TheChrisyd | 0:6f4be1a7962c | 482 | * @param sCodeWord Codeword to be sent |
TheChrisyd | 0:6f4be1a7962c | 483 | */ |
TheChrisyd | 0:6f4be1a7962c | 484 | void RCSwitch::sendTriState(char* sCodeWord) { |
TheChrisyd | 0:6f4be1a7962c | 485 | for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) { |
TheChrisyd | 0:6f4be1a7962c | 486 | int i = 0; |
TheChrisyd | 0:6f4be1a7962c | 487 | while (sCodeWord[i] != '\0') { |
TheChrisyd | 0:6f4be1a7962c | 488 | switch(sCodeWord[i]) { |
TheChrisyd | 0:6f4be1a7962c | 489 | case '0': |
TheChrisyd | 0:6f4be1a7962c | 490 | this->sendT0(); |
TheChrisyd | 0:6f4be1a7962c | 491 | break; |
TheChrisyd | 0:6f4be1a7962c | 492 | case 'F': |
TheChrisyd | 0:6f4be1a7962c | 493 | this->sendTF(); |
TheChrisyd | 0:6f4be1a7962c | 494 | break; |
TheChrisyd | 0:6f4be1a7962c | 495 | case '1': |
TheChrisyd | 0:6f4be1a7962c | 496 | this->sendT1(); |
TheChrisyd | 0:6f4be1a7962c | 497 | break; |
TheChrisyd | 0:6f4be1a7962c | 498 | } |
TheChrisyd | 0:6f4be1a7962c | 499 | i++; |
TheChrisyd | 0:6f4be1a7962c | 500 | } |
TheChrisyd | 0:6f4be1a7962c | 501 | this->sendSync(); |
TheChrisyd | 0:6f4be1a7962c | 502 | } |
TheChrisyd | 0:6f4be1a7962c | 503 | } |
TheChrisyd | 0:6f4be1a7962c | 504 | |
TheChrisyd | 0:6f4be1a7962c | 505 | void RCSwitch::send(unsigned long Code, unsigned int length) { |
TheChrisyd | 0:6f4be1a7962c | 506 | this->send( this->dec2binWzerofill(Code, length) ); |
TheChrisyd | 0:6f4be1a7962c | 507 | } |
TheChrisyd | 0:6f4be1a7962c | 508 | |
TheChrisyd | 0:6f4be1a7962c | 509 | void RCSwitch::send(char* sCodeWord) { |
TheChrisyd | 0:6f4be1a7962c | 510 | for (int nRepeat=0; nRepeat<nRepeatTransmit; nRepeat++) { |
TheChrisyd | 0:6f4be1a7962c | 511 | int i = 0; |
TheChrisyd | 0:6f4be1a7962c | 512 | while (sCodeWord[i] != '\0') { |
TheChrisyd | 0:6f4be1a7962c | 513 | switch(sCodeWord[i]) { |
TheChrisyd | 0:6f4be1a7962c | 514 | case '0': |
TheChrisyd | 0:6f4be1a7962c | 515 | this->send0(); |
TheChrisyd | 0:6f4be1a7962c | 516 | break; |
TheChrisyd | 0:6f4be1a7962c | 517 | case '1': |
TheChrisyd | 0:6f4be1a7962c | 518 | this->send1(); |
TheChrisyd | 0:6f4be1a7962c | 519 | break; |
TheChrisyd | 0:6f4be1a7962c | 520 | } |
TheChrisyd | 0:6f4be1a7962c | 521 | i++; |
TheChrisyd | 0:6f4be1a7962c | 522 | } |
TheChrisyd | 0:6f4be1a7962c | 523 | this->sendSync(); |
TheChrisyd | 0:6f4be1a7962c | 524 | } |
TheChrisyd | 0:6f4be1a7962c | 525 | } |
TheChrisyd | 0:6f4be1a7962c | 526 | |
TheChrisyd | 0:6f4be1a7962c | 527 | void RCSwitch::transmit(int nHighPulses, int nLowPulses) { |
TheChrisyd | 0:6f4be1a7962c | 528 | bool disabled_Receive = false; |
TheChrisyd | 0:6f4be1a7962c | 529 | |
TheChrisyd | 0:6f4be1a7962c | 530 | if (TransmitEnable) { |
TheChrisyd | 0:6f4be1a7962c | 531 | if (ReceiveEnabled) { |
TheChrisyd | 0:6f4be1a7962c | 532 | this->disableReceive(); |
TheChrisyd | 0:6f4be1a7962c | 533 | disabled_Receive = true; |
TheChrisyd | 0:6f4be1a7962c | 534 | } |
TheChrisyd | 0:6f4be1a7962c | 535 | _tx = 1; |
TheChrisyd | 0:6f4be1a7962c | 536 | wait_us(this->nPulseLength * nHighPulses); |
TheChrisyd | 0:6f4be1a7962c | 537 | _tx = 0; |
TheChrisyd | 0:6f4be1a7962c | 538 | wait_us(this->nPulseLength *nLowPulses); |
TheChrisyd | 0:6f4be1a7962c | 539 | if(disabled_Receive){ |
TheChrisyd | 0:6f4be1a7962c | 540 | this->enableReceive(); |
TheChrisyd | 0:6f4be1a7962c | 541 | } |
TheChrisyd | 0:6f4be1a7962c | 542 | } |
TheChrisyd | 0:6f4be1a7962c | 543 | } |
TheChrisyd | 0:6f4be1a7962c | 544 | /** |
TheChrisyd | 0:6f4be1a7962c | 545 | * Sends a "0" Bit |
TheChrisyd | 0:6f4be1a7962c | 546 | * _ |
TheChrisyd | 0:6f4be1a7962c | 547 | * Waveform Protocol 1: | |___ |
TheChrisyd | 0:6f4be1a7962c | 548 | * _ |
TheChrisyd | 0:6f4be1a7962c | 549 | * Waveform Protocol 2: | |__ |
TheChrisyd | 0:6f4be1a7962c | 550 | */ |
TheChrisyd | 0:6f4be1a7962c | 551 | void RCSwitch::send0() { |
TheChrisyd | 0:6f4be1a7962c | 552 | if (this->nProtocol == 1){ |
TheChrisyd | 0:6f4be1a7962c | 553 | this->transmit(1,3); |
TheChrisyd | 0:6f4be1a7962c | 554 | } |
TheChrisyd | 0:6f4be1a7962c | 555 | else if (this->nProtocol == 2) { |
TheChrisyd | 0:6f4be1a7962c | 556 | this->transmit(1,2); |
TheChrisyd | 0:6f4be1a7962c | 557 | } |
TheChrisyd | 0:6f4be1a7962c | 558 | else if (this->nProtocol == 3) { |
TheChrisyd | 0:6f4be1a7962c | 559 | this->transmit(4,11); |
TheChrisyd | 0:6f4be1a7962c | 560 | } |
TheChrisyd | 0:6f4be1a7962c | 561 | } |
TheChrisyd | 0:6f4be1a7962c | 562 | |
TheChrisyd | 0:6f4be1a7962c | 563 | /** |
TheChrisyd | 0:6f4be1a7962c | 564 | * Sends a "1" Bit |
TheChrisyd | 0:6f4be1a7962c | 565 | * ___ |
TheChrisyd | 0:6f4be1a7962c | 566 | * Waveform Protocol 1: | |_ |
TheChrisyd | 0:6f4be1a7962c | 567 | * __ |
TheChrisyd | 0:6f4be1a7962c | 568 | * Waveform Protocol 2: | |_ |
TheChrisyd | 0:6f4be1a7962c | 569 | */ |
TheChrisyd | 0:6f4be1a7962c | 570 | void RCSwitch::send1() { |
TheChrisyd | 0:6f4be1a7962c | 571 | if (this->nProtocol == 1){ |
TheChrisyd | 0:6f4be1a7962c | 572 | this->transmit(3,1); |
TheChrisyd | 0:6f4be1a7962c | 573 | } |
TheChrisyd | 0:6f4be1a7962c | 574 | else if (this->nProtocol == 2) { |
TheChrisyd | 0:6f4be1a7962c | 575 | this->transmit(2,1); |
TheChrisyd | 0:6f4be1a7962c | 576 | } |
TheChrisyd | 0:6f4be1a7962c | 577 | else if (this->nProtocol == 3) { |
TheChrisyd | 0:6f4be1a7962c | 578 | this->transmit(9,6); |
TheChrisyd | 0:6f4be1a7962c | 579 | } |
TheChrisyd | 0:6f4be1a7962c | 580 | } |
TheChrisyd | 0:6f4be1a7962c | 581 | |
TheChrisyd | 0:6f4be1a7962c | 582 | |
TheChrisyd | 0:6f4be1a7962c | 583 | /** |
TheChrisyd | 0:6f4be1a7962c | 584 | * Sends a Tri-State "0" Bit |
TheChrisyd | 0:6f4be1a7962c | 585 | * _ _ |
TheChrisyd | 0:6f4be1a7962c | 586 | * Waveform: | |___| |___ |
TheChrisyd | 0:6f4be1a7962c | 587 | */ |
TheChrisyd | 0:6f4be1a7962c | 588 | void RCSwitch::sendT0() { |
TheChrisyd | 0:6f4be1a7962c | 589 | this->transmit(1,3); |
TheChrisyd | 0:6f4be1a7962c | 590 | this->transmit(1,3); |
TheChrisyd | 0:6f4be1a7962c | 591 | } |
TheChrisyd | 0:6f4be1a7962c | 592 | |
TheChrisyd | 0:6f4be1a7962c | 593 | /** |
TheChrisyd | 0:6f4be1a7962c | 594 | * Sends a Tri-State "1" Bit |
TheChrisyd | 0:6f4be1a7962c | 595 | * ___ ___ |
TheChrisyd | 0:6f4be1a7962c | 596 | * Waveform: | |_| |_ |
TheChrisyd | 0:6f4be1a7962c | 597 | */ |
TheChrisyd | 0:6f4be1a7962c | 598 | void RCSwitch::sendT1() { |
TheChrisyd | 0:6f4be1a7962c | 599 | this->transmit(3,1); |
TheChrisyd | 0:6f4be1a7962c | 600 | this->transmit(3,1); |
TheChrisyd | 0:6f4be1a7962c | 601 | } |
TheChrisyd | 0:6f4be1a7962c | 602 | |
TheChrisyd | 0:6f4be1a7962c | 603 | /** |
TheChrisyd | 0:6f4be1a7962c | 604 | * Sends a Tri-State "F" Bit |
TheChrisyd | 0:6f4be1a7962c | 605 | * _ ___ |
TheChrisyd | 0:6f4be1a7962c | 606 | * Waveform: | |___| |_ |
TheChrisyd | 0:6f4be1a7962c | 607 | */ |
TheChrisyd | 0:6f4be1a7962c | 608 | void RCSwitch::sendTF() { |
TheChrisyd | 0:6f4be1a7962c | 609 | this->transmit(1,3); |
TheChrisyd | 0:6f4be1a7962c | 610 | this->transmit(3,1); |
TheChrisyd | 0:6f4be1a7962c | 611 | } |
TheChrisyd | 0:6f4be1a7962c | 612 | |
TheChrisyd | 0:6f4be1a7962c | 613 | /** |
TheChrisyd | 0:6f4be1a7962c | 614 | * Sends a "Sync" Bit |
TheChrisyd | 0:6f4be1a7962c | 615 | * _ |
TheChrisyd | 0:6f4be1a7962c | 616 | * Waveform Protocol 1: | |_______________________________ |
TheChrisyd | 0:6f4be1a7962c | 617 | * _ |
TheChrisyd | 0:6f4be1a7962c | 618 | * Waveform Protocol 2: | |__________ |
TheChrisyd | 0:6f4be1a7962c | 619 | */ |
TheChrisyd | 0:6f4be1a7962c | 620 | void RCSwitch::sendSync() { |
TheChrisyd | 0:6f4be1a7962c | 621 | |
TheChrisyd | 0:6f4be1a7962c | 622 | if (this->nProtocol == 1){ |
TheChrisyd | 0:6f4be1a7962c | 623 | this->transmit(1,31); |
TheChrisyd | 0:6f4be1a7962c | 624 | } |
TheChrisyd | 0:6f4be1a7962c | 625 | else if (this->nProtocol == 2) { |
TheChrisyd | 0:6f4be1a7962c | 626 | this->transmit(1,10); |
TheChrisyd | 0:6f4be1a7962c | 627 | } |
TheChrisyd | 0:6f4be1a7962c | 628 | else if (this->nProtocol == 3) { |
TheChrisyd | 0:6f4be1a7962c | 629 | this->transmit(1,71); |
TheChrisyd | 0:6f4be1a7962c | 630 | } |
TheChrisyd | 0:6f4be1a7962c | 631 | } |
TheChrisyd | 0:6f4be1a7962c | 632 | |
TheChrisyd | 0:6f4be1a7962c | 633 | /** |
TheChrisyd | 0:6f4be1a7962c | 634 | * Enable receiving data |
TheChrisyd | 0:6f4be1a7962c | 635 | */ |
TheChrisyd | 0:6f4be1a7962c | 636 | void RCSwitch::enableReceive() { |
TheChrisyd | 0:6f4be1a7962c | 637 | RCSwitch::nReceivedValue = NULL; |
TheChrisyd | 0:6f4be1a7962c | 638 | RCSwitch::nReceivedBitlength = NULL; |
TheChrisyd | 0:6f4be1a7962c | 639 | _rx.enable_irq(); |
TheChrisyd | 0:6f4be1a7962c | 640 | ReceiveEnabled = true; |
TheChrisyd | 0:6f4be1a7962c | 641 | } |
TheChrisyd | 0:6f4be1a7962c | 642 | |
TheChrisyd | 0:6f4be1a7962c | 643 | /** |
TheChrisyd | 0:6f4be1a7962c | 644 | * Disable receiving data This disables the interrupt |
TheChrisyd | 0:6f4be1a7962c | 645 | * which may disable the port |
TheChrisyd | 0:6f4be1a7962c | 646 | */ |
TheChrisyd | 0:6f4be1a7962c | 647 | void RCSwitch::disableReceive() { |
TheChrisyd | 0:6f4be1a7962c | 648 | _rx.disable_irq(); |
TheChrisyd | 0:6f4be1a7962c | 649 | ReceiveEnabled = false; |
TheChrisyd | 0:6f4be1a7962c | 650 | } |
TheChrisyd | 0:6f4be1a7962c | 651 | |
TheChrisyd | 0:6f4be1a7962c | 652 | bool RCSwitch::available() { |
TheChrisyd | 0:6f4be1a7962c | 653 | return RCSwitch::nReceivedValue != NULL; |
TheChrisyd | 0:6f4be1a7962c | 654 | } |
TheChrisyd | 0:6f4be1a7962c | 655 | |
TheChrisyd | 0:6f4be1a7962c | 656 | void RCSwitch::resetAvailable() { |
TheChrisyd | 0:6f4be1a7962c | 657 | RCSwitch::nReceivedValue = NULL; |
TheChrisyd | 0:6f4be1a7962c | 658 | } |
TheChrisyd | 0:6f4be1a7962c | 659 | |
TheChrisyd | 0:6f4be1a7962c | 660 | unsigned long RCSwitch::getReceivedValue() { |
TheChrisyd | 0:6f4be1a7962c | 661 | return RCSwitch::nReceivedValue; |
TheChrisyd | 0:6f4be1a7962c | 662 | } |
TheChrisyd | 0:6f4be1a7962c | 663 | |
TheChrisyd | 0:6f4be1a7962c | 664 | unsigned int RCSwitch::getReceivedBitlength() { |
TheChrisyd | 0:6f4be1a7962c | 665 | return RCSwitch::nReceivedBitlength; |
TheChrisyd | 0:6f4be1a7962c | 666 | } |
TheChrisyd | 0:6f4be1a7962c | 667 | |
TheChrisyd | 0:6f4be1a7962c | 668 | unsigned int RCSwitch::getReceivedDelay() { |
TheChrisyd | 0:6f4be1a7962c | 669 | return nReceivedDelay; |
TheChrisyd | 0:6f4be1a7962c | 670 | } |
TheChrisyd | 0:6f4be1a7962c | 671 | |
TheChrisyd | 0:6f4be1a7962c | 672 | unsigned int RCSwitch::getReceivedProtocol() { |
TheChrisyd | 0:6f4be1a7962c | 673 | return RCSwitch::nReceivedProtocol; |
TheChrisyd | 0:6f4be1a7962c | 674 | } |
TheChrisyd | 0:6f4be1a7962c | 675 | |
TheChrisyd | 0:6f4be1a7962c | 676 | unsigned int* RCSwitch::getReceivedRawdata() { |
TheChrisyd | 0:6f4be1a7962c | 677 | return RCSwitch::timings; |
TheChrisyd | 0:6f4be1a7962c | 678 | } |
TheChrisyd | 0:6f4be1a7962c | 679 | |
TheChrisyd | 0:6f4be1a7962c | 680 | /** |
TheChrisyd | 0:6f4be1a7962c | 681 | * |
TheChrisyd | 0:6f4be1a7962c | 682 | */ |
TheChrisyd | 0:6f4be1a7962c | 683 | bool RCSwitch::receiveProtocol1(unsigned int changeCount){ |
TheChrisyd | 0:6f4be1a7962c | 684 | |
TheChrisyd | 0:6f4be1a7962c | 685 | unsigned long code = 0; |
TheChrisyd | 0:6f4be1a7962c | 686 | unsigned long delay = RCSwitch::timings[0] / 31; |
TheChrisyd | 0:6f4be1a7962c | 687 | unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; |
TheChrisyd | 0:6f4be1a7962c | 688 | |
TheChrisyd | 0:6f4be1a7962c | 689 | for (int i = 1; i<changeCount ; i=i+2) { |
TheChrisyd | 0:6f4be1a7962c | 690 | |
TheChrisyd | 0:6f4be1a7962c | 691 | 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) { |
TheChrisyd | 0:6f4be1a7962c | 692 | code = code << 1; |
TheChrisyd | 0:6f4be1a7962c | 693 | } 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) { |
TheChrisyd | 0:6f4be1a7962c | 694 | code+=1; |
TheChrisyd | 0:6f4be1a7962c | 695 | code = code << 1; |
TheChrisyd | 0:6f4be1a7962c | 696 | } else { |
TheChrisyd | 0:6f4be1a7962c | 697 | // Failed |
TheChrisyd | 0:6f4be1a7962c | 698 | i = changeCount; |
TheChrisyd | 0:6f4be1a7962c | 699 | code = 0; |
TheChrisyd | 0:6f4be1a7962c | 700 | } |
TheChrisyd | 0:6f4be1a7962c | 701 | } |
TheChrisyd | 0:6f4be1a7962c | 702 | code = code >> 1; |
TheChrisyd | 0:6f4be1a7962c | 703 | if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise |
TheChrisyd | 0:6f4be1a7962c | 704 | RCSwitch::nReceivedValue = code; |
TheChrisyd | 0:6f4be1a7962c | 705 | RCSwitch::nReceivedBitlength = changeCount / 2; |
TheChrisyd | 0:6f4be1a7962c | 706 | nReceivedDelay = delay; |
TheChrisyd | 0:6f4be1a7962c | 707 | RCSwitch::nReceivedProtocol = 1; |
TheChrisyd | 0:6f4be1a7962c | 708 | } |
TheChrisyd | 0:6f4be1a7962c | 709 | |
TheChrisyd | 0:6f4be1a7962c | 710 | if (code == 0){ |
TheChrisyd | 0:6f4be1a7962c | 711 | return false; |
TheChrisyd | 0:6f4be1a7962c | 712 | } |
TheChrisyd | 0:6f4be1a7962c | 713 | return true; |
TheChrisyd | 0:6f4be1a7962c | 714 | } |
TheChrisyd | 0:6f4be1a7962c | 715 | |
TheChrisyd | 0:6f4be1a7962c | 716 | bool RCSwitch::receiveProtocol2(unsigned int changeCount){ |
TheChrisyd | 0:6f4be1a7962c | 717 | |
TheChrisyd | 0:6f4be1a7962c | 718 | unsigned long code = 0; |
TheChrisyd | 0:6f4be1a7962c | 719 | unsigned long delay = RCSwitch::timings[0] / 10; |
TheChrisyd | 0:6f4be1a7962c | 720 | unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; |
TheChrisyd | 0:6f4be1a7962c | 721 | |
TheChrisyd | 0:6f4be1a7962c | 722 | for (int i = 1; i<changeCount ; i=i+2) { |
TheChrisyd | 0:6f4be1a7962c | 723 | |
TheChrisyd | 0:6f4be1a7962c | 724 | 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) { |
TheChrisyd | 0:6f4be1a7962c | 725 | code = code << 1; |
TheChrisyd | 0:6f4be1a7962c | 726 | } 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) { |
TheChrisyd | 0:6f4be1a7962c | 727 | code+=1; |
TheChrisyd | 0:6f4be1a7962c | 728 | code = code << 1; |
TheChrisyd | 0:6f4be1a7962c | 729 | } else { |
TheChrisyd | 0:6f4be1a7962c | 730 | // Failed |
TheChrisyd | 0:6f4be1a7962c | 731 | i = changeCount; |
TheChrisyd | 0:6f4be1a7962c | 732 | code = 0; |
TheChrisyd | 0:6f4be1a7962c | 733 | } |
TheChrisyd | 0:6f4be1a7962c | 734 | } |
TheChrisyd | 0:6f4be1a7962c | 735 | code = code >> 1; |
TheChrisyd | 0:6f4be1a7962c | 736 | if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise |
TheChrisyd | 0:6f4be1a7962c | 737 | RCSwitch::nReceivedValue = code; |
TheChrisyd | 0:6f4be1a7962c | 738 | RCSwitch::nReceivedBitlength = changeCount / 2; |
TheChrisyd | 0:6f4be1a7962c | 739 | nReceivedDelay = delay; |
TheChrisyd | 0:6f4be1a7962c | 740 | RCSwitch::nReceivedProtocol = 2; |
TheChrisyd | 0:6f4be1a7962c | 741 | } |
TheChrisyd | 0:6f4be1a7962c | 742 | |
TheChrisyd | 0:6f4be1a7962c | 743 | if (code == 0){ |
TheChrisyd | 0:6f4be1a7962c | 744 | return false; |
TheChrisyd | 0:6f4be1a7962c | 745 | } |
TheChrisyd | 0:6f4be1a7962c | 746 | return true; |
TheChrisyd | 0:6f4be1a7962c | 747 | } |
TheChrisyd | 0:6f4be1a7962c | 748 | |
TheChrisyd | 0:6f4be1a7962c | 749 | /** Protocol 3 is used by BL35P02. |
TheChrisyd | 0:6f4be1a7962c | 750 | * |
TheChrisyd | 0:6f4be1a7962c | 751 | */ |
TheChrisyd | 0:6f4be1a7962c | 752 | bool RCSwitch::receiveProtocol3(unsigned int changeCount){ |
TheChrisyd | 0:6f4be1a7962c | 753 | |
TheChrisyd | 0:6f4be1a7962c | 754 | unsigned long code = 0; |
TheChrisyd | 0:6f4be1a7962c | 755 | unsigned long delay = RCSwitch::timings[0] / PROTOCOL3_SYNC_FACTOR; |
TheChrisyd | 0:6f4be1a7962c | 756 | unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01; |
TheChrisyd | 0:6f4be1a7962c | 757 | |
TheChrisyd | 0:6f4be1a7962c | 758 | for (int i = 1; i<changeCount ; i=i+2) { |
TheChrisyd | 0:6f4be1a7962c | 759 | |
TheChrisyd | 0:6f4be1a7962c | 760 | if (RCSwitch::timings[i] > delay*PROTOCOL3_0_HIGH_CYCLES - delayTolerance |
TheChrisyd | 0:6f4be1a7962c | 761 | && RCSwitch::timings[i] < delay*PROTOCOL3_0_HIGH_CYCLES + delayTolerance |
TheChrisyd | 0:6f4be1a7962c | 762 | && RCSwitch::timings[i+1] > delay*PROTOCOL3_0_LOW_CYCLES - delayTolerance |
TheChrisyd | 0:6f4be1a7962c | 763 | && RCSwitch::timings[i+1] < delay*PROTOCOL3_0_LOW_CYCLES + delayTolerance) { |
TheChrisyd | 0:6f4be1a7962c | 764 | code = code << 1; |
TheChrisyd | 0:6f4be1a7962c | 765 | } else if (RCSwitch::timings[i] > delay*PROTOCOL3_1_HIGH_CYCLES - delayTolerance |
TheChrisyd | 0:6f4be1a7962c | 766 | && RCSwitch::timings[i] < delay*PROTOCOL3_1_HIGH_CYCLES + delayTolerance |
TheChrisyd | 0:6f4be1a7962c | 767 | && RCSwitch::timings[i+1] > delay*PROTOCOL3_1_LOW_CYCLES - delayTolerance |
TheChrisyd | 0:6f4be1a7962c | 768 | && RCSwitch::timings[i+1] < delay*PROTOCOL3_1_LOW_CYCLES + delayTolerance) { |
TheChrisyd | 0:6f4be1a7962c | 769 | code+=1; |
TheChrisyd | 0:6f4be1a7962c | 770 | code = code << 1; |
TheChrisyd | 0:6f4be1a7962c | 771 | } else { |
TheChrisyd | 0:6f4be1a7962c | 772 | // Failed |
TheChrisyd | 0:6f4be1a7962c | 773 | i = changeCount; |
TheChrisyd | 0:6f4be1a7962c | 774 | code = 0; |
TheChrisyd | 0:6f4be1a7962c | 775 | } |
TheChrisyd | 0:6f4be1a7962c | 776 | } |
TheChrisyd | 0:6f4be1a7962c | 777 | code = code >> 1; |
TheChrisyd | 0:6f4be1a7962c | 778 | if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise |
TheChrisyd | 0:6f4be1a7962c | 779 | RCSwitch::nReceivedValue = code; |
TheChrisyd | 0:6f4be1a7962c | 780 | RCSwitch::nReceivedBitlength = changeCount / 2; |
TheChrisyd | 0:6f4be1a7962c | 781 | nReceivedDelay = delay; |
TheChrisyd | 0:6f4be1a7962c | 782 | RCSwitch::nReceivedProtocol = 3; |
TheChrisyd | 0:6f4be1a7962c | 783 | } |
TheChrisyd | 0:6f4be1a7962c | 784 | |
TheChrisyd | 0:6f4be1a7962c | 785 | if (code == 0){ |
TheChrisyd | 0:6f4be1a7962c | 786 | return false; |
TheChrisyd | 0:6f4be1a7962c | 787 | } |
TheChrisyd | 0:6f4be1a7962c | 788 | return true; |
TheChrisyd | 0:6f4be1a7962c | 789 | } |
TheChrisyd | 0:6f4be1a7962c | 790 | |
TheChrisyd | 0:6f4be1a7962c | 791 | void RCSwitch::RCSwitchRxPinChange() { |
TheChrisyd | 0:6f4be1a7962c | 792 | |
TheChrisyd | 0:6f4be1a7962c | 793 | static unsigned int duration; |
TheChrisyd | 0:6f4be1a7962c | 794 | static unsigned int changeCount; |
TheChrisyd | 0:6f4be1a7962c | 795 | static unsigned int repeatCount; |
TheChrisyd | 0:6f4be1a7962c | 796 | timer.stop(); |
TheChrisyd | 0:6f4be1a7962c | 797 | duration = timer.read_us(); |
TheChrisyd | 0:6f4be1a7962c | 798 | timer.reset(); |
TheChrisyd | 0:6f4be1a7962c | 799 | timer.start(); |
TheChrisyd | 0:6f4be1a7962c | 800 | |
TheChrisyd | 0:6f4be1a7962c | 801 | |
TheChrisyd | 0:6f4be1a7962c | 802 | if (duration > 5000 && duration > RCSwitch::timings[0] - 200 && duration < RCSwitch::timings[0] + 200) { |
TheChrisyd | 0:6f4be1a7962c | 803 | repeatCount++; |
TheChrisyd | 0:6f4be1a7962c | 804 | changeCount--; |
TheChrisyd | 0:6f4be1a7962c | 805 | |
TheChrisyd | 0:6f4be1a7962c | 806 | if (repeatCount == 2) { |
TheChrisyd | 0:6f4be1a7962c | 807 | if (receiveProtocol1(changeCount) == false){ |
TheChrisyd | 0:6f4be1a7962c | 808 | if (receiveProtocol2(changeCount) == false){ |
TheChrisyd | 0:6f4be1a7962c | 809 | if (receiveProtocol3(changeCount) == false){ |
TheChrisyd | 0:6f4be1a7962c | 810 | //failed |
TheChrisyd | 0:6f4be1a7962c | 811 | } |
TheChrisyd | 0:6f4be1a7962c | 812 | } |
TheChrisyd | 0:6f4be1a7962c | 813 | } |
TheChrisyd | 0:6f4be1a7962c | 814 | repeatCount = 0; |
TheChrisyd | 0:6f4be1a7962c | 815 | } |
TheChrisyd | 0:6f4be1a7962c | 816 | changeCount = 0; |
TheChrisyd | 0:6f4be1a7962c | 817 | } else if (duration > 5000) { |
TheChrisyd | 0:6f4be1a7962c | 818 | |
TheChrisyd | 0:6f4be1a7962c | 819 | changeCount = 0; |
TheChrisyd | 0:6f4be1a7962c | 820 | } |
TheChrisyd | 0:6f4be1a7962c | 821 | |
TheChrisyd | 0:6f4be1a7962c | 822 | if (changeCount >= RCSWITCH_MAX_CHANGES) { |
TheChrisyd | 0:6f4be1a7962c | 823 | changeCount = 0; |
TheChrisyd | 0:6f4be1a7962c | 824 | repeatCount = 0; |
TheChrisyd | 0:6f4be1a7962c | 825 | } |
TheChrisyd | 0:6f4be1a7962c | 826 | RCSwitch::timings[changeCount++] = duration; |
TheChrisyd | 0:6f4be1a7962c | 827 | } |
TheChrisyd | 0:6f4be1a7962c | 828 | |
TheChrisyd | 0:6f4be1a7962c | 829 | /** |
TheChrisyd | 0:6f4be1a7962c | 830 | * Turns a decimal value to its binary representation |
TheChrisyd | 0:6f4be1a7962c | 831 | */ |
TheChrisyd | 0:6f4be1a7962c | 832 | char* RCSwitch::dec2binWzerofill(unsigned long Dec, unsigned int bitLength){ |
TheChrisyd | 0:6f4be1a7962c | 833 | return dec2binWcharfill(Dec, bitLength, '0'); |
TheChrisyd | 0:6f4be1a7962c | 834 | } |
TheChrisyd | 0:6f4be1a7962c | 835 | |
TheChrisyd | 0:6f4be1a7962c | 836 | char* RCSwitch::dec2binWcharfill(unsigned long Dec, unsigned int bitLength, char fill){ |
TheChrisyd | 0:6f4be1a7962c | 837 | static char bin[64]; |
TheChrisyd | 0:6f4be1a7962c | 838 | unsigned int i=0; |
TheChrisyd | 0:6f4be1a7962c | 839 | |
TheChrisyd | 0:6f4be1a7962c | 840 | while (Dec > 0) { |
TheChrisyd | 0:6f4be1a7962c | 841 | bin[32+i++] = ((Dec & 1) > 0) ? '1' : fill; |
TheChrisyd | 0:6f4be1a7962c | 842 | Dec = Dec >> 1; |
TheChrisyd | 0:6f4be1a7962c | 843 | } |
TheChrisyd | 0:6f4be1a7962c | 844 | |
TheChrisyd | 0:6f4be1a7962c | 845 | for (unsigned int j = 0; j< bitLength; j++) { |
TheChrisyd | 0:6f4be1a7962c | 846 | if (j >= bitLength - i) { |
TheChrisyd | 0:6f4be1a7962c | 847 | bin[j] = bin[ 31 + i - (j - (bitLength - i)) ]; |
TheChrisyd | 0:6f4be1a7962c | 848 | }else { |
TheChrisyd | 0:6f4be1a7962c | 849 | bin[j] = fill; |
TheChrisyd | 0:6f4be1a7962c | 850 | } |
TheChrisyd | 0:6f4be1a7962c | 851 | } |
TheChrisyd | 0:6f4be1a7962c | 852 | bin[bitLength] = '\0'; |
TheChrisyd | 0:6f4be1a7962c | 853 | |
TheChrisyd | 0:6f4be1a7962c | 854 | return bin; |
TheChrisyd | 0:6f4be1a7962c | 855 | } |
TheChrisyd | 0:6f4be1a7962c | 856 | |
TheChrisyd | 0:6f4be1a7962c | 857 | |
TheChrisyd | 0:6f4be1a7962c | 858 |