PES 4 - Smart Medication Dispenser / Mbed 2 deprecated SDFileSystem_HelloWorld2

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by Neil Thiessen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 
00004 Timer timer;
00005 
00006 DigitalIn button(PC_13, PullUp);
00007 
00008 SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000);
00009 char buffer[4096];
00010 
00011 void writeTest()
00012 {
00013     //Test write performance by creating a 1MB file
00014     printf("Testing %iB write performance...", sizeof(buffer));
00015     FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
00016     if (file != NULL) {
00017         timer.start();
00018         for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
00019             if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
00020                 timer.stop();
00021                 printf("write error!\n");
00022                 timer.reset();
00023                 return;
00024             }
00025         }
00026         timer.stop();
00027         if (file->close())
00028             printf("failed to close file!\n");
00029         else
00030             printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
00031         timer.reset();
00032     } else {
00033         printf("failed to create file!\n");
00034     }
00035 }
00036 
00037 void readTest()
00038 {
00039     //Test read performance by reading the 1MB file created by writeTest()
00040     printf("Testing %iB read performance...", sizeof(buffer));
00041     FileHandle* file = sd.open("Test File.bin", O_RDONLY);
00042     if (file != NULL) {
00043         timer.start();
00044         int iterations = 0;
00045         while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
00046             iterations++;
00047         timer.stop();
00048         if (iterations != (1048576 / sizeof(buffer)))
00049             printf("read error!\n");
00050         else if (file->close())
00051             printf("failed to close file!\n");
00052         else if (sd.remove("Test File.bin"))
00053             printf("failed to delete file!\n");
00054         else
00055             printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
00056         timer.reset();
00057     } else {
00058         printf("failed to open file!\n");
00059     }
00060 }
00061 
00062 int main()
00063 {
00064     //Configure CRC, large frames, and write validation
00065     sd.crc(true);
00066     sd.large_frames(true);
00067     sd.write_validation(true);
00068 
00069     //Fill the buffer with random data for the write test
00070     srand(time(NULL));
00071     for (int i = 0; i < sizeof(buffer); i++)
00072         buffer[i] = rand();
00073 
00074     while(1) {
00075         //Simple button debouncing
00076         wait(0.5);
00077 
00078         //Print the start message
00079         printf("\nPress the button to perform tests: ");
00080 
00081         //Wait for the button to be pressed
00082         while(button);
00083 
00084         //Make sure a card is present
00085         if (!sd.card_present()) {
00086             printf("\nNo card present!\n");
00087             continue;
00088         }
00089 
00090         //Try to mount the SD card
00091         printf("\nMounting SD card...");
00092         if (sd.mount() != 0) {
00093             printf("failed!\n");
00094             continue;
00095         }
00096         printf("success!\n");
00097 
00098         //Display the card type
00099         printf("\tCard type: ");
00100         SDFileSystem::CardType cardType = sd.card_type();
00101         if (cardType == SDFileSystem::CARD_NONE)
00102             printf("None\n");
00103         else if (cardType == SDFileSystem::CARD_MMC)
00104             printf("MMC\n");
00105         else if (cardType == SDFileSystem::CARD_SD)
00106             printf("SD\n");
00107         else if (cardType == SDFileSystem::CARD_SDHC)
00108             printf("SDHC\n");
00109         else
00110             printf("Unknown\n");
00111 
00112         //Display the card capacity
00113         printf("\tSectors: %u\n", sd.disk_sectors());
00114         printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0);
00115 
00116         /*//Format the card
00117         printf("Formatting SD card...");
00118         if (sd.format() != 0) {
00119             printf("failed!\n");
00120             continue;
00121         }
00122         printf("success!\n");*/
00123 
00124         //Perform a read/write test
00125         writeTest();
00126         readTest();
00127 
00128         //Unmount the SD card
00129         sd.unmount();
00130 
00131         printf("Hello World!\n");
00132 
00133         mkdir("/sd/mydir", 0777);
00134 
00135         FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
00136         if(fp == NULL) {
00137             error("Could not open file for write\n");
00138         }
00139         fprintf(fp, "Hello fun SD Card World!");
00140         fclose(fp);
00141 
00142         printf("Goodbye World!\n");
00143 
00144     }
00145 }