
sensor
Dependencies: BMP280 ELEC350-Practicals-FZ429 BME280
main.cpp@4:17fe5b63cd8a, 2017-11-23 (annotated)
- Committer:
- noutram
- Date:
- Thu Nov 23 14:01:42 2017 +0000
- Revision:
- 4:17fe5b63cd8a
- Parent:
- 3:22e5460a8b42
- Child:
- 5:e24f1e549da1
Uses and event queue to sample data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martinsimpson | 0:1ce5a958aaf8 | 1 | /* Access an SD Card using SPI */ |
martinsimpson | 0:1ce5a958aaf8 | 2 | |
martinsimpson | 0:1ce5a958aaf8 | 3 | #include "mbed.h" |
martinsimpson | 0:1ce5a958aaf8 | 4 | #include "SDBlockDevice.h" |
martinsimpson | 0:1ce5a958aaf8 | 5 | #include "FATFileSystem.h" |
noutram | 2:fad34c30dcc4 | 6 | #include "sample_hardware.hpp" |
noutram | 4:17fe5b63cd8a | 7 | #include "mbed_events.h" |
martinsimpson | 0:1ce5a958aaf8 | 8 | |
noutram | 2:fad34c30dcc4 | 9 | //SD Card Object |
noutram | 4:17fe5b63cd8a | 10 | SDBlockDevice sd(D11, D12, D13, D10); // mosi, miso, sclk, cs |
noutram | 4:17fe5b63cd8a | 11 | |
noutram | 4:17fe5b63cd8a | 12 | //Event queue for main |
noutram | 4:17fe5b63cd8a | 13 | EventQueue queue; |
noutram | 4:17fe5b63cd8a | 14 | |
noutram | 4:17fe5b63cd8a | 15 | //File pointer for the SD card |
noutram | 4:17fe5b63cd8a | 16 | FILE* fp; |
noutram | 4:17fe5b63cd8a | 17 | |
noutram | 4:17fe5b63cd8a | 18 | //Flag to indicate if the user has ejected the SD card |
noutram | 4:17fe5b63cd8a | 19 | bool ejected = false; |
martinsimpson | 0:1ce5a958aaf8 | 20 | |
noutram | 4:17fe5b63cd8a | 21 | //This is called every 5 seconds by the main event queue (main thread) |
noutram | 4:17fe5b63cd8a | 22 | void takeSample() |
noutram | 4:17fe5b63cd8a | 23 | { |
noutram | 4:17fe5b63cd8a | 24 | //If the card is unmounted, just return |
noutram | 4:17fe5b63cd8a | 25 | if (ejected == true) { |
noutram | 4:17fe5b63cd8a | 26 | return; |
noutram | 4:17fe5b63cd8a | 27 | } |
noutram | 4:17fe5b63cd8a | 28 | |
noutram | 4:17fe5b63cd8a | 29 | //Read Sensors (I2C) |
noutram | 4:17fe5b63cd8a | 30 | float temp = sensor.getTemperature(); |
noutram | 4:17fe5b63cd8a | 31 | float pressure = sensor.getPressure(); |
noutram | 4:17fe5b63cd8a | 32 | |
noutram | 4:17fe5b63cd8a | 33 | //Write to SD Card |
noutram | 4:17fe5b63cd8a | 34 | fprintf(fp, "%f,%f\n", temp, pressure); |
noutram | 4:17fe5b63cd8a | 35 | |
noutram | 4:17fe5b63cd8a | 36 | //Flash to indicate the sampling is alive |
noutram | 4:17fe5b63cd8a | 37 | redLED = 1; |
noutram | 4:17fe5b63cd8a | 38 | Thread::wait(100); |
noutram | 4:17fe5b63cd8a | 39 | redLED = 0; |
noutram | 4:17fe5b63cd8a | 40 | |
noutram | 4:17fe5b63cd8a | 41 | //Chheck switches - press both to eject |
noutram | 4:17fe5b63cd8a | 42 | if ((SW1 == 1) && (SW2 == 1)) { |
noutram | 4:17fe5b63cd8a | 43 | //Close File |
noutram | 4:17fe5b63cd8a | 44 | fclose(fp); |
noutram | 4:17fe5b63cd8a | 45 | |
noutram | 4:17fe5b63cd8a | 46 | //Close down SD card (flush buffers) |
noutram | 4:17fe5b63cd8a | 47 | sd.deinit(); |
noutram | 4:17fe5b63cd8a | 48 | |
noutram | 4:17fe5b63cd8a | 49 | //Let user know |
noutram | 4:17fe5b63cd8a | 50 | puts("You can now remove the SD Card\n"); |
noutram | 4:17fe5b63cd8a | 51 | yellowLED = 1; |
noutram | 4:17fe5b63cd8a | 52 | |
noutram | 4:17fe5b63cd8a | 53 | //Flag to record the event |
noutram | 4:17fe5b63cd8a | 54 | ejected = true; |
noutram | 4:17fe5b63cd8a | 55 | } |
noutram | 4:17fe5b63cd8a | 56 | } |
noutram | 4:17fe5b63cd8a | 57 | |
martinsimpson | 0:1ce5a958aaf8 | 58 | int main() |
martinsimpson | 0:1ce5a958aaf8 | 59 | { |
noutram | 2:fad34c30dcc4 | 60 | //POWER ON SELF TEST |
noutram | 2:fad34c30dcc4 | 61 | post(); |
noutram | 2:fad34c30dcc4 | 62 | |
noutram | 2:fad34c30dcc4 | 63 | printf("Initialise\n"); |
martinsimpson | 0:1ce5a958aaf8 | 64 | //FileSystemLike(*sd); |
martinsimpson | 0:1ce5a958aaf8 | 65 | |
martinsimpson | 0:1ce5a958aaf8 | 66 | // call the SDBlockDevice instance initialisation method. |
noutram | 2:fad34c30dcc4 | 67 | if ( sd.init() != 0) { |
martinsimpson | 0:1ce5a958aaf8 | 68 | printf("Init failed \n"); |
noutram | 2:fad34c30dcc4 | 69 | errorCode(FATAL); |
noutram | 2:fad34c30dcc4 | 70 | } |
martinsimpson | 0:1ce5a958aaf8 | 71 | |
noutram | 2:fad34c30dcc4 | 72 | //Create a filing system for SD Card |
martinsimpson | 0:1ce5a958aaf8 | 73 | FATFileSystem fs("sd", &sd); |
martinsimpson | 0:1ce5a958aaf8 | 74 | |
noutram | 4:17fe5b63cd8a | 75 | // ************** |
noutram | 4:17fe5b63cd8a | 76 | // Open to APPEND |
noutram | 4:17fe5b63cd8a | 77 | // ************** |
noutram | 4:17fe5b63cd8a | 78 | fp = fopen("/sd/test.txt","a"); |
noutram | 4:17fe5b63cd8a | 79 | |
noutram | 2:fad34c30dcc4 | 80 | //Check file handle (stream) |
noutram | 2:fad34c30dcc4 | 81 | if (fp == NULL) { |
martinsimpson | 0:1ce5a958aaf8 | 82 | error("Could not open file for write\n"); |
noutram | 2:fad34c30dcc4 | 83 | errorCode(FATAL); |
martinsimpson | 0:1ce5a958aaf8 | 84 | } |
martinsimpson | 0:1ce5a958aaf8 | 85 | |
noutram | 4:17fe5b63cd8a | 86 | //Set up tasks on the main thread |
noutram | 4:17fe5b63cd8a | 87 | queue.call_every(5000, takeSample); |
martinsimpson | 0:1ce5a958aaf8 | 88 | |
noutram | 4:17fe5b63cd8a | 89 | //Main queue event loop |
noutram | 4:17fe5b63cd8a | 90 | queue.dispatch(); |
martinsimpson | 0:1ce5a958aaf8 | 91 | |
noutram | 4:17fe5b63cd8a | 92 | } |