Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Files at this revision

API Documentation at this revision

Comitter:
thomasmorris
Date:
Sat Jan 06 17:43:17 2018 +0000
Parent:
29:64b1f95a807c
Child:
31:4a88bf97b53e
Commit message:
Latest Revision LCD time setting

Changed in this revision

SETUP.hpp Show annotated file Show diff for this revision Revisions of this file
TIME.hpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SETUP.hpp	Wed Jan 03 22:00:07 2018 +0000
+++ b/SETUP.hpp	Sat Jan 06 17:43:17 2018 +0000
@@ -8,7 +8,7 @@
 #include "DATA.hpp"
 #include "LCD.hpp"
 #include "SERIAL.hpp"
-
+#include "TIME.hpp"
 #define SerialCommsTime 1
 #define NotSerialCommsTime 0
 #define SamplingTime 1
@@ -40,6 +40,7 @@
 
 
 Timeout sw1TimeOut;//Used to prevent switch bounce
+Timeout sw2TimeOut;//Used to prevent switch bounce
 
 LED Red_led(PE_15);
 LED Yellow_led(PB_10);
@@ -75,6 +76,8 @@
 void SW1TimeOutHandler();
 void Sampling_ISR();
 void Console_Output_ISR();
+void SW1RisingEdge();
+void SW2RisingEdge();
 
 
 using namespace std;
@@ -83,5 +86,6 @@
 
 Mail<DATA, mailsize> mail_box;       //Mail Queue, Type DATA, Capacity mailsize(defined above), name mail_box
 
+
 //DigitalIn  onBoardSwitch(USER_BUTTON);
 #endif 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TIME.hpp	Sat Jan 06 17:43:17 2018 +0000
