ESP32Servo
Public Member Functions | Static Public Member Functions | Data Fields | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes
ESP32PWM Class Reference

#include <ESP32PWM.h>

Collaboration diagram for ESP32PWM:
Collaboration graph
[legend]

Public Member Functions

 ESP32PWM ()
 
virtual ~ESP32PWM ()
 
void detachPin (int pin)
 
void attachPin (uint8_t pin, double freq, uint8_t resolution_bits=10)
 
bool attached ()
 
void write (uint32_t duty)
 
void writeScaled (float duty)
 
double writeTone (double freq)
 
double writeNote (note_t note, uint8_t octave)
 
void adjustFrequency (double freq, float dutyScaled=-1)
 
uint32_t read ()
 
double readFreq ()
 
float getDutyScaled ()
 
int getTimer ()
 
int getChannel ()
 
int getPin ()
 

Static Public Member Functions

static int timerAndIndexToChannel (int timer, int index)
 
static void allocateTimer (int timerNumber)
 
static bool hasPwm (int pin)
 
static int channelsRemaining ()
 

Data Fields

int timerNum = -1
 
uint32_t myDuty = 0
 

Static Public Attributes

static bool explicateAllocationMode =false
 
static int PWMCount = -1
 
static int timerCount [4] = { 0, 0, 0, 0 }
 
static ESP32PWMChannelUsed [NUM_PWM]
 
static long timerFreqSet [4] = { -1, -1, -1, -1 }
 

Private Member Functions

void attach (int pin)
 
int allocatenext (double freq)
 
bool checkFrequencyForSideEffects (double freq)
 
void adjustFrequencyLocal (double freq, float dutyScaled)
 
double setup (double freq, uint8_t resolution_bits=10)
 
void attachPin (uint8_t pin)
 
void deallocate ()
 

Static Private Member Functions

static double _ledcSetupTimerFreq (uint8_t chan, double freq, uint8_t bit_num)
 
static float mapf (float x, float in_min, float in_max, float out_min, float out_max)
 

Private Attributes

int pwmChannel = 0
 
bool attachedState = false
 
int pin
 
uint8_t resolutionBits
 
double myFreq
 

Detailed Description

Definition at line 17 of file ESP32PWM.h.

Constructor & Destructor Documentation

◆ ESP32PWM()

ESP32PWM::ESP32PWM ( )

Definition at line 36 of file ESP32PWM.cpp.

References ChannelUsed, myFreq, NUM_PWM, pin, PWM_BASE_INDEX, pwmChannel, PWMCount, and resolutionBits.

Referenced by mapf().

◆ ~ESP32PWM()

ESP32PWM::~ESP32PWM ( )
virtual

Definition at line 48 of file ESP32PWM.cpp.

References attached(), deallocate(), and pin.

Referenced by mapf().

Here is the call graph for this function:

Member Function Documentation

◆ _ledcSetupTimerFreq()

double ESP32PWM::_ledcSetupTimerFreq ( uint8_t  chan,
double  freq,
uint8_t  bit_num 
)
staticprivate

Definition at line 55 of file ESP32PWM.cpp.

Referenced by adjustFrequencyLocal().

◆ adjustFrequency()

void ESP32PWM::adjustFrequency ( double  freq,
float  dutyScaled = -1 
)

Definition at line 180 of file ESP32PWM.cpp.

References adjustFrequencyLocal(), ChannelUsed, getDutyScaled(), getTimer(), myFreq, pwm, timerAndIndexToChannel(), timerCount, and writeScaled().

Referenced by attached(), and loop().

Here is the call graph for this function:

◆ adjustFrequencyLocal()

void ESP32PWM::adjustFrequencyLocal ( double  freq,
float  dutyScaled 
)
private

Definition at line 166 of file ESP32PWM.cpp.

References _ledcSetupTimerFreq(), attached(), freq, getChannel(), getTimer(), myFreq, pin, resolutionBits, timerFreqSet, and writeScaled().

Referenced by adjustFrequency(), and writeTone().

Here is the call graph for this function:

◆ allocatenext()

int ESP32PWM::allocatenext ( double  freq)
private

Definition at line 73 of file ESP32PWM.cpp.

