Skip to content

Implement Print::availableForWrite #353

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 30, 2017
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
10 changes: 10 additions & 0 deletions pic32/cores/pic32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,16 @@ size_t HardwareSerial::write(uint8_t theChar)
return 1;
}

// Hardware serial has a buffer of length 1
int HardwareSerial::availableForWrite() {
if (uart->uxSta.reg & (1 << _UARTSTA_UTXBF)) {
return 0;
}
else {
return 1;
}
}

// Hardware serial is always connected regardless.
HardwareSerial::operator int() {
return 1;
Expand Down
1 change: 1 addition & 0 deletions pic32/cores/pic32/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class HardwareSerial : public Stream
void begin(unsigned long baudRate, uint8_t address);
void end();
virtual int available(void);
virtual int availableForWrite();
virtual int peek();
virtual int read(void);
virtual void flush(void);
Expand Down
10 changes: 7 additions & 3 deletions pic32/cores/pic32/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class Print
void setWriteError(int err = 1) { write_error = err; }
public:
Print() : write_error(0) {}

int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(0); }

virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL) return 0;
Expand All @@ -55,7 +55,11 @@ class Print
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}


// default to zero, meaning "a single write may block"
// should be overriden by subclasses with buffering
virtual int availableForWrite() { return 0; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is taken straight from arduino/Arduino

size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
Expand Down