newest
Dependencies: BME280 BMP280 TextLCD mbed
Fork of CW_watchdog_08012018_fran by
sd.h@4:5dbb5145d0a9, 2018-01-09 (annotated)
- Committer:
- ckalintra
- Date:
- Tue Jan 09 12:11:46 2018 +0000
- Revision:
- 4:5dbb5145d0a9
fran
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ckalintra | 4:5dbb5145d0a9 | 1 | #include "mbed.h" |
ckalintra | 4:5dbb5145d0a9 | 2 | #include "SDBlockDevice.h" |
ckalintra | 4:5dbb5145d0a9 | 3 | #include "FATFileSystem.h" |
ckalintra | 4:5dbb5145d0a9 | 4 | //#include "sample_hardware.hpp" |
ckalintra | 4:5dbb5145d0a9 | 5 | #include "mbed_events.h" |
ckalintra | 4:5dbb5145d0a9 | 6 | |
ckalintra | 4:5dbb5145d0a9 | 7 | |
ckalintra | 4:5dbb5145d0a9 | 8 | SDBlockDevice sd(D11, D12, D13, D10); |
ckalintra | 4:5dbb5145d0a9 | 9 | EventQueue eq; |
ckalintra | 4:5dbb5145d0a9 | 10 | FILE* fp; |
ckalintra | 4:5dbb5145d0a9 | 11 | bool ejected = false; |
ckalintra | 4:5dbb5145d0a9 | 12 | void takeSample() |
ckalintra | 4:5dbb5145d0a9 | 13 | { |
ckalintra | 4:5dbb5145d0a9 | 14 | if (ejected == true) { |
ckalintra | 4:5dbb5145d0a9 | 15 | return; |
ckalintra | 4:5dbb5145d0a9 | 16 | } |
ckalintra | 4:5dbb5145d0a9 | 17 | float temp = sensor.getTemperature(); |
ckalintra | 4:5dbb5145d0a9 | 18 | float pressure = sensor.getPressure(); |
ckalintra | 4:5dbb5145d0a9 | 19 | |
ckalintra | 4:5dbb5145d0a9 | 20 | fprintf(fp, "%f,%f\n", temp, pressure); |
ckalintra | 4:5dbb5145d0a9 | 21 | |
ckalintra | 4:5dbb5145d0a9 | 22 | redLED = 1; |
ckalintra | 4:5dbb5145d0a9 | 23 | Thread::wait(100); |
ckalintra | 4:5dbb5145d0a9 | 24 | redLED = 0; |
ckalintra | 4:5dbb5145d0a9 | 25 | |
ckalintra | 4:5dbb5145d0a9 | 26 | if ((SW1 == 1) && (SW2 == 1)) { |
ckalintra | 4:5dbb5145d0a9 | 27 | //Close File |
ckalintra | 4:5dbb5145d0a9 | 28 | fclose(fp); |
ckalintra | 4:5dbb5145d0a9 | 29 | |
ckalintra | 4:5dbb5145d0a9 | 30 | sd.deinit(); |
ckalintra | 4:5dbb5145d0a9 | 31 | |
ckalintra | 4:5dbb5145d0a9 | 32 | puts("You can now remove the SD Card\n"); |
ckalintra | 4:5dbb5145d0a9 | 33 | yellowLED = 1; |
ckalintra | 4:5dbb5145d0a9 | 34 | |
ckalintra | 4:5dbb5145d0a9 | 35 | ejected = true; |
ckalintra | 4:5dbb5145d0a9 | 36 | } |
ckalintra | 4:5dbb5145d0a9 | 37 | } |
ckalintra | 4:5dbb5145d0a9 | 38 | |
ckalintra | 4:5dbb5145d0a9 | 39 | int main() |
ckalintra | 4:5dbb5145d0a9 | 40 | { |
ckalintra | 4:5dbb5145d0a9 | 41 | //POWER ON SELF TEST |
ckalintra | 4:5dbb5145d0a9 | 42 | post(); |
ckalintra | 4:5dbb5145d0a9 | 43 | |
ckalintra | 4:5dbb5145d0a9 | 44 | printf("Initialise\n"); |
ckalintra | 4:5dbb5145d0a9 | 45 | //FileSystemLike(*sd); |
ckalintra | 4:5dbb5145d0a9 | 46 | |
ckalintra | 4:5dbb5145d0a9 | 47 | // call the SDBlockDevice instance initialisation method. |
ckalintra | 4:5dbb5145d0a9 | 48 | if ( sd.init() != 0) { |
ckalintra | 4:5dbb5145d0a9 | 49 | printf("Init failed \n"); |
ckalintra | 4:5dbb5145d0a9 | 50 | errorCode(FATAL); |
ckalintra | 4:5dbb5145d0a9 | 51 | } |
ckalintra | 4:5dbb5145d0a9 | 52 | |
ckalintra | 4:5dbb5145d0a9 | 53 | //Create a filing system for SD Card |
ckalintra | 4:5dbb5145d0a9 | 54 | FATFileSystem fs("sd", &sd); |
ckalintra | 4:5dbb5145d0a9 | 55 | |
ckalintra | 4:5dbb5145d0a9 | 56 | // ************** |
ckalintra | 4:5dbb5145d0a9 | 57 | // Open to APPEND |
ckalintra | 4:5dbb5145d0a9 | 58 | // ************** |
ckalintra | 4:5dbb5145d0a9 | 59 | fp = fopen("/sd/test.txt","a"); |
ckalintra | 4:5dbb5145d0a9 | 60 | |
ckalintra | 4:5dbb5145d0a9 | 61 | //Check file handle (stream) |
ckalintra | 4:5dbb5145d0a9 | 62 | if (fp == NULL) { |
ckalintra | 4:5dbb5145d0a9 | 63 | error("Could not open file for write\n"); |
ckalintra | 4:5dbb5145d0a9 | 64 | errorCode(FATAL); |
ckalintra | 4:5dbb5145d0a9 | 65 | } |
ckalintra | 4:5dbb5145d0a9 | 66 | |
ckalintra | 4:5dbb5145d0a9 | 67 | //Set up tasks on the main thread |
ckalintra | 4:5dbb5145d0a9 | 68 | queue.call_every(5000, takeSample); |
ckalintra | 4:5dbb5145d0a9 | 69 | |
ckalintra | 4:5dbb5145d0a9 | 70 | //Main queue event loop |
ckalintra | 4:5dbb5145d0a9 | 71 | queue.dispatch(); |
ckalintra | 4:5dbb5145d0a9 | 72 | |
ckalintra | 4:5dbb5145d0a9 | 73 | } |