diff --git a/extras/tests/Helpers/Arduino.h b/extras/tests/Helpers/Arduino.h index 4f24b263..bacc1a88 100644 --- a/extras/tests/Helpers/Arduino.h +++ b/extras/tests/Helpers/Arduino.h @@ -7,6 +7,5 @@ #include "api/Print.h" #include "api/Stream.h" #include "api/String.h" -#include "progmem_emulation.hpp" #define ARDUINO_H_INCLUDED 1 diff --git a/extras/tests/Misc/StringAdapters.cpp b/extras/tests/Misc/StringAdapters.cpp index 02282334..03c4196d 100644 --- a/extras/tests/Misc/StringAdapters.cpp +++ b/extras/tests/Misc/StringAdapters.cpp @@ -6,6 +6,7 @@ #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 #include "custom_string.hpp" +#include "progmem_emulation.hpp" #include "weird_strcmp.hpp" #include diff --git a/extras/tests/MixedConfiguration/enable_progmem_1.cpp b/extras/tests/MixedConfiguration/enable_progmem_1.cpp index 9ddf7f26..61e6c281 100644 --- a/extras/tests/MixedConfiguration/enable_progmem_1.cpp +++ b/extras/tests/MixedConfiguration/enable_progmem_1.cpp @@ -2,6 +2,8 @@ // Copyright © 2014-2022, Benoit BLANCHON // MIT License +#include "progmem_emulation.hpp" + #define ARDUINOJSON_ENABLE_PROGMEM 1 #include diff --git a/extras/tests/MixedConfiguration/enable_string_deduplication_0.cpp b/extras/tests/MixedConfiguration/enable_string_deduplication_0.cpp index d9ca166e..4adb5ca3 100644 --- a/extras/tests/MixedConfiguration/enable_string_deduplication_0.cpp +++ b/extras/tests/MixedConfiguration/enable_string_deduplication_0.cpp @@ -2,6 +2,8 @@ // Copyright © 2014-2022, Benoit BLANCHON // MIT License +#include "progmem_emulation.hpp" + #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 #define ARDUINOJSON_ENABLE_PROGMEM 1 #define ARDUINOJSON_ENABLE_STRING_DEDUPLICATION 0 diff --git a/extras/tests/MixedConfiguration/enable_string_deduplication_1.cpp b/extras/tests/MixedConfiguration/enable_string_deduplication_1.cpp index 27fc18d6..0059777c 100644 --- a/extras/tests/MixedConfiguration/enable_string_deduplication_1.cpp +++ b/extras/tests/MixedConfiguration/enable_string_deduplication_1.cpp @@ -2,6 +2,8 @@ // Copyright © 2014-2022, Benoit BLANCHON // MIT License +#include "progmem_emulation.hpp" + #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 #define ARDUINOJSON_ENABLE_PROGMEM 1 #define ARDUINOJSON_ENABLE_STRING_DEDUPLICATION 1 diff --git a/extras/tests/MixedConfiguration/issue1707.cpp b/extras/tests/MixedConfiguration/issue1707.cpp index befdee53..fce024c2 100644 --- a/extras/tests/MixedConfiguration/issue1707.cpp +++ b/extras/tests/MixedConfiguration/issue1707.cpp @@ -5,6 +5,8 @@ #define ARDUINO #define memcpy_P(dest, src, n) memcpy((dest), (src), (n)) +#include "progmem_emulation.hpp" + #include #include diff --git a/src/ArduinoJson/Deserialization/DeserializationError.hpp b/src/ArduinoJson/Deserialization/DeserializationError.hpp index 96b3a178..9720096f 100644 --- a/src/ArduinoJson/Deserialization/DeserializationError.hpp +++ b/src/ArduinoJson/Deserialization/DeserializationError.hpp @@ -6,8 +6,8 @@ #include #include +#include #include -#include #if ARDUINOJSON_ENABLE_STD_STREAM # include @@ -74,16 +74,15 @@ class DeserializationError : public SafeBoolIdom { #if ARDUINOJSON_ENABLE_PROGMEM const __FlashStringHelper* f_str() const { - ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s0, "Ok"); - ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s1, "EmptyInput"); - ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s2, "IncompleteInput"); - ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s3, "InvalidInput"); - ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s4, "NoMemory"); - ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s5, "TooDeep"); - ARDUINOJSON_DEFINE_STATIC_ARRAY( + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(char, s0, "Ok"); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(char, s1, "EmptyInput"); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(char, s2, "IncompleteInput"); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(char, s3, "InvalidInput"); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(char, s4, "NoMemory"); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(char, s5, "TooDeep"); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY( const char*, messages, ARDUINOJSON_EXPAND6({s0, s1, s2, s3, s4, s5})); - return ARDUINOJSON_READ_STATIC_ARRAY(const __FlashStringHelper*, messages, - _code); + return pgm_read(messages + _code); } #endif diff --git a/src/ArduinoJson/Numbers/FloatTraits.hpp b/src/ArduinoJson/Numbers/FloatTraits.hpp index 4d5782b7..be15108d 100644 --- a/src/ArduinoJson/Numbers/FloatTraits.hpp +++ b/src/ArduinoJson/Numbers/FloatTraits.hpp @@ -10,8 +10,8 @@ #include #include #include +#include #include -#include #include namespace ARDUINOJSON_NAMESPACE { @@ -49,7 +49,7 @@ struct FloatTraits { } static T positiveBinaryPowerOfTen(int index) { - ARDUINOJSON_DEFINE_STATIC_ARRAY( // + ARDUINOJSON_DEFINE_PROGMEM_ARRAY( // uint32_t, factors, ARDUINOJSON_EXPAND18({ 0x40240000, 0x00000000, // 1e1 @@ -62,13 +62,12 @@ struct FloatTraits { 0x5A827748, 0xF9301D32, // 1e128 0x75154FDD, 0x7F73BF3C // 1e256 })); - return forge( - ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, 2 * index), - ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, 2 * index + 1)); + return forge(pgm_read(factors + 2 * index), + pgm_read(factors + 2 * index + 1)); } static T negativeBinaryPowerOfTen(int index) { - ARDUINOJSON_DEFINE_STATIC_ARRAY( // + ARDUINOJSON_DEFINE_PROGMEM_ARRAY( // uint32_t, factors, ARDUINOJSON_EXPAND18({ 0x3FB99999, 0x9999999A, // 1e-1 @@ -81,13 +80,12 @@ struct FloatTraits { 0x255BBA08, 0xCF8C979D, // 1e-128 0x0AC80628, 0x64AC6F43 // 1e-256 })); - return forge( - ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, 2 * index), - ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, 2 * index + 1)); + return forge(pgm_read(factors + 2 * index), + pgm_read(factors + 2 * index + 1)); } static T negativeBinaryPowerOfTenPlusOne(int index) { - ARDUINOJSON_DEFINE_STATIC_ARRAY( // + ARDUINOJSON_DEFINE_PROGMEM_ARRAY( // uint32_t, factors, ARDUINOJSON_EXPAND18({ 0x3FF00000, 0x00000000, // 1e0 @@ -100,9 +98,8 @@ struct FloatTraits { 0x25915445, 0x81B7DEC2, // 1e-127 0x0AFE07B2, 0x7DD78B14 // 1e-255 })); - return forge( - ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, 2 * index), - ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, 2 * index + 1)); + return forge(pgm_read(factors + 2 * index), + pgm_read(factors + 2 * index + 1)); } static T nan() { @@ -175,42 +172,42 @@ struct FloatTraits { } static T positiveBinaryPowerOfTen(int index) { - ARDUINOJSON_DEFINE_STATIC_ARRAY(uint32_t, factors, - ARDUINOJSON_EXPAND6({ - 0x41200000, // 1e1f - 0x42c80000, // 1e2f - 0x461c4000, // 1e4f - 0x4cbebc20, // 1e8f - 0x5a0e1bca, // 1e16f - 0x749dc5ae // 1e32f - })); - return forge(ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, index)); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors, + ARDUINOJSON_EXPAND6({ + 0x41200000, // 1e1f + 0x42c80000, // 1e2f + 0x461c4000, // 1e4f + 0x4cbebc20, // 1e8f + 0x5a0e1bca, // 1e16f + 0x749dc5ae // 1e32f + })); + return forge(pgm_read(factors + index)); } static T negativeBinaryPowerOfTen(int index) { - ARDUINOJSON_DEFINE_STATIC_ARRAY(uint32_t, factors, - ARDUINOJSON_EXPAND6({ - 0x3dcccccd, // 1e-1f - 0x3c23d70a, // 1e-2f - 0x38d1b717, // 1e-4f - 0x322bcc77, // 1e-8f - 0x24e69595, // 1e-16f - 0x0a4fb11f // 1e-32f - })); - return forge(ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, index)); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors, + ARDUINOJSON_EXPAND6({ + 0x3dcccccd, // 1e-1f + 0x3c23d70a, // 1e-2f + 0x38d1b717, // 1e-4f + 0x322bcc77, // 1e-8f + 0x24e69595, // 1e-16f + 0x0a4fb11f // 1e-32f + })); + return forge(pgm_read(factors + index)); } static T negativeBinaryPowerOfTenPlusOne(int index) { - ARDUINOJSON_DEFINE_STATIC_ARRAY(uint32_t, factors, - ARDUINOJSON_EXPAND6({ - 0x3f800000, // 1e0f - 0x3dcccccd, // 1e-1f - 0x3a83126f, // 1e-3f - 0x33d6bf95, // 1e-7f - 0x26901d7d, // 1e-15f - 0x0c01ceb3 // 1e-31f - })); - return forge(ARDUINOJSON_READ_STATIC_ARRAY(uint32_t, factors, index)); + ARDUINOJSON_DEFINE_PROGMEM_ARRAY(uint32_t, factors, + ARDUINOJSON_EXPAND6({ + 0x3f800000, // 1e0f + 0x3dcccccd, // 1e-1f + 0x3a83126f, // 1e-3f + 0x33d6bf95, // 1e-7f + 0x26901d7d, // 1e-15f + 0x0c01ceb3 // 1e-31f + })); + return forge(pgm_read(factors + index)); } static T forge(uint32_t bits) { diff --git a/src/ArduinoJson/Polyfills/pgmspace_generic.hpp b/src/ArduinoJson/Polyfills/pgmspace_generic.hpp index d9e964b8..826827fb 100644 --- a/src/ArduinoJson/Polyfills/pgmspace_generic.hpp +++ b/src/ArduinoJson/Polyfills/pgmspace_generic.hpp @@ -5,11 +5,21 @@ #pragma once #include -#include -#include + +#if ARDUINOJSON_ENABLE_PROGMEM +# include +# include +#endif namespace ARDUINOJSON_NAMESPACE { +#if ARDUINOJSON_ENABLE_PROGMEM + +# ifndef ARDUINOJSON_DEFINE_PROGMEM_ARRAY +# define ARDUINOJSON_DEFINE_PROGMEM_ARRAY(type, name, value) \ + static type const name[] PROGMEM = value; +# endif + template typename enable_if::value, T>::type pgm_read(const void* p) { return reinterpret_cast(pgm_read_ptr(p)); @@ -20,5 +30,18 @@ typename enable_if::value, T>::type pgm_read( const void* p) { return pgm_read_dword(p); } +#else + +# ifndef ARDUINOJSON_DEFINE_PROGMEM_ARRAY +# define ARDUINOJSON_DEFINE_PROGMEM_ARRAY(type, name, value) \ + static type const name[] = value; +# endif + +template +inline T pgm_read(const void* p) { + return *reinterpret_cast(p); +} + +#endif } // namespace ARDUINOJSON_NAMESPACE diff --git a/src/ArduinoJson/Polyfills/static_array.hpp b/src/ArduinoJson/Polyfills/static_array.hpp deleted file mode 100644 index b2ebbf6b..00000000 --- a/src/ArduinoJson/Polyfills/static_array.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// ArduinoJson - https://arduinojson.org -// Copyright © 2014-2022, Benoit BLANCHON -// MIT License - -#pragma once - -#include - -#if ARDUINOJSON_ENABLE_PROGMEM - -# include - -# ifndef ARDUINOJSON_DEFINE_PROGMEM_ARRAY -# define ARDUINOJSON_DEFINE_PROGMEM_ARRAY(type, name, value) \ - static type const name[] PROGMEM = value; -# endif - -# define ARDUINOJSON_DEFINE_STATIC_ARRAY ARDUINOJSON_DEFINE_PROGMEM_ARRAY - -# define ARDUINOJSON_READ_STATIC_ARRAY(type, name, index) \ - pgm_read(name + index) - -#else // i.e. ARDUINOJSON_ENABLE_PROGMEM == 0 - -# define ARDUINOJSON_DEFINE_STATIC_ARRAY(type, name, value) \ - static type const name[] = value; - -# define ARDUINOJSON_READ_STATIC_ARRAY(type, name, index) name[index] - -#endif