Skip to content

PROGMEM support? #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brunnels opened this issue May 11, 2015 · 7 comments
Closed

PROGMEM support? #76

brunnels opened this issue May 11, 2015 · 7 comments
Assignees

Comments

@brunnels
Copy link

I'm pretty low on memory in one of my projects so I'm trying to scrape a few bytes together by moving strings into PROGMEM. Would it be possible to add support for this or should it work and I'm doing it wrong?

// normal way
root["relays"].as<uint8_t>();
// using PROGMEM macro that doesn't work but does compile
root[(const char*)F("relays")].as<uint8_t>();
@bblanchon
Copy link
Owner

It doesn't work with the current version of ArduinoJson.
Reading from Flash memory requires a bit of extra work: you need to call pgm_read_byte() etc.

I'll try to add that feature in 5.x but I don't make any promise yet because I fear that it will increase the code size significantly.

@bblanchon bblanchon added this to the 5.1 milestone May 11, 2015
@bblanchon bblanchon modified the milestones: 5.2, 5.1 May 31, 2015
@puppetmaster886
Copy link

I think maybe is easier to support PSTR() rather than F().

@JimGaleForce
Copy link

in my sketch, I have a struct which has a type and a pointer... if the type is 'P' (or whatever), it reads the pointer thru the pgm_read_byte(), otherwise, normal string/int/bool/etc. this adds little overhead.

@bblanchon bblanchon modified the milestones: 6.1, 5.2 Jan 25, 2016
@JarredSteenvoorden
Copy link

In the meantime until this is implemented I have been using the following macro to reduce memory usage.

#define PSTR_READ_INTO(buffer_name, string_literal)     char buffer_name[sizeof(string_literal)]; strcpy_P(buffer_name, (char*)PSTR(string_literal))

PSTR_READ_INTO(relays, "relays");
PSTR_READ_INTO(another_one, "another_one");

root[relays].as<uint8_t>();
root[another_one].as<uint8_t>();

@q2dg
Copy link

q2dg commented Apr 4, 2016

Sorry if I'm a little offtopic, but I just wanted to note that in ARM Arduinos there's no PROGMEM stuff. Instead, we can use some libraries to do the trick (https://github.com/cmaglie/FlashStorage is one example). Maybe this should be taken into account.

@bblanchon bblanchon removed this from the 6.1 milestone Jun 16, 2016
@bblanchon
Copy link
Owner

I just realize that this feature is partially supported since v5.0, thanks to an implicit conversion to String.

This is supported:

root[F("sensor")] = "gps";

This is not supported:

root[F("sensor")] = F("gps");

but can be made to work like this:

root[F("sensor")] = String(F("gps"));

However, I do not recommend to use this because it's terribly inefficient. Each flash string has to be first copied to the heap (this is done by the String constructor), then copied into the JsonBuffer.
I'll try to implement a direct copy to the JsonBuffer.

@bblanchon
Copy link
Owner

Support for PROGMEM has been added in ArduinoJson 5.7.1.
Please note that the content of the flash string is copied into the JsonBuffer when needed (just like it's done for String), so the JsonBuffer will be bigger.

@q2dg, I took your comment into account: I concentrated the logic for handling strings in a very small area so that support for other types of string can be added easily in the future.
Have a look at commit 1ce16ce to see how I added support for PROGMEM; it's a good start if you want to add another type of string.

Repository owner locked and limited conversation to collaborators Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants