Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
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.

LEDStripManager.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * LEDStripManager.cpp
  3. *
  4. * Created on: 19.06.2026
  5. * Author: FSmilari
  6. */
  7. #include "LEDStripManager.h"
  8. /*
  9. * Constructor
  10. */
  11. LEDStripManager::LEDStripManager(uint16_t numLeds, uint8_t pin, ConfigManager &configManager)
  12. : strip(numLeds, pin, NEO_GRBW + NEO_KHZ800), configManager(configManager) {
  13. }
  14. /*
  15. * Destructor
  16. */
  17. LEDStripManager::~LEDStripManager() {
  18. }
  19. void LEDStripManager::begin() {
  20. pinMode(LED_STRIP_POWER_PIN, OUTPUT);
  21. pinMode(LED_STRIP_DATA_PIN, OUTPUT);
  22. digitalWrite(LED_STRIP_POWER_PIN, HIGH); // LED zunächst EIN
  23. digitalWrite(LED_STRIP_DATA_PIN, HIGH);
  24. Config cfg = configManager.getConfig();
  25. if (cfg.version != CONFIG_VERSION) {
  26. // Initalize cfg with defaults
  27. cfg = configManager.setToDefaultConfig();
  28. configManager.loadConfig();
  29. cfg = configManager.getConfig();
  30. }
  31. strip.begin(); // Initialisiert den NeoPixel-Streifen
  32. strip.clear();
  33. strip.show(); // Setzt alle LEDs auf "aus"
  34. switch (cfg.mode) {
  35. case LM_SINGLE_COLOR:
  36. this->setBrightness(cfg.brightness);
  37. setAll(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
  38. return;
  39. case LM_SEGMENTS:
  40. this->setBrightness(cfg.brightness);
  41. setSegments();
  42. return;
  43. default:
  44. // Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss)
  45. strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
  46. // Setzt die zweite LED auf grün
  47. strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
  48. strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
  49. strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
  50. // Schickt die Farbinformationen zum LED-Streifen
  51. show();
  52. return;
  53. }
  54. }
  55. void LEDStripManager::clear() {
  56. strip.clear();
  57. }
  58. void LEDStripManager::show() {
  59. strip.show();
  60. }
  61. void LEDStripManager::on() {
  62. Serial.println("LEDStripManager LED ON");
  63. begin();
  64. }
  65. void LEDStripManager::off() {
  66. Serial.println("LEDStripManager LED OFF");
  67. clear();
  68. show();
  69. delay(50);
  70. digitalWrite(LED_STRIP_POWER_PIN, LOW); // LED zunächst AUS
  71. digitalWrite(LED_STRIP_DATA_PIN, LOW);
  72. }
  73. void LEDStripManager::setBrightness(uint8_t brightness) {
  74. strip.setBrightness(brightness);
  75. strip.show();
  76. }
  77. void LEDStripManager::setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  78. if (index >= strip.numPixels()) {
  79. return;
  80. }
  81. strip.setPixelColor(index, strip.Color(red, green, blue, white));
  82. strip.show();
  83. }
  84. void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  85. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  86. strip.setPixelColor(i, strip.Color(red, green, blue, white));
  87. }
  88. strip.show();
  89. }
  90. void LEDStripManager::onModeChange(LEDModes mode) {
  91. Config cfg = configManager.getConfig();
  92. cfg.mode = mode;
  93. configManager.saveConfig(cfg);
  94. // Handle only single action modes here. For Animations use loop functions with nonblocking timers
  95. switch (mode) {
  96. case LM_SINGLE_COLOR:
  97. this->setBrightness(cfg.brightness);
  98. setAll(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
  99. return;
  100. case LM_SEGMENTS:
  101. this->setBrightness(cfg.brightness);
  102. setSegments();
  103. return;
  104. default:
  105. return;
  106. }
  107. }
  108. void LEDStripManager::onColorChange(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  109. Config cfg = configManager.getConfig();
  110. cfg.color.r = red;
  111. cfg.color.g = green;
  112. cfg.color.b = blue;
  113. cfg.color.w = white;
  114. configManager.saveConfig(cfg);
  115. this->setAll(red, green, blue, white);
  116. }
  117. void LEDStripManager::onBrightnessChange(uint8_t brightness) {
  118. Config cfg = configManager.getConfig();
  119. cfg.brightness = brightness;
  120. configManager.saveConfig(cfg);
  121. this->setBrightness(brightness);
  122. }
  123. void LEDStripManager::setSegments() {
  124. int nbrPixel = strip.numPixels();
  125. int third = nbrPixel / 3;
  126. for (int i = 0; i < nbrPixel; i++) {
  127. if (i < third)
  128. setPixel(i, 255, 0, 0, 0);
  129. else if (i < 2 * third)
  130. setPixel(i, 0, 255, 0, 0);
  131. else
  132. setPixel(i, 0, 0, 255, 0);
  133. }
  134. show();
  135. }
  136. uint32_t LEDStripManager::wheel(uint8_t pos) {
  137. pos = 255 - pos;
  138. if (pos < 85) {
  139. return strip.Color(255 - pos * 3, 0, pos * 3);
  140. }
  141. if (pos < 170) {
  142. pos -= 85;
  143. return strip.Color(0, pos * 3, 255 - pos * 3);
  144. }
  145. pos -= 170;
  146. return strip.Color(pos * 3, 255 - pos * 3, 0);
  147. }
  148. void LEDStripManager::rainbow() {
  149. static uint8_t delay = 20;
  150. static uint32_t lastUpdate = 0;
  151. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  152. if (millis() - lastUpdate < delay)
  153. return;
  154. lastUpdate = millis();
  155. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  156. uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  157. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  158. }
  159. strip.show();
  160. firstPixelHue = (firstPixelHue + 256) % 65536;
  161. }
  162. }
  163. void LEDStripManager::flash() {
  164. static const uint32_t onMillis = 1000;
  165. static const uint32_t offMillis = 500;
  166. static bool ledOn = false;
  167. static uint32_t lastChange = 0;
  168. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  169. Config cfg = configManager.getConfig();
  170. uint32_t now = millis();
  171. if (ledOn) {
  172. if (now - lastChange >= onMillis) {
  173. // LEDs ausschalten
  174. clear();
  175. show();
  176. ledOn = false;
  177. lastChange = now;
  178. }
  179. } else {
  180. if (now - lastChange >= offMillis) {
  181. // LEDs einschalten
  182. setAll(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
  183. ledOn = true;
  184. lastChange = now;
  185. }
  186. }
  187. }
  188. }