Skip to content

Commit 8af9f76

Browse files
Add Print::availableForWrite method
Adds an availableForWrite() method to the Print class, matching current ArduinoCore-API commit 398e70f188e2b861c10d9ffe5e2bfcb6a4a4f489 . Hook availableForWrite into the SDFS filesystem (other FSes don't have this capability built-in). Fixes esp8266#7650
1 parent bbc14c0 commit 8af9f76

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

cores/esp8266/FS.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ int File::available() {
4646
return _p->size() - _p->position();
4747
}
4848

49+
int File::availableForWrite() {
50+
if (!_p)
51+
return false;
52+
53+
return _p->availableForWrite();
54+
}
55+
56+
4957
int File::read() {
5058
if (!_p)
5159
return -1;

cores/esp8266/FS.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class File : public Stream
5757
// Print methods:
5858
size_t write(uint8_t) override;
5959
size_t write(const uint8_t *buf, size_t size) override;
60+
int availableForWrite() override;
6061

6162
// Stream methods:
6263
int available() override;

cores/esp8266/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class FileImpl {
3535
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
3636
virtual size_t position() const = 0;
3737
virtual size_t size() const = 0;
38+
virtual int availableForWrite() { return 0; }
3839
virtual bool truncate(uint32_t size) = 0;
3940
virtual void close() = 0;
4041
virtual const char* name() const = 0;

cores/esp8266/Print.h

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Print {
7575
inline size_t write(char c) { return write((uint8_t) c); }
7676
inline size_t write(int8_t c) { return write((uint8_t) c); }
7777

78+
// default to zero, meaning "a single write may block"
79+
// should be overriden by subclasses with buffering
80+
virtual int availableForWrite() { return 0; }
81+
7882
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
7983
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
8084
size_t print(const __FlashStringHelper *);

libraries/SDFS/src/SDFS.h

+5
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ class SDFSFileImpl : public FileImpl
268268
close();
269269
}
270270

271+
int availableForWrite() override
272+
{
273+
return _opened ? _fd->availableForWrite() : 0;
274+
}
275+
271276
size_t write(const uint8_t *buf, size_t size) override
272277
{
273278
return _opened ? _fd->write(buf, size) : -1;

0 commit comments

Comments
 (0)