References ChannelUsed, freq, myFreq, pwmChannel, PWMCount, timerAndIndexToChannel(), timerCount, timerFreqSet, and timerNum.

Referenced by checkFrequencyForSideEffects().

Here is the call graph for this function:

◆ allocateTimer()

void ESP32PWM::allocateTimer ( int  timerNumber)
static

allocateTimer

Parameters
atimer number 0-3 indicating which timer to allocate in this library Switch to explicate allocation mode

Definition at line 25 of file ESP32PWM.cpp.

References explicateAllocationMode, and timerCount.

Referenced by attached(), and setup().

◆ attach()

void ESP32PWM::attach ( int  pin)
private

Definition at line 227 of file ESP32PWM.cpp.

References attachedState, and pin.

Referenced by attachPin().

◆ attached()

bool ESP32PWM::attached ( )
inline

◆ attachPin() [1/2]

void ESP32PWM::attachPin ( uint8_t  pin)
private

Definition at line 231 of file ESP32PWM.cpp.

References attach(), getChannel(), and hasPwm().

Referenced by analogWrite(), Servo::attach(), attachPin(), loop(), mapf(), Servo::setTimerWidth(), setup(), setup(), and tone().

Here is the call graph for this function:

◆ attachPin() [2/2]

void ESP32PWM::attachPin ( uint8_t  pin,
double  freq,
uint8_t  resolution_bits = 10 
)

Definition at line 244 of file ESP32PWM.cpp.

References attachPin(), hasPwm(), and setup().

Here is the call graph for this function:

◆ channelsRemaining()

static int ESP32PWM::channelsRemaining ( )
inlinestatic

Definition at line 110 of file ESP32PWM.h.

References NUM_PWM, pin, PWMCount, and pwmFactory().

Referenced by loop().

Here is the call graph for this function:

◆ checkFrequencyForSideEffects()

bool ESP32PWM::checkFrequencyForSideEffects ( double  freq)
private

Definition at line 275 of file ESP32PWM.cpp.

References allocatenext(), ChannelUsed, freq, getTimer(), myFreq, pwm, pwmChannel, timerAndIndexToChannel(), and timerCount.

Referenced by setup().

Here is the call graph for this function:

◆ deallocate()

void ESP32PWM::deallocate ( )
private

Definition at line 120 of file ESP32PWM.cpp.

References attachedState, ChannelUsed, getTimer(), pwmChannel, PWMCount, timerCount, timerFreqSet, and timerNum.

Referenced by detachPin(), mapf(), and ~ESP32PWM().

Here is the call graph for this function:

◆ detachPin()

void ESP32PWM::detachPin ( int  pin)

Definition at line 250 of file ESP32PWM.cpp.

References deallocate().

Referenced by analogWrite(), Servo::detach(), loop(), mapf(), noTone(), and Servo::setTimerWidth().

Here is the call graph for this function:

◆ getChannel()

int ESP32PWM::getChannel ( )

Definition at line 136 of file ESP32PWM.cpp.

References pwmChannel.

Referenced by adjustFrequencyLocal(), attachPin(), read(), setup(), and write().

◆ getDutyScaled()

float ESP32PWM::getDutyScaled ( )

Definition at line 155 of file ESP32PWM.cpp.

References mapf(), myDuty, and resolutionBits.

Referenced by adjustFrequency(), attached(), and writeTone().

Here is the call graph for this function:

◆ getPin()

int ESP32PWM::getPin ( )
inline

Definition at line 96 of file ESP32PWM.h.

References pin.

Referenced by pwmFactory().

◆ getTimer()

int ESP32PWM::getTimer ( )
inline

◆ hasPwm()

static bool ESP32PWM::hasPwm ( int  pin)
inlinestatic

Definition at line 99 of file ESP32PWM.h.

Referenced by Servo::attach(), attachPin(), and loop().

◆ mapf()

static float ESP32PWM::mapf ( float  x,
float  in_min,
float  in_max,
float  out_min,
float  out_max 
)
inlinestaticprivate

Definition at line 34 of file ESP32PWM.h.

References attachPin(), deallocate(), detachPin(), ESP32PWM(), freq, setup(), and ~ESP32PWM().

Referenced by getDutyScaled(), and writeScaled().

