LCD stuff
Dependents: Coursework_Version_5 Coursework_Version_6 Coursework_Version_8
SDcard.hpp@9:fa2c79151a92, 2018-12-07 (annotated)
- Committer:
- Alix955
- Date:
- Fri Dec 07 13:07:37 2018 +0000
- Revision:
- 9:fa2c79151a92
updated;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alix955 | 9:fa2c79151a92 | 1 | #ifndef _SDCARD_ |
Alix955 | 9:fa2c79151a92 | 2 | |
Alix955 | 9:fa2c79151a92 | 3 | #define _SDCARD_ |
Alix955 | 9:fa2c79151a92 | 4 | |
Alix955 | 9:fa2c79151a92 | 5 | #include "mbed.h" |
Alix955 | 9:fa2c79151a92 | 6 | |
Alix955 | 9:fa2c79151a92 | 7 | #include "SDBlockDevice.h" |
Alix955 | 9:fa2c79151a92 | 8 | |
Alix955 | 9:fa2c79151a92 | 9 | #include "FATFileSystem.h" |
Alix955 | 9:fa2c79151a92 | 10 | |
Alix955 | 9:fa2c79151a92 | 11 | #include "sample_hardware.hpp" |
Alix955 | 9:fa2c79151a92 | 12 | |
Alix955 | 9:fa2c79151a92 | 13 | #include "messageStruct.hpp" |
Alix955 | 9:fa2c79151a92 | 14 | |
Alix955 | 9:fa2c79151a92 | 15 | |
Alix955 | 9:fa2c79151a92 | 16 | |
Alix955 | 9:fa2c79151a92 | 17 | class SDcard |
Alix955 | 9:fa2c79151a92 | 18 | |
Alix955 | 9:fa2c79151a92 | 19 | { |
Alix955 | 9:fa2c79151a92 | 20 | |
Alix955 | 9:fa2c79151a92 | 21 | private: |
Alix955 | 9:fa2c79151a92 | 22 | |
Alix955 | 9:fa2c79151a92 | 23 | |
Alix955 | 9:fa2c79151a92 | 24 | |
Alix955 | 9:fa2c79151a92 | 25 | float temp; //current temperature of sensor |
Alix955 | 9:fa2c79151a92 | 26 | |
Alix955 | 9:fa2c79151a92 | 27 | float pressure; //current pressure of sensor |
Alix955 | 9:fa2c79151a92 | 28 | |
Alix955 | 9:fa2c79151a92 | 29 | float fLDR; //current light level from LDR |
Alix955 | 9:fa2c79151a92 | 30 | |
Alix955 | 9:fa2c79151a92 | 31 | |
Alix955 | 9:fa2c79151a92 | 32 | |
Alix955 | 9:fa2c79151a92 | 33 | void update_temp(double t) //use this function to update the current temperature value |
Alix955 | 9:fa2c79151a92 | 34 | |
Alix955 | 9:fa2c79151a92 | 35 | { |
Alix955 | 9:fa2c79151a92 | 36 | |
Alix955 | 9:fa2c79151a92 | 37 | temp = t; |
Alix955 | 9:fa2c79151a92 | 38 | |
Alix955 | 9:fa2c79151a92 | 39 | } |
Alix955 | 9:fa2c79151a92 | 40 | |
Alix955 | 9:fa2c79151a92 | 41 | void update_pressure(double p) //use this function to update the current pressure value |
Alix955 | 9:fa2c79151a92 | 42 | |
Alix955 | 9:fa2c79151a92 | 43 | { |
Alix955 | 9:fa2c79151a92 | 44 | |
Alix955 | 9:fa2c79151a92 | 45 | pressure = p; |
Alix955 | 9:fa2c79151a92 | 46 | |
Alix955 | 9:fa2c79151a92 | 47 | } |
Alix955 | 9:fa2c79151a92 | 48 | |
Alix955 | 9:fa2c79151a92 | 49 | void update_LDR(double L) |
Alix955 | 9:fa2c79151a92 | 50 | |
Alix955 | 9:fa2c79151a92 | 51 | { |
Alix955 | 9:fa2c79151a92 | 52 | |
Alix955 | 9:fa2c79151a92 | 53 | fLDR = L; |
Alix955 | 9:fa2c79151a92 | 54 | |
Alix955 | 9:fa2c79151a92 | 55 | } |
Alix955 | 9:fa2c79151a92 | 56 | |
Alix955 | 9:fa2c79151a92 | 57 | |
Alix955 | 9:fa2c79151a92 | 58 | |
Alix955 | 9:fa2c79151a92 | 59 | |
Alix955 | 9:fa2c79151a92 | 60 | |
Alix955 | 9:fa2c79151a92 | 61 | public: |
Alix955 | 9:fa2c79151a92 | 62 | |
Alix955 | 9:fa2c79151a92 | 63 | |
Alix955 | 9:fa2c79151a92 | 64 | |
Alix955 | 9:fa2c79151a92 | 65 | EventQueue SDcard_Queue; |
Alix955 | 9:fa2c79151a92 | 66 | |
Alix955 | 9:fa2c79151a92 | 67 | |
Alix955 | 9:fa2c79151a92 | 68 | |
Alix955 | 9:fa2c79151a92 | 69 | SDcard(){ //constructor, |
Alix955 | 9:fa2c79151a92 | 70 | |
Alix955 | 9:fa2c79151a92 | 71 | |
Alix955 | 9:fa2c79151a92 | 72 | |
Alix955 | 9:fa2c79151a92 | 73 | // initalising the SD card |
Alix955 | 9:fa2c79151a92 | 74 | |
Alix955 | 9:fa2c79151a92 | 75 | printf("Initialise\n"); |
Alix955 | 9:fa2c79151a92 | 76 | |
Alix955 | 9:fa2c79151a92 | 77 | if ( sd.init() != 0) { |
Alix955 | 9:fa2c79151a92 | 78 | |
Alix955 | 9:fa2c79151a92 | 79 | printf("Init failed \n"); |
Alix955 | 9:fa2c79151a92 | 80 | |
Alix955 | 9:fa2c79151a92 | 81 | errorCode(FATAL); |
Alix955 | 9:fa2c79151a92 | 82 | |
Alix955 | 9:fa2c79151a92 | 83 | } |
Alix955 | 9:fa2c79151a92 | 84 | |
Alix955 | 9:fa2c79151a92 | 85 | //Create a filing system for SD Card |
Alix955 | 9:fa2c79151a92 | 86 | |
Alix955 | 9:fa2c79151a92 | 87 | FATFileSystem fs("sd", &sd); |
Alix955 | 9:fa2c79151a92 | 88 | |
Alix955 | 9:fa2c79151a92 | 89 | |
Alix955 | 9:fa2c79151a92 | 90 | |
Alix955 | 9:fa2c79151a92 | 91 | temp = 0; |
Alix955 | 9:fa2c79151a92 | 92 | |
Alix955 | 9:fa2c79151a92 | 93 | pressure = 0; |
Alix955 | 9:fa2c79151a92 | 94 | |
Alix955 | 9:fa2c79151a92 | 95 | fLDR = 0; |
Alix955 | 9:fa2c79151a92 | 96 | |
Alix955 | 9:fa2c79151a92 | 97 | } |
Alix955 | 9:fa2c79151a92 | 98 | |
Alix955 | 9:fa2c79151a92 | 99 | |
Alix955 | 9:fa2c79151a92 | 100 | |
Alix955 | 9:fa2c79151a92 | 101 | ~SDcard(){ //Deconstructor, |
Alix955 | 9:fa2c79151a92 | 102 | |
Alix955 | 9:fa2c79151a92 | 103 | |
Alix955 | 9:fa2c79151a92 | 104 | |
Alix955 | 9:fa2c79151a92 | 105 | //Close down SD card |
Alix955 | 9:fa2c79151a92 | 106 | |
Alix955 | 9:fa2c79151a92 | 107 | sd.deinit(); |
Alix955 | 9:fa2c79151a92 | 108 | |
Alix955 | 9:fa2c79151a92 | 109 | printf("Finished\n"); |
Alix955 | 9:fa2c79151a92 | 110 | |
Alix955 | 9:fa2c79151a92 | 111 | errorCode(OK); |
Alix955 | 9:fa2c79151a92 | 112 | |
Alix955 | 9:fa2c79151a92 | 113 | |
Alix955 | 9:fa2c79151a92 | 114 | |
Alix955 | 9:fa2c79151a92 | 115 | temp = 0; |
Alix955 | 9:fa2c79151a92 | 116 | |
Alix955 | 9:fa2c79151a92 | 117 | pressure = 0; |
Alix955 | 9:fa2c79151a92 | 118 | |
Alix955 | 9:fa2c79151a92 | 119 | fLDR = 0; |
Alix955 | 9:fa2c79151a92 | 120 | |
Alix955 | 9:fa2c79151a92 | 121 | } |
Alix955 | 9:fa2c79151a92 | 122 | |
Alix955 | 9:fa2c79151a92 | 123 | |
Alix955 | 9:fa2c79151a92 | 124 | |
Alix955 | 9:fa2c79151a92 | 125 | |
Alix955 | 9:fa2c79151a92 | 126 | |
Alix955 | 9:fa2c79151a92 | 127 | 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 |
Alix955 | 9:fa2c79151a92 | 128 | |
Alix955 | 9:fa2c79151a92 | 129 | { |
Alix955 | 9:fa2c79151a92 | 130 | |
Alix955 | 9:fa2c79151a92 | 131 | update_temp(msg.temp); // Include message class passing of data |
Alix955 | 9:fa2c79151a92 | 132 | |
Alix955 | 9:fa2c79151a92 | 133 | update_pressure(msg.pressure); |
Alix955 | 9:fa2c79151a92 | 134 | |
Alix955 | 9:fa2c79151a92 | 135 | update_LDR(msg.ldr); |
Alix955 | 9:fa2c79151a92 | 136 | |
Alix955 | 9:fa2c79151a92 | 137 | } |
Alix955 | 9:fa2c79151a92 | 138 | |
Alix955 | 9:fa2c79151a92 | 139 | |
Alix955 | 9:fa2c79151a92 | 140 | |
Alix955 | 9:fa2c79151a92 | 141 | |
Alix955 | 9:fa2c79151a92 | 142 | |
Alix955 | 9:fa2c79151a92 | 143 | |
Alix955 | 9:fa2c79151a92 | 144 | |
Alix955 | 9:fa2c79151a92 | 145 | void Save_Data() { |
Alix955 | 9:fa2c79151a92 | 146 | |
Alix955 | 9:fa2c79151a92 | 147 | |
Alix955 | 9:fa2c79151a92 | 148 | |
Alix955 | 9:fa2c79151a92 | 149 | FILE* fp = fopen("/sd/SensorData.csv","a"); |
Alix955 | 9:fa2c79151a92 | 150 | |
Alix955 | 9:fa2c79151a92 | 151 | |
Alix955 | 9:fa2c79151a92 | 152 | |
Alix955 | 9:fa2c79151a92 | 153 | if (fp == NULL) { |
Alix955 | 9:fa2c79151a92 | 154 | |
Alix955 | 9:fa2c79151a92 | 155 | error("Could not open file for write\n"); |
Alix955 | 9:fa2c79151a92 | 156 | |
Alix955 | 9:fa2c79151a92 | 157 | errorCode(FATAL); |
Alix955 | 9:fa2c79151a92 | 158 | |
Alix955 | 9:fa2c79151a92 | 159 | } |
Alix955 | 9:fa2c79151a92 | 160 | |
Alix955 | 9:fa2c79151a92 | 161 | |
Alix955 | 9:fa2c79151a92 | 162 | |
Alix955 | 9:fa2c79151a92 | 163 | //Storing sensor data in csv file |
Alix955 | 9:fa2c79151a92 | 164 | |
Alix955 | 9:fa2c79151a92 | 165 | fprintf(fp, " Temperature , %4.2fC , Pressure , %4.2fmbar , Lux , %4.2f \n", temp , pressure , fLDR ); |
Alix955 | 9:fa2c79151a92 | 166 | |
Alix955 | 9:fa2c79151a92 | 167 | |
Alix955 | 9:fa2c79151a92 | 168 | |
Alix955 | 9:fa2c79151a92 | 169 | |
Alix955 | 9:fa2c79151a92 | 170 | |
Alix955 | 9:fa2c79151a92 | 171 | //Close the file |
Alix955 | 9:fa2c79151a92 | 172 | |
Alix955 | 9:fa2c79151a92 | 173 | fclose("/sd/SensorData.csv","a"); |
Alix955 | 9:fa2c79151a92 | 174 | |
Alix955 | 9:fa2c79151a92 | 175 | |
Alix955 | 9:fa2c79151a92 | 176 | |
Alix955 | 9:fa2c79151a92 | 177 | |
Alix955 | 9:fa2c79151a92 | 178 | |
Alix955 | 9:fa2c79151a92 | 179 | } |
Alix955 | 9:fa2c79151a92 | 180 | |
Alix955 | 9:fa2c79151a92 | 181 | |
Alix955 | 9:fa2c79151a92 | 182 | |
Alix955 | 9:fa2c79151a92 | 183 | } |
Alix955 | 9:fa2c79151a92 | 184 | |
Alix955 | 9:fa2c79151a92 | 185 | // creating the instance SD of the class SDcard |
Alix955 | 9:fa2c79151a92 | 186 | |
Alix955 | 9:fa2c79151a92 | 187 | SDcard m_oSD; |
Alix955 | 9:fa2c79151a92 | 188 | |
Alix955 | 9:fa2c79151a92 | 189 | #endif |