diff --git a/src/extras/PwmPin.cpp b/src/extras/PwmPin.cpp index ecd7967..1ad9556 100644 --- a/src/extras/PwmPin.cpp +++ b/src/extras/PwmPin.cpp @@ -1,6 +1,5 @@ #include "PwmPin.h" -#include PwmPin::PwmPin(uint8_t channel, uint8_t pin){ this->channel=channel & 0x0F; @@ -89,6 +88,59 @@ void PwmPin::HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ){ } } +/////////////////// + +LedPin::LedPin(uint8_t pin, uint8_t level){ + if(numChannels>15){ + Serial.printf("\n*** ERROR: Can't create LedPin(%d) - no open PWM channels ***\n\n",pin); + return; + } + + enabled=true; + this->pin=pin; + + if(numChannels==0){ // first instantiation of an LedPin + ledc_timer_config_t ledTimer; + ledTimer.timer_num=LEDC_TIMER_0; + ledTimer.duty_resolution=LEDC_TIMER_10_BIT; + ledTimer.freq_hz=5000; + + ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE; // configure both the HIGH-Speed Timer 0 and Low-Speed Timer 0 + ledc_timer_config(&ledTimer); + + ledTimer.speed_mode=LEDC_LOW_SPEED_MODE; + ledc_timer_config(&ledTimer); + } + + ledChannel.gpio_num=pin; + if(numChannels<8){ + ledChannel.speed_mode=LEDC_LOW_SPEED_MODE; + ledChannel.channel=(ledc_channel_t)(7-numChannels); + } else { + ledChannel.speed_mode=LEDC_HIGH_SPEED_MODE; + ledChannel.channel=(ledc_channel_t)(15-numChannels); + } + + numChannels++; + + ledChannel.intr_type=LEDC_INTR_DISABLE; + ledChannel.timer_sel=LEDC_TIMER_0; + ledChannel.hpoint=0; + ledc_channel_config(&ledChannel); + set(level); +} + +void LedPin::set(uint8_t level){ + if(!enabled) + return; + + ledChannel.duty=level*1023; + ledChannel.duty/=100; + ledChannel.duty&=0x03FF; + ledc_channel_config(&ledChannel); + +} + //////////////////////////// ServoPin::ServoPin(uint8_t channel, uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees){ @@ -129,3 +181,4 @@ void ServoPin::set(double degrees){ } const double ServoPin::micros2duty=65535.0/20000.0; +uint8_t LedPin::numChannels=0; diff --git a/src/extras/PwmPin.h b/src/extras/PwmPin.h index 34723ae..4aadbd9 100644 --- a/src/extras/PwmPin.h +++ b/src/extras/PwmPin.h @@ -8,6 +8,7 @@ // is hardcoded to 5000 Hz and either High-Speed Timer-0 (for channels 0-7) or Low-Speed Timer-0 // for channels (8-15) is configured and selected automatically. +#include #include class PwmPin { @@ -25,6 +26,25 @@ class PwmPin { }; +///////////////////////////////////// + +class LedPin { + uint8_t pin; + boolean enabled=false; + ledc_channel_config_t ledChannel; + static uint8_t numChannels; + + public: + LedPin(uint8_t pin, uint8_t level=0); // assigns pin to be output of one of 16 PWM channels within initial level + void set(uint8_t level); // sets the PWM duty to level (0-100) + int getPin(){return pin;} // returns the pin number + + static void HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ); // converts Hue/Saturation/Brightness to R/G/B + +}; + +///////////////////////////////////// + class ServoPin { uint8_t channel; // channel must be in range [0,7] (only HighSpeed Channels will be used) uint8_t pin; diff --git a/src/extras/extras.ino b/src/extras/extras.ino index e8d338a..e69cea3 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -4,9 +4,6 @@ #include "PwmPin.h" -PwmPin yellow(0,16); -PwmPin red(0,17); - //ServoPin servo(3,18,-90); ServoPin servo(3,18,0,500,2200,-90,90); @@ -19,33 +16,55 @@ void setup(){ Serial.println("Starting..."); + LedPin yellow(16); + LedPin d1(19); + LedPin d2(19); + LedPin d3(19); + LedPin d4(19); + LedPin d5(19); + LedPin d6(19); + LedPin d7(19); + LedPin d8(19); + LedPin d9(19); + LedPin d10(19); + LedPin d11(19); + LedPin d12(19); + LedPin d13(19); + LedPin d14(19); + LedPin d15(19); + LedPin d16(19); + LedPin red(17); + + while(1){ + for(int i=0;i<100;i++){ + yellow.set(i); + delay(10); + } + + for(int i=100;i>=0;i--){ + red.set(i); + delay(10); + } + } + } void loop(){ - double STEP=1; - - for(int i=-100*STEP;i<=100*STEP;i++){ - servo.set((double)i/STEP); - delay(10); - } - - for(int i=100*STEP;i>=-100*STEP;i--){ - servo.set((double)i/STEP); - delay(10); - } +// double STEP=1; +// +// for(int i=-100*STEP;i<=100*STEP;i++){ +// servo.set((double)i/STEP); +// delay(10); +// } +// +// for(int i=100*STEP;i>=-100*STEP;i--){ +// servo.set((double)i/STEP); +// delay(10); +// } - return; +// return; - for(int i=0;i<100;i++){ - red.set(22,i); - delay(10); - } - - for(int i=100;i>=0;i--){ - yellow.set(i); - delay(10); - } }