Added fadeTime parameter to LedPin::set()

This commit is contained in:
Gregg 2023-03-26 07:39:06 -05:00
parent a79b7569d1
commit c2df57815c
3 changed files with 15 additions and 23 deletions

View File

@ -102,7 +102,7 @@ LedPin::LedPin(uint8_t pin, float level, uint16_t freq, boolean invert) : LedC(p
/////////////////// ///////////////////
void LedPin::set(float level){ void LedPin::set(float level, uint32_t fadeTime){
if(!channel) if(!channel)
return; return;
@ -110,25 +110,14 @@ void LedPin::set(float level){
if(level>100) if(level>100)
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;
void LedPin::fade(float level, uint32_t fadeTime){ ledc_channel_config(channel);
} else {
if(!channel) ledc_set_fade_time_and_start(channel->speed_mode,channel->channel,(uint32_t)d,fadeTime,LEDC_FADE_NO_WAIT);
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);
} }
/////////////////// ///////////////////

View File

@ -76,8 +76,7 @@ class LedPin : public LedC {
public: 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 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); // sets the PWM duty to level (0-100) void set(float level, uint32_t fadeTime=0); // sets the PWM duty to level (0-100) within fadeTime in milliseconds
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 static void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ); // converts Hue/Saturation/Brightness to R/G/B
}; };

View File

@ -38,9 +38,13 @@ void setup() {
Serial.println("\n\nHomeSpan LED Fade Test\n"); Serial.println("\n\nHomeSpan LED Fade Test\n");
LedPin led(26,50); LedPin led(26);
for(int i=10;i<=100;i+=10){
led.set(i);
delay(1000);
}
Serial.printf("Start\n"); Serial.printf("Start\n");
led.fade(0,10000); led.set(0,5000);
Serial.printf("End\n"); Serial.printf("End\n");
while(1); while(1);