libraries / rc-switch-2.52 / RCSwitch.hon commit Added link to project report (97a3ba0)
   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
  58  public:
  59    RCSwitch();
  60    
  61    void switchOn(int nGroupNumber, int nSwitchNumber);
  62    void switchOff(int nGroupNumber, int nSwitchNumber);
  63    void switchOn(char* sGroup, int nSwitchNumber);
  64    void switchOff(char* sGroup, int nSwitchNumber);
  65    void switchOn(char sFamily, int nGroup, int nDevice);
  66    void switchOff(char sFamily, int nGroup, int nDevice);
  67    void switchOn(char* sGroup, char* sDevice);
  68    void switchOff(char* sGroup, char* sDevice);
  69    void switchOn(char sGroup, int nDevice);
  70    void switchOff(char sGroup, int nDevice);
  71
  72    void sendTriState(char* Code);
  73    void send(unsigned long Code, unsigned int length);
  74    void send(char* Code);
  75    
  76    #if not defined( RCSwitchDisableReceiving )
  77    void enableReceive(int interrupt);
  78    void enableReceive();
  79    void disableReceive();
  80    bool available();
  81    void resetAvailable();
  82        
  83    unsigned long getReceivedValue();
  84    unsigned int getReceivedBitlength();
  85    unsigned int getReceivedDelay();
  86    unsigned int getReceivedProtocol();
  87    unsigned int* getReceivedRawdata();
  88    #endif
  89  
  90    void enableTransmit(int nTransmitterPin);
  91    void disableTransmit();
  92    void setPulseLength(int nPulseLength);
  93    void setRepeatTransmit(int nRepeatTransmit);
  94    #if not defined( RCSwitchDisableReceiving )
  95    void setReceiveTolerance(int nPercent);
  96    #endif
  97    void setProtocol(int nProtocol);
  98    void setProtocol(int nProtocol, int nPulseLength);
  99  
 100  private:
 101    char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);
 102    char* getCodeWordA(char* sGroup, int nSwitchNumber, boolean bStatus);
 103    char* getCodeWordA(char* sGroup, char* sDevice, boolean bStatus);
 104    char* getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus);
 105    char* getCodeWordD(char group, int nDevice, boolean bStatus);
 106    void sendT0();
 107    void sendT1();
 108    void sendTF();
 109    void send0();
 110    void send1();
 111    void sendSync();
 112    void transmit(int nHighPulses, int nLowPulses);
 113
 114    static char* dec2binWzerofill(unsigned long dec, unsigned int length);
 115    static char* dec2binWcharfill(unsigned long dec, unsigned int length, char fill);
 116    
 117    #if not defined( RCSwitchDisableReceiving )
 118    static void handleInterrupt();
 119    static bool receiveProtocol1(unsigned int changeCount);
 120    static bool receiveProtocol2(unsigned int changeCount);
 121    static bool receiveProtocol3(unsigned int changeCount);
 122    int nReceiverInterrupt;
 123    #endif
 124    int nTransmitterPin;
 125    int nPulseLength;
 126    int nRepeatTransmit;
 127    char nProtocol;
 128
 129    #if not defined( RCSwitchDisableReceiving )
 130    static int nReceiveTolerance;
 131    static unsigned long nReceivedValue;
 132    static unsigned int nReceivedBitlength;
 133    static unsigned int nReceivedDelay;
 134    static unsigned int nReceivedProtocol;
 135    #endif
 136    /* 
 137     * timings[0] contains sync timing, followed by a number of bits
 138     */
 139    static unsigned int timings[RCSWITCH_MAX_CHANGES];
 140
 141    
 142};
 143
 144#endif