Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Final351CW_FINAL by
main.cpp@3:a88838ff33e7, 2017-12-07 (annotated)
- Committer:
- noutram
- Date:
- Thu Dec 07 15:29:33 2017 +0000
- Revision:
- 3:a88838ff33e7
- Parent:
- 2:40403785b690
- Child:
- 5:9b4844128e09
Coursework template for ELEC350 + ELEC351 2017
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 1:e1cf7663f5ff | 1 | #include "sample_hardware.hpp" |
| noutram | 3:a88838ff33e7 | 2 | #include "Networkbits.hpp" |
| noutram | 2:40403785b690 | 3 | |
| noutram | 3:a88838ff33e7 | 4 | // This is a very short demo that demonstrates all the hardware used in the coursework. |
| noutram | 3:a88838ff33e7 | 5 | // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1 |
| noutram | 0:36e89e3ed7c4 | 6 | |
| noutram | 3:a88838ff33e7 | 7 | //Threads |
| noutram | 3:a88838ff33e7 | 8 | Thread nwrkThread; |
| noutram | 1:e1cf7663f5ff | 9 | |
| noutram | 3:a88838ff33e7 | 10 | |
| noutram | 1:e1cf7663f5ff | 11 | int main() { |
| noutram | 1:e1cf7663f5ff | 12 | //Greeting |
| noutram | 3:a88838ff33e7 | 13 | printf("Testing\n\n"); |
| noutram | 1:e1cf7663f5ff | 14 | |
| noutram | 1:e1cf7663f5ff | 15 | //Power on self test |
| noutram | 1:e1cf7663f5ff | 16 | post(); |
| noutram | 1:e1cf7663f5ff | 17 | |
| noutram | 3:a88838ff33e7 | 18 | //Initialise the SD card (this needs to move) |
| noutram | 1:e1cf7663f5ff | 19 | if ( sd.init() != 0) { |
| noutram | 1:e1cf7663f5ff | 20 | printf("Init failed \n"); |
| noutram | 3:a88838ff33e7 | 21 | lcd.cls(); |
| noutram | 3:a88838ff33e7 | 22 | lcd.printf("CANNOT INIT SD"); |
| noutram | 1:e1cf7663f5ff | 23 | errorCode(FATAL); |
| noutram | 1:e1cf7663f5ff | 24 | } |
| noutram | 1:e1cf7663f5ff | 25 | |
| noutram | 1:e1cf7663f5ff | 26 | //Create a filing system for SD Card |
| noutram | 1:e1cf7663f5ff | 27 | FATFileSystem fs("sd", &sd); |
| noutram | 0:36e89e3ed7c4 | 28 | |
| noutram | 1:e1cf7663f5ff | 29 | //Open to WRITE |
| noutram | 1:e1cf7663f5ff | 30 | FILE* fp = fopen("/sd/test.csv","a"); |
| noutram | 1:e1cf7663f5ff | 31 | if (fp == NULL) { |
| noutram | 1:e1cf7663f5ff | 32 | error("Could not open file for write\n"); |
| noutram | 3:a88838ff33e7 | 33 | lcd.cls(); |
| noutram | 3:a88838ff33e7 | 34 | lcd.printf("CANNOT OPEN FILE\n\n"); |
| noutram | 1:e1cf7663f5ff | 35 | errorCode(FATAL); |
| noutram | 1:e1cf7663f5ff | 36 | } |
| noutram | 3:a88838ff33e7 | 37 | |
| noutram | 3:a88838ff33e7 | 38 | //Last message before sampling begins |
| noutram | 3:a88838ff33e7 | 39 | lcd.cls(); |
| noutram | 3:a88838ff33e7 | 40 | lcd.printf("READY\n\n"); |
| noutram | 3:a88838ff33e7 | 41 | |
| noutram | 3:a88838ff33e7 | 42 | |
| noutram | 1:e1cf7663f5ff | 43 | //Press either switch to unmount |
| noutram | 1:e1cf7663f5ff | 44 | while ((SW1 == 0) && (SW2 == 0)) { |
| noutram | 3:a88838ff33e7 | 45 | |
| noutram | 3:a88838ff33e7 | 46 | //Base loop delay |
| noutram | 3:a88838ff33e7 | 47 | wait(1.0); |
| noutram | 3:a88838ff33e7 | 48 | |
| noutram | 3:a88838ff33e7 | 49 | //Read environmental sensors |
| noutram | 0:36e89e3ed7c4 | 50 | double temp = sensor.getTemperature(); |
| noutram | 0:36e89e3ed7c4 | 51 | double pressure = sensor.getPressure(); |
| noutram | 3:a88838ff33e7 | 52 | |
| noutram | 3:a88838ff33e7 | 53 | //Write new data to LCD (not fast!) |
| noutram | 3:a88838ff33e7 | 54 | lcd.cls(); |
| noutram | 0:36e89e3ed7c4 | 55 | lcd.printf("Temp Pressure\n"); |
| noutram | 0:36e89e3ed7c4 | 56 | lcd.printf("%6.1f ",temp); |
| noutram | 0:36e89e3ed7c4 | 57 | lcd.printf("%.2f\n",pressure); |
| noutram | 1:e1cf7663f5ff | 58 | |
| noutram | 3:a88838ff33e7 | 59 | //Write to SD (potentially slow) |
| noutram | 1:e1cf7663f5ff | 60 | fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
| noutram | 1:e1cf7663f5ff | 61 | } |
| noutram | 1:e1cf7663f5ff | 62 | |
| noutram | 1:e1cf7663f5ff | 63 | //Close File |
| noutram | 1:e1cf7663f5ff | 64 | fclose(fp); |
| noutram | 1:e1cf7663f5ff | 65 | |
| noutram | 1:e1cf7663f5ff | 66 | //Close down |
| noutram | 1:e1cf7663f5ff | 67 | sd.deinit(); |
| noutram | 1:e1cf7663f5ff | 68 | printf("Unmounted...\n"); |
| noutram | 1:e1cf7663f5ff | 69 | lcd.cls(); |
| noutram | 1:e1cf7663f5ff | 70 | lcd.printf("Unmounted...\n\n"); |
| noutram | 1:e1cf7663f5ff | 71 | |
| noutram | 1:e1cf7663f5ff | 72 | //Flash to indicate goodness |
| noutram | 1:e1cf7663f5ff | 73 | while(true) { |
| noutram | 1:e1cf7663f5ff | 74 | greenLED = 1; |
| noutram | 1:e1cf7663f5ff | 75 | wait(0.5); |
| noutram | 1:e1cf7663f5ff | 76 | greenLED = 0; |
| noutram | 1:e1cf7663f5ff | 77 | wait(0.1); |
| noutram | 0:36e89e3ed7c4 | 78 | } |
| noutram | 0:36e89e3ed7c4 | 79 | } |
| noutram | 0:36e89e3ed7c4 | 80 | |
| noutram | 1:e1cf7663f5ff | 81 | |
| noutram | 1:e1cf7663f5ff | 82 |
