VS1053: delete & MOVE files to mSD card via USB from COMPUTE

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: VS1053: delete & MOVE files to mSD card via USB from COM

Post by DanPolka »

Ok, now I have what is likely a convoluted question, about how to get the filename out of the header structure. I could send one sketch with options of commenting out various parts to test different ways of getting/validating the filename, or I could send whole individual sketches, each trying different ways to do it.

I'll send the whole sketch with options indicated for commenting out different approaches, here in this post, but if that's too confusing, let me know and I'll send individual sketches demoing each result.

The problem seems to be that while I can use the header structure info to get & validate the data part by using headerBuf.dataBlockLen from the header,

Code: Select all

Serial.write(dataBuffer, headerBuf.dataBlockLen); // WORKS
I can't seem to pull out the headerBuf.fileName correctly.

The way that seemed most obvious to me was:

Code: Select all

Serial.write(headerBuf.fileName);
immediately after/below sending the dataBuffer , but that echoed way too much:
the filename(albeit with padded zeroes, I try to remove them later in another effort), a character 'd' that's the dummy I sent with the file to simulate checksum, AND the whole text test file!! Which shouldn't be being sent by this command, I assume.
(There is always the possibility that my terminal program screws up echo displays, but I don't think that's the case here, though I'll try to bear that in mind.)

Here's the sketch, which has to have various sections commented out to see the program flow for each different attempt to show the filename as an echo.

Code: Select all

    // now trying to do receiving MP3 files by arranging to know how many bytes of data to read

    // need to completely redo EuTerm,
    //   to configure and send header and mp3 data;

    // THEN make this sketch receive & process header & data correctly


        // header type for ALL chunks:
        typedef struct Header_t
        {
            uint32_t  syncPattern;   //4 bytes with a fixed value, e.g. 0xa5a5a5a5, marks the beginning of a header
            uint8_t   dataBlockLen;      //number of characters in data block
            char   fileName[13];  // the filename all the portions of MP3 data should be put into on SD card
            uint8_t  checksum;      //checksum of data block ; changed this from uint16 to 8 temporarily
        };
       
          Header_t headerBuf;  // this is a variable (or "structure"?) that end up containing each chunks header
                               // after it's sent by EuTerm and read here
                               
     int led = 13; // used in testing received serial transmission
     byte dataBuffer[100];  // at this time, I may only be sending 50 bytes at a time of data + filename
               
         
     void setup(){
       Serial.begin(9600);
       Serial.flush();  // just seems like a good idea???
       pinMode(led, OUTPUT);
       digitalWrite(led, LOW);  // turn it off
     } 
         
     void loop() {
       // the logic here should be something like:
       // EuTerm: get list of files to send, find first etc, get chunk of file to send, organize header info,
       //    send header, send data, wait, see if data ok, do another chunk or another file.
       // Uno sketch:  read header & data, use header info to read data, if ok send to SD card, tell EuTerm ok or not .

   
       
       if (Serial.available() > 0){
            Serial.readBytes((char*)&headerBuf, sizeof(Header_t));  // this presumably 'stops' the loop, reads until all of header in headerBuf
            if (0xa5a5a5a5 == headerBuf.syncPattern) {//make sure sync pattern matches
               digitalWrite(led, HIGH); // shows if sync pattern sent correctly
               Serial.readBytes((char*)&dataBuffer, headerBuf.dataBlockLen ); // reads all data?
               
          // THIS SENDS THE *DATA* BACK TO EuTerm FOR *VALIDATION OF RECEPTION*:
               // THIS BY ITSELF *WORKS PERFECTLY* AS LONG AS HEADER HAS 13 SPACES RESERVED FOR FILENAME AND
               // EuTerm PADS END OF FILE NAME WITH ZEROS TO MAKE IT HAVE 13 CHARACTERS. 
               
              // Serial.write(dataBuffer, headerBuf.dataBlockLen); // WORKS
        //====================================================================================
             
             //  A NUMBER OF DIFFERENT EFFORTS TO SEE THE *FILENAME*; 
             //  COMMENT OUT ALL BUT ONE AT A TIME, AS INDICATED:  
               
        //-  -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -      
              // FIRST WAY:
              // Serial.write(headerBuf.fileName);  // FIRST WAY ECHOES TOO MUCH!!
               // an echo: t1.txt0000000dFirst file is a short test file.
               // supposed to be just the text file NAME, but shows also data (contents of text file)
               // as well as the character 'd' which is the dummy checksum
        //----------------------------------------------------------------------------------     
               // FOLLOWING FOR IS FOR BOTH 2nd AND 3rd WAY, COMMENT OUT FOR FIRST, RETAIN FOR BOTH 2nd & 3rd:
               for (int n = 0; n < 12 ; n++ )  // KEEP THIS FOR BOTH 2ND AND THIRD WAY 
               {                               // AND THIS CURLY BRACE, TOO, and curly brace below 3rd way
               
                 // SECOND WAY:
                // Serial.write(headerBuf.fileName[n]);  // SECOND WAY WORKS *BY ITSELF*,
                                                       // WITH THE TRAILING ZEROES IN THE FILENAME
                 
               // THIRD WAY:  
                 if (headerBuf.fileName[n] = '.' ){  // BUT THIS, DOESN'T!
                    Serial.write(headerBuf.fileName[n]);  // I'M TRYING TO BEGIN TO CUT OUT TRAILING ZEROES                
                    break;                                // IT ECHOES THE '.',
                 }   
                 Serial.write(headerBuf.fileName[n]);   // BUT HAS *NOT* ECHOED LEADING CHARACTERS PREVIOUS TO THE '.' AS I EXPECTED (I think it should go though this if  the 'if' condition for '.' not met, and then break out of the if when '.' IS found.)               
                 
               }  // KEEP THIS CURLY BRACE FOR BOTH 2nd & 3rd WAYS, BUT NOT FIRST
               
               
            }  // sync pattern in header matches expected sync value

          }
     }     
Please let me know if it would be better to separate out each attempt into individual sketches; I'd think so, but maybe you can make sense of this better than I would think.

(Note that the reason for all the commenting out is that I only send one echo back to my terminal program at a time, so it will show the echo simply.)

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: VS1053: delete & MOVE files to mSD card via USB from COM

Post by DanPolka »

Ok, I sort of solved my problem, as the below modification of a portion of the previous sketch works to get a file name out from the header filename variable, which is:

Code: Select all

headerBuf.fileName
given that when I try to just use the header filename I also get the actual contents of the file in addition, such that

Code: Select all

Serial.write(headerBuf.fileName[n]);
yields:
eg.: t1.txt0000000dFirst file is a short test file.
where t1.txt0000000 is the padded filename, 'd' is dummy standing for checksum, and "First file is a short test file." is the actual contents of that file,
but I still don't understand why I can't just use the header filename variable itself, unless I can, but just don't know how.

This code gets rid of the padded zeroes, and puts just the filename with extent into "theFileName" array for later use in putting parts of MP3 files into SD card with correct file names:

Code: Select all

                int nn = 0;  // used to get out the filename extent past the '.' in the filename   
                for (int n = 0; n < 12 ; n++ )  // eliminates anything that's after the padded filename (shouldn't BE anything there, but apparently is, so get rid of it)
               {                               
                // gets the filename up to & including the '.'
                 theFileName[n]= headerBuf.fileName[n];
                 if (headerBuf.fileName[n] == '.' ){  
                    nn = n+1;                         // saves the position of beginning of extent
                    break;                                
                 }   
               }  
               for (int n = nn; n < nn +3; n ++){  // gets the extent
                   theFileName[n]= headerBuf.fileName[n];                  
               }
               for (int n = 0; n<13; n++){
                 Serial.write(theFileName[n]);  // shows me that I got the filename with just extent, no padding
                 
               }  

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: VS1053: delete & MOVE files to mSD card via USB from COM

Post by adafruit_support_rick »

Filename is just a string.
For instance, you can simply do this:

Code: Select all

theFile = SD.open(headerBuf.fileName);
You can also do

Code: Select all

Serial.print(headerBuf.fileName);
or

Code: Select all

Serial.write(headerBuf.filename, strlen(headerBuf.fileName));

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: VS1053: delete & MOVE files to mSD card via USB from COM

Post by DanPolka »

I was surprised, because
You can also do

Code: Select all

    Serial.print(headerBuf.fileName);
worked, and my earlier efforts to use that didn't; and they didn't because I hadn't found out how to null terminate the filename string, and hadn't at that time tried print rather than write. Thank you for persisting in trying to help me!

And

Code: Select all

theFile = SD.open(headerBuf.fileName);
will be very useful "soon"!

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”