Skip to content
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

Add support for F() macro #15

Open
phillipjohnston opened this issue Apr 27, 2020 · 3 comments
Open

Add support for F() macro #15

phillipjohnston opened this issue Apr 27, 2020 · 3 comments

Comments

@phillipjohnston
Copy link
Member

We do not currently handle strings stored in flash via the F() macro.

https://andybrown.me.uk/wk/wp-content/images//avr-gcc-4.7.0/WString.h
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Print.h

@phillipjohnston
Copy link
Member Author

The way I think we handle this is by providing an F() macro in the base logger, which is defined if there isn't one defined in the logging strategy. This would ensure that logger code doesn't have to change.

We would need a derived strategy which handles F() appropriately, needs to read the string from flash. We can't put this in the base necessarily because we might want to use the logger with unit tests that don't include Arduino.h/WString.h. We should allow swapping to a strategy which just passes these strings through if there's no overriding implementation.

One implication, however, is that the F() macro must be included/defined by the Arduino headers before including the base logger header.

@phillipjohnston
Copy link
Member Author

size_t Print::print(const __FlashStringHelper *ifsh) {

    PGM_P p = reinterpret_cast<PGM_P>(ifsh);




    char buff[128] __attribute__ ((aligned(4)));

    auto len = strlen_P(p);

    size_t n = 0;

    while (n < len) {

        int to_write = std::min(sizeof(buff), len - n);

        memcpy_P(buff, p, to_write);

        auto written = write(buff, to_write);

        n += written;

        p += written;

        if (!written) {

            // Some error, write() should write at least 1 byte before returning

            break;

        }

    }

    return n;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Arduino Libraries
  
Up Next
Development

No branches or pull requests

1 participant