Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
RCSwitch.h
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  - Max Horn / max(at)quendi(dot)de
12  - Robert ter Vehn / <first name>.<last name>(at)gmail(dot)com
13 
14  Project home: https://github.com/sui77/rc-switch/
15 
16  This library is free software; you can redistribute it and/or
17  modify it under the terms of the GNU Lesser General Public
18  License as published by the Free Software Foundation; either
19  version 2.1 of the License, or (at your option) any later version.
20 
21  This library is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  Lesser General Public License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public
27  License along with this library; if not, write to the Free Software
28  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30 #ifndef _RCSwitch_h
31 #define _RCSwitch_h
32 
33 #if defined(ARDUINO) && ARDUINO >= 100
34  #include "Arduino.h"
35 #elif defined(ENERGIA) // LaunchPad, FraunchPad and StellarPad specific
36  #include "Energia.h"
37 #elif defined(RPI) // Raspberry Pi
38  #define RaspberryPi
39  // PROGMEM och _P functions are for AVR based microprocessors,
40  // so we must normalize these for the ARM processor:
41  #define PROGMEM
42  #define memcpy_P(dest, src, num) memcpy((dest), (src), (num))
43  // Include libraries for RPi:
44  #include <string.h> /* memcpy */
45  #include <stdlib.h> /* abs */
46  #include <stddef.h> /* NULL */
47  #include <wiringPi.h>
48  #include <stdint.h>
49  #define CHANGE 1
50  // The following typedefs are needed to be able to compile RCSwitch.cpp
51  // with the RPi C++ compiler (g++)
52  #ifdef __cplusplus
53  extern "C"{
54  #endif
55  typedef uint8_t boolean;
56  typedef uint8_t byte;
57  #ifdef __cplusplus
58  }
59  #endif
60  // Last line within Raspberry Pi block
61 #else
62  #include "WProgram.h"
63 #endif
64 
65 
66 // At least for the ATTiny X4/X5, receiving has to be disabled due to
67 // missing libm depencies (udivmodhi4)
68 #if defined( __AVR_ATtinyX5__ ) or defined ( __AVR_ATtinyX4__ )
69 #define RCSwitchDisableReceiving
70 #endif
71 
72 // Number of maximum High/Low changes per packet.
73 // We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync
74 #define RCSWITCH_MAX_CHANGES 67
75 
76 class RCSwitch {
77 
78  public:
79  RCSwitch();
80 
81  void switchOn(int nGroupNumber, int nSwitchNumber);
82  void switchOff(int nGroupNumber, int nSwitchNumber);
83  void switchOn(const char* sGroup, int nSwitchNumber);
84  void switchOff(const char* sGroup, int nSwitchNumber);
85  void switchOn(char sFamily, int nGroup, int nDevice);
86  void switchOff(char sFamily, int nGroup, int nDevice);
87  void switchOn(const char* sGroup, const char* sDevice);
88  void switchOff(const char* sGroup, const char* sDevice);
89  void switchOn(char sGroup, int nDevice);
90  void switchOff(char sGroup, int nDevice);
91 
92  void sendTriState(const char* Code);
93  void send(unsigned long Code, unsigned int length);
94  void send(const char* Code);
95 
96  #if not defined( RCSwitchDisableReceiving )
97  void enableReceive(int pinNumber);
98  void enableReceive();
99  void disableReceive();
100  bool available();
101  void resetAvailable();
102 
103  unsigned long getReceivedValue();
104  unsigned int getReceivedBitlength();
105  unsigned int getReceivedDelay();
106  unsigned int getReceivedProtocol();
107  unsigned int* getReceivedRawdata();
108  #endif
109 
110  void enableTransmit(int nTransmitterPin);
111  void disableTransmit();
112  void setPulseLength(int nPulseLength);
113  void setRepeatTransmit(int nRepeatTransmit);
114  #if not defined( RCSwitchDisableReceiving )
115  void setReceiveTolerance(int nPercent);
116  #endif
117 
118  struct HighLow {
119  byte high;
120  byte low;
121  };
122 
123  struct Protocol {
124  int pulseLength;
125  HighLow syncFactor;
126  HighLow zero;
127  HighLow one;
128  };
129 
130  void setProtocol(Protocol protocol);
131  void setProtocol(int nProtocol);
132  void setProtocol(int nProtocol, int nPulseLength);
133 
134  private:
135  char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);
136  char* getCodeWordA(const char* sGroup, int nSwitchNumber, boolean bStatus);
137  char* getCodeWordA(const char* sGroup, const char* sDevice, boolean bStatus);
138  char* getCodeWordC(char sFamily, int nGroup, int nDevice, boolean bStatus);
139  char* getCodeWordD(char group, int nDevice, boolean bStatus);
140  void sendT0();
141  void sendT1();
142  void sendTF();
143  void send0();
144  void send1();
145  void sendSync();
146  void transmit(int nHighPulses, int nLowPulses);
147  void transmit(HighLow pulses);
148 
149  static char* dec2binWcharfill(unsigned long dec, unsigned int length, char fill);
150 
151  #if not defined( RCSwitchDisableReceiving )
152  static IRAM_ATTR void handleInterrupt();
153  static IRAM_ATTR bool receiveProtocol(const int p, unsigned int changeCount);
154  int nReceiverInterrupt;
155  #endif
156  int nTransmitterPin;
157  int nRepeatTransmit;
158 
159  Protocol protocol;
160 
161  #if not defined( RCSwitchDisableReceiving )
162  static int nReceiveTolerance;
163  static unsigned long nReceivedValue;
164  static unsigned int nReceivedBitlength;
165  static unsigned int nReceivedDelay;
166  static unsigned int nReceivedProtocol;
167  const static unsigned int nSeparationLimit;
168  /*
169  * timings[0] contains sync timing, followed by a number of bits
170  */
171  static unsigned int timings[RCSWITCH_MAX_CHANGES];
172  #endif
173 
174 
175 };
176 
177 #endif
Definition: RCSwitch.h:123
Definition: RCSwitch.h:118
Definition: RCSwitch.h:76