Skip to content

Commit

Permalink
SDtoSerial: add sendFile_interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
chipaudette committed Oct 16, 2024
1 parent f8e523d commit 8572ef5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def processLineIntoFilenames(line):

# Fourth, transfer the file itself
sendTextToSerial(serial_to_tympan, 't') #send the command to the Tympan
reply = readBytesFromSerial(serial_to_tympan,n_bytes) #get the one-line reply from the Tympan
reply = readBytesFromSerial(serial_to_tympan,n_bytes) #get the bytes sent by the Tympan
print("REPLY:",reply) #print the bytes to the screen here in Pyton


Expand Down
66 changes: 60 additions & 6 deletions src/SDtoSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,42 @@ bool SDtoSerial::sendFilenames(const char separator) {
return ret_val;
}

//set the filename that we will read from
String SDtoSerial::setFilename(const String &given_fname) {
if (isFileOpen()) file.close(); //close any file that is open
cur_filename.remove(0); //clear the current filename String
return cur_filename += given_fname; //append the given string and return
}

//receive a string as the filename
String SDtoSerial::receiveFilename(const char EOL_char) {
if (serial_ptr == nullptr) return String(""); //not ready to receive bytes from Serial
return setFilename((serial_ptr->readStringUntil(EOL_char)).trim());
}

bool SDtoSerial::open(const char *filename)
bool SDtoSerial::open(void)
{
if (state == SD_STATE_NOT_BEGUN) begin();

file.open(filename,O_READ); //open for reading
if (state == SD_STATE_NOT_BEGUN) return false; //failed to open SD
if (cur_filename.length() == 0) return false; //no filename
file.open(cur_filename.c_str(),O_READ); //open for reading
if (!isFileOpen()) return false;

state_read = READ_STATE_NORMAL;
//readHeader();

return true;
}


