Unix-style Fortune teller text display on LCD

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed

main.cpp

Committer:
alexcrepory
Date:
2015-03-09
Revision:
1:4d5e6b8edd00
Parent:
0:672a66c015ca
Child:
2:7507d0c0e509

File content as of revision 1:4d5e6b8edd00:

#include "mbed.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"

SDFileSystem sd(p11, p12, p13, p14, "sd"); //sd card
DigitalIn pb(p21); //pushbutton
uLCD_4DGL uLCD(p9,p10,p8); // serial tx, serial rx, reset pin;

int main() {
    char buffer[300]; //buffer to store the quotation
    float rando=0; //variable responsible to receive the random value
    uLCD.cls(); //clear the screen
    printf("Hello\n"); //check the conection with the computer

    mkdir("/sd/mydir", 0777); //create a folder calle mydir
    //Create the file with the quotations
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    }
    fprintf(fp, "It is much harder to find a job than to keep one.\nNo act of kindness, no matter how small, is ever wasted.   -- Aesop\nDistance doesn't make you any smaller, but it does make you part of a larger picture.\nGod instructs the heart, not by ideas, but by pains and contradictions.   -- De Caussade\nYou can always tell the people that are forging the new frontier. They're the ones with arrows sticking out of their backs.\nMan belongs wherever he wants to go.   -- Wernher von Braun\nLife is too important to take seriously   -- Corky Siegel\nShe never liked zippers, she said, until she opened one in bed.\nSales tax applies.\nGod created a few perfect heads.  The rest he covered with hair.\n");
    fclose(fp);
     
    printf("Press the button\n");//lets you know when the file is created
    while (true){
        if (pb == 1){
            //open the file to be read
            FILE *ft = fopen("/sd/mydir/sdtest.txt", "r+");
            if(ft == NULL) {
                error("Could not open file for write\n");
            }
            
            rando = rand()%10+1;//random value
            for(int i=0; i<rando; i++){ //copy to buffer the quotation of the random value
                fgets(buffer, 300, ft);
            }
            uLCD.cls();
            uLCD.printf("%s\n", buffer); //prints in the scrren the string
            printf("%s\n", buffer);
            fclose(ft);
            wait(0.2); 
        }
    }
}