Added printFlashString function

This commit is contained in:
Kristian Sloth Lauszus 2013-02-20 22:43:38 +01:00
parent 734f004671
commit 8a22dd66e7
2 changed files with 27 additions and 32 deletions

29
SPP.cpp
View file

@ -716,9 +716,6 @@ void SPP::print(const char* str) {
RFCOMM_Command(l2capoutbuf,length+4); RFCOMM_Command(l2capoutbuf,length+4);
} }
void SPP::print(uint8_t data) {
print(&data,1);
}
void SPP::print(uint8_t* array, uint8_t length) { void SPP::print(uint8_t* array, uint8_t length) {
if(!connected) if(!connected)
return; return;
@ -734,22 +731,6 @@ void SPP::print(uint8_t* array, uint8_t length) {
RFCOMM_Command(l2capoutbuf,length+4); RFCOMM_Command(l2capoutbuf,length+4);
} }
void SPP::print(const __FlashStringHelper *ifsh) {
const char PROGMEM *p = (const char PROGMEM *)ifsh;
size_t size = 0;
while (1) { // Calculate the size of the string
uint8_t c = pgm_read_byte(p+size);
if (c == 0)
break;
size++;
}
uint8_t buf[size];
for(uint8_t i = 0; i < size; i++)
buf[i] = pgm_read_byte(p++);
print(buf,size);
}
void SPP::println(const String &str) { void SPP::println(const String &str) {
String output = str + "\r\n"; String output = str + "\r\n";
print(output); print(output);
@ -771,23 +752,26 @@ void SPP::println(uint8_t* array, uint8_t length) {
buf[length+1] = '\n'; buf[length+1] = '\n';
print(buf,length+2); print(buf,length+2);
} }
void SPP::println(const __FlashStringHelper *ifsh) { void SPP::printFlashString(const __FlashStringHelper *ifsh, bool newline) {
const char PROGMEM *p = (const char PROGMEM *)ifsh; const char PROGMEM *p = (const char PROGMEM *)ifsh;
size_t size = 0; uint8_t size = 0;
while (1) { // Calculate the size of the string while (1) { // Calculate the size of the string
uint8_t c = pgm_read_byte(p+size); uint8_t c = pgm_read_byte(p+size);
if (c == 0) if (c == 0)
break; break;
size++; size++;
} }
uint8_t buf[size+2]; uint8_t buf[size+2]; // Add two extra in case it needs to print a newline and carriage return
for(uint8_t i = 0; i < size; i++) for(uint8_t i = 0; i < size; i++)
buf[i] = pgm_read_byte(p++); buf[i] = pgm_read_byte(p++);
if(newline) {
buf[size] = '\r'; buf[size] = '\r';
buf[size+1] = '\n'; buf[size+1] = '\n';
print(buf,size+2); print(buf,size+2);
} else
print(buf,size);
} }
void SPP::println(void) { void SPP::println(void) {
uint8_t buf[2] = {'\r','\n'}; uint8_t buf[2] = {'\r','\n'};
@ -806,7 +790,6 @@ void SPP::printNumberln(uint32_t n) {
strcat(output,"\r\n"); strcat(output,"\r\n");
print(output); print(output);
} }
void SPP::printNumber(int32_t n) { void SPP::printNumber(int32_t n) {
char output[12]; char output[12];
intToString(n,output); intToString(n,output);

18
SPP.h
View file

@ -128,6 +128,7 @@ public:
* @param str String to send. * @param str String to send.
*/ */
void println(const String &str); void println(const String &str);
/** /**
* Used to send standard strings. * Used to send standard strings.
* @param str String to send. * @param str String to send.
@ -138,16 +139,18 @@ public:
* @param str String to send. * @param str String to send.
*/ */
void println(const char* str); void println(const char* str);
/** /**
* Used to send single bytes. * Used to send single bytes.
* @param data Data to send. * @param data Data to send.
*/ */
void print(uint8_t data); void print(uint8_t data) { print(&data,1); };
/** /**
* Same as print(uint8_t data), but will include newline and carriage return. * Same as print(uint8_t data), but will include newline and carriage return.
* @param data Data to send. * @param data Data to send.
*/ */
void println(uint8_t data); void println(uint8_t data);
/** /**
* Used to send arrays. * Used to send arrays.
* @param array Array to send. * @param array Array to send.
@ -160,16 +163,25 @@ public:
* @param length Number of bytes to send. * @param length Number of bytes to send.
*/ */
void println(uint8_t* array, uint8_t length); void println(uint8_t* array, uint8_t length);
/** /**
* Used to print strings stored in flash. * Used to print strings stored in flash.
* @param ifsh String to send - see: http://playground.arduino.cc/Learning/Memory. * @param ifsh String to send - see: http://playground.arduino.cc/Learning/Memory.
*/ */
void print(const __FlashStringHelper *ifsh); void print(const __FlashStringHelper *ifsh) { printFlashString(ifsh,false); };
/** /**
* Same as print(const __FlashStringHelper *ifsh), but will include newline and carriage return. * Same as print(const __FlashStringHelper *ifsh), but will include newline and carriage return.
* @param ifsh String to send - see: http://playground.arduino.cc/Learning/Memory. * @param ifsh String to send - see: http://playground.arduino.cc/Learning/Memory.
*/ */
void println(const __FlashStringHelper *ifsh); void println(const __FlashStringHelper *ifsh) { printFlashString(ifsh,true); };
/**
* Helper function to convert from a string stored in flash to a string in ram.
* @param ifsh String to convert.
* @param newline Set this to true to include newline and carriage return.
*/
void printFlashString(const __FlashStringHelper *ifsh, bool newline);
/** Use this to print newline and carriage return. */ /** Use this to print newline and carriage return. */
void println(void); void println(void);