diff --git a/src/extras/PwmPin.cpp b/src/extras/PwmPin.cpp index 58c7fa6..ecd7967 100644 --- a/src/extras/PwmPin.cpp +++ b/src/extras/PwmPin.cpp @@ -88,3 +88,44 @@ void PwmPin::HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ){ break; } } + +//////////////////////////// + +ServoPin::ServoPin(uint8_t channel, uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees){ + this->channel=channel & 0x07; + this->pin=pin; + this->minMicros=minMicros; + this->maxMicros=maxMicros; + this->minDegrees=minDegrees; + microsPerDegree=(double)(maxMicros-minMicros)/(maxDegrees-minDegrees); + + ledc_timer_config_t ledTimer; + ledTimer.timer_num=LEDC_TIMER_1; + ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE; + ledTimer.duty_resolution=LEDC_TIMER_16_BIT; + ledTimer.freq_hz=50; + ledc_timer_config(&ledTimer); + + ledChannel.gpio_num=pin; + ledChannel.speed_mode=LEDC_HIGH_SPEED_MODE; + ledChannel.channel=(ledc_channel_t)(this->channel); + ledChannel.intr_type=LEDC_INTR_DISABLE; + ledChannel.timer_sel=LEDC_TIMER_1; + ledChannel.hpoint=0; + ledChannel.duty*=micros2duty; + set(initDegrees); +} + +void ServoPin::set(double degrees){ + ledChannel.duty=(degrees-minDegrees)*microsPerDegree+minMicros; + + if(ledChannel.dutymaxMicros) + ledChannel.duty=maxMicros; + + ledChannel.duty*=micros2duty; + ledc_channel_config(&ledChannel); +} + +const double ServoPin::micros2duty=65535.0/20000.0; diff --git a/src/extras/PwmPin.h b/src/extras/PwmPin.h index bb30eb7..34723ae 100644 --- a/src/extras/PwmPin.h +++ b/src/extras/PwmPin.h @@ -24,3 +24,23 @@ class PwmPin { 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; + uint16_t minMicros; + uint16_t maxMicros; + double minDegrees; + double microsPerDegree; + ledc_channel_config_t ledChannel; + + static const double micros2duty; + + public: + ServoPin(uint8_t channel, uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees); + ServoPin(uint8_t channel, uint8_t pin, double initDegrees=0) : ServoPin(channel,pin,initDegrees,1000,2000,-90,90) {}; + + void set(double degrees); // sets the Servo to degrees, where degrees is bounded by [minDegrees,maxDegrees] + int getPin(){return pin;} // returns the pin number + +}; diff --git a/src/extras/extras.ino b/src/extras/extras.ino index 67c5120..e8d338a 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -5,21 +5,39 @@ #include "PwmPin.h" PwmPin yellow(0,16); -PwmPin red(1,17); +PwmPin red(0,17); + +//ServoPin servo(3,18,-90); +ServoPin servo(3,18,0,500,2200,-90,90); void setup(){ - + Serial.begin(115200); delay(1000); Serial.print("\n\nTest sketch for HomeSpan Extras Library\n\n"); Serial.println("Starting..."); - + } 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); + } + + return; + for(int i=0;i<100;i++){ red.set(22,i); delay(10);