Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LEDStripManager.cpp 7.9KB

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