Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Mon Dec 11 19:25:35 2017 +0000
Revision:
7:dfe19413fdc2
Parent:
6:97f586597310
Child:
8:0e4481b64353
11/12/2017 Working LCD mode selection with time

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 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 7:dfe19413fdc2 15 #define Print_Time_to_LCD 1
thomasmorris 7:dfe19413fdc2 16 #define Dont_Print_Time_to_LCD 0
thomasmorris 5:2594b953f111 17 #define TimerInterval 15 //This is in seconds
thomasmorris 7:dfe19413fdc2 18 #define EDGE_RISEN 1
thomasmorris 7:dfe19413fdc2 19 #define EDGE_FALLEN 0
thomasmorris 5:2594b953f111 20 Serial pc(USBTX, USBRX);
thomasmorris 7:dfe19413fdc2 21 //SW1+SW2 are declared as interrupt ins in sample hardwarec.pp
noutram 3:a88838ff33e7 22 // This is a very short demo that demonstrates all the hardware used in the coursework.
noutram 3:a88838ff33e7 23 // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1
thomasmorris 5:2594b953f111 24 //Thread ID
thomasmorris 5:2594b953f111 25 osThreadId idMain;
thomasmorris 5:2594b953f111 26 osThreadId id1;
thomasmorris 5:2594b953f111 27 osThreadId id2;
thomasmorris 5:2594b953f111 28 osThreadId id3;
thomasmorris 5:2594b953f111 29 osThreadId id4;
noutram 0:36e89e3ed7c4 30
thomasmorris 7:dfe19413fdc2 31 Timeout sw1TimeOut;//Used to prevent switch bounce
thomasmorris 7:dfe19413fdc2 32
thomasmorris 5:2594b953f111 33 LED Red_led(PE_15);
thomasmorris 5:2594b953f111 34 LED Yellow_led(PB_10);
thomasmorris 5:2594b953f111 35 LED Green_led(PB_11);
thomasmorris 5:2594b953f111 36
thomasmorris 5:2594b953f111 37 Ticker Sample_timer;
noutram 3:a88838ff33e7 38 //Threads
noutram 3:a88838ff33e7 39 Thread nwrkThread;
thomasmorris 5:2594b953f111 40 Thread t1;
thomasmorris 5:2594b953f111 41 Thread t2;
thomasmorris 7:dfe19413fdc2 42 Thread t3;
thomasmorris 7:dfe19413fdc2 43 Thread t4;
thomasmorris 7:dfe19413fdc2 44 Thread t5;
thomasmorris 7:dfe19413fdc2 45 Thread t6;
thomasmorris 7:dfe19413fdc2 46
thomasmorris 7:dfe19413fdc2 47 double temp = 0;
thomasmorris 7:dfe19413fdc2 48 double pressure = 0;
thomasmorris 7:dfe19413fdc2 49 double lux = 0;
thomasmorris 7:dfe19413fdc2 50
thomasmorris 7:dfe19413fdc2 51 char buffer[32];
thomasmorris 7:dfe19413fdc2 52
thomasmorris 7:dfe19413fdc2 53
thomasmorris 7:dfe19413fdc2 54 void SW1FallingEdge();
thomasmorris 7:dfe19413fdc2 55 void SW1TimeOutHandler();
thomasmorris 7:dfe19413fdc2 56
thomasmorris 7:dfe19413fdc2 57 int mode = 0;
thomasmorris 7:dfe19413fdc2 58
thomasmorris 7:dfe19413fdc2 59 //Interrupt service routine for handling the timeout
thomasmorris 7:dfe19413fdc2 60 void SW1TimeOutHandler() {
thomasmorris 7:dfe19413fdc2 61 sw1TimeOut.detach(); //Stop the timeout counter firing
thomasmorris 7:dfe19413fdc2 62 SW1.fall(&SW1FallingEdge); //Now wait for a falling edge
thomasmorris 7:dfe19413fdc2 63 }
thomasmorris 7:dfe19413fdc2 64
thomasmorris 7:dfe19413fdc2 65 //Interrupt service routive for SW1 falling edge (release)
thomasmorris 7:dfe19413fdc2 66 void SW1FallingEdge() {
thomasmorris 7:dfe19413fdc2 67 SW1.fall(NULL); //Disable this interrupt
thomasmorris 7:dfe19413fdc2 68 Yellow_led.Toggle(); //Toggle LED
thomasmorris 7:dfe19413fdc2 69
thomasmorris 7:dfe19413fdc2 70 mode = mode +1;//Cycles through modes
thomasmorris 7:dfe19413fdc2 71 if(mode >1)
thomasmorris 7:dfe19413fdc2 72 {
thomasmorris 7:dfe19413fdc2 73 mode = 0;
thomasmorris 7:dfe19413fdc2 74 }
thomasmorris 7:dfe19413fdc2 75
thomasmorris 7:dfe19413fdc2 76 sw1TimeOut.attach(&SW1TimeOutHandler, 0.2); //Start timeout counter
thomasmorris 7:dfe19413fdc2 77 }
thomasmorris 7:dfe19413fdc2 78
thomasmorris 7:dfe19413fdc2 79 void ModeSelection()
thomasmorris 7:dfe19413fdc2 80 {
thomasmorris 7:dfe19413fdc2 81 while(1){
thomasmorris 7:dfe19413fdc2 82 Thread::wait(1000);
thomasmorris 7:dfe19413fdc2 83 //Detech the not required interrupt then rettach it when finshed
thomasmorris 7:dfe19413fdc2 84 if(mode == 0)//Print values to the LCD
thomasmorris 7:dfe19413fdc2 85 {
thomasmorris 7:dfe19413fdc2 86 //Write new data to LCD (not fast!)
thomasmorris 7:dfe19413fdc2 87 lcd.cls();
thomasmorris 7:dfe19413fdc2 88 lcd.printf("Temp Pres li\n");
thomasmorris 7:dfe19413fdc2 89 lcd.printf("%1.1f ",temp);
thomasmorris 7:dfe19413fdc2 90 lcd.printf("%1.1f ",pressure);
thomasmorris 7:dfe19413fdc2 91 lcd.printf("%1.1f\n",lux);
thomasmorris 7:dfe19413fdc2 92 }
thomasmorris 7:dfe19413fdc2 93 else if(mode == 1)//Print the Time to the LCD
thomasmorris 7:dfe19413fdc2 94 {
thomasmorris 7:dfe19413fdc2 95 //Write new data to LCD (not fast!)
thomasmorris 7:dfe19413fdc2 96 lcd.cls();
thomasmorris 7:dfe19413fdc2 97
thomasmorris 7:dfe19413fdc2 98 lcd.printf("Current Time:%s", buffer);
thomasmorris 7:dfe19413fdc2 99
thomasmorris 7:dfe19413fdc2 100 //Write to SD (potentially slow)
thomasmorris 7:dfe19413fdc2 101 //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
thomasmorris 7:dfe19413fdc2 102 }
thomasmorris 7:dfe19413fdc2 103 else
thomasmorris 7:dfe19413fdc2 104 {
thomasmorris 7:dfe19413fdc2 105 mode = 0;
thomasmorris 7:dfe19413fdc2 106 }
thomasmorris 7:dfe19413fdc2 107 }
thomasmorris 7:dfe19413fdc2 108 }
thomasmorris 7:dfe19413fdc2 109
thomasmorris 7:dfe19413fdc2 110 void PrintTime()
thomasmorris 7:dfe19413fdc2 111 {
thomasmorris 7:dfe19413fdc2 112 Thread::signal_wait(Print_Time_to_LCD);
thomasmorris 7:dfe19413fdc2 113 while(1)
thomasmorris 7:dfe19413fdc2 114 {
thomasmorris 7:dfe19413fdc2 115 //lcd.printf("Current Time: \n %s", buffer);
thomasmorris 7:dfe19413fdc2 116 Thread::wait(1000);//Waits the thread for 1 second
thomasmorris 7:dfe19413fdc2 117 }
thomasmorris 7:dfe19413fdc2 118 }
thomasmorris 7:dfe19413fdc2 119 void Time()
thomasmorris 7:dfe19413fdc2 120 {
thomasmorris 7:dfe19413fdc2 121 while (true)
thomasmorris 7:dfe19413fdc2 122 {
thomasmorris 7:dfe19413fdc2 123 time_t seconds = time(NULL);
thomasmorris 7:dfe19413fdc2 124 //pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds);
thomasmorris 7:dfe19413fdc2 125 //pc.printf("Time as a basic string = %s", ctime(&seconds));
thomasmorris 7:dfe19413fdc2 126 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
thomasmorris 7:dfe19413fdc2 127 pc.printf("Current Time:%s", buffer);
thomasmorris 7:dfe19413fdc2 128
thomasmorris 7:dfe19413fdc2 129 Thread::wait(1000);
thomasmorris 7:dfe19413fdc2 130 }
thomasmorris 7:dfe19413fdc2 131 }
thomasmorris 7:dfe19413fdc2 132
thomasmorris 5:2594b953f111 133
thomasmorris 5:2594b953f111 134 void Serial_Comms()//Thread for Serial Communications
thomasmorris 5:2594b953f111 135 {
thomasmorris 5:2594b953f111 136 pc.printf("Hello World \n");
thomasmorris 5:2594b953f111 137 while(1)
thomasmorris 5:2594b953f111 138 {
thomasmorris 7:dfe19413fdc2 139 pc.printf("Test\n");//Use this Line to output a string to Putty
thomasmorris 7:dfe19413fdc2 140 Green_led.Toggle();
thomasmorris 5:2594b953f111 141 Thread::wait(1000);
thomasmorris 5:2594b953f111 142 }
thomasmorris 5:2594b953f111 143 }
thomasmorris 5:2594b953f111 144
thomasmorris 5:2594b953f111 145 void Sample_signal_set()//Sets the Signal for when to sample the sensors
thomasmorris 5:2594b953f111 146 {
thomasmorris 5:2594b953f111 147 t1.signal_set(SamplingTime);
thomasmorris 5:2594b953f111 148 }
noutram 3:a88838ff33e7 149
thomasmorris 5:2594b953f111 150 void Sample()//Samples the hardware and prints the result to the LCD
thomasmorris 5:2594b953f111 151 {
thomasmorris 5:2594b953f111 152 while(1)
thomasmorris 5:2594b953f111 153 {
thomasmorris 5:2594b953f111 154 Thread::signal_wait(SamplingTime);
thomasmorris 7:dfe19413fdc2 155
thomasmorris 5:2594b953f111 156 //Read environmental sensors
thomasmorris 5:2594b953f111 157
thomasmorris 7:dfe19413fdc2 158 temp = sensor.getTemperature();
thomasmorris 7:dfe19413fdc2 159 pressure = sensor.getPressure();
thomasmorris 7:dfe19413fdc2 160 lux = adcIn.read();
thomasmorris 7:dfe19413fdc2 161
thomasmorris 5:2594b953f111 162
thomasmorris 5:2594b953f111 163 Red_led.Toggle();
thomasmorris 5:2594b953f111 164 t1.signal_set(NotSamplingTime);
thomasmorris 5:2594b953f111 165 }
thomasmorris 5:2594b953f111 166 }
thomasmorris 5:2594b953f111 167
thomasmorris 5:2594b953f111 168 int main()
thomasmorris 5:2594b953f111 169 {
noutram 1:e1cf7663f5ff 170 //Greeting
noutram 3:a88838ff33e7 171 printf("Testing\n\n");
thomasmorris 7:dfe19413fdc2 172 set_time(1512940530); // Set RTC time to December 10 2017
thomasmorris 5:2594b953f111 173 pc.baud(9600);//Sets the Serial Comms Baud Rate
thomasmorris 5:2594b953f111 174
thomasmorris 5:2594b953f111 175 post();//Power on Self Test
noutram 1:e1cf7663f5ff 176
noutram 3:a88838ff33e7 177 //Initialise the SD card (this needs to move)
noutram 1:e1cf7663f5ff 178 if ( sd.init() != 0) {
noutram 1:e1cf7663f5ff 179 printf("Init failed \n");
noutram 3:a88838ff33e7 180 lcd.cls();
noutram 3:a88838ff33e7 181 lcd.printf("CANNOT INIT SD");
noutram 1:e1cf7663f5ff 182 errorCode(FATAL);
noutram 1:e1cf7663f5ff 183 }
noutram 1:e1cf7663f5ff 184
noutram 1:e1cf7663f5ff 185 //Create a filing system for SD Card
noutram 1:e1cf7663f5ff 186 FATFileSystem fs("sd", &sd);
noutram 0:36e89e3ed7c4 187
noutram 1:e1cf7663f5ff 188 //Open to WRITE
noutram 1:e1cf7663f5ff 189 FILE* fp = fopen("/sd/test.csv","a");
noutram 1:e1cf7663f5ff 190 if (fp == NULL) {
noutram 1:e1cf7663f5ff 191 error("Could not open file for write\n");
noutram 3:a88838ff33e7 192 lcd.cls();
noutram 3:a88838ff33e7 193 lcd.printf("CANNOT OPEN FILE\n\n");
noutram 1:e1cf7663f5ff 194 errorCode(FATAL);
noutram 1:e1cf7663f5ff 195 }
noutram 3:a88838ff33e7 196
noutram 3:a88838ff33e7 197 //Last message before sampling begins
noutram 3:a88838ff33e7 198 lcd.cls();
noutram 3:a88838ff33e7 199 lcd.printf("READY\n\n");
thomasmorris 5:2594b953f111 200
thomasmorris 5:2594b953f111 201 //Run interrupt
thomasmorris 5:2594b953f111 202 Sample_timer.attach(&Sample_signal_set,TimerInterval);
thomasmorris 7:dfe19413fdc2 203 SW1.fall(&SW1FallingEdge);
thomasmorris 5:2594b953f111 204 //Run Threads
thomasmorris 5:2594b953f111 205
thomasmorris 5:2594b953f111 206 t1.start(Sample);
thomasmorris 5:2594b953f111 207 t2.start(Serial_Comms);
thomasmorris 7:dfe19413fdc2 208 t3.start(Time);
thomasmorris 7:dfe19413fdc2 209 t4.start(PrintTime);
thomasmorris 7:dfe19413fdc2 210 t5.start(ModeSelection);
thomasmorris 5:2594b953f111 211 //Main thread ID
thomasmorris 5:2594b953f111 212
thomasmorris 5:2594b953f111 213 idMain = osThreadGetId(); //CMSIS RTOS call
thomasmorris 5:2594b953f111 214
thomasmorris 5:2594b953f111 215 //Thread ID
thomasmorris 5:2594b953f111 216 id1 = t1.gettid();
thomasmorris 5:2594b953f111 217 id2 = t2.gettid();
thomasmorris 7:dfe19413fdc2 218 id3 = t3.gettid();
thomasmorris 5:2594b953f111 219
thomasmorris 5:2594b953f111 220 //Toggle Green LED after a button has been pressed
noutram 1:e1cf7663f5ff 221 //Press either switch to unmount
thomasmorris 7:dfe19413fdc2 222 DigitalIn onBoardSwitch(USER_BUTTON);
thomasmorris 7:dfe19413fdc2 223 while (onBoardSwitch == 0){
thomasmorris 7:dfe19413fdc2 224
noutram 1:e1cf7663f5ff 225 }
noutram 1:e1cf7663f5ff 226
noutram 1:e1cf7663f5ff 227 //Close File
noutram 1:e1cf7663f5ff 228 fclose(fp);
noutram 1:e1cf7663f5ff 229
noutram 1:e1cf7663f5ff 230 //Close down
noutram 1:e1cf7663f5ff 231 sd.deinit();
noutram 1:e1cf7663f5ff 232 printf("Unmounted...\n");
noutram 1:e1cf7663f5ff 233 lcd.cls();
noutram 1:e1cf7663f5ff 234 lcd.printf("Unmounted...\n\n");
noutram 1:e1cf7663f5ff 235
noutram 1:e1cf7663f5ff 236 while(true) {
noutram 1:e1cf7663f5ff 237 greenLED = 1;
noutram 1:e1cf7663f5ff 238 wait(0.5);
noutram 1:e1cf7663f5ff 239 greenLED = 0;
noutram 1:e1cf7663f5ff 240 wait(0.1);
noutram 0:36e89e3ed7c4 241 }
noutram 0:36e89e3ed7c4 242 }
noutram 0:36e89e3ed7c4 243
noutram 1:e1cf7663f5ff 244
noutram 1:e1cf7663f5ff 245