Skip to content

Commit 217975d

Browse files
pschatzmannfpistm
authored andcommitted
File inherits from Stream: proper support for print and println of all supported data type
1 parent 8e85a6d commit 217975d

File tree

4 files changed

+25
-47
lines changed

4 files changed

+25
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.swp
2+
.vscode

examples/Full/Full.ino

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ void setup()
251251
MyFile.println();
252252
MyFile.println("This should be line 6");
253253
MyFile.println(str);
254+
MyFile.print("This should be line ");
255+
MyFile.println(8);
254256
Serial.println("OK");
255257
Serial.print("Closing 'ARDUINO/SD/PRINT.txt' file");
256258
Serial.println("OK");
@@ -291,7 +293,26 @@ void setup()
291293
} else {
292294
Serial.println("KO --> Error to open 'ARDUINO/SD/WRITE.txt' file");
293295
}
296+
297+
/* Test readBytes(buf, len) method */
298+
Serial.print("Opening 'ARDUINO/SD/WRITE.txt' file...");
299+
MyFile = SD.open("ARDUINO/SD/WRITE.txt");
300+
if (MyFile) {
301+
Serial.println("OK");
302+
Serial.print(" Reading 'ARDUINO/SD/WRITE.txt' file: ");
303+
bytesread = MyFile.readBytes(rtext, MyFile.size());
304+
Serial.print(bytesread);
305+
Serial.println(" bytes read");
306+
Serial.println((const char*)rtext);
307+
Serial.print("Closing 'ARDUINO/SD/WRITE.txt' file...");
308+
MyFile.close();
309+
Serial.println("OK");
310+
} else {
311+
Serial.println("KO --> Error to open 'ARDUINO/SD/WRITE.txt' file");
312+
}
294313
Serial.println("###### End of the SD tests ######");
314+
315+
295316
}
296317

297318
void loop()

src/SD.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -547,47 +547,6 @@ size_t File::write(const uint8_t *buf, size_t size)
547547
return write((const char *)buf, size);
548548
}
549549

550-
/**
551-
* @brief Print data to the file
552-
* @param data: Data to write to the file
553-
* @retval Number of data written (1)
554-
*/
555-
size_t File::print(const char *data)
556-
{
557-
return write(data, strlen(data));
558-
}
559-
560-
/**
561-
* @brief Print data to the file
562-
* @retval Number of data written (1)
563-
*/
564-
size_t File::println()
565-
{
566-
return write("\r\n", 2);
567-
}
568-
569-
/**
570-
* @brief Print data to the file
571-
* @param data: Data to write to the file
572-
* @retval Number of data written (1)
573-
*/
574-
size_t File::println(const char *data)
575-
{
576-
size_t bytewritten = write(data, strlen(data));
577-
bytewritten += println();
578-
return bytewritten;
579-
}
580-
581-
/**
582-
* @brief Print data to the file
583-
* @param data: Data of type String to write to the file
584-
* @retval Number of data written (1)
585-
*/
586-
size_t File::println(String &data)
587-
{
588-
return println(data.c_str());
589-
}
590-
591550
/**
592551
* @brief Check if there are any bytes available for reading from the file
593552
* @retval Number of bytes available

src/STM32SD.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ uint8_t const LS_SIZE = 2;
3030
/** ls() flag for recursive list of subdirectories */
3131
uint8_t const LS_R = 4;
3232

33-
class File {
33+
class File : public Stream {
3434
public:
3535
File(FRESULT res = FR_OK);
3636
virtual size_t write(uint8_t);
@@ -57,11 +57,6 @@ class File {
5757
File openNextFile(uint8_t mode = FILE_READ);
5858
void rewindDirectory(void);
5959

60-
virtual size_t print(const char *data);
61-
virtual size_t println();
62-
virtual size_t println(const char *data);
63-
virtual size_t println(String &data);
64-
6560
// Print to Serial line
6661
void ls(uint8_t flags, uint8_t indent = 0);
6762
static void printFatDate(uint16_t fatDate);
@@ -78,6 +73,8 @@ class File {
7873
{
7974
return _res;
8075
}
76+
using Print::println;
77+
using Print::print;
8178

8279
};
8380

0 commit comments

Comments
 (0)