Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LEDStripManager.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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, WebSocketsServer &webSocket)
  12. : strip(numLeds, pin, NEO_GRBW + NEO_KHZ800), configManager(configManager), webSocket(webSocket) {
  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. webSocket.sendTXT(0, "LED ON");
  65. }
  66. void LEDStripManager::off() {
  67. Serial.println("LEDStripManager LED OFF");
  68. clear();
  69. show();
  70. delay(50);
  71. digitalWrite(LED_STRIP_POWER_PIN, LOW); // LED zunächst AUS
  72. digitalWrite(LED_STRIP_DATA_PIN, LOW);
  73. webSocket.sendTXT(0, "LED OFF");
  74. }
  75. bool LEDStripManager::isOn() {
  76. return digitalRead(LED_STRIP_POWER_PIN) == HIGH;
  77. }
  78. void LEDStripManager::setBrightness(uint8_t brightness) {
  79. strip.setBrightness(brightness);
  80. strip.show();
  81. }
  82. void LEDStripManager::setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  83. if (index >= strip.numPixels()) {
  84. return;
  85. }
  86. strip.setPixelColor(index, strip.Color(red, green, blue, white));
  87. strip.show();
  88. }
  89. void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  90. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  91. strip.setPixelColor(i, strip.Color(red, green, blue, white));
  92. }
  93. strip.show();
  94. }
  95. void LEDStripManager::onModeChange(LEDModes mode) {
  96. Config cfg = configManager.getConfig();
  97. cfg.mode = mode;
  98. configManager.saveConfig(cfg);
  99. // Handle only single action modes here. For Animations use loop functions with nonblocking timers
  100. switch (mode) {
  101. case LM_SINGLE_COLOR:
  102. this->setBrightness(cfg.brightness);
  103. setAll(cfg.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.w);
  104. return;
  105. case LM_SEGMENTS:
  106. this->setBrightness(cfg.brightness);
  107. setSegments();
  108. return;
  109. default:
  110. return;
  111. }
  112. }
  113. void LEDStripManager::onColorChange(String cfgVal, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
  114. Config cfg = configManager.getConfig();
  115. RGBWColor *color = nullptr;
  116. if (cfgVal == "singleColor")
  117. color = &cfg.singleColor;
  118. else if (cfgVal == "segmentColor1")
  119. color = &cfg.segmentColor1;
  120. else if (cfgVal == "segmentColor2")
  121. color = &cfg.segmentColor2;
  122. else if (cfgVal == "segmentColor3")
  123. color = &cfg.segmentColor3;
  124. if (color != nullptr) {
  125. color->r = red;
  126. color->g = green;
  127. color->b = blue;
  128. color->w = white;
  129. configManager.saveConfig(cfg);
  130. }
  131. if (cfgVal == "singleColor") {
  132. this->setAll(red, green, blue, white);
  133. } else {
  134. setSegments();
  135. }
  136. }
  137. void LEDStripManager::onBrightnessChange(uint8_t brightness) {
  138. Config cfg = configManager.getConfig();
  139. cfg.brightness = brightness;
  140. configManager.saveConfig(cfg);
  141. this->setBrightness(brightness);
  142. }
  143. void LEDStripManager::onCfgParamChange(String cfgVal, uint32_t amount) {
  144. Config cfg = configManager.getConfig();
  145. if (cfgVal == "rainbowDelay")
  146. cfg.rainbowDelay = amount;
  147. else if (cfgVal == "spectralStepTime")
  148. cfg.spectralStepTime = amount;
  149. else if (cfgVal == "fadingHoldTime")
  150. cfg.fadingHoldTime = amount;
  151. else if (cfgVal == "fadingFadeTime")
  152. cfg.fadingFadeTime = amount;
  153. else if (cfgVal == "flashOnTime")
  154. cfg.flashOnTime = amount;
  155. else if (cfgVal == "flashOffTime")
  156. cfg.flashOffTime = amount;
  157. configManager.saveConfig(cfg);
  158. }
  159. void LEDStripManager::setSegments() {
  160. Config cfg = configManager.getConfig();
  161. RGBWColor c1 = cfg.segmentColor1;
  162. RGBWColor c2 = cfg.segmentColor2;
  163. RGBWColor c3 = cfg.segmentColor3;
  164. int nbrPixel = strip.numPixels();
  165. int third = nbrPixel / 3;
  166. for (int i = 0; i < nbrPixel; i++) {
  167. if (i < third)
  168. setPixel(i, c1.r, c1.g, c1.b, c1.w);
  169. else if (i < 2 * third)
  170. setPixel(i, c2.r, c2.g, c2.b, c2.w);
  171. else
  172. setPixel(i, c3.r, c3.g, c3.b, c3.w);
  173. }
  174. show();
  175. }
  176. uint32_t LEDStripManager::wheel(uint8_t pos) {
  177. pos = 255 - pos;
  178. if (pos < 85) {
  179. return strip.Color(255 - pos * 3, 0, pos * 3);
  180. }
  181. if (pos < 170) {
  182. pos -= 85;
  183. return strip.Color(0, pos * 3, 255 - pos * 3);
  184. }
  185. pos -= 170;
  186. return strip.Color(pos * 3, 255 - pos * 3, 0);
  187. }
  188. void LEDStripManager::rainbow() {
  189. Config cfg = configManager.getConfig();
  190. const uint32_t delay = cfg.rainbowDelay;
  191. static uint32_t lastUpdate = 0;
  192. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  193. if (millis() - lastUpdate < delay)
  194. return;
  195. lastUpdate = millis();
  196. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  197. uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  198. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  199. }
  200. strip.show();
  201. firstPixelHue = (firstPixelHue + 256) % 65536;
  202. }
  203. }
  204. void LEDStripManager::doSpectral() {
  205. Config cfg = configManager.getConfig();
  206. static uint16_t hue = 0;
  207. static uint32_t lastUpdate = 0;
  208. const uint16_t hueStep = 65536 / 6; // Abstand der Hauptfarben
  209. const uint32_t stepTime = cfg.spectralStepTime; // Zeit bis zur nächsten Farbe
  210. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  211. uint32_t now = millis();
  212. if (now - lastUpdate >= 20) {
  213. lastUpdate = now;
  214. hue += (uint32_t) hueStep * 20 / stepTime;
  215. uint32_t color = strip.gamma32(strip.ColorHSV(hue));
  216. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  217. strip.setPixelColor(i, color);
  218. }
  219. strip.show();
  220. }
  221. }
  222. }
  223. void LEDStripManager::fade() {
  224. static const uint8_t colors[][3] = {
  225. { 255, 0, 0 }, // Rot
  226. { 255, 32, 0 }, // Orange
  227. { 255, 110, 0 }, // Gelb
  228. { 0, 255, 0 }, // Grün
  229. { 0, 255, 255 }, // Cyan
  230. { 0, 0, 255 }, // Blau
  231. { 255, 0, 255 } // Violett
  232. };
  233. enum State {
  234. HOLD,
  235. FADE_OUT,
  236. FADE_IN
  237. };
  238. Config cfg = configManager.getConfig();
  239. static State state = HOLD;
  240. static uint8_t currentColor = 0;
  241. static uint32_t stateStart = millis();
  242. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  243. uint32_t holdTime = cfg.fadingHoldTime;
  244. uint32_t fadeTime = cfg.fadingFadeTime;
  245. uint32_t now = millis();
  246. uint32_t elapsed = now - stateStart;
  247. float brightness = 1.0f;
  248. float maxBrightness = float(cfg.brightness) / 255;
  249. switch (state) {
  250. case HOLD:
  251. brightness = maxBrightness;
  252. if (elapsed >= holdTime) {
  253. state = FADE_OUT;
  254. stateStart = now;
  255. }
  256. break;
  257. case FADE_OUT:
  258. brightness = maxBrightness - (float) elapsed / fadeTime;
  259. if (brightness < 0.0f)
  260. brightness = 0.0f;
  261. if (elapsed >= fadeTime) {
  262. currentColor = (currentColor + 1) % (sizeof(colors) / sizeof(colors[0]));
  263. state = FADE_IN;
  264. stateStart = now;
  265. }
  266. break;
  267. case FADE_IN:
  268. brightness = (float) elapsed / fadeTime;
  269. if (brightness > maxBrightness)
  270. brightness = maxBrightness;
  271. if (elapsed >= fadeTime) {
  272. state = HOLD;
  273. stateStart = now;
  274. }
  275. break;
  276. }
  277. uint8_t r = colors[currentColor][0] * brightness;
  278. uint8_t g = colors[currentColor][1] * brightness;
  279. uint8_t b = colors[currentColor][2] * brightness;
  280. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  281. strip.setPixelColor(i, strip.Color(r, g, b));
  282. }
  283. strip.show();
  284. }
  285. }
  286. void LEDStripManager::flash() {
  287. static bool ledOn = false;
  288. static uint32_t lastChange = 0;
  289. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  290. Config cfg = configManager.getConfig();
  291. uint32_t onMillis = cfg.flashOnTime;
  292. uint32_t offMillis = cfg.flashOffTime;
  293. uint32_t now = millis();
  294. if (ledOn) {
  295. if (now - lastChange >= onMillis) {
  296. // LEDs ausschalten
  297. clear();
  298. show();
  299. ledOn = false;
  300. lastChange = now;
  301. }
  302. } else {
  303. if (now - lastChange >= offMillis) {
  304. // LEDs einschalten
  305. setAll(cfg.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.w);
  306. ledOn = true;
  307. lastChange = now;
  308. }
  309. }
  310. }
  311. }