Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Tue Dec 19 13:26:54 2017 +0000
Revision:
8:0e4481b64353
Parent:
7:dfe19413fdc2
Child:
10:46946784326d
Added DATA Class structure and networking example

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;
thomasmorris 7:dfe19413fdc2 60
thomasmorris 7:dfe19413fdc2 61 char buffer[32];
thomasmorris 7:dfe19413fdc2 62
thomasmorris 7:dfe19413fdc2 63
thomasmorris 7:dfe19413fdc2 64 void SW1FallingEdge();
thomasmorris 7:dfe19413fdc2 65 void SW1TimeOutHandler();
thomasmorris 7:dfe19413fdc2 66
thomasmorris 7:dfe19413fdc2 67 int mode = 0;
thomasmorris 7:dfe19413fdc2 68
thomasmorris 8:0e4481b64353 69
thomasmorris 8:0e4481b64353 70
thomasmorris 8:0e4481b64353 71 void Network()
thomasmorris 8:0e4481b64353 72 {
thomasmorris 8:0e4481b64353 73 NETWORK_Print();//Runs the network
thomasmorris 8:0e4481b64353 74 //Send DATA to the network
thomasmorris 8:0e4481b64353 75 }//Interrupt service routine for handling the timeout
thomasmorris 8:0e4481b64353 76 void SW1TimeOutHandler()
thomasmorris 8:0e4481b64353 77 {
thomasmorris 7:dfe19413fdc2 78 sw1TimeOut.detach(); //Stop the timeout counter firing
thomasmorris 7:dfe19413fdc2 79 SW1.fall(&SW1FallingEdge); //Now wait for a falling edge
thomasmorris 7:dfe19413fdc2 80 }
thomasmorris 7:dfe19413fdc2 81
thomasmorris 7:dfe19413fdc2 82 //Interrupt service routive for SW1 falling edge (release)
thomasmorris 7:dfe19413fdc2 83 void SW1FallingEdge() {
thomasmorris 7:dfe19413fdc2 84 SW1.fall(NULL); //Disable this interrupt
thomasmorris 7:dfe19413fdc2 85 Yellow_led.Toggle(); //Toggle LED
thomasmorris 7:dfe19413fdc2 86
thomasmorris 7:dfe19413fdc2 87 mode = mode +1;//Cycles through modes
thomasmorris 7:dfe19413fdc2 88 if(mode >1)
thomasmorris 7:dfe19413fdc2 89 {
thomasmorris 7:dfe19413fdc2 90 mode = 0;
thomasmorris 7:dfe19413fdc2 91 }
thomasmorris 7:dfe19413fdc2 92
thomasmorris 7:dfe19413fdc2 93 sw1TimeOut.attach(&SW1TimeOutHandler, 0.2); //Start timeout counter
thomasmorris 7:dfe19413fdc2 94 }
thomasmorris 7:dfe19413fdc2 95
thomasmorris 7:dfe19413fdc2 96 void ModeSelection()
thomasmorris 7:dfe19413fdc2 97 {
thomasmorris 7:dfe19413fdc2 98 while(1){
thomasmorris 8:0e4481b64353 99 Thread::wait(1000);
thomasmorris 7:dfe19413fdc2 100 //Detech the not required interrupt then rettach it when finshed
thomasmorris 7:dfe19413fdc2 101 if(mode == 0)//Print values to the LCD
thomasmorris 7:dfe19413fdc2 102 {
thomasmorris 7:dfe19413fdc2 103 //Write new data to LCD (not fast!)
thomasmorris 7:dfe19413fdc2 104 lcd.cls();
thomasmorris 7:dfe19413fdc2 105 lcd.printf("Temp Pres li\n");
thomasmorris 7:dfe19413fdc2 106 lcd.printf("%1.1f ",temp);
thomasmorris 7:dfe19413fdc2 107 lcd.printf("%1.1f ",pressure);
thomasmorris 7:dfe19413fdc2 108 lcd.printf("%1.1f\n",lux);
thomasmorris 7:dfe19413fdc2 109 }
thomasmorris 7:dfe19413fdc2 110 else if(mode == 1)//Print the Time to the LCD
thomasmorris 7:dfe19413fdc2 111 {
thomasmorris 7:dfe19413fdc2 112 //Write new data to LCD (not fast!)
thomasmorris 7:dfe19413fdc2 113 lcd.cls();
thomasmorris 7:dfe19413fdc2 114
thomasmorris 7:dfe19413fdc2 115 lcd.printf("Current Time:%s", buffer);
thomasmorris 7:dfe19413fdc2 116
thomasmorris 7:dfe19413fdc2 117 //Write to SD (potentially slow)
thomasmorris 7:dfe19413fdc2 118 //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
thomasmorris 7:dfe19413fdc2 119 }
thomasmorris 7:dfe19413fdc2 120 else
thomasmorris 7:dfe19413fdc2 121 {
thomasmorris 7:dfe19413fdc2 122 mode = 0;
thomasmorris 7:dfe19413fdc2 123 }
thomasmorris 7:dfe19413fdc2 124 }
thomasmorris 7:dfe19413fdc2 125 }
thomasmorris 7:dfe19413fdc2 126
thomasmorris 7:dfe19413fdc2 127 void PrintTime()
thomasmorris 7:dfe19413fdc2 128 {
thomasmorris 7:dfe19413fdc2 129 Thread::signal_wait(Print_Time_to_LCD);
thomasmorris 7:dfe19413fdc2 130 while(1)
thomasmorris 7:dfe19413fdc2 131 {
thomasmorris 7:dfe19413fdc2 132 //lcd.printf("Current Time: \n %s", buffer);
thomasmorris 7:dfe19413fdc2 133 Thread::wait(1000);//Waits the thread for 1 second
thomasmorris 7:dfe19413fdc2 134 }
thomasmorris 7:dfe19413fdc2 135 }
thomasmorris 7:dfe19413fdc2 136 void Time()
thomasmorris 7:dfe19413fdc2 137 {
thomasmorris 7:dfe19413fdc2 138 while (true)
thomasmorris 7:dfe19413fdc2 139 {
thomasmorris 7:dfe19413fdc2 140 time_t seconds = time(NULL);
thomasmorris 7:dfe19413fdc2 141 //pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds);
thomasmorris 7:dfe19413fdc2 142 //pc.printf("Time as a basic string = %s", ctime(&seconds));
thomasmorris 7:dfe19413fdc2 143 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
thomasmorris 7:dfe19413fdc2 144 pc.printf("Current Time:%s", buffer);
thomasmorris 7:dfe19413fdc2 145
thomasmorris 7:dfe19413fdc2 146 Thread::wait(1000);
thomasmorris 7:dfe19413fdc2 147 }
thomasmorris 7:dfe19413fdc2 148 }
thomasmorris 7:dfe19413fdc2 149
thomasmorris 5:2594b953f111 150
thomasmorris 5:2594b953f111 151 void Serial_Comms()//Thread for Serial Communications
thomasmorris 5:2594b953f111 152 {
thomasmorris 5:2594b953f111 153 pc.printf("Hello World \n");
thomasmorris 5:2594b953f111 154 while(1)
thomasmorris 5:2594b953f111 155 {
thomasmorris 7:dfe19413fdc2 156 pc.printf("Test\n");//Use this Line to output a string to Putty
thomasmorris 7:dfe19413fdc2 157 Green_led.Toggle();
thomasmorris 5:2594b953f111 158 Thread::wait(1000);
thomasmorris 5:2594b953f111 159 }
thomasmorris 5:2594b953f111 160 }
thomasmorris 5:2594b953f111 161
thomasmorris 5:2594b953f111 162 void Sample_signal_set()//Sets the Signal for when to sample the sensors
thomasmorris 5:2594b953f111 163 {
thomasmorris 5:2594b953f111 164 t1.signal_set(SamplingTime);
thomasmorris 5:2594b953f111 165 }
noutram 3:a88838ff33e7 166
thomasmorris 5:2594b953f111 167 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 168 {
thomasmorris 5:2594b953f111 169 while(1)
thomasmorris 5:2594b953f111 170 {
thomasmorris 5:2594b953f111 171 Thread::signal_wait(SamplingTime);
thomasmorris 7:dfe19413fdc2 172
thomasmorris 5:2594b953f111 173 //Read environmental sensors
thomasmorris 5:2594b953f111 174
thomasmorris 7:dfe19413fdc2 175 temp = sensor.getTemperature();
thomasmorris 7:dfe19413fdc2 176 pressure = sensor.getPressure();
thomasmorris 7:dfe19413fdc2 177 lux = adcIn.read();
thomasmorris 7:dfe19413fdc2 178
thomasmorris 5:2594b953f111 179
thomasmorris 5:2594b953f111 180 Red_led.Toggle();
thomasmorris 5:2594b953f111 181 t1.signal_set(NotSamplingTime);
thomasmorris 5:2594b953f111 182 }
thomasmorris 5:2594b953f111 183 }
thomasmorris 5:2594b953f111 184
thomasmorris 5:2594b953f111 185 int main()
thomasmorris 5:2594b953f111 186 {
noutram 1:e1cf7663f5ff 187 //Greeting
noutram 3:a88838ff33e7 188 printf("Testing\n\n");
thomasmorris 7:dfe19413fdc2 189 set_time(1512940530); // Set RTC time to December 10 2017
thomasmorris 5:2594b953f111 190 pc.baud(9600);//Sets the Serial Comms Baud Rate
thomasmorris 5:2594b953f111 191
thomasmorris 5:2594b953f111 192 post();//Power on Self Test
noutram 1:e1cf7663f5ff 193
noutram 3:a88838ff33e7 194 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 195 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 196 printf("Init failed \n");
noutram 3:a88838ff33e7 197 lcd.cls();
noutram 3:a88838ff33e7 198 lcd.printf("CANNOT INIT SD");
noutram 1:e1cf7663f5ff 199 errorCode(FATAL);
noutram 1:e1cf7663f5ff 200 }
noutram 1:e1cf7663f5ff 201
noutram 1:e1cf7663f5ff 202 //Create a filing system for SD Card
noutram 1:e1cf7663f5ff 203 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 204
noutram 1:e1cf7663f5ff 205 //Open to WRITE
noutram 1:e1cf7663f5ff 206 FILE* fp = fopen("/sd/test.csv","a");
noutram 1:e1cf7663f5ff 207 if (fp == NULL) {
noutram 1:e1cf7663f5ff 208 error("Could not open file for write\n");
noutram 3:a88838ff33e7 209 lcd.cls();
noutram 3:a88838ff33e7 210 lcd.printf("CANNOT OPEN FILE\n\n");
noutram 1:e1cf7663f5ff 211 errorCode(FATAL);
noutram 1:e1cf7663f5ff 212 }
noutram 3:a88838ff33e7 213
noutram 3:a88838ff33e7 214 //Last message before sampling begins
noutram 3:a88838ff33e7 215 lcd.cls();
noutram 3:a88838ff33e7 216 lcd.printf("READY\n\n");
thomasmorris 5:2594b953f111 217
thomasmorris 5:2594b953f111 218 //Run interrupt
thomasmorris 5:2594b953f111 219 Sample_timer.attach(&Sample_signal_set,TimerInterval);
thomasmorris 7:dfe19413fdc2 220 SW1.fall(&SW1FallingEdge);
thomasmorris 5:2594b953f111 221 //Run Threads
thomasmorris 5:2594b953f111 222
thomasmorris 5:2594b953f111 223 t1.start(Sample);
thomasmorris 5:2594b953f111 224 t2.start(Serial_Comms);
thomasmorris 7:dfe19413fdc2 225 t3.start(Time);
thomasmorris 7:dfe19413fdc2 226 t4.start(PrintTime);
thomasmorris 7:dfe19413fdc2 227 t5.start(ModeSelection);
thomasmorris 8:0e4481b64353 228 t6.start(Network);
thomasmorris 5:2594b953f111 229 //Main thread ID
thomasmorris 5:2594b953f111 230
thomasmorris 5:2594b953f111 231 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 5:2594b953f111 232
thomasmorris 5:2594b953f111 233 //Thread ID
thomasmorris 5:2594b953f111 234 id1 = t1.gettid();
thomasmorris 5:2594b953f111 235 id2 = t2.gettid();
thomasmorris 7:dfe19413fdc2 236 id3 = t3.gettid();
thomasmorris 8:0e4481b64353 237 id4 = t4.gettid();
thomasmorris 8:0e4481b64353 238 //id5 = t5.gettid();
thomasmorris 5:2594b953f111 239
thomasmorris 5:2594b953f111 240 //Toggle Green LED after a button has been pressed
noutram 1:e1cf7663f5ff 241 //Press either switch to unmount
thomasmorris 7:dfe19413fdc2 242 DigitalIn onBoardSwitch(USER_BUTTON);
thomasmorris 7:dfe19413fdc2 243 while (onBoardSwitch == 0){
thomasmorris 7:dfe19413fdc2 244
noutram 1:e1cf7663f5ff 245 }
noutram 1:e1cf7663f5ff 246
noutram 1:e1cf7663f5ff 247 //Close File
noutram 1:e1cf7663f5ff 248 fclose(fp);
noutram 1:e1cf7663f5ff 249
noutram 1:e1cf7663f5ff 250 //Close down
noutram 1:e1cf7663f5ff 251 sd.deinit();
noutram 1:e1cf7663f5ff 252 printf("Unmounted...\n");
noutram 1:e1cf7663f5ff 253 lcd.cls();
noutram 1:e1cf7663f5ff 254 lcd.printf("Unmounted...\n\n");
noutram 1:e1cf7663f5ff 255
noutram 1:e1cf7663f5ff 256 while(true) {
noutram 1:e1cf7663f5ff 257 greenLED = 1;
noutram 1:e1cf7663f5ff 258 wait(0.5);
noutram 1:e1cf7663f5ff 259 greenLED = 0;
noutram 1:e1cf7663f5ff 260 wait(0.1);
noutram 0:36e89e3ed7c4 261 }
noutram 0:36e89e3ed7c4 262 }
noutram 0:36e89e3ed7c4 263
noutram 1:e1cf7663f5ff 264
noutram 1:e1cf7663f5ff 265