Skip to content

Commit

Permalink
IDF 5 uses pin only, no pwmChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-neal authored Jul 1, 2024
1 parent dbff2f0 commit 78ed66a
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/melody_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ void MelodyPlayer::play() {
+ " duration:" + computedNote.duration);
if (melodyState->isSilence()) {
#ifdef ESP32
ledcWriteTone(pwmChannel, 0);
#if ESP_IDF_VERSION_MAJOR > 4
ledcWriteTone(pin, 0);
#else
ledcWriteTone(pwmChannel, 0);
#endif
#else
noTone(pin);
#endif
delay(0.3f * computedNote.duration);
} else {
#ifdef ESP32
#if ESP_IDF_VERSION_MAJOR > 4
ledcWriteTone(pin, computedNote.frequency);
#else
ledcWriteTone(pwmChannel, computedNote.frequency);
#else
#endif
#else
tone(pin, computedNote.frequency);
#endif
delay(computedNote.duration);
Expand Down Expand Up @@ -89,7 +97,11 @@ void changeTone(MelodyPlayer* player) {
if(!player->muted)
{
#ifdef ESP32
ledcWriteTone(player->pwmChannel, 0);
#if ESP_IDF_VERSION_MAJOR > 4
ledcWriteTone(player->pin, computedNote.frequency);
#else
ledcWriteTone(player->pwmChannel, computedNote.frequency);
#endif
#else
tone(player->pin, 0);
#endif
Expand All @@ -104,8 +116,14 @@ void changeTone(MelodyPlayer* player) {
if(!player->muted)
{
#ifdef ESP32
ledcWriteTone(player->pwmChannel, computedNote.frequency);
ledcWrite(player->pwmChannel,player->volume);
#if ESP_IDF_VERSION_MAJOR > 4
ledcWriteTone(player->pin, computedNote.frequency);
ledcWrite(player->pin,player->volume);
#else
ledcWriteTone(player->pwmChannel, computedNote.frequency);
ledcWrite(player->pwmChannel,player->volume);
#endif

#else
tone(player->pin, computedNote.frequency);
#endif
Expand Down Expand Up @@ -229,12 +247,14 @@ void MelodyPlayer::turnOn() {
// 2000 is a frequency, it will be changed at the first play
#if ESP_IDF_VERSION_MAJOR > 4
ledcAttach(pin, 2000, resolution);
ledcWrite(pin, volume);
#else
ledcSetup(pwmChannel, 2000, resolution);
ledcAttachPin(pin, pwmChannel);
ledcWrite(pwmChannel, volume);
#endif

ledcWrite(pwmChannel, volume);

#endif
}

Expand All @@ -243,17 +263,23 @@ void MelodyPlayer::setVolume(byte newVolume) {
#ifdef ESP32
if(state == State::PLAY)
{
ledcWrite(pwmChannel, volume);
#if ESP_IDF_VERSION_MAJOR > 4
ledcWrite(pin, volume);
#else
ledcWrite(pwmChannel, volume);
#endif
}
#endif
}

void MelodyPlayer::turnOff() {
#ifdef ESP32
ledcWrite(pwmChannel, 0);

#if ESP_IDF_VERSION_MAJOR > 4
ledcWrite(pin, 0);
ledcDetach(pin);
#else
ledcWrite(pwmChannel, 0);
ledcDetachPin(pin);
#endif

Expand Down

0 comments on commit 78ed66a

Please sign in to comment.