Skip to content

Commit 13d7330

Browse files
authored
Fix incorrect reading of floats in ESP8266 cores 2.5.1 and higher
ESP8266 cores 2.5.1 and 2.5.2 have an alignment issue causing incorrect reading of floats in PROGMEM See esp8266/Arduino#5628 and esp8266/Arduino#5692 By default the 2.5.1 and 2.5.2 cores use pgm_read_float_unaligned() but then the PROGMEM floats are not properly read. The read frequencies were way to high resulting in crackling noise or high pitched sound. Added a conditional redefine for ESP8266 environment which fixes properly reading the float values.
1 parent 9962bf8 commit 13d7330

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

MmlMusic.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ void MmlMusic::waitTone(unsigned long length, uint8_t nTrack) // defaults: lengt
116116

117117
void MmlMusic::playMML(const char* mml)
118118
{
119-
//Serial.print(F("MmlMusic play:"));
120-
//Serial.println(mml);
119+
//Serial.print(F("--DEBUG MmlMusic play:"));
120+
//Serial.print(mml);
121+
//Serial.println(F("--"));
121122
// __disable_irq();
122123
//noInterrupts();
123124
#if(OPT_MMLMUSIC_PLAYCALLBACK)
@@ -304,6 +305,18 @@ void MmlMusicTrack::executeCommandTrack(MmlMusic *pMusic)
304305
duration = WHOLE_NOTE_DURATION / getNumber(1, 64);
305306
}
306307

308+
#if defined (ARDUINO_ARCH_ESP8266)
309+
// ESP8266 cores 2.5.1 and 2.5.2 have an alignment issue causing incorrect reading of floats in PROGMEM
310+
// See https://github.com/esp8266/Arduino/issues/5628 and https://github.com/esp8266/Arduino/pull/5692
311+
// by default the 2.5.1 and 2.5.2 cores use pgm_read_float_unaligned() but then the PROGMEM floats are not properly read.
312+
// TODO: making the note array uint16_t may make the library faster and smaller (better for ATtiny's)
313+
//#define pgm_read_float(addr) (*reinterpret_cast<const float*>(addr)) //dw fixed
314+
//#define pgm_read_float(addr) (*(const float*)(addr)) // dw fixed
315+
#if defined(pgm_read_float_aligned)
316+
#define pgm_read_float(addr) pgm_read_float_aligned(addr)
317+
#endif
318+
#endif
319+
307320
if (freqIndex != NOTE_REST || (fCminus && _octave > 0)) {
308321
float ftFreq = pgm_read_float(&FREQ_TABLE[freqIndex + (_octave * 12)]);
309322

0 commit comments

Comments
 (0)