sd file test

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by Neil Thiessen

Files at this revision

API Documentation at this revision

Comitter:
jonah94
Date:
Wed Feb 08 03:10:58 2017 +0000
Parent:
25:444d09fee172
Commit message:
Senior design sd file

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 444d09fee172 -r 30d5cf62ddce main.cpp
--- a/main.cpp	Mon Aug 29 15:06:56 2016 +0000
+++ b/main.cpp	Wed Feb 08 03:10:58 2017 +0000
@@ -1,129 +1,106 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
+#include <string>
+#include <stdio.h>
+#include <iostream>
+#include <stdlib.h>
+#include <ctype.h>
+#include <stddef.h>
+#include <cstring>
 
-Timer timer;
-DigitalIn button(p21, PullUp);
-SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NEG_NO, 25000000);
-char buffer[4096];
+void textToText(FILE *fid){
+
+    const char s[3] = "?."; //delimiter array
+    char line[512]; //char array for storing whole line if match found
+    char *token; //for breaking question and answers into separate tokens
+    //char input[256];
+    char questionAnswer[512]; //for storing question and answer line together
+    int j = 0;
+    //pc.baud(115200);
+    
+    std::string str ("What time is it?"); //
+    char * input = new char [str.length()+1];
+    std::strcpy (input, str.c_str());
+
 
-void writeTest()
-{
-    //Test write performance by creating a 1MB file
-    printf("Testing %iB write performance...", sizeof(buffer));
-    FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
-    if (file != NULL) {
-        timer.start();
-        for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
-            if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
-                timer.stop();
-                printf("write error!\n");
-                timer.reset();
-                return;
+    while(1)
+    {
+        /*
+        //get serial data sent from matlab
+            if(pc.readable())
+            {
+                pc.gets(input, sizeof(input));
+            }
+            else
+            {
+                pc.printf("Error reading input string\n");
+                exit(EXIT_FAILURE);
+            }
+        */
+        
+        //convert question to upper case for accurate parsing        
+        while(input[j])
+        {
+            input[j] = toupper(input[j]);
+            j++;
+        }    
+            
+        //fid = fopen("robotQA.txt", "r");
+        if (fid == NULL)
+        {
+            printf("Error opening file\n");
+            exit(EXIT_FAILURE);
+        }
+    
+        while (!feof(fid))
+        {
+            while(fgets(line, sizeof(line), fid))
+            {
+                if (strstr(line, input)) //if line contains what's in <code>question</code>, store it.
+                {
+                    strncpy(questionAnswer, line, sizeof(questionAnswer));
+                }
             }
         }
-        timer.stop();
-        if (file->close())
-            printf("failed to close file!\n");
-        else
-            printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
-        timer.reset();
-    } else {
-        printf("failed to create file!\n");
-    }
-}
+        fclose(fid);
+        
+        if(strlen(questionAnswer) == 1)//if questionAnswer is empty after parsing, no answer is found
+               printf("I'm not sure. Ask/command something else");
+       
+        //split string, grab portion after '?' or '.' and send back to matlab to display
+        //grab first token
+        token = strtok(questionAnswer,s);
 
-void readTest()
-{
-    //Test read performance by reading the 1MB file created by writeTest()
-    printf("Testing %iB read performance...", sizeof(buffer));
-    FileHandle* file = sd.open("Test File.bin", O_RDONLY);
-    if (file != NULL) {
-        timer.start();
-        int iterations = 0;
-        while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
-            iterations++;
-        timer.stop();
-        if (iterations != (1048576 / sizeof(buffer)))
-            printf("read error!\n");
-        else if (file->close())
-            printf("failed to close file!\n");
-        else if (sd.remove("Test File.bin"))
-            printf("failed to delete file!\n");
-        else
-            printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
-        timer.reset();
-    } else {
-        printf("failed to open file!\n");
+        while(token != NULL)
+        {
+            token = strtok(NULL,s);
+            printf("%s\n", token);
+            //printf(token);
+        }
     }
 }
 
-int main()
-{
-    //Configure CRC, large frames, and write validation
-    sd.crc(true);
-    sd.large_frames(true);
-    sd.write_validation(true);
-
-    //Fill the buffer with random data for the write test
-    srand(time(NULL));
-    for (int i = 0; i < sizeof(buffer); i++)
-        buffer[i] = rand();
-
-    while(1) {
-        //Simple button debouncing
+int main() {
+    SDFileSystem sd(D11, D12, D13, D10, "sd");
+    FILE *fp;
+    //char str[100]; //to store the string read from SD card
+    
+    //Sometimes it takes a few attempts to mount the card succesfully.
+    while(sd.mount() != 0){
+        printf("Failed to mount SD card.\n");
         wait(0.5);
-
-        //Print the start message
-        printf("\nPress the button to perform tests: ");
-
-        //Wait for the button to be pressed
-        while(button);
-
-        //Make sure a card is present
-        if (!sd.card_present()) {
-            printf("\nNo card present!\n");
-            continue;
-        }
-
-        //Try to mount the SD card
-        printf("\nMounting SD card...");
-        if (sd.mount() != 0) {
-            printf("failed!\n");
-            continue;
-        }
-        printf("success!\n");
-
-        //Display the card type
-        printf("\tCard type: ");
-        SDFileSystem::CardType cardType = sd.card_type();
-        if (cardType == SDFileSystem::CARD_NONE)
-            printf("None\n");
-        else if (cardType == SDFileSystem::CARD_MMC)
-            printf("MMC\n");
-        else if (cardType == SDFileSystem::CARD_SD)
-            printf("SD\n");
-        else if (cardType == SDFileSystem::CARD_SDHC)
-            printf("SDHC\n");
-        else
-            printf("Unknown\n");
-
-        //Display the card capacity
-        printf("\tSectors: %u\n", sd.disk_sectors());
-        printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0);
-
-        /*//Format the card
-        printf("Formatting SD card...");
-        if (sd.format() != 0) {
-            printf("failed!\n");
-            continue;
-        }
-        printf("success!\n");*/
-
-        //Perform a read/write test
-        writeTest();
-        readTest();
-
-        //Unmount the SD card
-        sd.unmount();
+        continue;
     }
-}
+    printf("SD card mounted successfully!\n");
+ 
+    fp = fopen("/sd/qa/robotQA.txt", "r"); //Open file for reading
+    //Print error if file could not be opened for reading
+    if(fp == NULL) {
+        error("Could not open file for read\n");
+    }
+    //fscanf(fp, "%s", str); //read contents of file into character array
+    //printf("%s", str); //print out the contents of the file
+    
+    textToText(fp);
+    sd.unmount();//done, unmount the SD card
+}
\ No newline at end of file
diff -r 444d09fee172 -r 30d5cf62ddce mbed.bld
--- a/mbed.bld	Mon Aug 29 15:06:56 2016 +0000
+++ b/mbed.bld	Wed Feb 08 03:10:58 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/2241e3a39974
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/ad3be0349dc5
\ No newline at end of file