Gopi Sundaresan / Mbed 2 deprecated SDFileSystem

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "string.h"
00002 #include "mbed.h"
00003 #include "SDFileSystem.h"
00004 #include "PowerControl.h"
00005 #include "EthernetPowerControl.h"
00006 AnalogIn ain(p20);
00007 DigitalOut sensor(p15);
00008 Serial pc(p28, p27);
00009 SDFileSystem sd(p5, p6, p7, p8, "sd"); 
00010 #define USR_POWERDOWN    (0x104)
00011 
00012 int semihost_powerdown() {
00013     uint32_t arg;
00014     return __semihost(USR_POWERDOWN, &arg);
00015 }
00016 Ticker sense; // Ticker to Interrupt the sleeping mode
00017 Timer t;
00018 
00019 void sd_write_read() {
00020     char data[100];
00021     int time_stamp;
00022     time_stamp=t.read();
00023     sensor=1;   /* Power up Sensor only when necessary */
00024     pc.printf("\n Writing into SD card... \n");
00025     pc.printf("\n Writing Sensor Data... \n");
00026     mkdir("/sd/mydir", 0777);
00027     /* Following code does a conditional check on analog in to determine the distance from IR Sensor */
00028     if (ain < 0.3) {
00029         strcpy(data,"Its approximately 30 cm away");
00030     } else if (ain > 0.3&&ain < 0.5) {
00031         strcpy(data,"Its approximately 20 cm away");
00032     } else if (ain>0.5 && ain < 0.6) {
00033         strcpy(data,"Its approximately 15 cm away");
00034     } else {
00035         strcpy(data,"Its approximately 10 cm away");
00036     }
00037 
00038     /* Open the File for writing into the SD Card */
00039     FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
00040     if (fp == NULL) {
00041         error("Could not open file for write\n");
00042     }
00043     fprintf(fp, "%d : %s \n",time_stamp,data);  /* Record Sensor data along with time stamp */
00044     fclose(fp);
00045     sensor=0;   /* Power down Sensor after data is logged */
00046 }
00047 
00048 int main() {
00049     int result; 
00050     PHY_PowerDown();                    /* PHY Powerdown */
00051     result = semihost_powerdown();      /* Semihost Powerdown */
00052     sense.attach(&sd_write_read, 5);    /* Excecute sd_write_read function every 5s */
00053     t.start();                          /* Start of timer to record Time Stamp */
00054     while (1) {
00055         Sleep();                        /* Sleep mode to save power */
00056     }
00057 }
00058 
00059