Alix Germany / Mbed OS Coursework_Version_8

Dependencies:   BMP280 ELEC350-Practicals-FZ429 TextLCD BME280 ntp-client

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDcard.hpp Source File

SDcard.hpp

00001 #ifndef _SDCARD_ 
00002 #define _SDCARD_  
00003 #include "mbed.h" 
00004 #include "SDBlockDevice.h" 
00005 #include "FATFileSystem.h" 
00006 #include "sample_hardware.hpp" 
00007 
00008   
00009 
00010 class SDcard 
00011 
00012 {     
00013 
00014     private: 
00015      float temp;       //current temperature of sensor 
00016      float pressure;  //current pressure of sensor 
00017      float fLDR;      //current light level from LDR 
00018 
00019       void update_temp(double t) //use this function to update the current temperature value 
00020             { 
00021                 temp = t; 
00022             }  
00023 
00024         void update_pressure(double p) //use this function to update the current pressure value 
00025             { 
00026                 pressure = p;     
00027             }  
00028 
00029         void update_LDR(double L) 
00030             { 
00031                 fLDR = L;    
00032             } 
00033 
00034         
00035 
00036     public: 
00037     EventQueue SDcard_Queue; 
00038     
00039      SDcard(){                      //constructor, 
00040 
00041     }    
00042 
00043    
00044 
00045       ~SDcard(){                      //Deconstructor,  
00046 
00047     }
00048 
00049 
00050 
00051 
00052       void update_sensor_info(sample_message msg) //updates all current sensor information, this is called by a ticker every 5 seconds to read from the mailbox 
00053     { 
00054          update_temp(msg.temp);                  // Include message class passing of data 
00055          update_pressure(msg.pressure); 
00056          update_LDR(msg.ldr); 
00057     } 
00058     
00059 
00060 
00061 
00062     void Save_Data() {          
00063 
00064       
00065     // initalising the SD card 
00066     if ( sd.init() != 0) {
00067         printf("Init failed \n");
00068         errorCode(FATAL);
00069     }     
00070 
00071     //Create a filing system for SD Card 
00072 
00073     FATFileSystem fs("sd", &sd);
00074         
00075     FILE* fp = fopen("/sd/SensorData.csv","a");
00076 
00077     if (fp == NULL) {
00078         error("Could not open file for write\n");
00079         errorCode(FATAL);
00080     } 
00081 
00082     //Storing sensor data in csv file 
00083 
00084     fprintf(fp, " Temperature(C) , %4.2f , Pressure(mbar) , %4.2f , Lux , %4.2f \n", temp , pressure , fLDR );
00085 
00086     //Close the file
00087     fclose(fp); 
00088 
00089    //Close down SD card 
00090     sd.deinit(); 
00091     
00092     errorCode(OK);
00093     
00094         temp = 0;
00095         pressure = 0;
00096         fLDR = 0;
00097         
00098    } 
00099 }; 
00100 // creating the instance SD of the class SDcard 
00101 
00102 
00103 SDcard m_oSD; 
00104 
00105 #endif