Skip to content

PgmSpace working #236

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

Merged
merged 1 commit into from
May 13, 2015
Merged
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
19 changes: 19 additions & 0 deletions hardware/esp8266com/esp8266/cores/esp8266/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

Modified 23 November 2006 by David A. Mellis
Modified December 2014 by Ivan Grokhotkov
Modified May 2015 by Michael C. Miller - esp8266 progmem support
*/

#include <stdlib.h>
Expand All @@ -42,6 +43,18 @@ size_t ICACHE_FLASH_ATTR Print::write(const uint8_t *buffer, size_t size) {
return n;
}

size_t ICACHE_FLASH_ATTR Print::print(const __FlashStringHelper *ifsh) {
PGM_P p = reinterpret_cast<PGM_P>(ifsh);

size_t n = 0;
while (1) {
uint8_t c = pgm_read_byte(p++);
if (c == 0) break;
n += write(c);
}
return n;
}

size_t ICACHE_FLASH_ATTR Print::print(const String &s) {
return write(s.c_str(), s.length());
}
Expand Down Expand Up @@ -92,6 +105,12 @@ size_t ICACHE_FLASH_ATTR Print::print(double n, int digits) {
return printFloat(n, digits);
}

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

size_t ICACHE_FLASH_ATTR Print::print(const Printable& x) {
return x.printTo(*this);
}
Expand Down
2 changes: 2 additions & 0 deletions hardware/esp8266com/esp8266/cores/esp8266/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Print {
return write((const uint8_t *) buffer, size);
}

size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
Expand All @@ -74,6 +75,7 @@ class Print {
size_t print(double, int = 2);
size_t print(const Printable&);

size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
Expand Down
42 changes: 42 additions & 0 deletions hardware/esp8266com/esp8266/cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
Copyright 2011, Paul Stoffregen, paul@pjrc.com
Modified by Ivan Grokhotkov, 2014 - esp8266 support
Modified by Michael C. Miller, 2015 - esp8266 progmem support

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -44,6 +45,11 @@ ICACHE_FLASH_ATTR String::String(const String &value) {
*this = value;
}

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

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

String & ICACHE_FLASH_ATTR 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 ICACHE_FLASH_ATTR String::move(String &rhs) {
if(buffer) {
Expand Down Expand Up @@ -223,6 +239,14 @@ String & ICACHE_FLASH_ATTR String::operator =(const char *cstr) {
return *this;
}

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

return *this;
}

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

unsigned char ICACHE_FLASH_ATTR 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 @@ -373,6 +408,13 @@ StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, doubl
return a;
}

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

// /*********************************************/
// /* Comparison */
// /*********************************************/
Expand Down
19 changes: 15 additions & 4 deletions hardware/esp8266com/esp8266/cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define PROGMEM
#include <pgmspace.h>

// An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper;

typedef char* __FlashStringHelper;
//#define F(str) []() -> const char * { static const char tmp[] ICACHE_RODATA_ATTR = str; return &tmp[0]; }()
#define F(str) str
// an abstract class used as a means to proide a unique pointer type
// but really has no body
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))


// The string class
class String {
Expand All @@ -53,6 +55,7 @@ class String {
// be false).
String(const char *cstr = "");
String(const String &str);
String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval);
String(StringSumHelper &&rval);
Expand Down Expand Up @@ -81,6 +84,7 @@ class String {
// marked as invalid ("if (s)" will be false).
String & operator =(const String &rhs);
String & operator =(const char *cstr);
String & operator = (const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator =(String &&rval);
String & operator =(StringSumHelper &&rval);
Expand All @@ -101,6 +105,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 @@ -144,6 +149,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 @@ -155,6 +164,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 @@ -237,6 +247,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
8 changes: 4 additions & 4 deletions hardware/esp8266com/esp8266/cores/esp8266/libc_replacements.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ char* strncpy(char * dest, const char * src, size_t n) {
return ets_strncpy(dest, src, n);
}

size_t strnlen(const char *s, size_t len) {
size_t ICACHE_FLASH_ATTR strnlen(const char *s, size_t len) {
// there is no ets_strnlen
const char *cp;
for (cp = s; len != 0 && *cp != '\0'; cp++, len--);
Expand All @@ -127,7 +127,7 @@ char* strstr(const char *haystack, const char *needle) {
return ets_strstr(haystack, needle);
}

char* strchr(const char * str, int character) {
char* ICACHE_FLASH_ATTR strchr(const char * str, int character) {
while(1) {
if(*str == 0x00) {
return NULL;
Expand All @@ -139,7 +139,7 @@ char* strchr(const char * str, int character) {
}
}

char * strrchr(const char * str, int character) {
char * ICACHE_FLASH_ATTR strrchr(const char * str, int character) {
char * ret = NULL;
while(1) {
if(*str == 0x00) {
Expand Down Expand Up @@ -223,7 +223,7 @@ char* ICACHE_FLASH_ATTR strtok(char * str, const char * delimiters) {
return ret;
}

int strcasecmp(const char * str1, const char * str2) {
int ICACHE_FLASH_ATTR strcasecmp(const char * str1, const char * str2) {
int d = 0;
while(1) {
int c1 = tolower(*str1++);
Expand Down
Loading