Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
chills
Date:
Wed Dec 20 14:43:55 2017 +0000
Revision:
11:e7b5ed6cd3cf
Parent:
10:46946784326d
Child:
12:536eca338ae8
2017_12_20 14:40; Second Attempt at mail_box;

Who changed what in which revision?

UserRevisionLine numberNew 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 8:0e4481b64353 4 Current Verision 3
thomasmorris 6:97f586597310 5 Overiew: Working Tasks 1,5,7
thomasmorris 8:0e4481b64353 6
thomasmorris 8:0e4481b64353 7 Last Revision: Added DATA class structure
thomasmorris 8:0e4481b64353 8 Todo:
thomasmorris 8:0e4481b64353 9 make a mailbox of DATA class 120 long
thomasmorris 6:97f586597310 10 */
thomasmorris 6:97f586597310 11
thomasmorris 8:0e4481b64353 12 //Includes and Definitions
noutram 1:e1cf7663f5ff 13 #include "sample_hardware.hpp"
noutram 3:a88838ff33e7 14 #include "Networkbits.hpp"
thomasmorris 5:2594b953f111 15 #include "rtos.h"
thomasmorris 5:2594b953f111 16 #include "LED.hpp"
thomasmorris 8:0e4481b64353 17 #include "DATA.hpp"
thomasmorris 8:0e4481b64353 18 #include "NETWORK.hpp"
thomasmorris 5:2594b953f111 19 #define SamplingTime 1
thomasmorris 5:2594b953f111 20 #define NotSamplingTime 0
thomasmorris 7:dfe19413fdc2 21 #define Print_Time_to_LCD 1
thomasmorris 7:dfe19413fdc2 22 #define Dont_Print_Time_to_LCD 0
thomasmorris 5:2594b953f111 23 #define TimerInterval 15 //This is in seconds
thomasmorris 7:dfe19413fdc2 24 #define EDGE_RISEN 1
thomasmorris 7:dfe19413fdc2 25 #define EDGE_FALLEN 0
thomasmorris 8:0e4481b64353 26
thomasmorris 8:0e4481b64353 27
thomasmorris 5:2594b953f111 28 Serial pc(USBTX, USBRX);
thomasmorris 7:dfe19413fdc2 29 //SW1+SW2 are declared as interrupt ins in sample hardwarec.pp
thomasmorris 8:0e4481b64353 30
thomasmorris 8:0e4481b64353 31
thomasmorris 8:0e4481b64353 32 //Thread IDs
thomasmorris 5:2594b953f111 33 osThreadId idMain;
thomasmorris 5:2594b953f111 34 osThreadId id1;
thomasmorris 5:2594b953f111 35 osThreadId id2;
thomasmorris 5:2594b953f111 36 osThreadId id3;
thomasmorris 5:2594b953f111 37 osThreadId id4;
noutram 0:36e89e3ed7c4 38
thomasmorris 7:dfe19413fdc2 39 Timeout sw1TimeOut;//Used to prevent switch bounce
thomasmorris 7:dfe19413fdc2 40
thomasmorris 5:2594b953f111 41 LED Red_led(PE_15);
thomasmorris 5:2594b953f111 42 LED Yellow_led(PB_10);
thomasmorris 5:2594b953f111 43 LED Green_led(PB_11);
thomasmorris 5:2594b953f111 44
thomasmorris 8:0e4481b64353 45 //Tickers
thomasmorris 8:0e4481b64353 46
thomasmorris 5:2594b953f111 47 Ticker Sample_timer;
thomasmorris 8:0e4481b64353 48
noutram 3:a88838ff33e7 49 //Threads
thomasmorris 5:2594b953f111 50 Thread t1;
thomasmorris 5:2594b953f111 51 Thread t2;
thomasmorris 7:dfe19413fdc2 52 Thread t3;
thomasmorris 7:dfe19413fdc2 53 Thread t4;
thomasmorris 7:dfe19413fdc2 54 Thread t5;
thomasmorris 7:dfe19413fdc2 55 Thread t6;
thomasmorris 7:dfe19413fdc2 56
thomasmorris 7:dfe19413fdc2 57 double temp = 0;
thomasmorris 7:dfe19413fdc2 58 double pressure = 0;
thomasmorris 7:dfe19413fdc2 59 double lux = 0;
chills 10:46946784326d 60 string buffer_time = 0;
thomasmorris 7:dfe19413fdc2 61
thomasmorris 7:dfe19413fdc2 62 char buffer[32];
thomasmorris 7:dfe19413fdc2 63
thomasmorris 7:dfe19413fdc2 64
thomasmorris 7:dfe19413fdc2 65 void SW1FallingEdge();
thomasmorris 7:dfe19413fdc2 66 void SW1TimeOutHandler();
thomasmorris 7:dfe19413fdc2 67
thomasmorris 7:dfe19413fdc2 68 int mode = 0;
thomasmorris 7:dfe19413fdc2 69
chills 10:46946784326d 70 Mail<DATA, 120> mail_box; //Mail Queue, Type DATA, Capacity 120, name mail_box
thomasmorris 8:0e4481b64353 71
thomasmorris 8:0e4481b64353 72 void Network()
thomasmorris 8:0e4481b64353 73 {
thomasmorris 8:0e4481b64353 74 NETWORK_Print();//Runs the network
thomasmorris 8:0e4481b64353 75 //Send DATA to the network
thomasmorris 8:0e4481b64353 76 }//Interrupt service routine for handling the timeout
thomasmorris 8:0e4481b64353 77 void SW1TimeOutHandler()
thomasmorris 8:0e4481b64353 78 {
thomasmorris 7:dfe19413fdc2 79 sw1TimeOut.detach(); //Stop the timeout counter firing
thomasmorris 7:dfe19413fdc2 80 SW1.fall(&SW1FallingEdge); //Now wait for a falling edge
thomasmorris 7:dfe19413fdc2 81 }
thomasmorris 7:dfe19413fdc2 82
thomasmorris 7:dfe19413fdc2 83 //Interrupt service routive for SW1 falling edge (release)
thomasmorris 7:dfe19413fdc2 84 void SW1FallingEdge() {
thomasmorris 7:dfe19413fdc2 85 SW1.fall(NULL); //Disable this interrupt
thomasmorris 7:dfe19413fdc2 86 Yellow_led.Toggle(); //Toggle LED
thomasmorris 7:dfe19413fdc2 87
thomasmorris 7:dfe19413fdc2 88 mode = mode +1;//Cycles through modes
thomasmorris 7:dfe19413fdc2 89 if(mode >1)
thomasmorris 7:dfe19413fdc2 90 {
thomasmorris 7:dfe19413fdc2 91 mode = 0;
thomasmorris 7:dfe19413fdc2 92 }
thomasmorris 7:dfe19413fdc2 93
thomasmorris 7:dfe19413fdc2 94 sw1TimeOut.attach(&SW1TimeOutHandler, 0.2); //Start timeout counter
thomasmorris 7:dfe19413fdc2 95 }
thomasmorris 7:dfe19413fdc2 96
thomasmorris 7:dfe19413fdc2 97 void ModeSelection()
thomasmorris 7:dfe19413fdc2 98 {
thomasmorris 7:dfe19413fdc2 99 while(1){
thomasmorris 8:0e4481b64353 100 Thread::wait(1000);
thomasmorris 7:dfe19413fdc2 101 //Detech the not required interrupt then rettach it when finshed
thomasmorris 7:dfe19413fdc2 102 if(mode == 0)//Print values to the LCD
thomasmorris 7:dfe19413fdc2 103 {
thomasmorris 7:dfe19413fdc2 104 //Write new data to LCD (not fast!)
thomasmorris 7:dfe19413fdc2 105 lcd.cls();
thomasmorris 7:dfe19413fdc2 106 lcd.printf("Temp Pres li\n");
thomasmorris 7:dfe19413fdc2 107 lcd.printf("%1.1f ",temp);
thomasmorris 7:dfe19413fdc2 108 lcd.printf("%1.1f ",pressure);
thomasmorris 7:dfe19413fdc2 109 lcd.printf("%1.1f\n",lux);
thomasmorris 7:dfe19413fdc2 110 }
thomasmorris 7:dfe19413fdc2 111 else if(mode == 1)//Print the Time to the LCD
thomasmorris 7:dfe19413fdc2 112 {
thomasmorris 7:dfe19413fdc2 113 //Write new data to LCD (not fast!)
thomasmorris 7:dfe19413fdc2 114 lcd.cls();
thomasmorris 7:dfe19413fdc2 115
thomasmorris 7:dfe19413fdc2 116 lcd.printf("Current Time:%s", buffer);
thomasmorris 7:dfe19413fdc2 117
thomasmorris 7:dfe19413fdc2 118 //Write to SD (potentially slow)
thomasmorris 7:dfe19413fdc2 119 //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
thomasmorris 7:dfe19413fdc2 120 }
thomasmorris 7:dfe19413fdc2 121 else
thomasmorris 7:dfe19413fdc2 122 {
thomasmorris 7:dfe19413fdc2 123 mode = 0;
thomasmorris 7:dfe19413fdc2 124 }
thomasmorris 7:dfe19413fdc2 125 }
thomasmorris 7:dfe19413fdc2 126 }
thomasmorris 7:dfe19413fdc2 127
thomasmorris 7:dfe19413fdc2 128 void PrintTime()
thomasmorris 7:dfe19413fdc2 129 {
thomasmorris 7:dfe19413fdc2 130 Thread::signal_wait(Print_Time_to_LCD);
thomasmorris 7:dfe19413fdc2 131 while(1)
thomasmorris 7:dfe19413fdc2 132 {
thomasmorris 7:dfe19413fdc2 133 //lcd.printf("Current Time: \n %s", buffer);
thomasmorris 7:dfe19413fdc2 134 Thread::wait(1000);//Waits the thread for 1 second
thomasmorris 7:dfe19413fdc2 135 }
thomasmorris 7:dfe19413fdc2 136 }
thomasmorris 7:dfe19413fdc2 137 void Time()
thomasmorris 7:dfe19413fdc2 138 {
thomasmorris 7:dfe19413fdc2 139 while (true)
thomasmorris 7:dfe19413fdc2 140 {
thomasmorris 7:dfe19413fdc2 141 time_t seconds = time(NULL);
thomasmorris 7:dfe19413fdc2 142 //pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds);
thomasmorris 7:dfe19413fdc2 143 //pc.printf("Time as a basic string = %s", ctime(&seconds));
thomasmorris 7:dfe19413fdc2 144 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
thomasmorris 7:dfe19413fdc2 145 pc.printf("Current Time:%s", buffer);
thomasmorris 7:dfe19413fdc2 146
thomasmorris 7:dfe19413fdc2 147 Thread::wait(1000);
thomasmorris 7:dfe19413fdc2 148 }
thomasmorris 7:dfe19413fdc2 149 }
thomasmorris 7:dfe19413fdc2 150
thomasmorris 5:2594b953f111 151
thomasmorris 5:2594b953f111 152 void Serial_Comms()//Thread for Serial Communications
thomasmorris 5:2594b953f111 153 {
thomasmorris 5:2594b953f111 154 pc.printf("Hello World \n");
thomasmorris 5:2594b953f111 155 while(1)
thomasmorris 5:2594b953f111 156 {
chills 11:e7b5ed6cd3cf 157 osEvent evt = mail_box.get(); //Get the latest entry from "mail_box"
chills 10:46946784326d 158
chills 10:46946784326d 159 if (evt.status == osEventMail){
chills 11:e7b5ed6cd3cf 160 DATA *Rec_Data = (DATA*)evt.value.p;
chills 11:e7b5ed6cd3cf 161 DATA msg;
chills 11:e7b5ed6cd3cf 162
chills 11:e7b5ed6cd3cf 163 msg.set_time(Rec_Data->get_time());
chills 11:e7b5ed6cd3cf 164 msg.set_temperature(Rec_Data->get_temperature());
chills 11:e7b5ed6cd3cf 165 msg.set_pressure(Rec_Data->get_pressure());
chills 11:e7b5ed6cd3cf 166 msg.set_light(Rec_Data->get_light());
chills 11:e7b5ed6cd3cf 167
chills 11:e7b5ed6cd3cf 168 pc.printf("Time = %d\t", msg.get_time()); //Print Time
chills 11:e7b5ed6cd3cf 169 pc.printf("Temperature = %f", msg.get_temperature()); //Print Temperature
chills 11:e7b5ed6cd3cf 170 pc.printf("Pressure = %f", msg.get_pressure()); //Print Pressure
chills 11:e7b5ed6cd3cf 171 pc.printf("Light = %f", msg.get_light()); //Print Light
chills 10:46946784326d 172 }
chills 10:46946784326d 173
thomasmorris 7:dfe19413fdc2 174 pc.printf("Test\n");//Use this Line to output a string to Putty
thomasmorris 7:dfe19413fdc2 175 Green_led.Toggle();
thomasmorris 5:2594b953f111 176 Thread::wait(1000);
thomasmorris 5:2594b953f111 177 }
thomasmorris 5:2594b953f111 178 }
thomasmorris 5:2594b953f111 179
chills 10:46946784326d 180 void Sample_signal_set() //Sets the Signal for when to sample the sensors
thomasmorris 5:2594b953f111 181 {
chills 10:46946784326d 182 t1.signal_set(SamplingTime); //Set the sampling thread signal high
thomasmorris 5:2594b953f111 183 }
noutram 3:a88838ff33e7 184
chills 10:46946784326d 185
thomasmorris 5:2594b953f111 186 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 187 {
thomasmorris 5:2594b953f111 188 while(1)
thomasmorris 5:2594b953f111 189 {
chills 10:46946784326d 190 Thread::signal_wait(SamplingTime); //Set the time between samples
chills 10:46946784326d 191
chills 10:46946784326d 192 temp = sensor.getTemperature(); //Read Temperature
chills 10:46946784326d 193 pressure = sensor.getPressure(); //Read Pressure
chills 10:46946784326d 194 lux = adcIn.read(); //Read Light
chills 10:46946784326d 195 buffer_time = buffer; //Read Time
thomasmorris 7:dfe19413fdc2 196
chills 10:46946784326d 197 DATA *Send_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
thomasmorris 5:2594b953f111 198
chills 10:46946784326d 199 if (Send_Data == NULL){ //If Data is empty
chills 10:46946784326d 200 //pc.printf("Out of memory\n\r"); //Print out of memory warning
chills 10:46946784326d 201 return;
chills 10:46946784326d 202 }
chills 10:46946784326d 203
chills 10:46946784326d 204 Send_Data->set_time(buffer_time); //Pass in Time
chills 10:46946784326d 205 Send_Data->set_temperature(temp); //Pass in Temp
chills 10:46946784326d 206 Send_Data->set_pressure(pressure); //Pass in Pres
chills 10:46946784326d 207 Send_Data->set_light(lux); //Pass in Light
chills 10:46946784326d 208
chills 10:46946784326d 209 osStatus stat = mail_box.put(Send_Data); //Puts "Send_Data" into the mailbox
chills 10:46946784326d 210
chills 10:46946784326d 211 if (stat == osErrorResource){ //If mailbox overfills
chills 10:46946784326d 212 //pc.printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); //Print error message
chills 10:46946784326d 213 mail_box.free(Send_Data); //Free the mail box
chills 10:46946784326d 214 return;
chills 10:46946784326d 215 }
thomasmorris 5:2594b953f111 216 Red_led.Toggle();
thomasmorris 5:2594b953f111 217 t1.signal_set(NotSamplingTime);
thomasmorris 5:2594b953f111 218 }
thomasmorris 5:2594b953f111 219 }
thomasmorris 5:2594b953f111 220
thomasmorris 5:2594b953f111 221 int main()
thomasmorris 5:2594b953f111 222 {
noutram 1:e1cf7663f5ff 223 //Greeting
noutram 3:a88838ff33e7 224 printf("Testing\n\n");
chills 10:46946784326d 225 set_time(1512940530); //Set RTC time to December 10 2017
chills 10:46946784326d 226 pc.baud(9600); //Sets the Serial Comms Baud Rate
thomasmorris 5:2594b953f111 227
chills 10:46946784326d 228 post(); //Power on Self Test
noutram 1:e1cf7663f5ff 229
noutram 3:a88838ff33e7 230 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 231 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 232 printf("Init failed \n");
noutram 3:a88838ff33e7 233 lcd.cls();
noutram 3:a88838ff33e7 234 lcd.printf("CANNOT INIT SD");
noutram 1:e1cf7663f5ff 235 errorCode(FATAL);
noutram 1:e1cf7663f5ff 236 }
noutram 1:e1cf7663f5ff 237
noutram 1:e1cf7663f5ff 238 //Create a filing system for SD Card
noutram 1:e1cf7663f5ff 239 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 240
noutram 1:e1cf7663f5ff 241 //Open to WRITE
noutram 1:e1cf7663f5ff 242 FILE* fp = fopen("/sd/test.csv","a");
noutram 1:e1cf7663f5ff 243 if (fp == NULL) {
noutram 1:e1cf7663f5ff 244 error("Could not open file for write\n");
noutram 3:a88838ff33e7 245 lcd.cls();
noutram 3:a88838ff33e7 246 lcd.printf("CANNOT OPEN FILE\n\n");
noutram 1:e1cf7663f5ff 247 errorCode(FATAL);
noutram 1:e1cf7663f5ff 248 }
noutram 3:a88838ff33e7 249
noutram 3:a88838ff33e7 250 //Last message before sampling begins
noutram 3:a88838ff33e7 251 lcd.cls();
noutram 3:a88838ff33e7 252 lcd.printf("READY\n\n");
thomasmorris 5:2594b953f111 253
thomasmorris 5:2594b953f111 254 //Run interrupt
thomasmorris 5:2594b953f111 255 Sample_timer.attach(&Sample_signal_set,TimerInterval);
thomasmorris 7:dfe19413fdc2 256 SW1.fall(&SW1FallingEdge);
thomasmorris 5:2594b953f111 257 //Run Threads
thomasmorris 5:2594b953f111 258
thomasmorris 5:2594b953f111 259 t1.start(Sample);
thomasmorris 5:2594b953f111 260 t2.start(Serial_Comms);
thomasmorris 7:dfe19413fdc2 261 t3.start(Time);
thomasmorris 7:dfe19413fdc2 262 t4.start(PrintTime);
thomasmorris 7:dfe19413fdc2 263 t5.start(ModeSelection);
thomasmorris 8:0e4481b64353 264 t6.start(Network);
thomasmorris 5:2594b953f111 265 //Main thread ID
thomasmorris 5:2594b953f111 266
thomasmorris 5:2594b953f111 267 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 5:2594b953f111 268
thomasmorris 5:2594b953f111 269 //Thread ID
thomasmorris 5:2594b953f111 270 id1 = t1.gettid();
thomasmorris 5:2594b953f111 271 id2 = t2.gettid();
thomasmorris 7:dfe19413fdc2 272 id3 = t3.gettid();
thomasmorris 8:0e4481b64353 273 id4 = t4.gettid();
thomasmorris 8:0e4481b64353 274 //id5 = t5.gettid();
thomasmorris 5:2594b953f111 275
thomasmorris 5:2594b953f111 276 //Toggle Green LED after a button has been pressed
noutram 1:e1cf7663f5ff 277 //Press either switch to unmount
thomasmorris 7:dfe19413fdc2 278 DigitalIn onBoardSwitch(USER_BUTTON);
thomasmorris 7:dfe19413fdc2 279 while (onBoardSwitch == 0){
thomasmorris 7:dfe19413fdc2 280
noutram 1:e1cf7663f5ff 281 }
noutram 1:e1cf7663f5ff 282
noutram 1:e1cf7663f5ff 283 //Close File
noutram 1:e1cf7663f5ff 284 fclose(fp);
noutram 1:e1cf7663f5ff 285
noutram 1:e1cf7663f5ff 286 //Close down
noutram 1:e1cf7663f5ff 287 sd.deinit();
noutram 1:e1cf7663f5ff 288 printf("Unmounted...\n");
noutram 1:e1cf7663f5ff 289 lcd.cls();
noutram 1:e1cf7663f5ff 290 lcd.printf("Unmounted...\n\n");
noutram 1:e1cf7663f5ff 291
noutram 1:e1cf7663f5ff 292 while(true) {
noutram 1:e1cf7663f5ff 293 greenLED = 1;
noutram 1:e1cf7663f5ff 294 wait(0.5);
noutram 1:e1cf7663f5ff 295 greenLED = 0;
noutram 1:e1cf7663f5ff 296 wait(0.1);
noutram 0:36e89e3ed7c4 297 }
noutram 0:36e89e3ed7c4 298 }
noutram 0:36e89e3ed7c4 299
noutram 1:e1cf7663f5ff 300
noutram 1:e1cf7663f5ff 301