Wemos D1 Mini Frimware zur Steuerung einer RGBW-LED-Lampe
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LEDStripManager.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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::doSpectral() {
  164. static uint16_t hue = 0;
  165. static uint32_t lastUpdate = 0;
  166. const uint32_t stepTime = 10000; // 5 s bis zur nächsten Farbe
  167. const uint16_t hueStep = 65536 / 6; // Abstand der Hauptfarben
  168. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  169. uint32_t now = millis();
  170. if (now - lastUpdate >= 20) {
  171. lastUpdate = now;
  172. hue += (uint32_t) hueStep * 20 / stepTime;
  173. uint32_t color = strip.gamma32(strip.ColorHSV(hue));
  174. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  175. strip.setPixelColor(i, color);
  176. }
  177. strip.show();
  178. }
  179. }
  180. }
  181. void LEDStripManager::fade() {
  182. static const uint8_t colors[][3] = {
  183. { 255, 0, 0 }, // Rot
  184. { 255, 32, 0 }, // Orange
  185. { 255, 110, 0 }, // Gelb
  186. { 0, 255, 0 }, // Grün
  187. { 0, 255, 255 }, // Cyan
  188. { 0, 0, 255 }, // Blau
  189. { 255, 0, 255 } // Violett
  190. };
  191. enum State {
  192. HOLD,
  193. FADE_OUT,
  194. FADE_IN
  195. };
  196. static State state = HOLD;
  197. static uint8_t currentColor = 0;
  198. static uint32_t stateStart = millis();
  199. const uint32_t holdTime = 5000;
  200. const uint32_t fadeTime = 1000;
  201. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  202. Config cfg = configManager.getConfig();
  203. uint32_t now = millis();
  204. uint32_t elapsed = now - stateStart;
  205. float brightness = 1.0f;
  206. float maxBrightness = float(cfg.brightness) / 255;
  207. switch (state) {
  208. case HOLD:
  209. brightness = maxBrightness;
  210. if (elapsed >= holdTime) {
  211. state = FADE_OUT;
  212. stateStart = now;
  213. }
  214. break;
  215. case FADE_OUT:
  216. brightness = maxBrightness - (float) elapsed / fadeTime;
  217. if (brightness < 0.0f)
  218. brightness = 0.0f;
  219. if (elapsed >= fadeTime) {
  220. currentColor = (currentColor + 1) % (sizeof(colors) / sizeof(colors[0]));
  221. state = FADE_IN;
  222. stateStart = now;
  223. }
  224. break;
  225. case FADE_IN:
  226. brightness = (float) elapsed / fadeTime;
  227. if (brightness > maxBrightness)
  228. brightness = maxBrightness;
  229. if (elapsed >= fadeTime) {
  230. state = HOLD;
  231. stateStart = now;
  232. }
  233. break;
  234. }
  235. uint8_t r = colors[currentColor][0] * brightness;
  236. uint8_t g = colors[currentColor][1] * brightness;
  237. uint8_t b = colors[currentColor][2] * brightness;
  238. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  239. strip.setPixelColor(i, strip.Color(r, g, b));
  240. }
  241. strip.show();
  242. }
  243. }
  244. void LEDStripManager::flash() {
  245. static const uint32_t onMillis = 1000;
  246. static const uint32_t offMillis = 500;
  247. static bool ledOn = false;
  248. static uint32_t lastChange = 0;
  249. if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
  250. Config cfg = configManager.getConfig();
  251. uint32_t now = millis();
  252. if (ledOn) {
  253. if (now - lastChange >= onMillis) {
  254. // LEDs ausschalten
  255. clear();
  256. show();
  257. ledOn = false;
  258. lastChange = now;
  259. }
  260. } else {
  261. if (now - lastChange >= offMillis) {
  262. // LEDs einschalten
  263. setAll(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
  264. ledOn = true;
  265. lastChange = now;
  266. }
  267. }
  268. }
  269. }