@@ -0,0 +1,82 @@
+#ifndef _TIME_HPP_ //Known as header guards
+#define _TIME_HPP_
+
+#include "mbed.h"
+#include "rtos.h"
+int current_time_global = 0;
+int new_time = 0;
+int get_current_time()
+{
+
+current_time_global = time(0);
+return current_time_global;
+}
+//Time File
+
+//Seconds to Seconds
+void Add_Second()
+{
+    new_time = time(0) + 1;
+    set_time(new_time);    
+}
+void Subtract_Second()
+{
+    new_time = time(0) - 1;
+    set_time(new_time);    
+}
+//Minutes to Seconds
+void Add_Minute()
+{
+    new_time = time(0) + 60;
+    set_time(new_time);    
+}
+void Subtract_Minute()
+{
+    new_time = time(0) - 60;
+    set_time(new_time);    
+}
+//Hours to Seconds
+void Add_hour()
+{
+    new_time = time(0) + 3600;
+    set_time(new_time); 
+}
+void Subtract_hour()
+{
+    new_time = time(0) - 3600;
+    set_time(new_time);   
+}
+//Days to Seconds
+void Add_Day()
+{
+    new_time = time(0) - 86400;
+    set_time(new_time);  
+}
+void Subtract_Day()
+{
+    new_time = time(0) - 86400;
+    set_time(new_time);  
+}
+//Months to Seconds
+void Add_Month()
+{
+    new_time = time(0) + 2629743;
+    set_time(new_time);  
+}
+void Subtract_Month()
+{
+    new_time = time(0) - 2629743;
+    set_time(new_time);  
+}
+//Years
+void Add_Year()
+{
+    new_time = time(0) + 31556926;
+    set_time(new_time);     
+}
+void Subtract_Year()
+{
+    new_time = time(0) + 31556926;
+    set_time(new_time);     
+}
+#endif
\ No newline at end of file
--- a/main.cpp	Wed Jan 03 22:00:07 2018 +0000
+++ b/main.cpp	Sat Jan 06 17:43:17 2018 +0000
@@ -1,7 +1,7 @@
 /*
 ELEC 351 Group T
 Team Members : Christopher Hills, Thomas Morris
-Current Verision 12
+Current Verision 13
 Overiew: Working Tasks 1,2,4,5,6,7,8,10,11,12,13
 
 Last Revision: Added Mail Box to serial
@@ -29,11 +29,15 @@
         networktest();
     }
 }
-
+void SW2TimeOutHandler()
+{
+    sw2TimeOut.detach();//Stop the timeout counter firing
+    SW2.rise(&SW2RisingEdge);//Now wait for a rising edge
+}
 void SW1TimeOutHandler()
 {
     sw1TimeOut.detach();        //Stop the timeout counter firing
-    SW1.fall(&SW1FallingEdge);  //Now wait for a falling edge
+    SW1.rise(&SW1RisingEdge);  //Now wait for a rising edge
 }
 void SDWrite()
 {
@@ -50,58 +54,126 @@
         mail_box.free(Rec_Data_SD);                                     //Free space in the mailbox (delete earliest sample taken)
     }
 }
-//Interrupt service routive for SW1 falling edge (release)
-void SW1FallingEdge()
+//Interrupt service routive for SW1 rising edge (release)
+void SW1RisingEdge()
 {
-    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.35);     //Start timeout counter
+    wait(0.1);
+    if(SW2 == 1 & SW1 == 1)
+    {
+    wait(0.2);                                      //Wait 200ms
+        if(SW1 == 0 & SW2 == 0)
+        {
+            if(mode < 7)
+            {
+                mode = mode + 1;
+            }
+            else if (mode == 7)
+            {
+                mode = 0;    
+            }   
+        }
     }
+}
 
-    sw1TimeOut.attach(&SW1TimeOutHandler, 0.2); //Start timeout counter
+void SW2RisingEdge()
+{
+    
+    while (SW1 == 0) { }
+    
+    //Insert a small delay to reduce switch bounce effects
+    wait(0.05);
+    
+    // Wait for SW1 to be released
+    while (SW1 == 1) { }
+    
+    mode = mode + 1;
 }
-void ModeSelection()
-{
-    while(1) 
+
+void LCD_Output()
+{          
+    while(1)
     {
+        Thread::wait(10);//Dont Delete
         osEvent evt_lcd = mail_box.get();                        //Get the latest entry from "mail_box"
-
-        if (evt_lcd.status == osEventMail) {
+        DATA msg_lcd;  
+        if (evt_lcd.status == osEventMail)
+        {
             DATA *Rec_Data_LCD = (DATA*)evt_lcd.value.p;                    //Create pointer to mailbox
-            DATA msg_lcd;                                                   //Create temporary instance of DATA class
+                                                             //Create temporary instance of DATA class
 
             msg_lcd.set_time(Rec_Data_LCD->get_time());                     //Copy time from mailbox to temporary instance
             msg_lcd.set_temperature(Rec_Data_LCD->get_temperature());       //Copy temperature from mailbox to temporary instance
             msg_lcd.set_pressure(Rec_Data_LCD->get_pressure());             //Copy pressure from mailbox to temporary instance
             msg_lcd.set_light(Rec_Data_LCD->get_light());                   //Copy light from mailbox to temporary instance
             mail_box.free(Rec_Data_LCD);                                    //Free space in the mailbox (delete earliest sample taken)
-
-            if(mode == 0) { //Print values to the LCD
-                //Write new data to LCD (not fast!)
-
-                sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting
-                
-                LCD.DDRAM_Address(0x00);
-                LCD.Write_String("Temp Pres  li");
-                LCD.DDRAM_Address(0x40);
-                LCD.Write_String(LCD_buffer);
+        }
+        if(mode == 0)//Default mode
+        { 
+            cout << "In mode 0" << endl;
+            SW1.rise(&SW1RisingEdge);//Now wait for a rising edg
+            //SW2.rise(&SW2RisingEdge);//Now wait for a rising edg
+            Thread::wait(3000);
+            
+            sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting   
+            LCD.DDRAM_Address(0x00);
+            LCD.Write_String("Temp Pres  li");
+            LCD.DDRAM_Address(0x40);
+            LCD.Write_String(LCD_buffer);
 
-            } 
-            else if(mode == 1)  //Print the Time to the LCD
-            {    
-                time_t msel_time = msg_lcd.get_time();                                  //Declare local variable for time
-                strftime(scom_time_buffer, 32, "%I:%M %p", localtime(&msel_time));     //Format time as a string
-                LCD.Display_Clear();
-                LCD_sprintf = sprintf (LCD_buffer, "%s",scom_time_buffer);
-                LCD.DDRAM_Address(0x00);
-                LCD.Write_String("Current Time:");
-                LCD.DDRAM_Address(0x40);
-                LCD.Write_String(LCD_buffer);
+            Thread::wait(3000);    
+                
+            time_t msel_time = msg_lcd.get_time();                                 //Declare local variable for time
+            strftime(scom_time_buffer, 32, "%I:%M %p", localtime(&msel_time));     //Format time as a string
+            LCD.Display_Clear();
+            LCD_sprintf = sprintf (LCD_buffer, "%s",scom_time_buffer);
+            LCD.DDRAM_Address(0x00);
+            LCD.Write_String("Current Time:");
+            LCD.DDRAM_Address(0x40);
+            LCD.Write_String(LCD_buffer);
+        }
+        else if(mode == 1)//Choose either date setting or time setting
+        {
+            SW1.rise(NULL);
+            SW2.rise(NULL);
+            Thread::wait(10);
+            cout << "In Mode 1" << endl;
+            LCD.Display_Clear();
+            LCD.DDRAM_Address(0x00);
+            LCD.Write_String("Date        Time");
+            LCD.DDRAM_Address(0x40);
+            LCD.Write_String("<              >");
+            
+            
 
-            }else{mode = 0;}//Set a default mode
+        } 
+        else if(mode == 2)//Set the Year
+        {
+            cout << "In Mode 2" << endl;
+        } 
+        else if(mode == 3)//Set the Month
+        {
+            cout << "In Mode 3" << endl;
+        }
+        else if(mode == 4)//Set the Day
+        {
+            cout << "In Mode 4" << endl;
+        } 
+        else if(mode == 5)//Set the Hour
+        {
+            cout << "In Mode 5" << endl;
+        } 
+        else if(mode == 6)//Set the Minute
+        {
+            cout << "In Mode 6" << endl;
+        }  
+        else if(mode == 7)//Set the Seconds
+        {
+            cout << "In Mode 7" << endl;
+        }
+        else
+        {
+            mode == 0;
         }
     }
 }
@@ -117,6 +189,7 @@
         if (Serial_Input == "Test")
         {
             cout << "Test Confirmed" << endl;
+            cout << "This is the time " << get_current_time() << endl;
         } 
         else if(Serial_Input == "READALL")
         {
@@ -192,6 +265,10 @@
         {
             Console_Output_Timer.detach(); //Stops Sampling   
         }
+         else if(Serial_Input == "Blank Blank")
+        {
+           cout << "Blank Blank complete" << endl;
+        }
         else 
         {
             cout << "Please enter an acceptable command" << endl;
@@ -233,7 +310,8 @@
 
 void Sample()//Samples the hardware and prints the result to the LCD
 {
-    while(1) {
+    while(1) 
+    {
         Thread::signal_wait(SamplingTime);      //Set the time between samples
 
         temp = sensor.getTemperature();         //Read Temperature
@@ -253,11 +331,6 @@
         Send_Data->set_light(lux);              //Pass in Light
 
         osStatus stat = mail_box.put(Send_Data);    //Puts "Send_Data" into the mailbox
-        
-        //if(stat == osErrorNoMemory)
-       // {
-        //    mail_box.free(Send_Data);
-       // }
 
         if (stat == osErrorResource) {  //If mailbox overfills
             mail_box.free(Send_Data);   //Free the mail box
@@ -273,6 +346,7 @@
     pc.printf("\n\r");
 
     set_time(1512940530);               //Set RTC time to December 10 2017
+    
     pc.baud(9600);                      //Sets the Serial Comms Baud Rate
 
     LCD.Initialise();
@@ -300,17 +374,16 @@
     }
     //Last message before sampling begins
     LCD.Display_Clear();
-    LCD.Write_String("READY\n\n");
+    LCD.Write_String("READY");
 
 
     Sample_Rate = TimerInterval;
     //Run interrupt
-    SW1.fall(&SW1FallingEdge);
     Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
 
     t1.start(Sample);
     t2.start(Serial_Comms);
-    t3.start(ModeSelection);
+    t3.start(LCD_Output);
     //t4.start(Network);
     t5.start(Serial_Commands);