NewRemoteReceiver.h 4.8 KB

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