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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Config cfg = configManager.getConfig();
  24. if (cfg.version != CONFIG_VERSION) {
  25. // Initalize cfg with defaults
  26. cfg = configManager.setToDefaultConfig();
  27. }
  28. strip.begin(); // Initialisiert den NeoPixel-Streifen
  29. strip.clear();
  30. strip.show(); // Setzt alle LEDs auf "aus"
  31. strip.setBrightness(cfg.brightness); // Helligkeit (0 bis 255)
  32. // Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss)
  33. strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
  34. // Setzt die zweite LED auf grün
  35. strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
  36. strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
  37. strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
  38. // Schickt die Farbinformationen zum LED-Streifen
  39. show();
  40. }
  41. void LEDStripManager::clear() {
  42. strip.clear();
  43. }
  44. void LEDStripManager::show() {
  45. strip.show();
  46. }
  47. void LEDStripManager::on() {
  48. Serial.println("LEDStripManager LED ON");
  49. digitalWrite(LED_STRIP_POWER_PIN, HIGH);
  50. delay(50);
  51. digitalWrite(LED_STRIP_DATA_PIN, HIGH);
  52. delay(50);
  53. strip.begin();
  54. strip.show();
  55. // TODO: Load the state from the eeprom
  56. strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
  57. strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
  58. strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
  59. strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
  60. strip.show();
  61. }
  62. void LEDStripManager::off() {
  63. Serial.println("LEDStripManager LED OFF");
  64. clear();
  65. show();
  66. delay(50);
  67. digitalWrite(LED_STRIP_POWER_PIN, LOW); // LED zunächst AUS
  68. digitalWrite(LED_STRIP_DATA_PIN, LOW);
  69. }
  70. void LEDStripManager::setBrightness(uint8_t brightness) {
  71. strip.setBrightness(brightness);
  72. }
  73. void LEDStripManager::setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue) {
  74. if (index >= strip.numPixels()) {
  75. return;
  76. }
  77. strip.setPixelColor(index, strip.Color(red, green, blue));
  78. }
  79. void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue) {
  80. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  81. strip.setPixelColor(i, strip.Color(red, green, blue));
  82. }
  83. strip.show();
  84. }
  85. void LEDStripManager::onModeChange(LEDModes mode) {
  86. Config cfg = configManager.getConfig();
  87. cfg.mode = mode;
  88. configManager.saveConfig(cfg);
  89. //TODO: react on mode change on strip
  90. }
  91. uint32_t
  92. LEDStripManager::wheel(uint8_t pos) {
  93. pos = 255 - pos;
  94. if (pos < 85) {
  95. return strip.Color(255 - pos * 3, 0, pos * 3);
  96. }
  97. if (pos < 170) {
  98. pos -= 85;
  99. return strip.Color(0, pos * 3, 255 - pos * 3);
  100. }
  101. pos -= 170;
  102. return strip.Color(pos * 3, 255 - pos * 3, 0);
  103. }
  104. void LEDStripManager::rainbow(uint8_t wait) {
  105. for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
  106. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  107. uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  108. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  109. }
  110. strip.show();
  111. delay(wait);
  112. }
  113. }