/*******************************************************************************
    PocketScreen "SD Write Text File" Example
    written by Florian Keller
    last changed 20 Nov 20

    Writes a text file named 'test-write.txt' onto the SD card. If any errors
    occur, they are printed to serial monitor.
    You'll need the SdFat library (https://github.com/greiman/SdFat) to compile
    this sketch.
*******************************************************************************/

#include <PocketScreen.h>
PocketScreen pocketscreen = PocketScreen();

// include SdFat
#include <SdFat.h>
SdFat SD;

const char *text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, "
                   "sed diam nonumy eirmod tempor invidunt ut labore et dolore "
                   "magna aliquyam erat, sed diam voluptua.";

void setup() {
    pocketscreen.begin();
    
    // start serial
    // don't forget to set your serial monitor baud rate to 115200, too!
    SerialUSB.begin(115200);

    // wait until the serial monitor is open
    while (!SerialUSB)
        ;

    // open SD connection
    if (!SD.begin()) {
        // it failed. Check if there is an SD inserted, and print the appopriate error message.
        if (!pocketscreen.isSDInserted()) {
            SerialUSB.println("Error: no SD inserted.");
        } else {
            SerialUSB.println("Error: could not read SD.");
        }
        return;
    }

    // open file
    File file = SD.open("test-write.txt", FILE_WRITE);
    if (!file) {
        // unable to open file, print error
        SerialUSB.println("Error: could not open 'test-write.txt'.");
    } else {
        // write text to file
        SerialUSB.print("Writing to file...");
        file.write(text);
        //file.write(text, sizeof(text));
        // close file
        file.close();
        SerialUSB.print("Done.");
    }
}

void loop() {
    // don't do anything here
}
