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

    Reads a text file from the SD and prints the content to the serial monitor.
    You'll need the SdFat library (https://github.com/greiman/SdFat) to compile
    this sketch. Put a file named 'test.txt' into the root directory of the SD
    card.
*******************************************************************************/

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

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

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.txt", FILE_READ);
    if (!file) {
        // unable to open file, print error
        SerialUSB.println("Error: could not open 'test.txt'.");
    } else {
        // print file content to serial
        SerialUSB.print("File content: '");
        while (file.available())
            SerialUSB.write(file.read());
        SerialUSB.println("'");
        // close file
        file.close();
    }
}

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