Announcement

Collapse
No announcement yet.

MicroSD Card Write

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #31
    Yeah!! This works. Thanks, Paul.
    But there is still a problem. This is the final code, but FILE_WRITE doesn't overwrite if the file exists:
    Code:
    %%Display%%.DefineResetLine ;      // *Replaced* at compile time with define for reset line connected to the display
    %%Display%%.DefineDisplaySerialx ; // *Replaced* at compile time with define the Serial Port connected to the display
    
    #include "TestSDConst.h"
    #include "SD4dPicaso.h"
    
    %%Display%%.IncludeSerial_4DLib ;          // *Replaced* at compile time with an Include the Serial Library relevant to the display
    %%Display%%.IncludeSerial_Const4D ;        // *Replaced* at compile time with an Include the Constants file relevant to the display
    %%Display%%.AssignDisplaySerialtoLibrary ; // *Replaced* at compile time with an Assign of the correct Serial port to the correct library
    
    word hndl ;
    unsigned long last = 0;
    long count = 0;
    bool read = 0;
    
    File myFile;
    
    void setup()
    {
      Serial.begin(9600) ;        // serial to USB port
    
      pinMode(RESETLINE, OUTPUT);       // Display reset pin
      %%Display%%.Toggle_Reset_On ;       // *Replaced* at compile time with correct rest on logic for the attached display
      delay(100);                       // wait for it to be recognised
      %%Display%%.Toggle_Reset_Off ;      // *Replaced* at compile time with correct rest off logic for the attached display
      delay(3000) ;                     // give display time to startup
    
      // now start display as Serial lines should have 'stabilised'
      %%Display%%.DisplaySerial.Begin_Speed ; // *Replaced* at compile time with command to start the serial port at the correct speed
      Display.TimeLimit4D = 5000 ;      // 5 second timeout on all commands
    
    
    
      Display.gfx_ScreenMode(PORTRAIT) ; // change manually if orientation change
      Display.putstr("Mounting...\n");
    
      if (!(Display.file_Mount()))
      {
        while(!(Display.file_Mount()))
        {
          Display.putstr("Drive not mounted...");
          delay(200);
          Display.gfx_Cls();
          delay(200);
        }
      }
    
      hndl = Display.file_LoadImageControl("TESTSD~1.dat", "TESTSD~1.gci", 1);
            if (!SD.begin()) //Init SD
      {
         Display.putstr("Error SD!");
      }
      Display.gfx_Cls();            //clear the screen
    }
    
    void loop()
    {
      if (!read) {
     myFile = SD.open("40.jpg", FILE_WRITE);
      while (millis() - last < 1000 ||  count== 0)     // Delay time, try to change if doesn't work
        {
          while(Serial.available() > 0)
          {
            myFile.write(Serial.read());
            last = millis();
            count++;
         }
        }
          myFile.close();
        read = 1;
      Display.putstr("DONE!");
      delay(1000);
      }
    }

    Comment


    • #32
      Hi,

      Great, thanks for letting me know.

      You can use this to remove the file if it exist's

      if(SD.exists("40.jpg")){
      SD.remove("40.jpg");
      }

      Place this code after SD.begin();

      It will delete the file if it exist's and then write a new file with your serial data.

      Place

      Comment


      • #33
        Ok, now it works
        I suppose it must be a library problem, because with SD.h it doesn't happen, does it?

        Comment


        • #34
          Hi,

          I am pretty sure you can't overwite a SD.h file either and it too has to be removed and re-written but I will check that. There is a trick in using TRUNC in the SD.h but that isn't supported with SD4dPicaso.h as the SD is attached to the Picaso processor of the display and is more a compatibilty layer between Picaso commands and the Arduino, but not quite 100% compatible.

          Best regards

          Paul

          Comment


          • #35
            Originally posted by pauleilio View Post
            Hi,
            I am pretty sure you can't overwite a SD.h file either and it too has to be removed and re-written but I will check that. There is a trick in using TRUNC in the SD.h but that isn't supported with SD4dPicaso.h as the SD is attached to the Picaso processor of the display and is more a compatibilty layer between Picaso commands and the Arduino, but not quite 100% compatible.
            Best regards
            Paul
            Yes, I suppose you're right. Well It's not problem for me to use: if exists...remove.
            Now, I only need some code to list the files into the SD card. I have checked this code, based on example for SD.h (listfiles.ino), but openNextFile() seems not to work on SD4dPicaso.h
            Code:
              
            File root;
            ...setup()...
               root = SD.open("/");
               printDirectory(root);  //prints files on root
            }
            
            void printDirectory(File dir) {
              while (true) {
                File entry =  dir.openNextFile();
                if (! entry) {
                  break;
                }
                Serial.println(entry.name());
                entry.close();
              }
            }

            Comment


            • #36
              Hi,

              Yes those functions aren't supported. This is a funcion I used as a work around

              Code:
               void printDirectory(){
                int entno;
                File entry;
                boolean scandir = true;
                while (scandir) {
                if(entno < 1){
                char *tfname = SD.getFirstFilename();
                if(tfname[0] < 48) scandir = false;
                Serial.println(tfname);
                }
                if(entno > 0){
                char *tfname = SD.getNextFilename();
                if(tfname[0] < 48) scandir = false;
                Serial.println(tfname);
                }
                entno ++;
                }
              }

              It won't give a file size, just a name but will list all the files on the SD

              Best regards

              Paul

              Comment


              • #37
                Hi,

                We could probably add a openNextFile() function in the library using the commands above without the directory changing that is in the SD.h library. I will look into it over the weekend to see if it is feasible.

                Best regards

                Paul

                Comment


                • #38
                  Originally posted by pauleilio View Post
                  Hi,

                  Yes those functions aren't supported. This is a funcion I used as a work around
                  It won't give a file size, just a name but will list all the files on the SD
                  Best regards
                  Paul
                  Great, is enough for me!!
                  "Stranger things" (Serial ghosts)
                  It works but didn't print the files list. I have had to include a Serial.print before Serial.println(tfname) to work OK (I have included a print of 'entno' variable, but also works with another variable. This is the code:
                  Code:
                  void printDirectory()
                    {
                    int entno;
                    boolean scandir = true;
                  Serial.print(entno);
                    while (scandir) {
                    if(entno < 1){
                    char *tfname = SD.getFirstFilename();
                    if(tfname[0] < 48) scandir = false;
                    Serial.println(tfname);
                    }
                    if(entno > 0){
                    char *tfname = SD.getNextFilename();
                    if(tfname[0] < 48) scandir = false;
                    Serial.println(tfname);
                    }
                    entno ++;
                    }
                  }

                  Comment


                  • #39
                    Hi,

                    openNextFile is now supported so this code now works

                    Code:
                        File root;
                        root = SD.open("/");
                        printDirectory(root);  //prints files on root
                    
                       void printDirectory(File dir) {
                       while (true) {
                        File entry =  dir.openNextFile();
                        if (! entry) {
                          break;    
                        }
                        Serial.println(entry.name());
                        entry.close();  
                    }
                    }
                    Attached Files

                    Comment


                    • #40
                      Originally posted by pauleilio View Post
                      Hi,
                      openNextFile is now supported so this code now works
                      Great job, thank you!! I´ll use it. Anyway I'm going to keep functions getFirstFilename and getNextFilename because I want to do an interactive sketch where I can list the files, delete any of them and transfer any file in the computer to SD card.
                      By the way, this is my first GCI file uploaded to SD, in my DIY 4Duino box.

                      Click image for larger version  Name:	wbgZXO.jpg Views:	1 Size:	76.6 KB ID:	63303

                      Comment


                      • #41
                        Hi

                        Very neat, glad it's worked out for you.

                        Best regards

                        Paul

                        Comment

                        Working...
                        X