diff --git a/src/src/extras/PwmPin.cpp b/src/src/extras/PwmPin.cpp index 404105f..b019dfd 100644 --- a/src/src/extras/PwmPin.cpp +++ b/src/src/extras/PwmPin.cpp @@ -102,7 +102,7 @@ LedPin::LedPin(uint8_t pin, float level, uint16_t freq, boolean invert) : LedC(p /////////////////// -void LedPin::set(float level, uint32_t fadeTime){ +void LedPin::set(float level){ if(!channel) return; @@ -110,14 +110,25 @@ void LedPin::set(float level, uint32_t fadeTime){ if(level>100) level=100; - float d=level*(pow(2,(int)timer->duty_resolution)-1)/100.0; + float d=level*(pow(2,(int)timer->duty_resolution)-1)/100.0; + + channel->duty=d; + ledc_channel_config(channel); +} - if(fadeTime==0){ - channel->duty=d; - ledc_channel_config(channel); - } else { - ledc_set_fade_time_and_start(channel->speed_mode,channel->channel,(uint32_t)d,fadeTime,LEDC_FADE_NO_WAIT); - } +/////////////////// + +void LedPin::fade(float level, uint32_t fadeTime){ + + if(!channel) + return; + + if(level>100) + level=100; + + float d=level*(pow(2,(int)timer->duty_resolution)-1)/100.0; + + ledc_set_fade_time_and_start(channel->speed_mode,channel->channel,(uint32_t)d,fadeTime,LEDC_FADE_NO_WAIT); } /////////////////// diff --git a/src/src/extras/PwmPin.h b/src/src/extras/PwmPin.h index b6c6452..68751ca 100644 --- a/src/src/extras/PwmPin.h +++ b/src/src/extras/PwmPin.h @@ -76,7 +76,8 @@ class LedPin : public LedC { public: LedPin(uint8_t pin, float level=0, uint16_t freq=DEFAULT_PWM_FREQ, boolean invert=false); // assigns pin to be output of one of 16 PWM channels initial level and frequency - void set(float level, uint32_t fadeTime=0); // sets the PWM duty to level (0-100) within fadeTime in milliseconds + void set(float level); // sets the PWM duty to level (0-100) + void fade(float level, uint32_t fadeTime); // sets the PWM duty to level (0-100) within fadeTime in milliseconds static void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ); // converts Hue/Saturation/Brightness to R/G/B }; diff --git a/src/src/extras/extras.ino b/src/src/extras/extras.ino index 43b2fbd..a982c54 100644 --- a/src/src/extras/extras.ino +++ b/src/src/extras/extras.ino @@ -38,13 +38,9 @@ void setup() { Serial.println("\n\nHomeSpan LED Fade Test\n"); - LedPin led(26); - for(int i=10;i<=100;i+=10){ - led.set(i); - delay(1000); - } + LedPin led(26,50); Serial.printf("Start\n"); - led.set(0,5000); + led.fade(0,10000); Serial.printf("End\n"); while(1);