1/* 2 RCSwitch - Arduino libary for remote control outlet switches 3 Copyright (c) 2011 Suat Özgür. All right reserved. 4 5 Contributors: 6 - Andre Koehler / info(at)tomate-online(dot)de 7 - Gordeev Andrey Vladimirovich / gordeev(at)openpyro(dot)com 8 - Skineffect / http://forum.ardumote.com/viewtopic.php?f=2&t=46 9 - Dominik Fischer / dom_fischer(at)web(dot)de 10 - Frank Oltmanns / <first name>.<last name>(at)gmail(dot)com 11 12 Project home: http://code.google.com/p/rc-switch/ 13 14 This library is free software; you can redistribute it and/or 15 modify it under the terms of the GNU Lesser General Public 16 License as published by the Free Software Foundation; either 17 version 2.1 of the License, or (at your option) any later version. 18 19 This library is distributed in the hope that it will be useful, 20 but WITHOUT ANY WARRANTY; without even the implied warranty of 21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 Lesser General Public License for more details. 23 24 You should have received a copy of the GNU Lesser General Public 25 License along with this library; if not, write to the Free Software 26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 27*/ 28#ifndef _RCSwitch_h 29#define _RCSwitch_h 30 31#if defined(ARDUINO) && ARDUINO >= 100 32#include"Arduino.h" 33#elif defined(ENERGIA)// LaunchPad, FraunchPad and StellarPad specific 34#include"Energia.h" 35#else 36#include"WProgram.h" 37#endif 38 39 40// At least for the ATTiny X4/X5, receiving has to be disabled due to 41// missing libm depencies (udivmodhi4) 42#if defined( __AVR_ATtinyX5__ ) or defined ( __AVR_ATtinyX4__ ) 43#define RCSwitchDisableReceiving 44#endif 45 46// Number of maximum High/Low changes per packet. 47// We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync 48#define RCSWITCH_MAX_CHANGES 67 49 50#define PROTOCOL3_SYNC_FACTOR 71 51#define PROTOCOL3_0_HIGH_CYCLES 4 52#define PROTOCOL3_0_LOW_CYCLES 11 53#define PROTOCOL3_1_HIGH_CYCLES 9 54#define PROTOCOL3_1_LOW_CYCLES 6 55 56class RCSwitch { 57 58public: 59RCSwitch(); 60 61voidswitchOn(int nGroupNumber,int nSwitchNumber); 62voidswitchOff(int nGroupNumber,int nSwitchNumber); 63voidswitchOn(char* sGroup,int nSwitchNumber); 64voidswitchOff(char* sGroup,int nSwitchNumber); 65voidswitchOn(char sFamily,int nGroup,int nDevice); 66voidswitchOff(char sFamily,int nGroup,int nDevice); 67voidswitchOn(char* sGroup,char* sDevice); 68voidswitchOff(char* sGroup,char* sDevice); 69voidswitchOn(char sGroup,int nDevice); 70voidswitchOff(char sGroup,int nDevice); 71 72voidsendTriState(char* Code); 73voidsend(unsigned long Code,unsigned int length); 74voidsend(char* Code); 75 76#if not defined( RCSwitchDisableReceiving ) 77voidenableReceive(int interrupt); 78voidenableReceive(); 79voiddisableReceive(); 80boolavailable(); 81voidresetAvailable(); 82 83unsigned longgetReceivedValue(); 84unsigned intgetReceivedBitlength(); 85unsigned intgetReceivedDelay(); 86unsigned intgetReceivedProtocol(); 87unsigned int*getReceivedRawdata(); 88#endif 89 90voidenableTransmit(int nTransmitterPin); 91voiddisableTransmit(); 92voidsetPulseLength(int nPulseLength); 93voidsetRepeatTransmit(int nRepeatTransmit); 94#if not defined( RCSwitchDisableReceiving ) 95voidsetReceiveTolerance(int nPercent); 96#endif 97voidsetProtocol(int nProtocol); 98voidsetProtocol(int nProtocol,int nPulseLength); 99 100private: 101char*getCodeWordB(int nGroupNumber,int nSwitchNumber, boolean bStatus); 102char*getCodeWordA(char* sGroup,int nSwitchNumber, boolean bStatus); 103char*getCodeWordA(char* sGroup,char* sDevice, boolean bStatus); 104char*getCodeWordC(char sFamily,int nGroup,int nDevice, boolean bStatus); 105char*getCodeWordD(char group,int nDevice, boolean bStatus); 106voidsendT0(); 107voidsendT1(); 108voidsendTF(); 109voidsend0(); 110voidsend1(); 111voidsendSync(); 112voidtransmit(int nHighPulses,int nLowPulses); 113 114static char*dec2binWzerofill(unsigned long dec,unsigned int length); 115static char*dec2binWcharfill(unsigned long dec,unsigned int length,char fill); 116 117#if not defined( RCSwitchDisableReceiving ) 118static voidhandleInterrupt(); 119static boolreceiveProtocol1(unsigned int changeCount); 120static boolreceiveProtocol2(unsigned int changeCount); 121static boolreceiveProtocol3(unsigned int changeCount); 122int nReceiverInterrupt; 123#endif 124int nTransmitterPin; 125int nPulseLength; 126int nRepeatTransmit; 127char nProtocol; 128 129#if not defined( RCSwitchDisableReceiving ) 130static int nReceiveTolerance; 131static unsigned long nReceivedValue; 132static unsigned int nReceivedBitlength; 133static unsigned int nReceivedDelay; 134static unsigned int nReceivedProtocol; 135#endif 136/* 137 * timings[0] contains sync timing, followed by a number of bits 138 */ 139static unsigned int timings[RCSWITCH_MAX_CHANGES]; 140 141 142}; 143 144#endif