Latest
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
main.cpp@7:e2bf2d703867, 2018-11-07 (annotated)
- Committer:
- Swabey89
- Date:
- Wed Nov 07 19:57:27 2018 +0000
- Revision:
- 7:e2bf2d703867
- Parent:
- 6:a5394c9e5927
- Child:
- 8:c81b0ff8b822
Serial interface working - requires exception handling.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swabey89 | 7:e2bf2d703867 | 1 | #include "mbed.h" |
Swabey89 | 1:31c7b4ab552b | 2 | #include "sample_hardware.hpp" |
Swabey89 | 1:31c7b4ab552b | 3 | #include "Networkbits.hpp" |
Swabey89 | 5:956984cbe447 | 4 | #include "serial_terminal.hpp" |
Swabey89 | 6:a5394c9e5927 | 5 | #include "SDCard.hpp" |
Swabey89 | 5:956984cbe447 | 6 | #include "rtos.h" |
Swabey89 | 7:e2bf2d703867 | 7 | #include "mbed_events.h" |
Swabey89 | 5:956984cbe447 | 8 | |
Swabey89 | 7:e2bf2d703867 | 9 | EventQueue queue; |
Swabey89 | 1:31c7b4ab552b | 10 | |
Swabey89 | 1:31c7b4ab552b | 11 | //Threads |
Swabey89 | 5:956984cbe447 | 12 | //Thread nwrkThread; |
Swabey89 | 5:956984cbe447 | 13 | Thread serial_terminal; |
Swabey89 | 6:a5394c9e5927 | 14 | Thread SD_thread; |
Swabey89 | 0:4afd4940a189 | 15 | |
Swabey89 | 7:e2bf2d703867 | 16 | |
Swabey89 | 7:e2bf2d703867 | 17 | |
Swabey89 | 7:e2bf2d703867 | 18 | int main() { |
Swabey89 | 5:956984cbe447 | 19 | //Move threads into a thread init function |
Swabey89 | 5:956984cbe447 | 20 | serial_terminal.start(serialterm); |
Swabey89 | 7:e2bf2d703867 | 21 | queue.call_every(20000, SDalive); |
Swabey89 | 7:e2bf2d703867 | 22 | SD_thread.start(callback(&queue, &EventQueue::dispatch_forever)); |
Swabey89 | 5:956984cbe447 | 23 | |
Swabey89 | 1:31c7b4ab552b | 24 | //Greeting |
Swabey89 | 1:31c7b4ab552b | 25 | printf("Testing\n\n"); |
Swabey89 | 1:31c7b4ab552b | 26 | |
Swabey89 | 1:31c7b4ab552b | 27 | //Power on self test |
Swabey89 | 1:31c7b4ab552b | 28 | post(); |
Swabey89 | 1:31c7b4ab552b | 29 | |
Swabey89 | 6:a5394c9e5927 | 30 | printf("Send commands\n\r"); |
Swabey89 | 5:956984cbe447 | 31 | /* |
Swabey89 | 1:31c7b4ab552b | 32 | //Initialise the SD card (this needs to move) |
Swabey89 | 1:31c7b4ab552b | 33 | if ( sd.init() != 0) { |
Swabey89 | 1:31c7b4ab552b | 34 | printf("Init failed \n"); |
Swabey89 | 1:31c7b4ab552b | 35 | lcd.cls(); |
Swabey89 | 1:31c7b4ab552b | 36 | lcd.printf("CANNOT INIT SD"); |
Swabey89 | 1:31c7b4ab552b | 37 | errorCode(FATAL); |
Swabey89 | 1:31c7b4ab552b | 38 | } |
Swabey89 | 1:31c7b4ab552b | 39 | |
Swabey89 | 1:31c7b4ab552b | 40 | //Create a filing system for SD Card |
Swabey89 | 1:31c7b4ab552b | 41 | FATFileSystem fs("sd", &sd); |
Swabey89 | 1:31c7b4ab552b | 42 | |
Swabey89 | 1:31c7b4ab552b | 43 | //Open to WRITE |
Swabey89 | 1:31c7b4ab552b | 44 | FILE* fp = fopen("/sd/test.csv","a"); |
Swabey89 | 1:31c7b4ab552b | 45 | if (fp == NULL) { |
Swabey89 | 1:31c7b4ab552b | 46 | error("Could not open file for write\n"); |
Swabey89 | 1:31c7b4ab552b | 47 | lcd.cls(); |
Swabey89 | 1:31c7b4ab552b | 48 | lcd.printf("CANNOT OPEN FILE\n\n"); |
Swabey89 | 1:31c7b4ab552b | 49 | errorCode(FATAL); |
Swabey89 | 1:31c7b4ab552b | 50 | } |
Swabey89 | 1:31c7b4ab552b | 51 | |
Swabey89 | 1:31c7b4ab552b | 52 | //Last message before sampling begins |
Swabey89 | 1:31c7b4ab552b | 53 | lcd.cls(); |
Swabey89 | 1:31c7b4ab552b | 54 | lcd.printf("READY\n\n"); |
Swabey89 | 3:b1583f309b43 | 55 | |
Swabey89 | 1:31c7b4ab552b | 56 | |
Swabey89 | 1:31c7b4ab552b | 57 | //Press either switch to unmount |
Swabey89 | 1:31c7b4ab552b | 58 | while ((SW1 == 0) && (SW2 == 0)) { |
Swabey89 | 1:31c7b4ab552b | 59 | |
Swabey89 | 1:31c7b4ab552b | 60 | //Base loop delay |
Swabey89 | 1:31c7b4ab552b | 61 | wait(1.0); |
Swabey89 | 1:31c7b4ab552b | 62 | |
Swabey89 | 1:31c7b4ab552b | 63 | //Read environmental sensors |
Swabey89 | 1:31c7b4ab552b | 64 | double temp = sensor.getTemperature(); |
Swabey89 | 1:31c7b4ab552b | 65 | double pressure = sensor.getPressure(); |
Swabey89 | 1:31c7b4ab552b | 66 | |
Swabey89 | 1:31c7b4ab552b | 67 | //Write new data to LCD (not fast!) |
Swabey89 | 1:31c7b4ab552b | 68 | lcd.cls(); |
Swabey89 | 1:31c7b4ab552b | 69 | lcd.printf("Temp Pressure\n"); |
Swabey89 | 1:31c7b4ab552b | 70 | lcd.printf("%6.1f ",temp); |
Swabey89 | 1:31c7b4ab552b | 71 | lcd.printf("%.2f\n",pressure); |
Swabey89 | 1:31c7b4ab552b | 72 | |
Swabey89 | 1:31c7b4ab552b | 73 | //Write to SD (potentially slow) |
Swabey89 | 5:956984cbe447 | 74 | //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
Swabey89 | 1:31c7b4ab552b | 75 | } |
Swabey89 | 1:31c7b4ab552b | 76 | |
Swabey89 | 1:31c7b4ab552b | 77 | //Close File |
Swabey89 | 1:31c7b4ab552b | 78 | fclose(fp); |
Swabey89 | 1:31c7b4ab552b | 79 | |
Swabey89 | 1:31c7b4ab552b | 80 | //Close down |
Swabey89 | 1:31c7b4ab552b | 81 | sd.deinit(); |
Swabey89 | 1:31c7b4ab552b | 82 | printf("Unmounted...\n"); |
Swabey89 | 1:31c7b4ab552b | 83 | lcd.cls(); |
Swabey89 | 1:31c7b4ab552b | 84 | lcd.printf("Unmounted...\n\n"); |
Swabey89 | 5:956984cbe447 | 85 | */ |
Swabey89 | 1:31c7b4ab552b | 86 | |
Swabey89 | 1:31c7b4ab552b | 87 | //Flash to indicate goodness |
Swabey89 | 1:31c7b4ab552b | 88 | while(true) { |
Swabey89 | 5:956984cbe447 | 89 | greenLED = !greenLED; |
Swabey89 | 5:956984cbe447 | 90 | Thread::wait(500); |
Swabey89 | 0:4afd4940a189 | 91 | } |
Swabey89 | 0:4afd4940a189 | 92 | } |
Swabey89 | 1:31c7b4ab552b | 93 | |
Swabey89 | 1:31c7b4ab552b | 94 | |
Swabey89 | 1:31c7b4ab552b | 95 | |
Swabey89 | 1:31c7b4ab552b | 96 |