Task 1,7,8 Working
Dependencies: BME280 BMP280 ELEC350-Coursework-2017 TextLCD
Fork of ELEC350-CWTEMPLATE-2017 by
main.cpp@5:2594b953f111, 2017-12-10 (annotated)
- Committer:
- thomasmorris
- Date:
- Sun Dec 10 17:30:00 2017 +0000
- Revision:
- 5:2594b953f111
- Parent:
- 3:a88838ff33e7
Task 1,5,8 Working
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" |
thomasmorris | 5:2594b953f111 | 3 | #include "rtos.h" |
thomasmorris | 5:2594b953f111 | 4 | #include "LED.hpp" |
thomasmorris | 5:2594b953f111 | 5 | #define SamplingTime 1 |
thomasmorris | 5:2594b953f111 | 6 | #define NotSamplingTime 0 |
thomasmorris | 5:2594b953f111 | 7 | #define TimerInterval 15 //This is in seconds |
thomasmorris | 5:2594b953f111 | 8 | |
thomasmorris | 5:2594b953f111 | 9 | Serial pc(USBTX, USBRX); |
noutram | 2:40403785b690 | 10 | |
noutram | 3:a88838ff33e7 | 11 | // This is a very short demo that demonstrates all the hardware used in the coursework. |
noutram | 3:a88838ff33e7 | 12 | // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1 |
thomasmorris | 5:2594b953f111 | 13 | //Thread ID |
thomasmorris | 5:2594b953f111 | 14 | osThreadId idMain; |
thomasmorris | 5:2594b953f111 | 15 | osThreadId id1; |
thomasmorris | 5:2594b953f111 | 16 | osThreadId id2; |
thomasmorris | 5:2594b953f111 | 17 | osThreadId id3; |
thomasmorris | 5:2594b953f111 | 18 | osThreadId id4; |
noutram | 0:36e89e3ed7c4 | 19 | |
thomasmorris | 5:2594b953f111 | 20 | LED Red_led(PE_15); |
thomasmorris | 5:2594b953f111 | 21 | LED Yellow_led(PB_10); |
thomasmorris | 5:2594b953f111 | 22 | LED Green_led(PB_11); |
thomasmorris | 5:2594b953f111 | 23 | |
thomasmorris | 5:2594b953f111 | 24 | Ticker Sample_timer; |
noutram | 3:a88838ff33e7 | 25 | //Threads |
noutram | 3:a88838ff33e7 | 26 | Thread nwrkThread; |
thomasmorris | 5:2594b953f111 | 27 | Thread t1; |
thomasmorris | 5:2594b953f111 | 28 | Thread t2; |
thomasmorris | 5:2594b953f111 | 29 | |
thomasmorris | 5:2594b953f111 | 30 | void Serial_Comms()//Thread for Serial Communications |
thomasmorris | 5:2594b953f111 | 31 | { |
thomasmorris | 5:2594b953f111 | 32 | pc.printf("Hello World \n"); |
thomasmorris | 5:2594b953f111 | 33 | while(1) |
thomasmorris | 5:2594b953f111 | 34 | { |
thomasmorris | 5:2594b953f111 | 35 | pc.printf("Test\n"); |
thomasmorris | 5:2594b953f111 | 36 | Thread::wait(1000); |
thomasmorris | 5:2594b953f111 | 37 | } |
thomasmorris | 5:2594b953f111 | 38 | } |
thomasmorris | 5:2594b953f111 | 39 | |
thomasmorris | 5:2594b953f111 | 40 | void Sample_signal_set()//Sets the Signal for when to sample the sensors |
thomasmorris | 5:2594b953f111 | 41 | { |
thomasmorris | 5:2594b953f111 | 42 | t1.signal_set(SamplingTime); |
thomasmorris | 5:2594b953f111 | 43 | } |
noutram | 3:a88838ff33e7 | 44 | |
thomasmorris | 5:2594b953f111 | 45 | void Sample()//Samples the hardware and prints the result to the LCD |
thomasmorris | 5:2594b953f111 | 46 | { |
thomasmorris | 5:2594b953f111 | 47 | while(1) |
thomasmorris | 5:2594b953f111 | 48 | { |
thomasmorris | 5:2594b953f111 | 49 | Thread::signal_wait(SamplingTime); |
thomasmorris | 5:2594b953f111 | 50 | //Read environmental sensors |
thomasmorris | 5:2594b953f111 | 51 | double temp = sensor.getTemperature(); |
thomasmorris | 5:2594b953f111 | 52 | double pressure = sensor.getPressure(); |
thomasmorris | 5:2594b953f111 | 53 | double lux = adcIn.read(); |
thomasmorris | 5:2594b953f111 | 54 | //Write new data to LCD (not fast!) |
thomasmorris | 5:2594b953f111 | 55 | lcd.cls(); |
thomasmorris | 5:2594b953f111 | 56 | lcd.printf("Temp Pres li\n"); |
thomasmorris | 5:2594b953f111 | 57 | lcd.printf("%1.1f ",temp); |
thomasmorris | 5:2594b953f111 | 58 | lcd.printf("%1.1f ",pressure); |
thomasmorris | 5:2594b953f111 | 59 | lcd.printf("%1.1f\n",lux); |
thomasmorris | 5:2594b953f111 | 60 | |
thomasmorris | 5:2594b953f111 | 61 | //Write to SD (potentially slow) |
thomasmorris | 5:2594b953f111 | 62 | //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
thomasmorris | 5:2594b953f111 | 63 | |
thomasmorris | 5:2594b953f111 | 64 | Red_led.Toggle(); |
thomasmorris | 5:2594b953f111 | 65 | t1.signal_set(NotSamplingTime); |
thomasmorris | 5:2594b953f111 | 66 | //Thread::wait(15000);//Time interval |
thomasmorris | 5:2594b953f111 | 67 | } |
thomasmorris | 5:2594b953f111 | 68 | } |
thomasmorris | 5:2594b953f111 | 69 | |
thomasmorris | 5:2594b953f111 | 70 | int main() |
thomasmorris | 5:2594b953f111 | 71 | { |
noutram | 1:e1cf7663f5ff | 72 | //Greeting |
noutram | 3:a88838ff33e7 | 73 | printf("Testing\n\n"); |
noutram | 1:e1cf7663f5ff | 74 | |
thomasmorris | 5:2594b953f111 | 75 | pc.baud(9600);//Sets the Serial Comms Baud Rate |
thomasmorris | 5:2594b953f111 | 76 | |
thomasmorris | 5:2594b953f111 | 77 | post();//Power on Self Test |
noutram | 1:e1cf7663f5ff | 78 | |
noutram | 3:a88838ff33e7 | 79 | //Initialise the SD card (this needs to move) |
noutram | 1:e1cf7663f5ff | 80 | if ( sd.init() != 0) { |
noutram | 1:e1cf7663f5ff | 81 | printf("Init failed \n"); |
noutram | 3:a88838ff33e7 | 82 | lcd.cls(); |
noutram | 3:a88838ff33e7 | 83 | lcd.printf("CANNOT INIT SD"); |
noutram | 1:e1cf7663f5ff | 84 | errorCode(FATAL); |
noutram | 1:e1cf7663f5ff | 85 | } |
noutram | 1:e1cf7663f5ff | 86 | |
noutram | 1:e1cf7663f5ff | 87 | //Create a filing system for SD Card |
noutram | 1:e1cf7663f5ff | 88 | FATFileSystem fs("sd", &sd); |
noutram | 0:36e89e3ed7c4 | 89 | |
noutram | 1:e1cf7663f5ff | 90 | //Open to WRITE |
noutram | 1:e1cf7663f5ff | 91 | FILE* fp = fopen("/sd/test.csv","a"); |
noutram | 1:e1cf7663f5ff | 92 | if (fp == NULL) { |
noutram | 1:e1cf7663f5ff | 93 | error("Could not open file for write\n"); |
noutram | 3:a88838ff33e7 | 94 | lcd.cls(); |
noutram | 3:a88838ff33e7 | 95 | lcd.printf("CANNOT OPEN FILE\n\n"); |
noutram | 1:e1cf7663f5ff | 96 | errorCode(FATAL); |
noutram | 1:e1cf7663f5ff | 97 | } |
noutram | 3:a88838ff33e7 | 98 | |
noutram | 3:a88838ff33e7 | 99 | //Last message before sampling begins |
noutram | 3:a88838ff33e7 | 100 | lcd.cls(); |
noutram | 3:a88838ff33e7 | 101 | lcd.printf("READY\n\n"); |
thomasmorris | 5:2594b953f111 | 102 | |
thomasmorris | 5:2594b953f111 | 103 | //Run interrupt |
thomasmorris | 5:2594b953f111 | 104 | Sample_timer.attach(&Sample_signal_set,TimerInterval); |
thomasmorris | 5:2594b953f111 | 105 | |
thomasmorris | 5:2594b953f111 | 106 | //Run Threads |
thomasmorris | 5:2594b953f111 | 107 | |
thomasmorris | 5:2594b953f111 | 108 | |
thomasmorris | 5:2594b953f111 | 109 | t1.start(Sample); |
thomasmorris | 5:2594b953f111 | 110 | t2.start(Serial_Comms); |
thomasmorris | 5:2594b953f111 | 111 | |
thomasmorris | 5:2594b953f111 | 112 | //Main thread ID |
thomasmorris | 5:2594b953f111 | 113 | |
thomasmorris | 5:2594b953f111 | 114 | idMain = osThreadGetId(); //CMSIS RTOS call |
thomasmorris | 5:2594b953f111 | 115 | |
thomasmorris | 5:2594b953f111 | 116 | //Thread ID |
thomasmorris | 5:2594b953f111 | 117 | id1 = t1.gettid(); |
thomasmorris | 5:2594b953f111 | 118 | id2 = t2.gettid(); |
thomasmorris | 5:2594b953f111 | 119 | |
thomasmorris | 5:2594b953f111 | 120 | |
thomasmorris | 5:2594b953f111 | 121 | //Toggle Green LED after a button has been pressed |
noutram | 1:e1cf7663f5ff | 122 | //Press either switch to unmount |
noutram | 1:e1cf7663f5ff | 123 | while ((SW1 == 0) && (SW2 == 0)) { |
thomasmorris | 5:2594b953f111 | 124 | |
noutram | 1:e1cf7663f5ff | 125 | } |
noutram | 1:e1cf7663f5ff | 126 | |
noutram | 1:e1cf7663f5ff | 127 | //Close File |
noutram | 1:e1cf7663f5ff | 128 | fclose(fp); |
noutram | 1:e1cf7663f5ff | 129 | |
noutram | 1:e1cf7663f5ff | 130 | //Close down |
noutram | 1:e1cf7663f5ff | 131 | sd.deinit(); |
noutram | 1:e1cf7663f5ff | 132 | printf("Unmounted...\n"); |
noutram | 1:e1cf7663f5ff | 133 | lcd.cls(); |
noutram | 1:e1cf7663f5ff | 134 | lcd.printf("Unmounted...\n\n"); |
noutram | 1:e1cf7663f5ff | 135 | |
noutram | 1:e1cf7663f5ff | 136 | while(true) { |
noutram | 1:e1cf7663f5ff | 137 | greenLED = 1; |
noutram | 1:e1cf7663f5ff | 138 | wait(0.5); |
noutram | 1:e1cf7663f5ff | 139 | greenLED = 0; |
noutram | 1:e1cf7663f5ff | 140 | wait(0.1); |
noutram | 0:36e89e3ed7c4 | 141 | } |
noutram | 0:36e89e3ed7c4 | 142 | } |
noutram | 0:36e89e3ed7c4 | 143 | |
noutram | 1:e1cf7663f5ff | 144 | |
noutram | 1:e1cf7663f5ff | 145 |