尝试通过类构造函数动态设置 LED 引脚时出现编译错误

问题描述 投票:0回答:1

我正在尝试创建一个使用 FastLED 的类,其中可以动态设置 LED 引脚。但是,当我尝试使用成员变量 pin 调用 FastLED.addLeds 时,出现编译错误。

// LogoLED.h
#ifndef LOGOLED_H
#define LOGOLED_H
#include <FastLED.h>

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<WS2812B, _dataPin, GRB>(_leds, _numLeds);
    off();
}

// ... other methods ...

// main.cpp
#include <Arduino.h>
#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函数,以便动态设置引脚?我想避免将引脚作为模板参数传递,因为我想保持在运行时更改引脚的灵活性。

c++ class fastled
1个回答
0
投票

似乎

_dataPin
不是有效的模板参数,因为它在编译时未知(不是
constexpr
)。

尝试将其替换为硬编码数字,如果有效,请考虑如何在代码中表示它。也许作为您的类 LogoLED 的模板参数?

© www.soinside.com 2019 - 2024. All rights reserved.