TinyCircuits

Please tell me how to write data to SD card with TinyArcade

Do you know how to write data to SD card with TinyArcade?
I want to save game data with SdFat library but I will fail by mistake.
If Sample.txt exists on the SD card, only 1st succeeds. 2nd will fail.
If Sample.txt does not exist, both 1st and 2nd will fail. zero byte Sample.txt is created.


#include <TinyScreen.h>
#include "SdFat.h"

TinyScreen tiny_screen = TinyScreen(TinyScreenPlus);
SdFat sd_fat;
int write_bytes1 = -1;
int write_bytes2 = -1;

void setup()
{
   // Initialize SD card
   sd_fat.begin(10, SPI_FULL_SPEED);
   // Initialize screen
   tiny_screen.begin();
   tiny_screen.setBitDepth(TSBitDepth8);
   tiny_screen.setBrightness( 8 );
   tiny_screen.setFont(liberationSansNarrow_12ptFontInfo);
   tiny_screen.fontColor(TS_8b_White, TS_8b_Black);
   // Write file 1st
   SdFile file1;
   if(file1.open("Sample.txt", O_RDWR | O_CREAT))
   {
      char text[128];
      sprintf(text, "123456 text");
      write_bytes1 = file1.write(text, 12);
      file1.close();
   }
   // Write file 2nd
   SdFile file2;
   if(file2.open("Sample.txt", O_RDWR | O_CREAT))
   {
      char text[128];
      sprintf(text, "654321 text");
      write_bytes2 = file2.write(text, 12);
      file2.close();
   }
}

void loop()
{
   char text[128];
   sprintf(text, "1st %dbytes", write_bytes1);
   tiny_screen.setCursor(0, 0);
   tiny_screen.print(text);
   sprintf(text, "2nd %dbytes", write_bytes2);
   tiny_screen.setCursor(0, 14);
   tiny_screen.print(text);
}
« Last Edit: May 18, 2017, 09:21:44 AM by kuran_kuran »