Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Revision:
7:dfe19413fdc2
Parent:
6:97f586597310
Child:
8:0e4481b64353
--- a/main.cpp	Sun Dec 10 17:40:58 2017 +0000
+++ b/main.cpp	Mon Dec 11 19:25:35 2017 +0000
@@ -12,10 +12,13 @@
 #include "LED.hpp"
 #define SamplingTime 1
 #define NotSamplingTime 0
+#define Print_Time_to_LCD 1
+#define Dont_Print_Time_to_LCD 0
 #define TimerInterval 15 //This is in seconds
-
+#define EDGE_RISEN 1
+#define EDGE_FALLEN 0
 Serial pc(USBTX, USBRX);
-
+//SW1+SW2 are declared as interrupt ins in sample hardwarec.pp
 // This is a very short demo that demonstrates all the hardware used in the coursework.
 // You will need a network connection set up (covered elsewhere). The host PC should have the address 10.0.0.1
 //Thread ID
@@ -25,6 +28,8 @@
 osThreadId id3;
 osThreadId id4;
 
+Timeout sw1TimeOut;//Used to prevent switch bounce
+
 LED Red_led(PE_15);
 LED Yellow_led(PB_10);
 LED Green_led(PB_11);
@@ -34,13 +39,105 @@
 Thread nwrkThread;
 Thread t1;
 Thread t2;
+Thread t3;
+Thread t4;
+Thread t5;
+Thread t6;
+
+double temp = 0;
+double pressure = 0;
+double lux = 0;
+
+char buffer[32];
+        
+
+void SW1FallingEdge();
+void SW1TimeOutHandler();
+
+int mode = 0;
+
+//Interrupt service routine for handling the timeout
+void SW1TimeOutHandler() {
+    sw1TimeOut.detach();        //Stop the timeout counter firing
+    SW1.fall(&SW1FallingEdge);  //Now wait for a falling edge
+}
+
+//Interrupt service routive for SW1 falling edge (release)
+void SW1FallingEdge() {
+    SW1.fall(NULL);                             //Disable this interrupt
+    Yellow_led.Toggle();                         //Toggle LED  
+    
+    mode = mode +1;//Cycles through modes
+    if(mode >1)
+    {
+     mode = 0;   
+    }
+      
+    sw1TimeOut.attach(&SW1TimeOutHandler, 0.2); //Start timeout counter    
+}
+
+void ModeSelection()
+{
+    while(1){
+            Thread::wait(1000);
+        //Detech the not required interrupt then rettach it when finshed
+        if(mode == 0)//Print values to the LCD
+        {
+                         //Write new data to LCD (not fast!)
+        lcd.cls();
+        lcd.printf("Temp Pres  li\n"); 
+        lcd.printf("%1.1f ",temp);
+        lcd.printf("%1.1f ",pressure);
+        lcd.printf("%1.1f\n",lux);
+        }
+        else if(mode == 1)//Print the Time to the LCD
+        {
+                    //Write new data to LCD (not fast!)
+        lcd.cls();
+        
+        lcd.printf("Current Time:%s", buffer);
+        
+        //Write to SD (potentially slow)
+        //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+        }
+        else
+        {
+            mode = 0;
+        }
+    }
+}
+
+void PrintTime()
+{
+    Thread::signal_wait(Print_Time_to_LCD);
+    while(1)
+    {
+        //lcd.printf("Current Time: \n %s", buffer);
+        Thread::wait(1000);//Waits the thread for 1 second
+    }
+}
+void Time()
+{
+    while (true) 
+    {
+        time_t seconds = time(NULL);
+        //pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds);       
+        //pc.printf("Time as a basic string = %s", ctime(&seconds));
+        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
+        pc.printf("Current Time:%s", buffer);
+        
+        Thread::wait(1000);
+    }   
+}
+
 
 void Serial_Comms()//Thread for Serial Communications
 {
     pc.printf("Hello World \n");
     while(1)
     {
-        pc.printf("Test\n");
+        pc.printf("Test\n");//Use this Line to output a string to Putty
+        Green_led.Toggle();
         Thread::wait(1000);   
     }
 }
@@ -55,23 +152,16 @@
     while(1)
     {     
         Thread::signal_wait(SamplingTime);
+        
         //Read environmental sensors
-        double temp = sensor.getTemperature();
-        double pressure = sensor.getPressure();
-        double lux = adcIn.read();
-        //Write new data to LCD (not fast!)
-        lcd.cls();
-        lcd.printf("Temp Pres  li\n"); 
-        lcd.printf("%1.1f ",temp);
-        lcd.printf("%1.1f ",pressure);
-        lcd.printf("%1.1f\n",lux);
         
-        //Write to SD (potentially slow)
-        //fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure);
+        temp = sensor.getTemperature();
+        pressure = sensor.getPressure();
+        lux = adcIn.read();
+    
     
         Red_led.Toggle();
         t1.signal_set(NotSamplingTime);
-        //Thread::wait(15000);//Time interval
     }
 } 
 
@@ -79,7 +169,7 @@
 {  
     //Greeting
     printf("Testing\n\n");    
-    
+    set_time(1512940530);  // Set RTC time to December 10 2017
     pc.baud(9600);//Sets the Serial Comms Baud Rate
     
     post();//Power on Self Test
@@ -110,13 +200,14 @@
     
     //Run interrupt
     Sample_timer.attach(&Sample_signal_set,TimerInterval);
-    
+    SW1.fall(&SW1FallingEdge);
     //Run Threads
     
-    
     t1.start(Sample);
     t2.start(Serial_Comms);
-    
+    t3.start(Time);
+    t4.start(PrintTime);
+    t5.start(ModeSelection);
     //Main thread ID
     
     idMain = osThreadGetId();   //CMSIS RTOS call
@@ -124,12 +215,13 @@
     //Thread ID
     id1 = t1.gettid();
     id2 = t2.gettid();
-    
+    id3 = t3.gettid();
     
     //Toggle Green LED after a button has been pressed
     //Press either switch to unmount
-    while ((SW1 == 0) && (SW2 == 0)) {
-    
+    DigitalIn  onBoardSwitch(USER_BUTTON);
+    while (onBoardSwitch == 0){
+        
     }
     
     //Close File