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 FlashString abstract class #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

kezorm
Copy link

@kezorm kezorm commented Nov 2, 2018

Add FlashString abstract class to support extension of String and Print methods to core specific program memory implementation. The FlashString abstract class completely eliminates the dependency on the file pgmspace.h. ArduinoCore-API should never include any references to a specific implementation (e.g. AVR).

An example implementation is shown below, tested on a SAMD21 core. I have not had a chance to implement / test on an AVR-based Arduino, but should be easily implemented at avr core level.

class FlashStringTest : public FlashString
{
  public:
    FlashStringTest(uintptr_t address): _address(address) {}
    
    String toString() const;
    size_t printTo(Print& p) const;

    protected:
    uintptr_t _address;
};

String FlashStringTest::toString() const
{
  return String((const char *)_address);
}
 
 size_t FlashStringTest::printTo(Print& p) const
{
  size_t n = 0;
  const char* str = (const char*)_address;
  while (1) {
    char c = *str++;
    if (c == 0) break;
    if (p.print(c)) n++;
    else break;
  }
  return n;
}

#ifdef F
#undef F
#endif

#define F(string_literal) (FlashStringTest((uintptr_t)(string_literal)))

…nt methods to core specific program memory implementation.
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@PaulStoffregen
Copy link
Sponsor

Is your intention to break all libraries which have used __FlashStringHelper?

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

Successfully merging this pull request may close these issues.

None yet

3 participants