Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LEDStripManager.cpp 8.8KB

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