Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.9 KiB

  1. /*
  2. * RemoteSwitch library v2.0.0 made by Randy Simons http://randysimons.nl
  3. *
  4. * License: "Free BSD license". See license.txt
  5. */
  6. #ifndef RemoteReceiver_h
  7. #define RemoteReceiver_h
  8. //#include "WProgram.h"
  9. #include "Arduino.h"
  10. typedef void (*RemoteReceiverCallBack)(unsigned long, unsigned int);
  11. /**
  12. * See RemoteSwitch for introduction.
  13. *
  14. * RemoteReceiver decodes the signal received from a 433MHz-receiver, like the "KlikAanKlikUit"-system
  15. * as well as the signal sent by the RemoteSwtich class. When a correct signal is received,
  16. * a user-defined callback function is called.
  17. *
  18. * Note that in the callback function, the interrupts are still disabled. You can enabled them, if needed.
  19. * A call to the callback must b finished before RemoteReceiver will call the callback function again, thus
  20. * there is no re-entrant problem.
  21. *
  22. * When sending your own code using RemoteSwich, disable() the receiver first.
  23. *
  24. * This is a pure static class, for simplicity and to limit memory-use.
  25. */
  26. class RemoteReceiver {
  27. public:
  28. /**
  29. * Initializes the decoder.
  30. *
  31. * @param interrupt The interrupt as is used by Arduino's attachInterrupt function. See attachInterrupt for details.
  32. * @param minRepeats The number of times the same code must be received in a row before the callback is calles
  33. * @param callback Pointer to a callback function, with signature void (*func)(unsigned long, unsigned int). First parameter is the decoded data, the second the period of the timing.
  34. */
  35. static void init(unsigned short interrupt, unsigned short minRepeats, RemoteReceiverCallBack callback);
  36. /**
  37. * Enabled decoding. No need to call enable() after init().
  38. */
  39. static void enable();
  40. /**
  41. * Disable decoding. You can enable decoding by calling enable();
  42. */
  43. static void disable();
  44. /**
  45. * Tells wether a signal is being received. Since it makes no sense to transmit while another transmitter is active,
  46. * it's best to wait for isReceiving() to false.
  47. *
  48. * Note: isReceiving() depends on interrupts enabled. Thus, when disabled()'ed, or when interrupts are disabled (as is
  49. * the case in the callback), isReceiving() will not work properly.
  50. * @param waitMillis number of milliseconds to monitor for signal.
  51. * @return boolean If after waitMillis no signal was being processed, returns false. If before expiration a signal was being processed, returns true.
  52. */
  53. static boolean isReceiving(int waitMillis = 50);
  54. private:
  55. static void interruptHandler();
  56. static unsigned short _interrupt; //Radio input interrupt
  57. volatile static int _state; //State of decoding process. There are 49 states, 1 for "waiting for signal" and 48 for decoding the 48 edges in a valid code.
  58. static unsigned short _minRepeats;
  59. static RemoteReceiverCallBack _callback;
  60. static boolean _inCallback; //When true, the callback function is being executed; prevents re-entrance.
  61. };
  62. #endif