Announcement

Collapse
No announcement yet.

MicroSD Card Write

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

  • pauleilio
    replied
    Hi

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

    Best regards

    Paul

    Leave a comment:


  • jlsogorb
    replied
    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

    Leave a comment:


  • pauleilio
    replied
    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

    Leave a comment:


  • jlsogorb
    replied
    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 ++;
      }
    }

    Leave a comment:


  • pauleilio
    replied
    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

    Leave a comment:


  • pauleilio
    replied
    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

    Leave a comment:


  • jlsogorb
    replied
    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();
      }
    }

    Leave a comment:


  • pauleilio
    replied
    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

    Leave a comment:


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

    Leave a comment:


  • pauleilio
    replied
    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

    Leave a comment:


  • jlsogorb
    replied
    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);
      }
    }

    Leave a comment:


  • pauleilio
    replied
    Hi,

    if you move,

    if (!SD.begin())
    {
    Serial.println("Error SD!");
    return;
    }
    else Serial.println("OK SD!");


    to below this line,


    hndl = Display.file_LoadImageControl("TESTSD~1.dat", "TESTSD~1.gci", 1);


    It should work ok for you.
    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) ;      
    // Note! The next statement will stop the sketch from running until the serial monitor is started
    //       If it is not present the monitor will be missing the initial writes
    //    while (!Serial) ;             // wait for serial to be established
     
     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
      {
        Serial.println("Error SD!");
        return;
      }
      else Serial.println("OK SD!");
    
    
      Display.gfx_Cls();            //clear the screen    */
    
      myFile = SD.open("test4.txt", FILE_WRITE);
      if (myFile) {
        Serial.print("Writing to test.txt...");
        myFile.println("testing 1, 2, 3.");
        myFile.close();
        Serial.println("done.");
      } else {
        Serial.println("error opening test.txt");
      }
    }
    
    void loop()
    {
    
    }
    Paul

    Leave a comment:


  • jlsogorb
    replied
    This is the whole code (I'm not at home now, so I'll cannot test it until tomorrow):

    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) ;      
    // Note! The next statement will stop the sketch from running until the serial monitor is started
    //       If it is not present the monitor will be missing the initial writes
    //    while (!Serial) ;             // wait for serial to be established
     if (!SD.begin())  // Init SD
      {
        Serial.println("Error SD!");
        return;
      }
      else Serial.println("OK SD!");
    
     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);
    
      Display.gfx_Cls();            //clear the screen    */
    
      myFile = SD.open("test4.txt", FILE_WRITE);
      if (myFile) {
        Serial.print("Writing to test.txt...");
        myFile.println("testing 1, 2, 3.");
        myFile.close();
        Serial.println("done.");
      } else {
        Serial.println("error opening test.txt");
      }
    }
    
    void loop()
    {
    
    }

    Leave a comment:


  • pauleilio
    replied
    Hi,

    I just tested your code and it worked fine with the SD4dPicaso library, The date and time won't be correct as the 4Duino doesn't know that. I have attached a screenshot

    Click image for larger version

Name:	screenSD4d.png
Views:	32
Size:	60.7 KB
ID:	63187
    It would be interesting to know why it failed on your 4Duino. Would you mind sending me your project that you used to test the library so I can find the reason why it failed.

    Best regards

    Paul

    Leave a comment:


  • pauleilio
    replied
    Hello,

    It worked well in testing on my 4Duino but it is experimental and was a quick conversion from Diablo to Picaso. I will test your code with my 4Duino and let you know the result.

    Best regards

    Paul

    Leave a comment:

Working...
X