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

Added __FlashStringHelper support for PROGMEM #182

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sming/Services/ArduinoJson/src/JsonBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;

JsonStringStorage JsonStringStorage::_invalid(NULL);
JsonStringStorage JsonStringStorage::_invalid("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not NULL or nullptr for invalid values?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULL is macro'd to 0 which is int so throws a build error. You can try
nullptr and see if it builds but I think I tried it.
On Jul 26, 2015 10:41 AM, "Skurydin Alexey" notifications@github.com
wrote:

In Sming/Services/ArduinoJson/src/JsonBuffer.cpp
#182 (comment):

@@ -14,7 +14,7 @@
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;

-JsonStringStorage JsonStringStorage::_invalid(NULL);
+JsonStringStorage JsonStringStorage::_invalid("");

Why not NULL or nullptr for invalid values?


Reply to this email directly or view it on GitHub
https://github.com/anakod/Sming/pull/182/files#r35494126.


JsonArray &JsonBuffer::createArray() {
JsonArray *ptr = new (this) JsonArray(this);
Expand Down
2 changes: 1 addition & 1 deletion Sming/SmingCore/Network/NtpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void NtpClient::setNtpServer(String server)

void NtpClient::setNtpServer(IPAddress serverIp)
{
this->server = NULL;
this->server = "";
this->serverAddress = serverIp;
}

Expand Down
17 changes: 17 additions & 0 deletions Sming/Wiring/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ size_t Print::write(const uint8_t *buffer, size_t size)
return n;
}

size_t Print::print(const __FlashStringHelper *ifsh)
{
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
size_t n = 0;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) break;
n += write(c);
}
return n;
}

// Base method (character)
size_t Print::print(char c)
Expand Down Expand Up @@ -115,6 +126,12 @@ size_t Print::print(const String &s)
return write(s.c_str(), s.length());
}

size_t Print::println(const __FlashStringHelper *ifsh)
{
size_t n = print(ifsh);
n += println();
return n;
}

size_t Print::println(void)
{
Expand Down
4 changes: 3 additions & 1 deletion Sming/Wiring/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#define PRINT_H

#ifdef __cplusplus

#include "WiringFrameworkDependencies.h"
#include "Printable.h"
#include "WString.h"

#define INITIAL_PRINTF_BUFFSIZE 128

Expand Down Expand Up @@ -54,6 +54,7 @@ class Print
}

// print
size_t print(const __FlashStringHelper *);
size_t print(char);
size_t print(const char[]);

Expand All @@ -69,6 +70,7 @@ class Print
size_t print(const String &s);

// println
size_t println(const __FlashStringHelper *);
size_t println(void);

size_t println(const char[]);
Expand Down
46 changes: 41 additions & 5 deletions Sming/Wiring/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ String::String(const String &value)
*this = value;
}

String::String(const __FlashStringHelper *pstr) {
init();
*this = pstr; // see operator =
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
String::String(String &&rval)
{
Expand Down Expand Up @@ -191,6 +196,16 @@ String & String::copy(const char *cstr, unsigned int length)
return *this;
}

String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
if (!reserve(length)) {
invalidate();
return *this;
}
len = length;
strcpy_P(buffer, (PGM_P)pstr);
return *this;
}

#ifdef __GXX_EXPERIMENTAL_CXX0X__
void String::move(String &rhs)
{
Expand Down Expand Up @@ -237,6 +252,14 @@ String & String::operator = (const char *cstr)
return *this;
}

String & String::operator = (const __FlashStringHelper *pstr)
{
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
else invalidate();

return *this;
}

/*********************************************/
/* concat */
/*********************************************/
Expand Down Expand Up @@ -320,6 +343,17 @@ unsigned char String::concat(double num)
return concat(string, strlen(string));
}

unsigned char String::concat(const __FlashStringHelper * str) {
if (!str) return 0;
int length = strlen_P((PGM_P)str);
if (length == 0) return 1;
unsigned int newlen = len + length;
if (!reserve(newlen)) return 0;
strcpy_P(buffer + len, (PGM_P)str);
len = newlen;
return 1;
}

/*********************************************/
/* Concatenate */
/*********************************************/
Expand Down Expand Up @@ -394,6 +428,13 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
return a;
}

StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(rhs)) a.invalidate();
return a;
}

/*********************************************/
/* Comparison */
/*********************************************/
Expand Down Expand Up @@ -728,8 +769,3 @@ float String::toFloat(void) const
return 0;
}

/*void String::printTo(Print &p) const
{
p.print(buffer);
}*/

16 changes: 14 additions & 2 deletions Sming/Wiring/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
// -felide-constructors
// -std=c++0x

class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

// An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper;
Expand All @@ -56,6 +59,7 @@ class String
IRAM_ATTR String(const char *cstr = "");
IRAM_ATTR String(const char *cstr, unsigned int length);
IRAM_ATTR String(const String &str);
IRAM_ATTR String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
IRAM_ATTR String(String && rval);
IRAM_ATTR String(StringSumHelper && rval);
Expand Down Expand Up @@ -87,6 +91,7 @@ class String
// marked as invalid ("if (s)" will be false).
String & IRAM_ATTR operator = (const String &rhs);
String & IRAM_ATTR operator = (const char *cstr);
String & IRAM_ATTR operator = (const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator = (String && rval);
String & operator = (StringSumHelper && rval);
Expand All @@ -107,6 +112,7 @@ class String
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper * str);

// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
Expand Down Expand Up @@ -160,6 +166,10 @@ class String
concat(num);
return (*this);
}
String & operator += (const __FlashStringHelper *str){
concat(str);
return (*this);
}

friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
Expand All @@ -171,6 +181,7 @@ class String
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
friend StringSumHelper & operator +(const StringSumHelper &lhs, const __FlashStringHelper *rhs);

// comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const
Expand Down Expand Up @@ -208,8 +219,8 @@ class String
// character acccess
char IRAM_ATTR charAt(unsigned int index) const;
void IRAM_ATTR setCharAt(unsigned int index, char c);
char IRAM_ATTR operator [](unsigned int index) const;
char& IRAM_ATTR operator [](unsigned int index);
char IRAM_ATTR operator[](unsigned int index) const;
char& IRAM_ATTR operator[](unsigned int index);
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const
{
Expand Down Expand Up @@ -262,6 +273,7 @@ class String

// copy and move
String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs);
#endif
Expand Down