arduino-logger icon indicating copy to clipboard operation
arduino-logger copied to clipboard

Add support for F() macro

Open phillipjohnston opened this issue 5 years ago • 3 comments

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 avatar Apr 27 '20 18:04 phillipjohnston

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 avatar Apr 27 '20 18:04 phillipjohnston

https://www.arduino.cc/reference/en/language/variables/utilities/progmem/ https://www.nongnu.org/avr-libc/user-manual/pgmspace.html

phillipjohnston avatar Apr 29 '20 23:04 phillipjohnston

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;

}

phillipjohnston avatar Apr 29 '20 23:04 phillipjohnston