Skip to content

Commit

Permalink
fix bug which causes the crash at boot in esp8266 core v3.0.x
Browse files Browse the repository at this point in the history
The unordered_map is replaced by static array: now the initialization of the notes mapping is more efficient and the sketch size is reduced by 5kb
  • Loading branch information
fabianoriccardi committed May 21, 2022
1 parent dcd32f3 commit a0a9a7a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
15 changes: 10 additions & 5 deletions src/melody_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
* along with this library; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include "melody_factory.h"
#include "pitches_unordered_map.h"
#include "notes_array.h"

#include <algorithm>

static void removeCarriageReturn(String& s) {
if (s.charAt(s.length() - 1) == '\r') { s = s.substring(0, s.length() - 1); }
Expand Down Expand Up @@ -189,10 +191,13 @@ bool MelodyFactoryClass::loadNote(String token) {
}

if (noteFormat == NoteFormat::STRING) {
std::string mystr(aux.c_str());
std::unordered_map<std::string, unsigned short>::const_iterator n = noteMapping.find(mystr);
if (n != noteMapping.end()) {
note.frequency = noteMapping.at(mystr);
auto n = std::find_if(noteMapping.cbegin(), noteMapping.cend(),
[&aux](std::pair<StringView, unsigned short> e) {
return e.first == aux.c_str();
});

if (n != noteMapping.cend()) {
note.frequency = n->second;
} else {
if (debug) Serial.println(String("This note doesn't exist: ") + aux);
return false;
Expand Down
39 changes: 35 additions & 4 deletions src/pitches_unordered_map.cpp → src/notes_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,38 @@
* You should have received a copy of the GNU General Public License *
* along with this library; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include "pitches_unordered_map.h"
#ifndef PITCHES_UNORDERED_MAP_H
#define PITCHES_UNORDERED_MAP_H

// Notes are expressed in English convention
const std::unordered_map<std::string, unsigned short> noteMapping{
#include <array>

/**
* This class resembles the std::string_view class introduced with c++17. MelodyPlayer uses this
* version to ensure retro-compatibility with esp8266-core v2.x.x, which uses GCC v4.8.2.
*/
struct StringView {
StringView() = delete;
constexpr StringView(const char* s) : str(s), lenght(__builtin_strlen(s)) {}

constexpr bool operator==(const StringView& other) const {
return lenght == other.lenght && (__builtin_memcmp(str, other.str, lenght) == 0);
}

constexpr size_t length() const {
return lenght;
}

constexpr const char* data() const {
return str;
}

private:
const char* str;
size_t lenght;
};

// clang-format off
constexpr std::array<std::pair<StringView, unsigned short>, 92> noteMapping{{
{"SILENCE", 0 },
{ "B0", 31 },
{ "C1", 33 },
Expand Down Expand Up @@ -113,4 +141,7 @@ const std::unordered_map<std::string, unsigned short> noteMapping{
{ "CS8", 4435},
{ "D8", 4699},
{ "DS8", 4978}
};
}};
// clang-format on

#endif // END PITCHES_UNORDERED_MAP_H
35 changes: 0 additions & 35 deletions src/pitches_unordered_map.h

This file was deleted.

0 comments on commit a0a9a7a

Please sign in to comment.