Coursework template
Dependencies: BMP280 TextLCD BME280
main.cpp@0:57d39f966513, 2018-11-24 (annotated)
- Committer:
- noutram
- Date:
- Sat Nov 24 09:35:51 2018 +0000
- Revision:
- 0:57d39f966513
updated for 2018;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:57d39f966513 | 1 | #include "sample_hardware.hpp" |
noutram | 0:57d39f966513 | 2 | #include "Networkbits.hpp" |
noutram | 0:57d39f966513 | 3 | |
noutram | 0:57d39f966513 | 4 | // This is a very short demo that demonstrates all the hardware used in the coursework. |
noutram | 0:57d39f966513 | 5 | // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1 |
noutram | 0:57d39f966513 | 6 | |
noutram | 0:57d39f966513 | 7 | //Threads |
noutram | 0:57d39f966513 | 8 | Thread nwrkThread; |
noutram | 0:57d39f966513 | 9 | |
noutram | 0:57d39f966513 | 10 | |
noutram | 0:57d39f966513 | 11 | int main() { |
noutram | 0:57d39f966513 | 12 | //Greeting |
noutram | 0:57d39f966513 | 13 | printf("Testing\n\n"); |
noutram | 0:57d39f966513 | 14 | |
noutram | 0:57d39f966513 | 15 | //Power on self test |
noutram | 0:57d39f966513 | 16 | post(); |
noutram | 0:57d39f966513 | 17 | |
noutram | 0:57d39f966513 | 18 | //Initialise the SD card (this needs to move) |
noutram | 0:57d39f966513 | 19 | if ( sd.init() != 0) { |
noutram | 0:57d39f966513 | 20 | printf("Init failed \n"); |
noutram | 0:57d39f966513 | 21 | lcd.cls(); |
noutram | 0:57d39f966513 | 22 | lcd.printf("CANNOT INIT SD"); |
noutram | 0:57d39f966513 | 23 | errorCode(FATAL); |
noutram | 0:57d39f966513 | 24 | } |
noutram | 0:57d39f966513 | 25 | |
noutram | 0:57d39f966513 | 26 | //Create a filing system for SD Card |
noutram | 0:57d39f966513 | 27 | FATFileSystem fs("sd", &sd); |
noutram | 0:57d39f966513 | 28 | |
noutram | 0:57d39f966513 | 29 | //Open to WRITE |
noutram | 0:57d39f966513 | 30 | FILE* fp = fopen("/sd/test.csv","a"); |
noutram | 0:57d39f966513 | 31 | if (fp == NULL) { |
noutram | 0:57d39f966513 | 32 | error("Could not open file for write\n"); |
noutram | 0:57d39f966513 | 33 | lcd.cls(); |
noutram | 0:57d39f966513 | 34 | lcd.printf("CANNOT OPEN FILE\n\n"); |
noutram | 0:57d39f966513 | 35 | errorCode(FATAL); |
noutram | 0:57d39f966513 | 36 | } |
noutram | 0:57d39f966513 | 37 | |
noutram | 0:57d39f966513 | 38 | //Last message before sampling begins |
noutram | 0:57d39f966513 | 39 | lcd.cls(); |
noutram | 0:57d39f966513 | 40 | lcd.printf("READY\n\n"); |
noutram | 0:57d39f966513 | 41 | |
noutram | 0:57d39f966513 | 42 | |
noutram | 0:57d39f966513 | 43 | //Press either switch to unmount |
noutram | 0:57d39f966513 | 44 | while ((SW1 == 0) && (SW2 == 0)) { |
noutram | 0:57d39f966513 | 45 | |
noutram | 0:57d39f966513 | 46 | //Base loop delay |
noutram | 0:57d39f966513 | 47 | wait(1.0); |
noutram | 0:57d39f966513 | 48 | |
noutram | 0:57d39f966513 | 49 | //Read environmental sensors |
noutram | 0:57d39f966513 | 50 | double temp = sensor.getTemperature(); |
noutram | 0:57d39f966513 | 51 | double pressure = sensor.getPressure(); |
noutram | 0:57d39f966513 | 52 | |
noutram | 0:57d39f966513 | 53 | //Write new data to LCD (not fast!) |
noutram | 0:57d39f966513 | 54 | lcd.cls(); |
noutram | 0:57d39f966513 | 55 | lcd.printf("Temp Pressure\n"); |
noutram | 0:57d39f966513 | 56 | lcd.printf("%6.1f ",temp); |
noutram | 0:57d39f966513 | 57 | lcd.printf("%.2f\n",pressure); |
noutram | 0:57d39f966513 | 58 | |
noutram | 0:57d39f966513 | 59 | //Write to SD (potentially slow) |
noutram | 0:57d39f966513 | 60 | fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
noutram | 0:57d39f966513 | 61 | } |
noutram | 0:57d39f966513 | 62 | |
noutram | 0:57d39f966513 | 63 | //Close File |
noutram | 0:57d39f966513 | 64 | fclose(fp); |
noutram | 0:57d39f966513 | 65 | |
noutram | 0:57d39f966513 | 66 | //Close down |
noutram | 0:57d39f966513 | 67 | sd.deinit(); |
noutram | 0:57d39f966513 | 68 | printf("Unmounted...\n"); |
noutram | 0:57d39f966513 | 69 | lcd.cls(); |
noutram | 0:57d39f966513 | 70 | lcd.printf("Unmounted...\n\n"); |
noutram | 0:57d39f966513 | 71 | |
noutram | 0:57d39f966513 | 72 | //Flash to indicate goodness |
noutram | 0:57d39f966513 | 73 | while(true) { |
noutram | 0:57d39f966513 | 74 | greenLED = 1; |
noutram | 0:57d39f966513 | 75 | wait(0.5); |
noutram | 0:57d39f966513 | 76 | greenLED = 0; |
noutram | 0:57d39f966513 | 77 | wait(0.1); |
noutram | 0:57d39f966513 | 78 | } |
noutram | 0:57d39f966513 | 79 | } |
noutram | 0:57d39f966513 | 80 | |
noutram | 0:57d39f966513 | 81 | |
noutram | 0:57d39f966513 | 82 |