PROJ515 / Mbed OS ELEC-351-GROUP-E-CW

Dependencies:   BMP280

Files at this revision

API Documentation at this revision

Comitter:
Swaggie
Date:
Tue Jan 09 11:53:11 2018 +0000
Parent:
19:40c721f01ed2
Child:
21:6e733076f49c
Commit message:
Inputted Logging in main function and in LCD function.

Changed in this revision

LCD.cpp Show annotated file Show diff for this revision Revisions of this file
LCD.h Show annotated file Show diff for this revision Revisions of this file
Logging.cpp Show annotated file Show diff for this revision Revisions of this file
Logging.h 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/LCD.cpp	Tue Jan 09 11:39:59 2018 +0000
+++ b/LCD.cpp	Tue Jan 09 11:53:11 2018 +0000
@@ -39,6 +39,8 @@
         cls();
         switch (_currentState) {
             case SCROLLREADINGS:
+                LogEvent(Log_LCDScroll);   //Log position
+                
                 tm T = ReturnDateTimeStruct(_latestTime);   //Get a time structure
                 printf("Temp: %5.1fC\n",_latestTemp);   //Print temperature
                 printf("Pres: %5.1fmBar\n",_latestPres);//Print Pressure
@@ -51,6 +53,7 @@
                 break;
 
             case MESSAGE:
+                LogEvent(Log_LCDMessage);   //Log position
                 printf("%s\n",_message);    //Print custom message to display
                 Thread::wait(SCREENDELAY_MS);//Hold screen for specified time
                 if (_AutoQuit) {
--- a/LCD.h	Tue Jan 09 11:39:59 2018 +0000
+++ b/LCD.h	Tue Jan 09 11:53:11 2018 +0000
@@ -5,6 +5,7 @@
 #include "TextLCD.h"
 #include "rtos.h"
 #include "TimeInterface.h"
+#include "Logging.h"
 #include <string>
 /*
 *The ENVDISPLAY class enhances the abilities of the TextLCD class to be
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Logging.cpp	Tue Jan 09 11:53:11 2018 +0000
@@ -0,0 +1,38 @@
+#include "Logging.h"
+#include <string>
+
+using namespace std;
+Mail<uint32_t,32> ActivityLog;
+bool logging = false;
+
+void LogEvent(uint32_t eventCode)
+{
+    if (logging) {  //Only do if logging is enabled
+        uint32_t* Status = ActivityLog.alloc(); //Allocate space in mailbox
+        *Status = eventCode;    //Move data into memory
+        ActivityLog.put(Status);//data is in queue 
+    }
+}
+
+string CheckLoggingQueue(void)
+{
+    string returnString = "";
+    if (logging) {  //Only do if logging is enabled
+        osEvent evt = ActivityLog.get();    //Get the object from mailbox. This blocks until there is data
+        if (evt.status == osEventMail) {
+            //Does it have content? If not, there is no mail.
+            uint32_t* mail = (uint32_t*)evt.value.p;    //extract the value passed
+            //Convert int value to string
+            char intStr[4]; //Has to be done in two steps because of known to_string bug stackoverflow.com/questions/12975341
+            sprintf(intStr, "%d", *mail);
+            string mailAsString = string(intStr);
+            ActivityLog.free(mail);     //Now we can release the space in the mailbox
+            returnString = "Data Logged:\n\r" + mailAsString;
+            return returnString;
+        } else {
+            //There wasn't any data
+            string returnString = "ERROR: There wasn't any data";
+        }
+    }
+    return returnString;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Logging.h	Tue Jan 09 11:53:11 2018 +0000
@@ -0,0 +1,48 @@
+#ifndef __Logging__
+#define __Logging__
+
+/*
+* This module provides the logging functionality of the system.
+* The LogEvent function is inserted into the code at key points, passing an
+* integer value which represents the state. These are listed in the #defines
+* below.
+* The values are then insterted into a mailbox, where they fetched by the
+* CheckLoggingQueue function. This returns a string which also has a suffix
+* indicating logging. This can then be outputted to an interface. This function
+* should be placed within a repeating thread. To use the function with a printf
+* statement, you must convert between string and char array using:
+
+string QueueData = CheckLoggingQueue(); //Get the data from queue
+char char_array[QueueData.length()+1];  //Char array for message
+strcpy(char_array, QueueData.c_str());  //String must be converted to char array for printf
+PC.printf("%s\n\r",char_array); 
+
+* Functions incorporate a check on the state of Logging before acting. CheckLoggingQueue
+* will return an empty string if there is no logging.
+*/
+#include "mbed.h"
+#include "rtos.h"
+#include <string>
+
+#define Log_SampleTicker 0
+#define Log_EnviromSampled 1
+#define Log_TimeSampled 2
+#define Log_IndexInc 3
+#define Log_OldestInc 4
+#define Log_LCDScroll 5
+#define Log_LCDMessage 6
+#define Log_LCDTime 7
+#define Log_SetDateFail 8
+#define Log_SetTimeFail 9
+#define Log_EthCongifd 10
+#define Log_EthRequest 11
+
+extern Mail<uint32_t,32> ActivityLog;   //Mailbox that holds the activity
+
+extern bool logging;
+//Has logging been enabled in the system?
+void LogEvent(uint32_t eventCode);
+//Producer function - adds state to queue
+string CheckLoggingQueue(void);
+//Consumer function - wraps the number into a string with a suffix.
+#endif
\ No newline at end of file
--- a/main.cpp	Tue Jan 09 11:39:59 2018 +0000
+++ b/main.cpp	Tue Jan 09 11:53:11 2018 +0000
@@ -5,6 +5,7 @@
 #include "LCD.h"
 #include "SDCard.h"
 #include "SDBlockDevice.h"
+#include "Logging.h"
 
 //Hardware setup
 BMP280 sensor(D14, D15);    //enviromental sensor
@@ -24,6 +25,7 @@
 {
     //Initialise devices
     firstSample = true; //Set only at start of program
+    logging = true;
     //Hardware Self Test
 
     //Initialise interrupts and times
@@ -44,8 +46,13 @@
 
             //push latest samples to LCD.
             lcd.UpdateData(tempReadings[currentIndex],presReadings[currentIndex],LDRReadings[currentIndex],timeReadings[currentIndex]);
-
+            LogEvent(Log_IndexInc);   //Log position
         }
-
+        if (logging) {
+            string QueueData = CheckLoggingQueue(); //Get the data from queue
+            char char_array[QueueData.length()+1];  //Char array for message
+            strcpy(char_array, QueueData.c_str());  //String must be converted to char array for printf
+            PC.printf("%s\n\r",char_array);
+        }
     }
 }
\ No newline at end of file