Nakul Dureja / Mbed 2 deprecated Mbed_Text_Reader

Dependencies:   FATFileSystem mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <mbed.h>
00002 #include <string>
00003 #include <list>
00004 #include <vector>
00005 
00006 #include <mpr121.h> // Touchpad
00007 #include "SDFileSystem.h" // SD Card
00008 #include "uLCD_4DGL.h" // uLCD
00009 
00010 
00011 //  Global Variables
00012 int wpm;            // Words Per Minute
00013 int wpmChange;
00014 int play;           // 1 -> Play  ||  0-> Pause
00015 int pause;          // Slight pause
00016 int reset;          // Reset Flag
00017 int menuX;          // Menu cursor
00018 int menuY;
00019 int numFiles;       // Number of files in dir
00020 int fileChoose;     // File choose flag
00021 
00022 
00023 
00024 
00025 // Setup the Serial to the PC for debugging
00026 Serial pc(USBTX, USBRX);
00027 
00028 
00029 // **********   LCD    **********
00030 uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
00031 
00032 
00033 // ********** Touchpad **********
00034 
00035 // Create the interrupt receiver object on pin 26
00036 InterruptIn interrupt(p26);
00037 
00038 // Setup the i2c bus on pins 9 and 10
00039 I2C i2c(p9, p10);
00040 
00041 // Setup the Mpr121:
00042 // constructor(i2c object, i2c address of the mpr121)
00043 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00044 
00045 void fallInterrupt()
00046 {
00047     double value = mpr121.read(0x00);
00048     value +=mpr121.read(0x01)<<8;
00049 
00050     if(value!=0) {
00051         int val = log(value)/log(2.0);
00052         // uLCD.printf("%i\n",val);
00053 
00054         switch(val) {
00055             case 0: // Repeat
00056                 reset = 1;
00057                 break;
00058             case 2: // Decrease speed
00059                 if(wpm>100) {
00060                     wpm -=100;
00061                     wpmChange = 1;
00062                 }
00063 
00064                 break;
00065             case 5: // Down
00066 
00067                 if(menuY<numFiles+1)
00068                     menuY++;
00069                 break;
00070             case 6: // Play/Pause
00071                 play = (play==0) ? 1 :0;
00072                 break;
00073             case 7: // Up
00074 
00075                 if(menuY>2)
00076                     menuY--;
00077                 break;
00078             case 8: // Enter
00079                 fileChoose = 1;
00080                 break;
00081             case 10: // Increase Speed
00082 
00083                 if(wpm<500) {
00084                     wpm +=100;
00085                     wpmChange = 1;
00086                 }
00087 
00088                 break;
00089 
00090             default:
00091                 break;
00092         }
00093 
00094 
00095     }
00096 
00097 }
00098 
00099 // ********** SD Card **********
00100 SDFileSystem sd(p5, p6, p7, p8, "sd");
00101 
00102 //read all directory and file names in current directory into filename vector
00103 vector<string> filenames;
00104 void read_file_names(char *dir)
00105 {
00106     DIR *dp;
00107     struct dirent *dirp;
00108     dp = opendir(dir);
00109     while((dirp = readdir(dp)) != NULL) {
00110         filenames.push_back(string(dirp->d_name));
00111     }
00112     closedir(dp);
00113 }
00114 
00115 
00116 // Function to print the words per minute on uLCD
00117 void printWPM()
00118 {
00119     uLCD.text_width(1);
00120     uLCD.text_height(2);
00121     uLCD.locate(0,7);
00122     uLCD.color(WHITE);
00123     uLCD.printf("%i wpm",wpm);
00124 }
00125 
00126 int main()
00127 {
00128 
00129     // Initializations
00130     wpm = 300;
00131     play = 1;
00132     wpmChange = 0;
00133     reset = 0;
00134     pause = 0;
00135     menuX = 15;
00136     menuY = 2;
00137     numFiles = 0;
00138     fileChoose = 0;
00139 
00140     // Check for input from the touchpad
00141     interrupt.mode(PullUp);
00142     interrupt.fall(&fallInterrupt);
00143 
00144 
00145     // Debug in console
00146     pc.printf("\nHello World\n\r");
00147 
00148     read_file_names("/sd/mydir");
00149     uLCD.color(WHITE);
00150     uLCD.printf("/sd/mydir\n\n");
00151     uLCD.color(GREEN);
00152 
00153     // Display file names in the menu
00154     for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) {
00155         uLCD.printf(" - %s\n\r",(*it).c_str());
00156         numFiles++;
00157     }
00158 
00159     // Choose the file to read
00160     while(fileChoose == 0) {
00161         uLCD.filled_rectangle(107,0,128,128,BLACK);
00162         interrupt.fall(&fallInterrupt);
00163         uLCD.locate(menuX,menuY);
00164         uLCD.color(BLUE);
00165         uLCD.printf("<--");
00166     }
00167 
00168     char fn[30] = "";
00169     int num = 0;
00170     for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) {
00171         if(num==(menuY-2))
00172             strcpy(fn,(*it).c_str());
00173         num++;
00174     }
00175 
00176     uLCD.cls();
00177 
00178 
00179     pc.printf("%s",fn);
00180 
00181     uLCD.text_width(3);
00182     uLCD.text_height(3);
00183     uLCD.locate(0,1);
00184     uLCD.color(WHITE);
00185     uLCD.printf("%s",fn);
00186     wait(2);
00187 
00188     /*
00189 
00190     // Write to file if needed
00191      pc.printf("Start writing\n\r");
00192      mkdir("/sd/mydir", 0777);
00193 
00194 
00195      FILE *fp = fopen("/sd/mydir/sdtest", "w");
00196      if(fp == NULL) {
00197          error("Could not open file for write\n");
00198      }
00199 
00200     fprintf(fp, "This is a demo for our project uLCD Text Reader. You can read at speeds between 100 words per minute to 500 words per minute. ");
00201     fprintf(fp, "You can use the MPR121 12-key touch pad to play, pause and reset the text stream. The mbed reads the text file from the microSD card. ");
00202     fprintf(fp, "Give it a try: ");
00203     fprintf(fp, "3! 2! 1! Go Jackets! ");
00204     fprintf(fp, "I'm a Ramblin' Wreck from Georgia Tech, and a hell of an engineer. ");
00205     fprintf(fp, "A helluva, helluva, Helluva, helluva, hell of an engineer. ");
00206     fprintf(fp, "Like all the jolly good fellows, I drink my whiskey clear. ");
00207     fprintf(fp, "I'm a Ramblin' Wreck from Georgia Tech and a hell of an engineer. ");
00208 
00209     fprintf(fp, "Oh! If I had a daughter, sir, I'd dress her in White and Gold, ");
00210     fprintf(fp, "And put her on the campus to cheer the brave and bold. ");
00211     fprintf(fp, "But if I had a son, sir, I'll tell you what he'd do— ");
00212     fprintf(fp, "He would yell, 'To hell with Georgia!' like his daddy used to do. ");
00213 
00214     fprintf(fp, "Oh, I wish I had a barrel of rum and sugar three thousand pounds, ");
00215     fprintf(fp, "A college bell to put it in and a clapper to stir it round. ");
00216     fprintf(fp, "I'd drink to all the good fellows who come from far and near. ");
00217     fprintf(fp, "I'm a ramblin', gamblin', hell of an engineer! ");
00218 
00219 
00220     fclose(fp);
00221     */
00222 
00223     // Open the selected file in read mode
00224     char path[] = "/sd/mydir/";
00225     strcat(path,fn);
00226     pc.printf("Start reading\n\r %s",path);
00227 
00228     FILE *fp1 = fopen(path, "r");
00229     if(fp1 == NULL) {
00230         error("Could not open file for read\n");
00231     }
00232 
00233     // Setup the display on uLCD
00234     uLCD.cls();
00235     uLCD.line(0, 82, 127, 82, WHITE);
00236     uLCD.line(0, 58, 127, 58, WHITE);
00237     uLCD.line(62, 54, 62, 62, RED);
00238     uLCD.line(63, 54, 63, 62, RED);
00239     uLCD.line(62, 80, 62, 88, RED);
00240     uLCD.line(63, 80, 63, 88, RED);
00241     printWPM();
00242 
00243 
00244     char oneword[9]; // The current temp word buffer
00245     char disp[9];    // The current word
00246     int len;        // length of the current word
00247 
00248 
00249     while(1) {
00250 
00251         // Read till the end of the file
00252         while(!feof(fp1)) {
00253 
00254             // Reser Mode
00255             if(reset == 1) {
00256                 fclose(fp1);
00257                 char path[] = "/sd/mydir/";
00258                 strcat(path,fn);
00259                 FILE *fp1 = fopen(path, "r");
00260 
00261                 if(fp1 == NULL) {
00262                     pc.printf("Start reading\n\r %s",path);
00263                     error("Could not open file for read\n");
00264                 }
00265                 reset = 0;
00266                 uLCD.text_width(3);
00267                 uLCD.text_height(3);
00268                 uLCD.locate(2,0);
00269                 uLCD.color(RED);
00270                 uLCD.printf("<<");
00271                 wait(0.5);
00272                 uLCD.filled_rectangle(0,0,128,40,BLACK);
00273             }
00274 
00275             // Pause
00276             while(play == 0) {
00277 
00278                 uLCD.text_width(3);
00279                 uLCD.text_height(3);
00280                 uLCD.locate(2,0);
00281                 uLCD.color(RED);
00282                 uLCD.printf("||");
00283                 interrupt.fall(&fallInterrupt);
00284                 printWPM();
00285                 if(reset == 1) {
00286                     fclose(fp1);
00287                     char path[] = "/sd/mydir/";
00288                     strcat(path,fn);
00289                     FILE *fp1 = fopen(path, "r");
00290                     if(fp1 == NULL) {
00291                         pc.printf("Start reading\n\r %s",path);
00292                         error("321Could not open file for read\n");
00293                     }
00294                     reset = 0;
00295                     play = 1;
00296                     uLCD.text_width(3);
00297                     uLCD.text_height(3);
00298                     uLCD.locate(2,0);
00299                     uLCD.color(RED);
00300                     uLCD.printf("<<");
00301                     wait(0.5);
00302                     uLCD.filled_rectangle(0,0,128,40,BLACK);
00303 
00304 
00305                 }
00306                 if(play==1) {
00307                     uLCD.text_width(3);
00308                     uLCD.text_height(3);
00309                     uLCD.locate(2,0);
00310                     uLCD.color(RED);
00311                     uLCD.printf("> ");
00312                     wait(0.5);
00313                     uLCD.filled_rectangle(0,0,128,40,BLACK);
00314                 }
00315             }
00316 
00317             // Change in words per minute
00318             if(wpmChange == 1) {
00319                 printWPM();
00320                 wpmChange = 0;
00321             }
00322             uLCD.text_width(2);
00323             uLCD.text_height(2);
00324             uLCD.color(WHITE);
00325             uLCD.locate(0,4);
00326 
00327             // Diplay the current word
00328 
00329             fscanf(fp1, "%s",oneword);
00330             len = strlen(oneword);
00331             //pc.printf("%i",len);
00332 
00333             for(int i = 0; i< (9-len)/2 ; i++) {
00334                 disp[i] = ' ';
00335             }
00336             for(int j = 0 , i = (9-len)/2  ; j< len ; i++) {
00337                 if(oneword[j] == '.' || oneword[j] == '!' || oneword[j] == ',') {
00338                     pause = 1;
00339                 }
00340                 disp[i] = oneword[j++];
00341             }
00342 
00343             for(int i = (9-len)/2   +  len ; i<9; i++) {
00344                 disp[i] = ' ';
00345             }
00346 
00347             for(int i=0; i<9; i++) {
00348                 if(i==4)
00349                     uLCD.color(RED);
00350                 else
00351                     uLCD.color(WHITE);
00352                 uLCD.printf("%c", disp[i]);
00353             }
00354 
00355             // Handle wait time for wpm speed and processing time
00356             // Offer slight pause in the sentence to enhance reading
00357             float temp = (pause ==1) ? (60.0/(wpm-(0.5*wpm))) : (60.0/wpm);
00358             wait(temp - 0.1);
00359             pause = 0;
00360 
00361 
00362 
00363 
00364         }
00365         fclose(fp1);
00366         interrupt.fall(&fallInterrupt);
00367     }
00368 
00369 
00370 }