33 lines
657 B
C++
33 lines
657 B
C++
#ifndef LEDOutput_cpp
|
|
#define LEDOutput_cpp
|
|
|
|
#include <Arduino.h>
|
|
|
|
#define LED_OUTPUT_FREQUENCY 490
|
|
#define LED_OUTPUT_RESOLUTION 15
|
|
#define LED_OUTPUT_PWM_RANGE 32767
|
|
|
|
class LEDOutput {
|
|
unsigned int _channel;
|
|
bool _invert;
|
|
|
|
public:
|
|
LEDOutput(unsigned int channel, bool invert = false) {
|
|
_channel = channel;
|
|
_invert = invert;
|
|
ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION);
|
|
}
|
|
|
|
void attach(unsigned int pin){
|
|
ledcAttachPin(pin, _channel);
|
|
}
|
|
|
|
void writeFraction(float fraction){
|
|
auto value = fraction * LED_OUTPUT_PWM_RANGE;
|
|
if (_invert)
|
|
value = LED_OUTPUT_PWM_RANGE - value;
|
|
ledcWrite(_channel, value);
|
|
}
|
|
};
|
|
|
|
#endif
|