forked from 1technophile/NewRemoteSwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewRemoteReceiver.h
135 lines (113 loc) · 5.96 KB
/
NewRemoteReceiver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* NewRemoteSwitch library v1.2.0 (20140128) made by Randy Simons http://randysimons.nl/
*
* License: GPLv3. See license.txt
*/
#ifndef NewRemoteReceiver_h
#define NewRemoteReceiver_h
#include <Arduino.h>
typedef enum SwitchType {
off = 0,
on = 1,
dim = 2
} switchType;
struct NewRemoteCode {
enum SwitchType {
off = 0,
on = 1,
dim = 2
};
unsigned int period; // Detected duration in microseconds of 1T in the received signal
unsigned long address; // Address of received code. [0..2^26-1]
boolean groupBit; // Group bit set or not
SwitchType switchType; // off, on, dim, on_with_dim.
byte unit; // Unit code of received code [0..15]
boolean dimLevelPresent; // Dim level present or not. Will be available for switchType dim, but might be available for on or off too, depending on remote.
byte dimLevel; // Dim level [0..15]. Will be available if switchType is dim, on_with_dim or off_with_dim.
};
#if defined ESP8266 || defined ESP32
#include <functional>
#define CALLBACK_SIGNATUREH typedef std::function<void(unsigned int period, unsigned long address, unsigned long groupBit, unsigned long unit, unsigned long switchType, boolean dimLevelPresent, byte dimLevel)> NewRemoteReceiverCallBack
// Type definition for callback function with NewRemoteCode struct as parameter
#define CALLBACK_SIGNATURE_STRUCTH typedef std::function<void(NewRemoteCode receivedCode)> NewRemoteReceiverCallBackStruct
#else
#define CALLBACK_SIGNATUREH typedef void (*NewRemoteReceiverCallBack)(unsigned int period, unsigned long address, unsigned long groupBit, unsigned long unit, unsigned long switchType, boolean dimLevelPresent, byte dimLevel)
// Type definition for callback function with NewRemoteCode struct as parameter
#define CALLBACK_SIGNATURE_STRUCTH typedef void(*NewRemoteReceiverCallBackStruct)(NewRemoteCode)
#endif
CALLBACK_SIGNATUREH;
CALLBACK_SIGNATURE_STRUCTH;
/**
* See RemoteSwitch for introduction.
*
* NewRemoteReceiver decodes the signal received from a 433MHz-receiver, like the "KlikAanKlikUit"-system
* as well as the signal sent by the RemoteSwtich class. When a correct signal is received,
* a user-defined callback function is called.
*
* Note that in the callback function, the interrupts are still disabled. You can enabled them, if needed.
* A call to the callback must be finished before NewRemoteReceiver will call the callback function again, thus
* there is no re-entrant problem.
*
* When sending your own code using NewRemoteSwich, disable() the receiver first.
*
* This is a pure static class, for simplicity and to limit memory-use.
*/
class NewRemoteReceiver {
public:
/**
* Initializes the decoder.
*
* If interrupt >= 0, init will register pin <interrupt> to this library.
* If interrupt < 0, no interrupt is registered. In that case, you have to call interruptHandler()
* yourself whenever the output of the receiver changes, or you can use InterruptChain.
*
* @param interrupt The interrupt as is used by Arduino's attachInterrupt function. See attachInterrupt for details.
If < 0, you must call interruptHandler() yourself.
* @param minRepeats The number of times the same code must be received in a row before the callback is calles
* @param callback Pointer to a callback function, with two possible signatures depending on init()'s call:
- void (*func)(unsigned int period, unsigned long address, unsigned long groupBit, unsigned long unit, unsigned long switchType, boolean dimLevelPresent, byte dimLevel)
- void (*func)(NewRemoteCode receivedCode)
*/
static void init(int8_t interrupt, byte minRepeats, NewRemoteReceiverCallBack callback);
// Overload init() to support a callback function with NewRemoteCode struct as parameter
static void init(int8_t interrupt, byte minRepeats, NewRemoteReceiverCallBackStruct callback);
/**
* Enable decoding. No need to call enable() after init().
*/
static void enable();
/**
* Disable decoding. You can re-enable decoding by calling enable();
*/
static void disable();
/**
* Deinitializes the decoder. Disables decoding and detaches the interrupt handler. If you want to
* re-enable decoding, call init() again.
*/
static void deinit();
/**
* Tells wether a signal is being received. If a compatible signal is detected within the time out, isReceiving returns true.
* Since it makes no sense to transmit while another transmitter is active, it's best to wait for isReceiving() to false.
* By default it waits for 150ms, in which a (relative slow) KaKu signal can be broadcasted three times.
*
* Note: isReceiving() depends on interrupts enabled. Thus, when disabled()'ed, or when interrupts are disabled (as is
* the case in the callback), isReceiving() will not work properly.
*
* @param waitMillis number of milliseconds to monitor for signal.
* @return boolean If after waitMillis no signal was being processed, returns false. If before expiration a signal was being processed, returns true.
*/
static boolean isReceiving(int waitMillis = 150);
/**
* Called every time the signal level changes (high to low or vice versa). Usually called by interrupt.
*/
static void interruptHandler();
private:
static int8_t _interrupt; // Radio input interrupt
volatile static short _state; // State of decoding process.
static byte _minRepeats;
static NewRemoteReceiverCallBack _callback;
static NewRemoteReceiverCallBackStruct _callback_struct; // Variable to store the pointer to callback function with NewRemoteCode struct as parameter
static boolean _isCallbackStruct; // Flag to switch which callback function call at receive code
static boolean _inCallback; // When true, the callback function is being executed; prevents re-entrance.
static boolean _enabled; // If true, monitoring and decoding is enabled. If false, interruptHandler will return immediately.
};
#endif