Skip to content

Commit cb5a3cc

Browse files
committed
Implement Print::availableForWrite
This was added in arduino/Arduino#5789 Not implemented yet for USB serial, as that is harder
1 parent 4179f52 commit cb5a3cc

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

pic32/cores/pic32/HardwareSerial.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,16 @@ size_t HardwareSerial::write(uint8_t theChar)
624624
return 1;
625625
}
626626

627+
// Hardware serial has a buffer of length 1
628+
int HardwareSerial::availableForWrite() {
629+
if (uart->uxSta.reg & (1 << _UARTSTA_UTXBF)) {
630+
return 0;
631+
}
632+
else {
633+
return 1;
634+
}
635+
}
636+
627637
// Hardware serial is always connected regardless.
628638
HardwareSerial::operator int() {
629639
return 1;

pic32/cores/pic32/HardwareSerial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class HardwareSerial : public Stream
122122
void begin(unsigned long baudRate, uint8_t address);
123123
void end();
124124
virtual int available(void);
125+
virtual int availableForWrite();
125126
virtual int peek();
126127
virtual int read(void);
127128
virtual void flush(void);

pic32/cores/pic32/Print.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class Print
4242
void setWriteError(int err = 1) { write_error = err; }
4343
public:
4444
Print() : write_error(0) {}
45-
45+
4646
int getWriteError() { return write_error; }
4747
void clearWriteError() { setWriteError(0); }
48-
48+
4949
virtual size_t write(uint8_t) = 0;
5050
size_t write(const char *str) {
5151
if (str == NULL) return 0;
@@ -55,7 +55,11 @@ class Print
5555
size_t write(const char *buffer, size_t size) {
5656
return write((const uint8_t *)buffer, size);
5757
}
58-
58+
59+
// default to zero, meaning "a single write may block"
60+
// should be overriden by subclasses with buffering
61+
virtual int availableForWrite() { return 0; }
62+
5963
size_t print(const __FlashStringHelper *);
6064
size_t print(const String &);
6165
size_t print(const char[]);

0 commit comments

Comments
 (0)