Here is the call graph for this function:

◆ read()

uint32_t ESP32PWM::read ( )

Definition at line 221 of file ESP32PWM.cpp.

References getChannel().

Referenced by attached().

Here is the call graph for this function:

◆ readFreq()

double ESP32PWM::readFreq ( )

Definition at line 224 of file ESP32PWM.cpp.

References myFreq.

Referenced by attached().

◆ setup()

double ESP32PWM::setup ( double  freq,
uint8_t  resolution_bits = 10 
)
private

Definition at line 143 of file ESP32PWM.cpp.

References attached(), attachPin(), checkFrequencyForSideEffects(), getChannel(), pin, resolutionBits, and val.

Referenced by attachPin(), and mapf().

Here is the call graph for this function:

◆ timerAndIndexToChannel()

int ESP32PWM::timerAndIndexToChannel ( int  timer,
int  index 
)
static

Definition at line 61 of file ESP32PWM.cpp.

References NUM_PWM.

Referenced by adjustFrequency(), allocatenext(), attached(), checkFrequencyForSideEffects(), and writeTone().

◆ write()

void ESP32PWM::write ( uint32_t  duty)

Definition at line 162 of file ESP32PWM.cpp.

References getChannel(), and myDuty.

Referenced by analogWrite(), attached(), Servo::writeMicroseconds(), writeScaled(), and writeTone().

Here is the call graph for this function:

◆ writeNote()

double ESP32PWM::writeNote ( note_t  note,
uint8_t  octave 
)

Definition at line 208 of file ESP32PWM.cpp.

References writeTone().

Referenced by attached().

Here is the call graph for this function:

◆ writeScaled()

void ESP32PWM::writeScaled ( float  duty)

Definition at line 159 of file ESP32PWM.cpp.

References mapf(), resolutionBits, and write().

Referenced by adjustFrequency(), adjustFrequencyLocal(), attached(), and loop().

Here is the call graph for this function:

◆ writeTone()

double ESP32PWM::writeTone ( double  freq)

Definition at line 194 of file ESP32PWM.cpp.

References adjustFrequencyLocal(), ChannelUsed, getDutyScaled(), getTimer(), myFreq, pwm, resolutionBits, timerAndIndexToChannel(), timerCount, and write().

Referenced by attached(), tone(), and writeNote().

Here is the call graph for this function:

Field Documentation

◆ attachedState

bool ESP32PWM::attachedState = false
private

Definition at line 22 of file ESP32PWM.h.

Referenced by attach(), attached(), and deallocate().

◆ ChannelUsed

ESP32PWM * ESP32PWM::ChannelUsed
static

◆ explicateAllocationMode

bool ESP32PWM::explicateAllocationMode =false
static

Definition at line 83 of file ESP32PWM.h.

Referenced by allocateTimer().

◆ myDuty

uint32_t ESP32PWM::myDuty = 0

Definition at line 88 of file ESP32PWM.h.

Referenced by getDutyScaled(), and write().

◆ myFreq

double ESP32PWM::myFreq
private

◆ pin

int ESP32PWM::pin
private

◆ pwmChannel

int ESP32PWM::pwmChannel = 0
private

◆ PWMCount

int ESP32PWM::PWMCount = -1
static

Definition at line 90 of file ESP32PWM.h.

Referenced by allocatenext(), channelsRemaining(), deallocate(), and ESP32PWM().

◆ resolutionBits

uint8_t ESP32PWM::resolutionBits
private

Definition at line 24 of file ESP32PWM.h.

Referenced by adjustFrequencyLocal(), ESP32PWM(), getDutyScaled(), setup(), writeScaled(), and writeTone().

◆ timerCount

int ESP32PWM::timerCount = { 0, 0, 0, 0 }
static

◆ timerFreqSet

long ESP32PWM::timerFreqSet = { -1, -1, -1, -1 }
static

Definition at line 93 of file ESP32PWM.h.

Referenced by adjustFrequencyLocal(), allocatenext(), and deallocate().

◆ timerNum

int ESP32PWM::timerNum = -1

Definition at line 87 of file ESP32PWM.h.

Referenced by allocatenext(), deallocate(), and getTimer().


The documentation for this class was generated from the following files: