Starting work on ServoPin update

This commit is contained in:
HomeSpan 2021-09-12 12:31:08 -05:00
parent a5d31b1ea5
commit 0a0c536b6d
3 changed files with 72 additions and 55 deletions

View File

@ -3,7 +3,7 @@
/////////////////// ///////////////////
LedPin::LedPin(uint8_t pin, uint8_t level, uint16_t freq){ LedC::LedC(uint8_t pin, uint16_t freq){
if(freq==0) if(freq==0)
freq=DEFAULT_PWM_FREQ; freq=DEFAULT_PWM_FREQ;
@ -35,43 +35,49 @@ LedPin::LedPin(uint8_t pin, uint8_t level, uint16_t freq){
channelList[nChannel][nMode]->intr_type=LEDC_INTR_DISABLE; channelList[nChannel][nMode]->intr_type=LEDC_INTR_DISABLE;
channelList[nChannel][nMode]->hpoint=0; channelList[nChannel][nMode]->hpoint=0;
channelList[nChannel][nMode]->gpio_num=pin; channelList[nChannel][nMode]->gpio_num=pin;
ledTimer=timerList[nTimer][nMode]; timer=timerList[nTimer][nMode];
ledChannel=channelList[nChannel][nMode]; channel=channelList[nChannel][nMode];
set(level);
return; return;
} }
} }
} }
} }
} }
}
Serial.printf("\n*** ERROR: Can't create LedPin(%d) - no open PWM channels and/or Timers ***\n\n",pin); ///////////////////
LedPin::LedPin(uint8_t pin, uint8_t level, uint16_t freq) : LedC(pin, freq){
if(!channel)
Serial.printf("\n*** ERROR: Can't create LedPin(%d) - no open PWM channels and/or Timers ***\n\n",pin);
set(level);
} }
/////////////////// ///////////////////
void LedPin::set(uint8_t level){ void LedPin::set(uint8_t level){
if(!ledChannel) if(!channel)
return; return;
Serial.printf("pin=%d, ch=%d, mode=%d, timer=%d, freq=%d, res=%d\n", Serial.printf("pin=%d, ch=%d, mode=%d, timer=%d, freq=%d, res=%d\n",
ledChannel->gpio_num, channel->gpio_num,
ledChannel->channel, channel->channel,
ledChannel->speed_mode, channel->speed_mode,
ledChannel->timer_sel, channel->timer_sel,
ledTimer->freq_hz, timer->freq_hz,
ledTimer->duty_resolution timer->duty_resolution
); );
if(level>100) if(level>100)
level=100; level=100;
ledChannel->duty=level*(pow(2,ledTimer->duty_resolution)-1); channel->duty=level*(pow(2,timer->duty_resolution)-1);
ledChannel->duty/=100; channel->duty/=100;
ledc_channel_config(ledChannel); ledc_channel_config(channel);
} }
@ -134,37 +140,42 @@ void LedPin::HSVtoRGB(float h, float s, float v, float *r, float *g, float *b ){
//////////////////////////// ////////////////////////////
ServoPin::ServoPin(uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees){ ServoPin::ServoPin(uint8_t pin, double initDegrees, uint16_t minMicros, uint16_t maxMicros, double minDegrees, double maxDegrees) : LedC(pin, 50){
if(numChannels>7 || numChannels>(15-LedPin::numChannels)){ // if(numChannels>7 || numChannels>(15-numChannels)){
Serial.printf("\n*** ERROR: Can't create ServoPin(%d) - no open PWM channels ***\n\n",pin); // Serial.printf("\n*** ERROR: Can't create ServoPin(%d) - no open PWM channels ***\n\n",pin);
servoChannel.gpio_num=0; // servoChannel.gpio_num=0;
return; // return;
} // }
//
enabled=true; // enabled=true;
if(!channel)
Serial.printf("\n*** ERROR: Can't create LedPin(%d) - no open PWM channels and/or Timers ***\n\n",pin);
set(initDegrees);
this->minMicros=minMicros; this->minMicros=minMicros;
this->maxMicros=maxMicros; this->maxMicros=maxMicros;
this->minDegrees=minDegrees; this->minDegrees=minDegrees;
microsPerDegree=(double)(maxMicros-minMicros)/(maxDegrees-minDegrees); microsPerDegree=(double)(maxMicros-minMicros)/(maxDegrees-minDegrees);
if(numChannels==0){ // first instantiation of a ServoPin // if(numChannels==0){ // first instantiation of a ServoPin
ledc_timer_config_t ledTimer; // ledc_timer_config_t ledTimer;
ledTimer.timer_num=LEDC_TIMER_1; // ledTimer.timer_num=LEDC_TIMER_1;
ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE; // ledTimer.speed_mode=LEDC_HIGH_SPEED_MODE;
ledTimer.duty_resolution=LEDC_TIMER_16_BIT; // ledTimer.duty_resolution=LEDC_TIMER_16_BIT;
ledTimer.freq_hz=50; // ledTimer.freq_hz=50;
ledc_timer_config(&ledTimer); // ledc_timer_config(&ledTimer);
} // }
//
servoChannel.gpio_num=pin; // servoChannel.gpio_num=pin;
servoChannel.speed_mode=LEDC_HIGH_SPEED_MODE; // servoChannel.speed_mode=LEDC_HIGH_SPEED_MODE;
servoChannel.channel=(ledc_channel_t)numChannels++; // servoChannel.channel=(ledc_channel_t)numChannels++;
servoChannel.intr_type=LEDC_INTR_DISABLE; // servoChannel.intr_type=LEDC_INTR_DISABLE;
servoChannel.timer_sel=LEDC_TIMER_1; // servoChannel.timer_sel=LEDC_TIMER_1;
servoChannel.hpoint=0; // servoChannel.hpoint=0;
servoChannel.duty*=micros2duty; // servoChannel.duty*=micros2duty;
set(initDegrees); // set(initDegrees);
} }
/////////////////// ///////////////////
@ -187,10 +198,8 @@ void ServoPin::set(double degrees){
//////////////////////////// ////////////////////////////
const double ServoPin::micros2duty=65535.0/20000.0; const double ServoPin::micros2duty=65535.0/20000.0;
uint8_t LedPin::numChannels=0; //uint8_t LedPin::numChannels=0;
uint8_t ServoPin::numChannels=0; uint8_t ServoPin::numChannels=0;
uint8_t LedC::nChannels=0;
vector<ledc_timer_config_t> LedC::timers;
ledc_channel_config_t *LedC::channelList[LEDC_CHANNEL_MAX][LEDC_SPEED_MODE_MAX]={}; ledc_channel_config_t *LedC::channelList[LEDC_CHANNEL_MAX][LEDC_SPEED_MODE_MAX]={};
ledc_timer_config_t *LedC::timerList[LEDC_TIMER_MAX][LEDC_SPEED_MODE_MAX]={}; ledc_timer_config_t *LedC::timerList[LEDC_TIMER_MAX][LEDC_SPEED_MODE_MAX]={};

View File

@ -33,30 +33,33 @@ using std::vector;
class LedC { class LedC {
protected: protected:
static uint8_t nChannels;
static vector<ledc_timer_config_t> timers;
static ledc_channel_config_t *channelList[LEDC_CHANNEL_MAX][LEDC_SPEED_MODE_MAX]; static ledc_channel_config_t *channelList[LEDC_CHANNEL_MAX][LEDC_SPEED_MODE_MAX];
static ledc_timer_config_t *timerList[LEDC_TIMER_MAX][LEDC_SPEED_MODE_MAX]; static ledc_timer_config_t *timerList[LEDC_TIMER_MAX][LEDC_SPEED_MODE_MAX];
ledc_channel_config_t *channel=NULL;
ledc_timer_config_t *timer;
LedC(uint8_t pin, uint16_t freq);
public:
int getPin(){return(channel?channel->gpio_num:-1);} // returns the pin number
}; };
///////////////////////////////////// /////////////////////////////////////
class LedPin : LedC { class LedPin : public LedC {
ledc_channel_config_t *ledChannel=NULL;
ledc_timer_config_t *ledTimer;
public: public:
LedPin(uint8_t pin, uint8_t level=0, uint16_t freq=DEFAULT_PWM_FREQ); // assigns pin to be output of one of 16 PWM channels initial level and frequency LedPin(uint8_t pin, uint8_t level=0, uint16_t freq=DEFAULT_PWM_FREQ); // assigns pin to be output of one of 16 PWM channels initial level and frequency
void set(uint8_t level); // sets the PWM duty to level (0-100) void set(uint8_t level); // sets the PWM duty to level (0-100)
int getPin(){return(ledChannel?ledChannel->gpio_num:-1);} // returns the pin number
static uint8_t numChannels;
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
}; };
///////////////////////////////////// /////////////////////////////////////
class ServoPin { class ServoPin : public LedC {
boolean enabled=false; boolean enabled=false;
uint16_t minMicros; uint16_t minMicros;
uint16_t maxMicros; uint16_t maxMicros;

View File

@ -36,6 +36,11 @@ void setup(){
led0.set(5); led0.set(5);
led2.set(100); led2.set(100);
Serial.println(led0.getPin());
Serial.println(led14.getPin());
Serial.println(led15.getPin());
Serial.println(led16.getPin());
uint32_t v=REG_READ(LEDC_HSTIMER0_CONF_REG); uint32_t v=REG_READ(LEDC_HSTIMER0_CONF_REG);
Serial.printf("HS %d %d %d %d\n",(v>>25)&1,v&0x1f,(v>>13)&0x3FF,(v>>5)&0xFF); Serial.printf("HS %d %d %d %d\n",(v>>25)&1,v&0x1f,(v>>13)&0x3FF,(v>>5)&0xFF);