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