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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.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.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.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(String cfgVal, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  109. Config cfg = configManager.getConfig();
  110. RGBWColor *color = nullptr;
  111. if (cfgVal == "singleColor")
  112. color = &cfg.singleColor;
  113. else if (cfgVal == "segmentColor1")
  114. color = &cfg.segmentColor1;
  115. else if (cfgVal == "segmentColor2")
  116. color = &cfg.segmentColor2;
  117. else if (cfgVal == "segmentColor3")
  118. color = &cfg.segmentColor3;
  119. if (color != nullptr) {
  120. color->r = red;
  121. color->g = green;
  122. color->b = blue;
  123. color->w = white;
  124. configManager.saveConfig(cfg);
  125. }
  126. if (cfgVal == "singleColor") {
  127. this->setAll(red, green, blue, white);
  128. } else {
  129. setSegments();
  130. }
  131. }
  132. void LEDStripManager::onBrightnessChange(uint8_t brightness) {
  133. Config cfg = configManager.getConfig();
  134. cfg.brightness = brightness;
  135. configManager.saveConfig(cfg);
  136. this->setBrightness(brightness);
  137. }
  138. void LEDStripManager::onCfgParamChange(String cfgVal, uint32_t amount) {
  139. Config cfg = configManager.getConfig();
  140. if (cfgVal == "rainbowDelay")
  141. cfg.rainbowDelay = amount;
  142. else if (cfgVal == "spectralStepTime")
  143. cfg.spectralStepTime = amount;
  144. else if (cfgVal == "fadingHoldTime")
  145. cfg.fadingHoldTime = amount;
  146. else if (cfgVal == "fadingFadeTime")
  147. cfg.fadingFadeTime = amount;
  148. else if (cfgVal == "flashOnTime")
  149. cfg.flashOnTime = amount;
  150. else if (cfgVal == "flashOffTime")
  151. cfg.flashOffTime = amount;
  152. configManager.saveConfig(cfg);
  153. }
  154. void LEDStripManager::setSegments() {
  155. Config cfg = configManager.getConfig();
  156. RGBWColor c1 = cfg.segmentColor1;
  157. RGBWColor c2 = cfg.segmentColor2;
  158. RGBWColor c3 = cfg.segmentColor3;
  159. int nbrPixel = strip.numPixels();
  160. int third = nbrPixel / 3;
  161. for (int i = 0; i < nbrPixel; i++) {
  162. if (i < third)
  163. setPixel(i, c1.r, c1.g, c1.b, c1.w);
  164. else if (i < 2 * third)
  165. setPixel(i, c2.r, c2.g, c2.b, c2.w);
  166. else
  167. setPixel(i, c3.r, c3.g, c3.b, c3.w);
  168. }
  169. show();
  170. }
  171. uint32_t LEDStripManager::wheel(uint8_t pos) {
  172. pos = 255 - pos;
  173. if (pos < 85) {
  174. return strip.Color(255 - pos * 3, 0, pos * 3);
  175. }
  176. if (pos < 170) {
  177. pos -= 85;
  178. return strip.Color(0, pos * 3, 255 - pos * 3);
  179. }
  180. pos -= 170;
  181. return strip.Color(pos * 3, 255 - pos * 3, 0);
  182. }
  183. void LEDStripManager::rainbow() {
  184. Config cfg = configManager.getConfig();
  185. const uint32_t delay = cfg.rainbowDelay;
  186. static uint32_t lastUpdate = 0;
  187. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  188. if (millis() - lastUpdate < delay)
  189. return;
  190. lastUpdate = millis();
  191. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  192. uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  193. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  194. }
  195. strip.show();
  196. firstPixelHue = (firstPixelHue + 256) % 65536;
  197. }
  198. }
  199. void LEDStripManager::doSpectral() {
  200. Config cfg = configManager.getConfig();
  201. static uint16_t hue = 0;
  202. static uint32_t lastUpdate = 0;
  203. const uint16_t hueStep = 65536 / 6; // Abstand der Hauptfarben
  204. const uint32_t stepTime = cfg.spectralStepTime; // Zeit bis zur nächsten Farbe
  205. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  206. uint32_t now = millis();
  207. if (now - lastUpdate >= 20) {
  208. lastUpdate = now;
  209. hue += (uint32_t) hueStep * 20 / stepTime;
  210. uint32_t color = strip.gamma32(strip.ColorHSV(hue));
  211. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  212. strip.setPixelColor(i, color);
  213. }
  214. strip.show();
  215. }
  216. }
  217. }
  218. void LEDStripManager::fade() {
  219. static const uint8_t colors[][3] = {
  220. { 255, 0, 0 }, // Rot
  221. { 255, 32, 0 }, // Orange
  222. { 255, 110, 0 }, // Gelb
  223. { 0, 255, 0 }, // Grün
  224. { 0, 255, 255 }, // Cyan
  225. { 0, 0, 255 }, // Blau
  226. { 255, 0, 255 } // Violett
  227. };
  228. enum State {
  229. HOLD,
  230. FADE_OUT,
  231. FADE_IN
  232. };
  233. Config cfg = configManager.getConfig();
  234. static State state = HOLD;
  235. static uint8_t currentColor = 0;
  236. static uint32_t stateStart = millis();
  237. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  238. uint32_t holdTime = cfg.fadingHoldTime;
  239. uint32_t fadeTime = cfg.fadingFadeTime;
  240. uint32_t now = millis();
  241. uint32_t elapsed = now - stateStart;
  242. float brightness = 1.0f;
  243. float maxBrightness = float(cfg.brightness) / 255;
  244. switch (state) {
  245. case HOLD:
  246. brightness = maxBrightness;
  247. if (elapsed >= holdTime) {
  248. state = FADE_OUT;
  249. stateStart = now;
  250. }
  251. break;
  252. case FADE_OUT:
  253. brightness = maxBrightness - (float) elapsed / fadeTime;
  254. if (brightness < 0.0f)
  255. brightness = 0.0f;
  256. if (elapsed >= fadeTime) {
  257. currentColor = (currentColor + 1) % (sizeof(colors) / sizeof(colors[0]));
  258. state = FADE_IN;
  259. stateStart = now;
  260. }
  261. break;
  262. case FADE_IN:
  263. brightness = (float) elapsed / fadeTime;
  264. if (brightness > maxBrightness)
  265. brightness = maxBrightness;
  266. if (elapsed >= fadeTime) {
  267. state = HOLD;
  268. stateStart = now;
  269. }
  270. break;
  271. }
  272. uint8_t r = colors[currentColor][0] * brightness;
  273. uint8_t g = colors[currentColor][1] * brightness;
  274. uint8_t b = colors[currentColor][2] * brightness;
  275. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  276. strip.setPixelColor(i, strip.Color(r, g, b));
  277. }
  278. strip.show();
  279. }
  280. }
  281. void LEDStripManager::flash() {
  282. static bool ledOn = false;
  283. static uint32_t lastChange = 0;
  284. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  285. Config cfg = configManager.getConfig();
  286. uint32_t onMillis = cfg.flashOnTime;
  287. uint32_t offMillis = cfg.flashOffTime;
  288. uint32_t now = millis();
  289. if (ledOn) {
  290. if (now - lastChange >= onMillis) {
  291. // LEDs ausschalten
  292. clear();
  293. show();
  294. ledOn = false;
  295. lastChange = now;
  296. }
  297. } else {
  298. if (now - lastChange >= offMillis) {
  299. // LEDs einschalten
  300. setAll(cfg.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.w);
  301. ledOn = true;
  302. lastChange = now;
  303. }
  304. }
  305. }
  306. }