Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Wed Dec 20 15:18:00 2017 +0000
Revision:
12:536eca338ae8
Parent:
11:e7b5ed6cd3cf
Child:
13:db857b3744c6
20/12/2017 v4

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