Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
chills
Date:
Wed Dec 20 14:32:40 2017 +0000
Revision:
10:46946784326d
Parent:
8:0e4481b64353
Child:
11:e7b5ed6cd3cf
2017_12_20 14:29; First 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 10:46946784326d 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 10:46946784326d 160 DATA *Rec_Data = (DATA*)evt.value.p;
chills 10:46946784326d 161 pc.printf("Time = %d\t", Rec_Data->get_time()); //Print Time
chills 10:46946784326d 162 pc.printf("Temperature = %f", Rec_Data->get_temperature()); //Print Temperature
chills 10:46946784326d 163 pc.printf("Pressure = %f", Rec_Data->get_pressure()); //Print Pressure
chills 10:46946784326d 164 pc.printf("Light = %f", Rec_Data->get_light()); //Print Light
chills 10:46946784326d 165 }
chills 10:46946784326d 166
thomasmorris 7:dfe19413fdc2 167 pc.printf("Test\n");//Use this Line to output a string to Putty
thomasmorris 7:dfe19413fdc2 168 Green_led.Toggle();
thomasmorris 5:2594b953f111 169 Thread::wait(1000);
thomasmorris 5:2594b953f111 170 }
thomasmorris 5:2594b953f111 171 }
thomasmorris 5:2594b953f111 172
chills 10:46946784326d 173 void Sample_signal_set() //Sets the Signal for when to sample the sensors
thomasmorris 5:2594b953f111 174 {
chills 10:46946784326d 175 t1.signal_set(SamplingTime); //Set the sampling thread signal high
thomasmorris 5:2594b953f111 176 }
noutram 3:a88838ff33e7 177
chills 10:46946784326d 178
thomasmorris 5:2594b953f111 179 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 180 {
thomasmorris 5:2594b953f111 181 while(1)
thomasmorris 5:2594b953f111 182 {
chills 10:46946784326d 183 Thread::signal_wait(SamplingTime); //Set the time between samples
chills 10:46946784326d 184
chills 10:46946784326d 185 temp = sensor.getTemperature(); //Read Temperature
chills 10:46946784326d 186 pressure = sensor.getPressure(); //Read Pressure
chills 10:46946784326d 187 lux = adcIn.read(); //Read Light
chills 10:46946784326d 188 buffer_time = buffer; //Read Time
thomasmorris 7:dfe19413fdc2 189
chills 10:46946784326d 190 DATA *Send_Data = mail_box.alloc(); //Allocate a block from the memory pool, Type Data
thomasmorris 5:2594b953f111 191
chills 10:46946784326d 192 if (Send_Data == NULL){ //If Data is empty
chills 10:46946784326d 193 //pc.printf("Out of memory\n\r"); //Print out of memory warning
chills 10:46946784326d 194 return;
chills 10:46946784326d 195 }
chills 10:46946784326d 196
chills 10:46946784326d 197 Send_Data->set_time(buffer_time); //Pass in Time
chills 10:46946784326d 198 Send_Data->set_temperature(temp); //Pass in Temp
chills 10:46946784326d 199 Send_Data->set_pressure(pressure); //Pass in Pres
chills 10:46946784326d 200 Send_Data->set_light(lux); //Pass in Light
chills 10:46946784326d 201
chills 10:46946784326d 202 osStatus stat = mail_box.put(Send_Data); //Puts "Send_Data" into the mailbox
chills 10:46946784326d 203
chills 10:46946784326d 204 if (stat == osErrorResource){ //If mailbox overfills
chills 10:46946784326d 205 //pc.printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); //Print error message
chills 10:46946784326d 206 mail_box.free(Send_Data); //Free the mail box
chills 10:46946784326d 207 return;
chills 10:46946784326d 208 }
thomasmorris 5:2594b953f111 209 Red_led.Toggle();
thomasmorris 5:2594b953f111 210 t1.signal_set(NotSamplingTime);
thomasmorris 5:2594b953f111 211 }
thomasmorris 5:2594b953f111 212 }
thomasmorris 5:2594b953f111 213
thomasmorris 5:2594b953f111 214 int main()
thomasmorris 5:2594b953f111 215 {
noutram 1:e1cf7663f5ff 216 //Greeting
noutram 3:a88838ff33e7 217 printf("Testing\n\n");
chills 10:46946784326d 218 set_time(1512940530); //Set RTC time to December 10 2017
chills 10:46946784326d 219 pc.baud(9600); //Sets the Serial Comms Baud Rate
thomasmorris 5:2594b953f111 220
chills 10:46946784326d 221 post(); //Power on Self Test
noutram 1:e1cf7663f5ff 222
noutram 3:a88838ff33e7 223 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 224 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 225 printf("Init failed \n");
noutram 3:a88838ff33e7 226 lcd.cls();
noutram 3:a88838ff33e7 227 lcd.printf("CANNOT INIT SD");
noutram 1:e1cf7663f5ff 228 errorCode(FATAL);
noutram 1:e1cf7663f5ff 229 }
noutram 1:e1cf7663f5ff 230
noutram 1:e1cf7663f5ff 231 //Create a filing system for SD Card
noutram 1:e1cf7663f5ff 232 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 233
noutram 1:e1cf7663f5ff 234 //Open to WRITE
noutram 1:e1cf7663f5ff 235 FILE* fp = fopen("/sd/test.csv","a");
noutram 1:e1cf7663f5ff 236 if (fp == NULL) {
noutram 1:e1cf7663f5ff 237 error("Could not open file for write\n");
noutram 3:a88838ff33e7 238 lcd.cls();
noutram 3:a88838ff33e7 239 lcd.printf("CANNOT OPEN FILE\n\n");
noutram 1:e1cf7663f5ff 240 errorCode(FATAL);
noutram 1:e1cf7663f5ff 241 }
noutram 3:a88838ff33e7 242
noutram 3:a88838ff33e7 243 //Last message before sampling begins
noutram 3:a88838ff33e7 244 lcd.cls();
noutram 3:a88838ff33e7 245 lcd.printf("READY\n\n");
thomasmorris 5:2594b953f111 246
thomasmorris 5:2594b953f111 247 //Run interrupt
thomasmorris 5:2594b953f111 248 Sample_timer.attach(&Sample_signal_set,TimerInterval);
thomasmorris 7:dfe19413fdc2 249 SW1.fall(&SW1FallingEdge);
thomasmorris 5:2594b953f111 250 //Run Threads
thomasmorris 5:2594b953f111 251
thomasmorris 5:2594b953f111 252 t1.start(Sample);
thomasmorris 5:2594b953f111 253 t2.start(Serial_Comms);
thomasmorris 7:dfe19413fdc2 254 t3.start(Time);
thomasmorris 7:dfe19413fdc2 255 t4.start(PrintTime);
thomasmorris 7:dfe19413fdc2 256 t5.start(ModeSelection);
thomasmorris 8:0e4481b64353 257 t6.start(Network);
thomasmorris 5:2594b953f111 258 //Main thread ID
thomasmorris 5:2594b953f111 259
thomasmorris 5:2594b953f111 260 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 5:2594b953f111 261
thomasmorris 5:2594b953f111 262 //Thread ID
thomasmorris 5:2594b953f111 263 id1 = t1.gettid();
thomasmorris 5:2594b953f111 264 id2 = t2.gettid();
thomasmorris 7:dfe19413fdc2 265 id3 = t3.gettid();
thomasmorris 8:0e4481b64353 266 id4 = t4.gettid();
thomasmorris 8:0e4481b64353 267 //id5 = t5.gettid();
thomasmorris 5:2594b953f111 268
thomasmorris 5:2594b953f111 269 //Toggle Green LED after a button has been pressed
noutram 1:e1cf7663f5ff 270 //Press either switch to unmount
thomasmorris 7:dfe19413fdc2 271 DigitalIn onBoardSwitch(USER_BUTTON);
thomasmorris 7:dfe19413fdc2 272 while (onBoardSwitch == 0){
thomasmorris 7:dfe19413fdc2 273
noutram 1:e1cf7663f5ff 274 }
noutram 1:e1cf7663f5ff 275
noutram 1:e1cf7663f5ff 276 //Close File
noutram 1:e1cf7663f5ff 277 fclose(fp);
noutram 1:e1cf7663f5ff 278
noutram 1:e1cf7663f5ff 279 //Close down
noutram 1:e1cf7663f5ff 280 sd.deinit();
noutram 1:e1cf7663f5ff 281 printf("Unmounted...\n");
noutram 1:e1cf7663f5ff 282 lcd.cls();
noutram 1:e1cf7663f5ff 283 lcd.printf("Unmounted...\n\n");
noutram 1:e1cf7663f5ff 284
noutram 1:e1cf7663f5ff 285 while(true) {
noutram 1:e1cf7663f5ff 286 greenLED = 1;
noutram 1:e1cf7663f5ff 287 wait(0.5);
noutram 1:e1cf7663f5ff 288 greenLED = 0;
noutram 1:e1cf7663f5ff 289 wait(0.1);
noutram 0:36e89e3ed7c4 290 }
noutram 0:36e89e3ed7c4 291 }
noutram 0:36e89e3ed7c4 292
noutram 1:e1cf7663f5ff 293
noutram 1:e1cf7663f5ff 294