From 250386802f3674311acd1b1481426c3e00aeebdd Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 24 Dec 2013 10:57:32 +0100 Subject: [PATCH 1/3] Add Print::write(const char *, size_t) The new function just calls Print::write(const uint8_t *, size_t), but this allows writing out a buffer of chars (without having to learn about casts). --- hardware/arduino/avr/cores/arduino/Print.h | 3 +++ hardware/arduino/sam/cores/arduino/Print.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/hardware/arduino/avr/cores/arduino/Print.h b/hardware/arduino/avr/cores/arduino/Print.h index dc7615087..7b53aa4d1 100644 --- a/hardware/arduino/avr/cores/arduino/Print.h +++ b/hardware/arduino/avr/cores/arduino/Print.h @@ -51,6 +51,9 @@ class Print return write((const uint8_t *)str, strlen(str)); } virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } size_t print(const __FlashStringHelper *); size_t print(const String &); diff --git a/hardware/arduino/sam/cores/arduino/Print.h b/hardware/arduino/sam/cores/arduino/Print.h index dc7615087..7b53aa4d1 100644 --- a/hardware/arduino/sam/cores/arduino/Print.h +++ b/hardware/arduino/sam/cores/arduino/Print.h @@ -51,6 +51,9 @@ class Print return write((const uint8_t *)str, strlen(str)); } virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } size_t print(const __FlashStringHelper *); size_t print(const String &); From f304abe35f8c878021f4cba818e413e352ecdd4a Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 24 Dec 2013 13:02:48 +0100 Subject: [PATCH 2/3] Add uint8_t* versions of methods in Stream The new functions just call their char* equivalents, but this allows reading bytes into a buffer of uint8_t as well as chars. --- hardware/arduino/avr/cores/arduino/Stream.h | 6 ++++++ hardware/arduino/sam/cores/arduino/Stream.h | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/hardware/arduino/avr/cores/arduino/Stream.h b/hardware/arduino/avr/cores/arduino/Stream.h index 007b4bc66..5cf5ddf01 100644 --- a/hardware/arduino/avr/cores/arduino/Stream.h +++ b/hardware/arduino/avr/cores/arduino/Stream.h @@ -57,14 +57,18 @@ class Stream : public Print void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } // returns true if target string is found, false if timed out (see setTimeout) bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } // returns true if target string is found, false if timed out bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } long parseInt(); // returns the first valid (long) integer value from the current position. @@ -74,10 +78,12 @@ class Stream : public Print float parseFloat(); // float version of parseInt size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } // terminates if length characters have been read or timeout (see setTimeout) // returns the number of characters placed in the buffer (0 means no valid data found) size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } // terminates if length characters have been read, timeout, or if the terminator character detected // returns the number of characters placed in the buffer (0 means no valid data found) diff --git a/hardware/arduino/sam/cores/arduino/Stream.h b/hardware/arduino/sam/cores/arduino/Stream.h index 007b4bc66..5cf5ddf01 100644 --- a/hardware/arduino/sam/cores/arduino/Stream.h +++ b/hardware/arduino/sam/cores/arduino/Stream.h @@ -57,14 +57,18 @@ class Stream : public Print void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } // returns true if target string is found, false if timed out (see setTimeout) bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } // returns true if target string is found, false if timed out bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } long parseInt(); // returns the first valid (long) integer value from the current position. @@ -74,10 +78,12 @@ class Stream : public Print float parseFloat(); // float version of parseInt size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } // terminates if length characters have been read or timeout (see setTimeout) // returns the number of characters placed in the buffer (0 means no valid data found) size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } // terminates if length characters have been read, timeout, or if the terminator character detected // returns the number of characters placed in the buffer (0 means no valid data found) From 2ea12d02201b0085391105b456044288f377fb21 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 24 Dec 2013 13:21:42 +0100 Subject: [PATCH 3/3] Remove unneeded casts in Print::write(const String&) Now that Print::write(const char*) is also available, these casts are no longer needed. --- hardware/arduino/avr/cores/arduino/Print.cpp | 2 +- hardware/arduino/sam/cores/arduino/Print.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/Print.cpp b/hardware/arduino/avr/cores/arduino/Print.cpp index f6e499ba2..9a6b964d2 100644 --- a/hardware/arduino/avr/cores/arduino/Print.cpp +++ b/hardware/arduino/avr/cores/arduino/Print.cpp @@ -53,7 +53,7 @@ size_t Print::print(const __FlashStringHelper *ifsh) size_t Print::print(const String &s) { - return write(reinterpret_cast(s.c_str()), s.length()); + return write(s.c_str(), s.length()); } size_t Print::print(const char str[]) diff --git a/hardware/arduino/sam/cores/arduino/Print.cpp b/hardware/arduino/sam/cores/arduino/Print.cpp index 5e4810d55..23f6a2372 100644 --- a/hardware/arduino/sam/cores/arduino/Print.cpp +++ b/hardware/arduino/sam/cores/arduino/Print.cpp @@ -46,7 +46,7 @@ size_t Print::print(const __FlashStringHelper *ifsh) size_t Print::print(const String &s) { - return write(reinterpret_cast(s.c_str()), s.length()); + return write(s.c_str(), s.length()); } size_t Print::print(const char str[])