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

@ -54,7 +54,6 @@ public:
} }
void loop() { void loop() {
float multiplier = getOutputMultiplier(); _output->writeFraction(getOutputMultiplier());
_output->writeFraction(1 - multiplier);
} }
}; };

View file

@ -6,10 +6,12 @@
class LEDOutput { class LEDOutput {
unsigned int _channel; unsigned int _channel;
bool _invert;
public: public:
LEDOutput(unsigned int channel) { LEDOutput(unsigned int channel, bool invert = false) {
_channel = channel; _channel = channel;
_invert = invert;
ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION); ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION);
} }
@ -17,6 +19,9 @@ class LEDOutput {
ledcAttachPin(pin, _channel); ledcAttachPin(pin, _channel);
} }
void writeFraction(float fraction){ 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);
} }
}; };

View file

@ -32,7 +32,7 @@ class BluetoothCallback : public BLECharacteristicCallbacks {
}; };
LEDOutput _ledOutput(0); LEDOutput _ledOutput(0, true);
LED _led(&_ledOutput, FADE_IN_DURATION, FADE_OUT_DURATION); LED _led(&_ledOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
void toggle(){ void toggle(){