Я пытаюсь создать класс, использующий FastLED, в котором вывод светодиода можно будет устанавливать динамически. Однако я получаю ошибку компиляции при попытке вызвать FastLED.addLeds с помощью PIN-кода переменной-члена.
// LogoLED.h
#ifndef LOGOLED_H
#define LOGOLED_H
#include
class LogoLED
{
public:
LogoLED(uint8_t numLeds, uint8_t dataPin);
void begin();
// ... other methods ...
private:
uint8_t _numLeds;
uint8_t _dataPin;
CRGB *_leds;
bool _isOn;
};
#endif
// LogoLED.cpp
#include "LogoLED.h"
LogoLED::LogoLED(uint8_t numLeds, uint8_t dataPin) : _numLeds(numLeds), _dataPin(dataPin), _isOn(false)
{
_leds = new CRGB[_numLeds];
}
void LogoLED::begin()
{
FastLED.addLeds(_leds, _numLeds);
off();
}
// ... other methods ...
// main.cpp
#include
#include "LogoLED.h"
#define LED_PIN 6
LogoLED logoLED(4, LED_PIN);
void setup()
{
logoLED.begin();
// ... more setup code ...
}
void loop()
{
// ... loop code ...
}
При компиляции получаю следующую ошибку:
CopyNo instance of overloaded function "CFastLED::addLeds" matches the argument list.
Argument types are: (CRGB *, uint8_t)
Object type is: CFastLED
Как правильно вызвать функцию FastLED.addLeds, чтобы пин можно было устанавливать динамически? Я хотел бы избежать передачи PIN-кода в качестве параметра шаблона, так как я хочу сохранить возможность изменения PIN-кода во время выполнения.
-------- -- Решение на основе шаблонов в виде одного файла --------------
#include
#include
#define NUMBER_OF_LEDS 4
#define LED_PIN 6
template
class LogoLED
{
public:
LogoLED(uint8_t numLeds);
void begin();
void on();
void off();
void setColor(uint8_t r, uint8_t g, uint8_t b);
void setBrightness(uint8_t brightness);
private:
uint8_t _numLeds;
CRGB *_leds;
bool _isOn;
};
// Implementierung der LogoLED Methoden
template
LogoLED::LogoLED(uint8_t numLeds) : _numLeds(numLeds), _isOn(false)
{
_leds = new CRGB[_numLeds]; // Dynamisches Array für LEDs erzeugen
}
template
void LogoLED::begin()
{
FastLED.addLeds(_leds, _numLeds);
off(); // Startet mit ausgeschalteten LEDs
}
template
void LogoLED::on()
{
_isOn = true;
FastLED.show();
}
template
void LogoLED::off()
{
_isOn = false;
fill_solid(_leds, _numLeds, CRGB::Black); // Alle LEDs ausschalten
FastLED.show();
}
template
void LogoLED::setColor(uint8_t r, uint8_t g, uint8_t b)
{
fill_solid(_leds, _numLeds, CRGB(r, g, b));
if (_isOn)
{
FastLED.show();
}
}
template
void LogoLED::setBrightness(uint8_t brightness)
{
FastLED.setBrightness(brightness);
if (_isOn)
{
FastLED.show();
}
}
// Hauptprogramm
LogoLED logoLED(NUMBER_OF_LEDS);
void setup()
{
logoLED.begin();
logoLED.setBrightness(10);
logoLED.setColor(255, 255, 255);
logoLED.on();
}
void loop()
{
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... onstructor