Skip to content

Arduino Stream fix #97

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

Open
wants to merge 3 commits into
base: S130
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

#include <inttypes.h>

#include "WStream.h"
#include "Stream.h"

class HardwareSerial : public WStream
class HardwareSerial : public Stream
{
public:
void begin( const uint32_t dwBaudRate );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
*/

#include "Arduino.h"
#include "WStream.h"
#include "Stream.h"

#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field

// private method to read stream with timeout
int WStream::timedRead()
int Stream::timedRead()
{
int c;
_startMillis = millis();
Expand All @@ -40,7 +40,7 @@ int WStream::timedRead()
}

// private method to peek stream with timeout
int WStream::timedPeek()
int Stream::timedPeek()
{
int c;
_startMillis = millis();
Expand All @@ -53,7 +53,7 @@ int WStream::timedPeek()

// returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters
int WStream::peekNextDigit()
int Stream::peekNextDigit()
{
int c;
while (1) {
Expand All @@ -68,34 +68,34 @@ int WStream::peekNextDigit()
// Public Methods
//////////////////////////////////////////////////////////////

void WStream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
{
_timeout = timeout;
}

// find returns true if the target string is found
bool WStream::find(char *target)
bool Stream::find(char *target)
{
return findUntil(target, NULL);
}

// reads data from the stream until the target string of given length is found
// returns true if target string is found, false if timed out
bool WStream::find(char *target, size_t length)
bool Stream::find(char *target, size_t length)
{
return findUntil(target, length, NULL, 0);
}

// as find but search ends if the terminator string is found
bool WStream::findUntil(char *target, char *terminator)
bool Stream::findUntil(char *target, char *terminator)
{
return findUntil(target, strlen(target), terminator, strlen(terminator));
}

// reads data from the stream until the target string of the given length is found
// search terminated if the terminator string is found
// returns true if target string is found, false if terminated or timed out
bool WStream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
{
size_t index = 0; // maximum target string length is 64k bytes!
size_t termIndex = 0;
Expand Down Expand Up @@ -129,14 +129,14 @@ bool WStream::findUntil(char *target, size_t targetLen, char *terminator, size_t
// returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// function is terminated by the first character that is not a digit.
long WStream::parseInt()
long Stream::parseInt()
{
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
}

// as above but a given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
long WStream::parseInt(char skipChar)
long Stream::parseInt(char skipChar)
{
boolean isNegative = false;
long value = 0;
Expand Down Expand Up @@ -166,14 +166,14 @@ long WStream::parseInt(char skipChar)


// as parseInt but returns a floating point value
float WStream::parseFloat()
float Stream::parseFloat()
{
return parseFloat(NO_SKIP_CHAR);
}

// as above but the given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
float WStream::parseFloat(char skipChar){
float Stream::parseFloat(char skipChar){
boolean isNegative = false;
boolean isFraction = false;
long value = 0;
Expand Down Expand Up @@ -215,7 +215,7 @@ float WStream::parseFloat(char skipChar){
// returns the number of characters placed in the buffer
// the buffer is NOT null terminated.
//
size_t WStream::readBytes(char *buffer, size_t length)
size_t Stream::readBytes(char *buffer, size_t length)
{
size_t count = 0;
while (count < length) {
Expand All @@ -232,7 +232,7 @@ size_t WStream::readBytes(char *buffer, size_t 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)

size_t WStream::readBytesUntil(char terminator, char *buffer, size_t length)
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
{
if (length < 1) return 0;
size_t index = 0;
Expand All @@ -245,7 +245,7 @@ size_t WStream::readBytesUntil(char terminator, char *buffer, size_t length)
return index; // return number of characters, not including null terminator
}

String WStream::readString()
String Stream::readString()
{
String ret;
int c = timedRead();
Expand All @@ -257,7 +257,7 @@ String WStream::readString()
return ret;
}

String WStream::readStringUntil(char terminator)
String Stream::readStringUntil(char terminator)
{
String ret;
int c = timedRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
parsing functions based on TextFinder library by Michael Margolis
*/

#ifndef WStream_h
#define WStream_h
#ifndef Stream_h
#define Stream_h

#include <inttypes.h>
#include "Print.h"
Expand All @@ -36,7 +36,7 @@
readBytesBetween( pre_string, terminator, buffer, length)
*/

class WStream : public Print
class Stream : public Print
{
private:
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
Expand All @@ -51,7 +51,7 @@ class WStream : public Print
virtual int peek() = 0;
virtual void flush() = 0;

WStream() {_timeout=1000;}
Stream() {_timeout=1000;}

// parsing methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ extern void mbed_set_unbuffered_stream(FILE *_file);
extern int mbed_getc(FILE *_file);
extern char* mbed_gets(char *s, int size, FILE *_file);

class Stream : public FileLike {
class MStream : public FileLike {

public:
Stream(const char *name=NULL);
virtual ~Stream();
MStream(const char *name=NULL);
virtual ~MStream();

int putc(int c);
int puts(const char *s);
Expand Down Expand Up @@ -59,8 +59,8 @@ class Stream : public FileLike {

/* disallow copy constructor and assignment operators */
private:
Stream(const Stream&);
Stream & operator = (const Stream&);
MStream(const MStream&);
MStream & operator = (const MStream&);
};

} // namespace mbed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#if DEVICE_SERIAL

#include "Stream.h"
#include "MStream.h"
#include "SerialBase.h"
#include "serial_api.h"

Expand All @@ -44,7 +44,7 @@ namespace mbed {
* }
* @endcode
*/
class Serial : public SerialBase, public Stream {
class Serial : public SerialBase, public MStream {

public:
#if DEVICE_SERIAL_ASYNCH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#if DEVICE_SERIAL

#include "Stream.h"
#include "MStream.h"
#include "FunctionPointer.h"
#include "serial_api.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Stream.h"
#include "MStream.h"

namespace mbed {

Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
MStream::MStream(const char *name) : FileLike(name), _file(NULL) {
/* open ourselves */
char buf[12]; /* :0x12345678 + null byte */
std::sprintf(buf, ":%p", this);
_file = std::fopen(buf, "w+");
mbed_set_unbuffered_stream(_file);
}

Stream::~Stream() {
MStream::~MStream() {
fclose(_file);
}

int Stream::putc(int c) {
int MStream::putc(int c) {
fflush(_file);
return std::fputc(c, _file);
}
int Stream::puts(const char *s) {
int MStream::puts(const char *s) {
fflush(_file);
return std::fputs(s, _file);
}
int Stream::getc() {
int MStream::getc() {
fflush(_file);
return mbed_getc(_file);
}
char* Stream::gets(char *s, int size) {
char* MStream::gets(char *s, int size) {
fflush(_file);
return mbed_gets(s,size,_file);
}

int Stream::close() {
int MStream::close() {
return 0;
}

ssize_t Stream::write(const void* buffer, size_t length) {
ssize_t MStream::write(const void* buffer, size_t length) {
const char* ptr = (const char*)buffer;
const char* end = ptr + length;
while (ptr != end) {
Expand All @@ -61,7 +61,7 @@ ssize_t Stream::write(const void* buffer, size_t length) {
return ptr - (const char*)buffer;
}

ssize_t Stream::read(void* buffer, size_t length) {
ssize_t MStream::read(void* buffer, size_t length) {
char* ptr = (char*)buffer;
char* end = ptr + length;
while (ptr != end) {
Expand All @@ -72,23 +72,23 @@ ssize_t Stream::read(void* buffer, size_t length) {
return ptr - (const char*)buffer;
}

off_t Stream::lseek(off_t offset, int whence) {
off_t MStream::lseek(off_t offset, int whence) {
return 0;
}

int Stream::isatty() {
int MStream::isatty() {
return 0;
}

int Stream::fsync() {
int MStream::fsync() {
return 0;
}

off_t Stream::flen() {
off_t MStream::flen() {
return 0;
}

int Stream::printf(const char* format, ...) {
int MStream::printf(const char* format, ...) {
std::va_list arg;
va_start(arg, format);
fflush(_file);
Expand All @@ -97,7 +97,7 @@ int Stream::printf(const char* format, ...) {
return r;
}

int Stream::scanf(const char* format, ...) {
int MStream::scanf(const char* format, ...) {
std::va_list arg;
va_start(arg, format);
fflush(_file);
Expand All @@ -106,13 +106,13 @@ int Stream::scanf(const char* format, ...) {
return r;
}

int Stream::vprintf(const char* format, std::va_list args) {
int MStream::vprintf(const char* format, std::va_list args) {
fflush(_file);
int r = vfprintf(_file, format, args);
return r;
}

int Stream::vscanf(const char* format, std::va_list args) {
int MStream::vscanf(const char* format, std::va_list args) {
fflush(_file);
int r = vfscanf(_file, format, args);
return r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace mbed {

Serial::Serial(PinName tx, PinName rx, const char *name) : SerialBase(tx, rx), Stream(name) {
Serial::Serial(PinName tx, PinName rx, const char *name) : SerialBase(tx, rx), MStream(name) {
}

int Serial::_getc() {
Expand Down
4 changes: 2 additions & 2 deletions arduino-1.6.x/hardware/RBL/RBL_nRF51822/libraries/Wire/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#ifndef _WIRE_H_
#define _WIRE_H_

#include "WStream.h"
#include "Stream.h"
#include "Arduino.h"


Expand All @@ -41,7 +41,7 @@
#define TWI_FREQUENCY_400K 400000


class TwoWire : public WStream
class TwoWire : public Stream
{
public :
enum TwoWireStatus {
Expand Down