9 #include "esp32-hal-ledc.h" 26 if(timerNumber<0 || timerNumber>3)
42 for (
int i = 0; i <
NUM_PWM; i++)
57 return ledcSetup(chan, freq, bit_num);
63 for (
int j = 0; j <
NUM_PWM; j++) {
64 if (((j / 2) % 4) == timerNum) {
65 if (localIndex == index) {
74 long freqlocal = (long) freq;
76 for (
int i = 0; i < 4; i++) {
87 for (
int index=0; index<4; ++index)
90 if ((myTimerNumber >= 0) && (!
ChannelUsed[myTimerNumber]))
115 "ERROR All PWM timers allocated! Can't accomodate " + String(freq)
116 +
"Hz\r\nHalting...");
138 Serial.println(
"FAIL! must setup() before using get channel!");
153 return ledcSetup(
getChannel(), freq, resolution_bits);
209 const uint16_t noteFrequencyBase[12] = {
211 4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459,
214 if (octave > 8 || note >= NOTE_MAX) {
217 double noteFreq = (double) noteFrequencyBase[note]
218 / (
double) (1 << (8 - octave));
238 "ERROR PWM channel unavailible on pin requested! " + String(pin)
239 +
"\r\nPWM availible on: 2,4,5,12-19,21-23,25-27,32-33");
247 setup(freq, resolution_bits);
286 if (abs(diff) > 0.1) {
289 +
" shares a timer with channel " 291 "\tchanging the frequency to " 293 +
" Hz will ALSO change channel " 295 +
" \n\tfrom its previous frequency of " 306 for (
int i = 0; i <
NUM_PWM; i++)
307 if (ESP32PWM::ChannelUsed[i] != NULL) {
308 if (ESP32PWM::ChannelUsed[i]->
getPin() == pin)
309 return ESP32PWM::ChannelUsed[i];
void adjustFrequency(double freq, float dutyScaled=-1)
void adjustFrequencyLocal(double freq, float dutyScaled)
double setup(double freq, uint8_t resolution_bits=10)
static void allocateTimer(int timerNumber)
static int timerAndIndexToChannel(int timer, int index)
ESP32PWM * pwmFactory(int pin)
void attachPin(uint8_t pin)
static ESP32PWM * ChannelUsed[NUM_PWM]
bool checkFrequencyForSideEffects(double freq)
static float mapf(float x, float in_min, float in_max, float out_min, float out_max)
static bool explicateAllocationMode
void write(uint32_t duty)
int allocatenext(double freq)
static double _ledcSetupTimerFreq(uint8_t chan, double freq, uint8_t bit_num)
double writeTone(double freq)
double writeNote(note_t note, uint8_t octave)
static long timerFreqSet[4]
static bool hasPwm(int pin)
void writeScaled(float duty)