Skip to content

Commit

Permalink
[RTTTL] Fix argument index for melody + prevent crash when changing song
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Sep 28, 2023
1 parent ce68d2a commit 7852c42
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/source/Plugin/P000_Buzzer_RTTTL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ From ESP Easy v2.0.0 its possible to play melodies via `RTTTL <https://en.wikipe

.. code-block:: none
http://<ESP IP address>/control?cmd=rtttl,14:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
http://<ESP IP address>/control?cmd=rtttl,14,:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
This plays a melody on pin 14.

Expand All @@ -68,7 +68,7 @@ To make a boot-sound on startup, create a rule like this:
.. code-block:: none
On System#Boot do
rtttl,14:d=10,o=6,b=180,c,e,g
rtttl,14,:d=10,o=6,b=180,c,e,g
endon
`A place to find more tunes <http://www.picaxe.com/RTTTL-Ringtones-for-Tune-Command/>`_, all saved as txt files.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/Plugin/P000_Piezo_element_RTTTL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ From ESP Easy v2.0.0 its possible to play melodies via `RTTTL <https://en.wikipe

.. code-block:: none
http://<ESP IP address>/control?cmd=rtttl,14:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
http://<ESP IP address>/control?cmd=rtttl,14,:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
This plays a melody on pin 14.

Expand All @@ -65,7 +65,7 @@ To make a boot-sound on startup, create a rule like this:
.. code-block:: none
On System#Boot do
rtttl,14:d=10,o=6,b=180,c,e,g
rtttl,14,:d=10,o=6,b=180,c,e,g
endon
`A place to find more tunes <http://www.picaxe.com/RTTTL-Ringtones-for-Tune-Command/>`_, all saved as txt files.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/Plugin/P000_Speaker_RTTTL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ From ESP Easy v2.0.0 its possible to play melodies via `RTTTL <https://en.wikipe

.. code-block:: none
http://<ESP IP address>/control?cmd=rtttl,14:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
http://<ESP IP address>/control?cmd=rtttl,14,:d=4,o=5,b=112:8a,8a,a,8a,8a,a,8a,8c6,8f.,16g,2a,8a-,8a-,8a-.,16a-,8a-,8a,8a.,16a,8a,8g,8g,8a,g,c6
This plays a melody on pin 14.

Expand All @@ -65,7 +65,7 @@ To make a boot-sound on startup, create a rule like this:
.. code-block:: none
On System#Boot do
rtttl,14:d=10,o=6,b=180,c,e,g
rtttl,14,:d=10,o=6,b=180,c,e,g
endon
`A place to find more tunes <http://www.picaxe.com/RTTTL-Ringtones-for-Tune-Command/>`_, all saved as txt files.
Expand Down
8 changes: 8 additions & 0 deletions src/src/Commands/GPIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,16 @@ const __FlashStringHelper * Command_GPIO_RTTTL(struct EventStruct *event, const
// play a tune via a RTTTL string, look at https://www.letscontrolit.com/forum/viewtopic.php?f=4&t=343&hilit=speaker&start=10 for
// more info.

// First assume 'old' syntax: rtttl,<gpio><rtttl string>
// No comma between the GPIO argument and the melody
String melody = parseStringToEndKeepCase(Line, 2);
if (melody.indexOf(':') == -1) {
// Apparently this is now using the 'new' (correct) syntax:
// rtttl,<gpio>,<rtttl string>
melody = parseStringToEndKeepCase(Line, 3);
}
melody.replace('-', '#');
melody.replace('_', '#');

if (loglevelActiveFor(LOG_LEVEL_INFO)) {
addLog(LOG_LEVEL_INFO, strformat(F("RTTTL: pin: %d melody: %s"), event->Par1, melody.c_str()));
Expand Down
18 changes: 11 additions & 7 deletions src/src/Helpers/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ static bool rtttlPlaying = false;
# endif // if FEATURE_RTTTL_EVENTS
# if FEATURE_ANYRTTTL_ASYNC
static String rtttlMelody;

void clear_rtttl_melody() {
// The non-blocking play will read from a char pointer.
// So we must stop the playing before changing the string as it could otherwise lead to a crash.
anyrtttl::nonblocking::stop();
rtttlMelody = String();
}

void set_rtttl_melody(String& melody) {
if (melody.isEmpty()) {
rtttlMelody = String();
} else {
rtttlMelody = String(melody);
}
clear_rtttl_melody();
rtttlMelody = melody;
}

# endif // if FEATURE_ANYRTTTL_ASYNC
Expand Down Expand Up @@ -106,8 +111,7 @@ void update_rtttl() {
rtttlPlaying = false;
}
# endif // if FEATURE_RTTTL_EVENTS
String dummy;
set_rtttl_melody(dummy); // Release memory
clear_rtttl_melody(); // Release memory
}
}

Expand Down
1 change: 1 addition & 0 deletions src/src/Helpers/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bool play_rtttl(int8_t _pin,
const char *p);
# if FEATURE_ANYRTTTL_LIB && FEATURE_ANYRTTTL_ASYNC
void update_rtttl();
void clear_rtttl_melody();
void set_rtttl_melody(String& melody);
# endif // if FEATURE_ANYRTTTL_LIB && FEATURE_ANYRTTTL_ASYNC
#endif // if FEATURE_RTTTL
Expand Down

0 comments on commit 7852c42

Please sign in to comment.