Created LedPin()

This is a replacement for PwmPin().   It keeps track of channel numbers internally, which greatly simplifies the user interface.  Starts by using the 8 Low Speed Timer channels and then moves to the 8 High Speed Timer channels if more than 8 LedPins are instantiated.  Throws a non-fatal error if more than 16 LedPins are instantiated, and ignores any attempts to set the duty cycle of those channels.
This commit is contained in:
Gregg 2021-03-20 17:14:19 -05:00
parent 65df97c563
commit bc498c32e4
3 changed files with 117 additions and 25 deletions

View File

@ -1,6 +1,5 @@
#include "PwmPin.h" #include "PwmPin.h"
#include <Arduino.h>
PwmPin::PwmPin(uint8_t channel, uint8_t pin){ PwmPin::PwmPin(uint8_t channel, uint8_t pin){
this->channel=channel & 0x0F; 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){ 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; const double ServoPin::micros2duty=65535.0/20000.0;
uint8_t LedPin::numChannels=0;

View File

@ -8,6 +8,7 @@
// is hardcoded to 5000 Hz and either High-Speed Timer-0 (for channels 0-7) or Low-Speed Timer-0 // 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. // for channels (8-15) is configured and selected automatically.
#include <Arduino.h>
#include <driver/ledc.h> #include <driver/ledc.h>
class PwmPin { 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 { class ServoPin {
uint8_t channel; // channel must be in range [0,7] (only HighSpeed Channels will be used) uint8_t channel; // channel must be in range [0,7] (only HighSpeed Channels will be used)
uint8_t pin; uint8_t pin;

View File

@ -4,9 +4,6 @@
#include "PwmPin.h" #include "PwmPin.h"
PwmPin yellow(0,16);
PwmPin red(0,17);
//ServoPin servo(3,18,-90); //ServoPin servo(3,18,-90);
ServoPin servo(3,18,0,500,2200,-90,90); ServoPin servo(3,18,0,500,2200,-90,90);
@ -19,33 +16,55 @@ void setup(){
Serial.println("Starting..."); 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(){ void loop(){
double STEP=1; // double STEP=1;
//
for(int i=-100*STEP;i<=100*STEP;i++){ // for(int i=-100*STEP;i<=100*STEP;i++){
servo.set((double)i/STEP); // servo.set((double)i/STEP);
delay(10); // delay(10);
} // }
//
for(int i=100*STEP;i>=-100*STEP;i--){ // for(int i=100*STEP;i>=-100*STEP;i--){
servo.set((double)i/STEP); // servo.set((double)i/STEP);
delay(10); // 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);
}
} }