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.

160 lines
2.6 KiB

  1. /*
  2. LED MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include "espurna.h"
  7. #include <vector>
  8. constexpr size_t LedsMax { 8ul };
  9. enum class LedMode {
  10. Manual,
  11. WiFi,
  12. Follow,
  13. FollowInverse,
  14. FindMe,
  15. FindMeWiFi,
  16. On,
  17. Off,
  18. Relay,
  19. RelayWiFi
  20. };
  21. enum class LedDelayMode {
  22. Finite,
  23. Infinite,
  24. None
  25. };
  26. struct LedDelay {
  27. LedDelay() = delete;
  28. constexpr LedDelay(unsigned long on_ms, unsigned long off_ms, unsigned char repeats) :
  29. _mode(repeats ? LedDelayMode::Finite : LedDelayMode::Infinite),
  30. _on(microsecondsToClockCycles(on_ms * 1000)),
  31. _off(microsecondsToClockCycles(off_ms * 1000)),
  32. _repeats(repeats)
  33. {}
  34. constexpr LedDelay(unsigned long on_ms, unsigned long off_ms) :
  35. LedDelay(on_ms, off_ms, 0)
  36. {}
  37. constexpr LedDelayMode mode() const {
  38. return _mode;
  39. }
  40. constexpr unsigned long on() const {
  41. return _on;
  42. }
  43. constexpr unsigned long off() const {
  44. return _off;
  45. }
  46. unsigned char repeats() const {
  47. return _repeats;
  48. }
  49. bool repeat() {
  50. if (_repeats) {
  51. --_repeats;
  52. }
  53. return _repeats;
  54. }
  55. private:
  56. LedDelayMode _mode;
  57. unsigned long _on;
  58. unsigned long _off;
  59. unsigned char _repeats;
  60. };
  61. struct LedPattern {
  62. using Delays = std::vector<LedDelay>;
  63. LedPattern() = default;
  64. LedPattern(const Delays& delays);
  65. void start();
  66. void stop();
  67. bool started();
  68. bool ready();
  69. Delays delays;
  70. Delays queue;
  71. unsigned long clock_last;
  72. unsigned long clock_delay;
  73. };
  74. struct led_t {
  75. led_t() = delete;
  76. led_t(unsigned char pin, bool inverse, LedMode mode) :
  77. _pin(pin),
  78. _inverse(inverse),
  79. _mode(mode)
  80. {
  81. init();
  82. }
  83. unsigned char pin() const {
  84. return _pin;
  85. }
  86. LedMode mode() const {
  87. return _mode;
  88. }
  89. void mode(LedMode mode) {
  90. _mode = mode;
  91. }
  92. bool inverse() const {
  93. return _inverse;
  94. }
  95. LedPattern& pattern() {
  96. return _pattern;
  97. }
  98. void init();
  99. void start() {
  100. _pattern.stop();
  101. }
  102. bool started() {
  103. return _pattern.started();
  104. }
  105. void stop() {
  106. _pattern.stop();
  107. }
  108. bool status();
  109. bool status(bool new_status);
  110. bool toggle();
  111. private:
  112. unsigned char _pin;
  113. bool _inverse;
  114. LedMode _mode;
  115. LedPattern _pattern;
  116. };
  117. size_t ledCount();
  118. void ledUpdate(bool do_update);
  119. bool ledStatus(size_t id, bool status);
  120. bool ledStatus(size_t id);
  121. void ledSetup();