Reads the text file on USB in Host mode and displays on the LCD.

Dependencies:   C12832_lcd USBHost mbed

Fork of USBHostMSD_HelloWorld by Samuel Mokrani

main.cpp

Committer:
bhakti08
Date:
2014-03-13
Revision:
9:27174ecbb331
Parent:
4:f8a5c8aa895a

File content as of revision 9:27174ecbb331:

#include "mbed.h"
#include "USBHostMSD.h"
#include "C12832_lcd.h"
#include <stdlib.h>

DigitalOut led(LED1);
DigitalOut file_open(LED2);
DigitalOut file_write(LED3);
C12832_LCD lcd;
Serial pc(USBTX,USBRX);

void msd_task(void const *) {
    lcd.cls();
    USBHostMSD msd("usb");
    char data[100];
    char data_write;
        lcd.cls();
        
        // try to connect a MSD device
        while(!msd.connect()) {
            Thread::wait(500);
        }
        lcd.cls();
        lcd.printf("Device Detected!\n");
        wait (1);
        lcd.cls();
        lcd.locate(0,0);
        
        // in a loop, append a file
        // if the device is disconnected, we try to connect it again
            
            // append a file
            FILE * fp = fopen("/usb/test1.txt", "r");
            //FILE *fp_out = fopen("/usb/out_file.txt","w");
        
            if (fp != NULL) {
                file_open = 1;
                lcd.cls();
                while (!feof(fp))
                {
                    fscanf(fp,"%s",data);
                    lcd.printf("%s",data);
                    
                }
                wait(2);
                fclose(fp);
            } else {
                lcd.cls();
                lcd.printf("File not found\n");
            }
            
            /*if(fp_out != NULL)
            {
                
                if (pc.readable())
                {
                    file_write = 1;
                    pc.scanf("%c",&data_write);
                    fprintf(fp_out,"%c ",data_write);
                    
                }
            }*/
            
            Thread::wait(500);
        
            // if device disconnected, try to connect again
            if (!msd.connected())
                lcd.printf("Connect device");
            
    
}


int main() {
    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
    pc.baud(9600);
    while(1) {
        led=!led;
        Thread::wait(500);
    }
}