Unix-style Fortune teller text display on LCD

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed

Committer:
alexcrepory
Date:
Mon Mar 09 02:09:20 2015 +0000
Revision:
0:672a66c015ca
Child:
1:4d5e6b8edd00
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexcrepory 0:672a66c015ca 1 #include "mbed.h"
alexcrepory 0:672a66c015ca 2 #include "SDFileSystem.h"
alexcrepory 0:672a66c015ca 3 #include "uLCD_4DGL.h"
alexcrepory 0:672a66c015ca 4
alexcrepory 0:672a66c015ca 5 SDFileSystem sd(p11, p12, p13, p14, "sd"); //sd card
alexcrepory 0:672a66c015ca 6 DigitalIn pb(p21); //pushbutton
alexcrepory 0:672a66c015ca 7 uLCD_4DGL uLCD(p9,p10,p8); // serial tx, serial rx, reset pin;
alexcrepory 0:672a66c015ca 8
alexcrepory 0:672a66c015ca 9 int main() {
alexcrepory 0:672a66c015ca 10 char buffer[300]; //buffer to store the quotation
alexcrepory 0:672a66c015ca 11 float rando=0; //variable responsible to receive the random value
alexcrepory 0:672a66c015ca 12 uLCD.cls(); //clear the screen
alexcrepory 0:672a66c015ca 13 printf("Hello\n"); //check the conection with the computer
alexcrepory 0:672a66c015ca 14
alexcrepory 0:672a66c015ca 15 mkdir("/sd/mydir", 0777); //create a folder calle mydir
alexcrepory 0:672a66c015ca 16 //Create the file with the quotations
alexcrepory 0:672a66c015ca 17 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
alexcrepory 0:672a66c015ca 18 if(fp == NULL) {
alexcrepory 0:672a66c015ca 19 error("Could not open file for write\n");
alexcrepory 0:672a66c015ca 20 }
alexcrepory 0:672a66c015ca 21 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");
alexcrepory 0:672a66c015ca 22 fclose(fp);
alexcrepory 0:672a66c015ca 23
alexcrepory 0:672a66c015ca 24 printf("Aperte o botao\n");//lets you know when the file is created
alexcrepory 0:672a66c015ca 25 while (true){
alexcrepory 0:672a66c015ca 26 if (pb == 1){
alexcrepory 0:672a66c015ca 27 //open the file to be read
alexcrepory 0:672a66c015ca 28 FILE *ft = fopen("/sd/mydir/sdtest.txt", "r+");
alexcrepory 0:672a66c015ca 29 if(ft == NULL) {
alexcrepory 0:672a66c015ca 30 error("Could not open file for write\n");
alexcrepory 0:672a66c015ca 31 }
alexcrepory 0:672a66c015ca 32
alexcrepory 0:672a66c015ca 33 rando = rand()%10+1;//random value
alexcrepory 0:672a66c015ca 34 for(int i=0; i<rando; i++){ //copy to buffer the quotation of the random value
alexcrepory 0:672a66c015ca 35 fgets(buffer, 300, ft);
alexcrepory 0:672a66c015ca 36 }
alexcrepory 0:672a66c015ca 37 uLCD.cls();
alexcrepory 0:672a66c015ca 38 uLCD.printf("%s\n", buffer); //prints in the scrren the string
alexcrepory 0:672a66c015ca 39 printf("%s\n", buffer);
alexcrepory 0:672a66c015ca 40 fclose(ft);
alexcrepory 0:672a66c015ca 41 wait(0.2);
alexcrepory 0:672a66c015ca 42 }
alexcrepory 0:672a66c015ca 43 }
alexcrepory 0:672a66c015ca 44 }