//send the file size as a character string (with end-of-line)
bool SDtoSerial::sendFileSize(void) {
if (file.isOpen() && serial_ptr) {
if (file.isOpen() && (serial_ptr != nullptr)) {
serial_ptr->println(getFileSize());
return true;
}
return false;
}

/*
//opens the file, sends the size in bytes (as text), then sends the raw contents of the file, then closes the file
uint64_t SDtoSerial::sendSizeThenFile(const String &filename) {
Expand All @@ -64,6 +76,48 @@ uint64_t SDtoSerial::sendSizeThenFile(const String &filename) {
//send the file
return sendFile(); //sends as raw bytes
}
*/

//automates the transfer process by sending requests and receiving requests over serial
uint64_t SDtoSerial::sendFile_interactive(void) {
if (serial_ptr == nullptr) {
Serial.println("SDtoSerial: sendFile_interactive: *** ERROR ***: Serial pointer not provided. Exiting.");
return 0;
}

//get file name from the user
Serial.println("SDtoSerial: sendFile_interactive: send filename to read from (end with newline)");
Serial.setTimeout(10000); //set timeout in milliseconds...make longer for human interaction
String fname = receiveFilename('\n'); //also sets the filename
Serial.setTimeout(1000); //change the Serial timeout time back to default

//open the file
bool isOpen = open(); //opens using the current filename
if (!isOpen) {
Serial.println("SDtoSerial: sendFile_interactive: *** ERROR ***: Cannot open file " + String(fname) + " for reading. Exiting.");
return 0;
}

//send the file size in bytes
Serial.println("SDtoSerial: sending file size in bytes (followed by newline)");
uint64_t bytes_to_send = getFileSize();
bool success = sendFileSize();
if (!success) {
Serial.println("SDtoSerial: sendFile_interactive: *** ERROR ***: Could not send file size. Exiting.");
return 0;
}

//send the file itself
Serial.println("SDtoSerial: sendFile_interactive: Sending the file bytes (after this newline)");
uint64_t bytes_sent = sendFile();
if (bytes_sent != bytes_to_send) {
Serial.println("SDtoSerial: sendFile_interactive: Only sent " + String(bytes_sent) + " bytes. File transfer has stopped.");
} else {
Serial.println("SDfromSerial: sendFile_interactive: Successfully received " + String(bytes_sent) + " bytes into " + fname + ". File transfer complete.");
}

return bytes_sent;
}

//open the file, send it, close it
uint64_t SDtoSerial::sendFile(char *filename) {
Expand Down
30 changes: 18 additions & 12 deletions src/SDtoSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@
// Alternatively, one can collapse this process:
//
// ###) User initiates the whole transfer process with one call:
// * Use SDtoSerial::sendFile(filename);
// * SDtoSerial automatically sends the file size (text, ending in newline)
// * SDtoSerial automatically starts sending the bytes (binary)
// * Use SDtoSerial::sendFile_interactive();
// * SDtoSerial automatically asks the user to send the file name (as text, ending with newline)
// * SDtoSerial automatically sends to the user the file size in bytes (as text, ending with newline)
// * SDtoSerial automatically sends to the user the file's bytes
//
// MIT License. Use at your own risk.


#include <SdFat.h> //included in Teensy install as of Teensyduino 1.54-bete3
#include <Print.h> //frpm Arduino cores

class Print_ReplaceChar; //forward declare
class Print_RemoveChar; //forward declare


class SDtoSerial {
public:
SDtoSerial(void) : sd_ptr(NULL), serial_ptr(&Serial) { init(); }
SDtoSerial(void) : sd_ptr(nullptr), serial_ptr(&Serial) { init(); }
SDtoSerial(SdFs * _sd) : sd_ptr(_sd), serial_ptr(&Serial) { init(); }
SDtoSerial(Stream *ser_ptr) : sd_ptr(NULL), serial_ptr(ser_ptr) { init(); }
SDtoSerial(Stream *ser_ptr) : sd_ptr(nullptr), serial_ptr(ser_ptr) { init(); }
SDtoSerial(SdFs * _sd, Stream *ser_ptr) : sd_ptr(_sd), serial_ptr(ser_ptr) { init(); }

enum SD_STATE {SD_STATE_BEGUN=0, SD_STATE_NOT_BEGUN=99};
Expand All @@ -50,8 +50,13 @@ class SDtoSerial {
virtual void begin(void); //begin the SD card
virtual bool sendFilenames(void) { return sendFilenames('\n'); }; //list files in the current directory
virtual bool sendFilenames(const char separator); //list files in the current directory
virtual bool open(const String &filename) { return open(filename.c_str()); } //returns true if successful
virtual bool open(const char *filename); //returns true if successful
virtual String setFilename(const String &given_fname); //close any open files and set a new filename
virtual String getFilename(void) { return cur_filename; }
virtual String receiveFilename(void) { return receiveFilename('\n'); }; //receive the filename over the serial link
virtual String receiveFilename(const char EOL_char); //receive the filename over the serial link
virtual bool open(const String &filename) { setFilename(filename); return open(); } //returns true if successful
virtual bool open(const char *filename) { return open(String(filename)); } //returns true if successful
virtual bool open(void);
virtual bool isFileOpen(void) { if ((serial_ptr != nullptr) && file.isOpen()) { return true; } return false; }
virtual void close(void) { file.close(); }

Expand All @@ -65,7 +70,7 @@ class SDtoSerial {
unsigned int setBlockDelay_msec(unsigned int foo_msec) { return block_delay_msec = foo_msec; }
unsigned int getBlockDelay_msec(void) { return block_delay_msec; }

virtual uint64_t sendSizeThenFile(const String &filename); //this is the most likely method to be used
virtual uint64_t sendFile_interactive(void); //automates the transfer process by sending requests and receiving requests over serial
virtual uint64_t sendFile(const String &filename) { return sendFile(filename.c_str()); }
virtual uint64_t sendFile(char *filename); //open the file, send it, close it
virtual uint64_t sendFile(void); //sends in blocks. returns the number of bytes sent. Uses sendOneBlock()
Expand All @@ -84,6 +89,7 @@ class SDtoSerial {
uint8_t state;
uint8_t state_read;

String cur_filename = String(""); //current filename
};


Expand All @@ -102,7 +108,7 @@ class Print_ReplaceChar : public Print {
bool isActive = false;

private:
Print *print_ptr;
Print *print_ptr = nullptr;
char targ_char = 'A', replacement_char = 'A';
};

Expand All @@ -120,7 +126,7 @@ class Print_RemoveChar : public Print {
bool isActive = false;

private:
Print *print_ptr;
Print *print_ptr = nullptr;
char targ_char = 'A'; //default, will be overwritten in constructor
};

Expand Down

0 comments on commit 8572ef5

Please sign in to comment.