Make LEDOutput responsible for inverting output

This commit is contained in:
Robert Marshall 2021-07-30 08:48:45 +01:00
parent 8c50498c7d
commit 0432071759
3 changed files with 10 additions and 6 deletions

View file

@ -6,10 +6,12 @@
class LEDOutput {
unsigned int _channel;
bool _invert;
public:
LEDOutput(unsigned int channel) {
public:
LEDOutput(unsigned int channel, bool invert = false) {
_channel = channel;
_invert = invert;
ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION);
}
@ -17,6 +19,9 @@ class LEDOutput {
ledcAttachPin(pin, _channel);
}
void writeFraction(float fraction){
ledcWrite(_channel, fraction * LED_OUTPUT_PWM_RANGE);
auto value = fraction * LED_OUTPUT_PWM_RANGE;
if (_invert)
value = LED_OUTPUT_PWM_RANGE - value;
ledcWrite(_channel, value);
}
};