Skip to content

Changed type of LEDC frequency from double to uint32_t #6570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cores/esp32/esp32-hal-ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

uint8_t channels_resolution[LEDC_CHANNELS] = {0};

double ledcSetup(uint8_t chan, double freq, uint8_t bit_num)
uint32_t ledcSetup(uint8_t chan, uint32_t freq, uint8_t bit_num)
{
if(chan >= LEDC_CHANNELS || bit_num > LEDC_MAX_BIT_WIDTH){
log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH);
Expand Down Expand Up @@ -106,7 +106,7 @@ uint32_t ledcRead(uint8_t chan)
return ledc_get_duty(group,channel);
}

double ledcReadFreq(uint8_t chan)
uint32_t ledcReadFreq(uint8_t chan)
{
if(!ledcRead(chan)){
return 0;
Expand All @@ -115,7 +115,7 @@ double ledcReadFreq(uint8_t chan)
return ledc_get_freq(group,timer);
}

double ledcWriteTone(uint8_t chan, double freq)
uint32_t ledcWriteTone(uint8_t chan, uint32_t freq)
{
if(chan >= LEDC_CHANNELS){
return 0;
Expand All @@ -142,12 +142,12 @@ double ledcWriteTone(uint8_t chan, double freq)
}
channels_resolution[chan] = 10;

double res_freq = ledc_get_freq(group,timer);
uint32_t res_freq = ledc_get_freq(group,timer);
ledcWrite(chan, 0x1FF);
return res_freq;
}

double ledcWriteNote(uint8_t chan, note_t note, uint8_t octave){
uint32_t ledcWriteNote(uint8_t chan, note_t note, uint8_t octave){
const uint16_t noteFrequencyBase[12] = {
// C C# D Eb E F F# G G# A Bb B
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
Expand All @@ -156,7 +156,7 @@ double ledcWriteNote(uint8_t chan, note_t note, uint8_t octave){
if(octave > 8 || note >= NOTE_MAX){
return 0;
}
double noteFreq = (double)noteFrequencyBase[note] / (double)(1 << (8-octave));
uint32_t noteFreq = (uint32_t)noteFrequencyBase[note] / (uint32_t)(1 << (8-octave));
return ledcWriteTone(chan, noteFreq);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ void ledcDetachPin(uint8_t pin)
pinMatrixOutDetach(pin, false, false);
}

double ledcChangeFrequency(uint8_t chan, double freq, uint8_t bit_num)
uint32_t ledcChangeFrequency(uint8_t chan, uint32_t freq, uint8_t bit_num)
{
if(chan >= LEDC_CHANNELS || bit_num > LEDC_MAX_BIT_WIDTH){
log_e("LEDC channel not available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH);
Expand Down
10 changes: 5 additions & 5 deletions cores/esp32/esp32-hal-ledc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ typedef enum {
} note_t;

//channel 0-15 resolution 1-16bits freq limits depend on resolution
double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);
uint32_t ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
void ledcWrite(uint8_t channel, uint32_t duty);
double ledcWriteTone(uint8_t channel, double freq);
double ledcWriteNote(uint8_t channel, note_t note, uint8_t octave);
uint32_t ledcWriteTone(uint8_t channel, uint32_t freq);
uint32_t ledcWriteNote(uint8_t channel, note_t note, uint8_t octave);
uint32_t ledcRead(uint8_t channel);
double ledcReadFreq(uint8_t channel);
uint32_t ledcReadFreq(uint8_t channel);
void ledcAttachPin(uint8_t pin, uint8_t channel);
void ledcDetachPin(uint8_t pin);
double ledcChangeFrequency(uint8_t channel, double freq, uint8_t resolution_bits);
uint32_t ledcChangeFrequency(uint8_t channel, uint32_t freq, uint8_t resolution_bits);


#ifdef __cplusplus
Expand Down