Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Sun Nov 04 23:12:38 2018 +0000
Revision:
1:31c7b4ab552b
Parent:
0:4afd4940a189
Child:
3:b1583f309b43
Project updated with the skeleton code

Who changed what in which revision?

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