Baseline for testing

Dependents:   mbed_escm2000

Revision:
2:486d068deff9
Parent:
1:aaddf80105fe
Child:
3:2b760f267603
--- a/EventLog.cpp	Thu Sep 12 11:28:11 2019 +0000
+++ b/EventLog.cpp	Tue Sep 17 13:48:38 2019 +0000
@@ -1,4 +1,25 @@
+/**************************************************************************
+ * @file     EventLog.cpp
+ * @brief    Base class for wrapping the interface with the ESCM Event Log 
+ * @version: V1.0
+ * @date:    9/17/2019
 
+ *
+ * @note
+ * Copyright (C) 2019 E3 Design. All rights reserved.
+ *
+ * @par
+ * E3 Designers LLC is supplying this software for use with Cortex-M3 LPC1768
+ * processor based microcontroller for the ESCM 2000 Monitor and Display.  
+ *  *
+ * @par
+ * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
+ * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
+ * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
+ * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
+ *
+ ******************************************************************************/
 #include "mbed.h"
 #include "EventLog.h"
 #include "stdio.h"
@@ -14,16 +35,19 @@
 const char * filename = "/local/events.bin";
     
 
+/******************************************************************************/
 ESCM_Event::ESCM_Event(): address(0),port(0xFF)
 {
     
 }
 
+/******************************************************************************/
 ESCM_Event::ESCM_Event(uint16_t address): address(address),port(0xFF)
 {
     setTimeNow();
 }
 
+/******************************************************************************/
 void ESCM_Event::setTimeNow(void)
 {
     time_t rawtime;
@@ -40,50 +64,52 @@
     day     = timeinfo->tm_mday;
 }
 
+/******************************************************************************/
 void ESCM_Event::getTime (char * buffer) {
     sprintf(buffer,"%02d:%02d %02d/%02d/%04d", hours,mins,month,day,year);
 }
     
 
 
+/******************************************************************************/
 ESCM_EventLog::ESCM_EventLog() {
     full_=false;
+    dirty=false;
     head_=0;
     tail_=0;
 
 }
 
+/******************************************************************************/
 ESCM_EventLog::~ESCM_EventLog() {
 }
 
+/******************************************************************************/
 int ESCM_EventLog::init()
 {
     full_=false;
+    dirty = true;
     head_=0;
     tail_=0;
-    
+
     printf("Initializing Event Log\n\r");
-    
-    if ( !load() )
-    {
+    if ( !load() ) {
         memset ( events,0,sizeof(ESCM_Event)*MAX_EVENTS);
         // for testing
-        for ( int i=0;i<MAX_EVENTS;i++)
-        { 
+        for ( int i=0; i<MAX_EVENTS; i++) {
             add(i*2);
         }
         save();
-         
+
     }
-    
+
     return 1;
 }
 
+/******************************************************************************/
 int ESCM_EventLog::load()
-{
-#if 1
-    
-    printf("Reading %s ...",filename );
+{    
+    printf("Reading %s ...\n\r",filename );
     FILE *input = fopen(filename, "rb");
     if(input!= NULL){
         
@@ -93,7 +119,8 @@
         fread(&code,  sizeof(uint16_t),1,input);
         fread(&size,  sizeof(uint16_t),1,input);
         
-        if (code != 0xFAFA && size > 50 ) {
+        if (code != EVENT_LOG_FORMAT && size != MAX_EVENTS ) {
+            fclose(input);
             return 0;
         }
             
@@ -102,7 +129,6 @@
         
         for (int i=0;i<MAX_EVENTS;i++)
         {
-            printf("." );
             fread( &events[i].port,        sizeof(uint16_t),1 , input);
             fread( &events[i].address,     sizeof(uint16_t),1 , input);
             fread( &events[i].hours,       sizeof(uint16_t),1 , input);
@@ -121,19 +147,23 @@
         printf("Could not Read %s\n\r",filename );
         return 0;
     }
-#endif
     return 1;  
 }
 
+/******************************************************************************/
 int ESCM_EventLog::save()
 {
-#if 1
-    printf("Saving %s ...",filename );
+    int result = 1;
+    
+    if (!dirty) 
+        return 0;
+
+    printf("Saving %s ...\n\r",filename );
     
     FILE *output = fopen(filename, "wb");
     if (output != NULL) {
         
-        uint16_t code = 0xFAFA;
+        uint16_t code = EVENT_LOG_FORMAT;
         uint16_t size = MAX_EVENTS;
         
         fwrite(&code,  sizeof(uint16_t),1,output);
@@ -142,8 +172,7 @@
         fwrite(&tail_, sizeof(uint16_t),1,output);
         
         for (int i=0;i<MAX_EVENTS;i++)
-        {
-            //printf("." );    
+        {  
             fwrite( &events[i].port,        sizeof(uint16_t),1 , output);
             fwrite( &events[i].address,     sizeof(uint16_t),1 , output);
             fwrite( &events[i].hours,       sizeof(uint16_t),1 , output);
@@ -152,23 +181,25 @@
             fwrite( &events[i].day,         sizeof(uint16_t),1 , output);
             fwrite( &events[i].month,       sizeof(uint16_t),1 , output);
             fwrite( &events[i].year,        sizeof(uint16_t),1 , output);
+            ThisThread::sleep_for(1);
                 
         }
         fflush(output);
         fclose(output);
+        dirty = false;
+        result = 1;
     }
     else
     {
         printf("Could not save %s\n\r",filename );
-        return 0;
+        result = 0;
     }
     
-#endif
-    
     printf("Done \n\r" );
-    return 1;
+    return result;
 }
 
+/******************************************************************************/
 void ESCM_EventLog::display(Serial *pc)
 {
     int j = tail_;
@@ -204,50 +235,30 @@
     }
     pc->printf("-----------------------------\n\r");
 }
+/******************************************************************************/
 void ESCM_EventLog::add(uint16_t address, uint16_t port)
 {
-    
-    
     ESCM_Event newEvent;
     newEvent.address = address;
     newEvent.port    = port;
     newEvent.setTimeNow();
-    //sprintf ( newEvent.timeStr , "%s", "===");
-    
     put( newEvent );
-    
 }
 
+/******************************************************************************/
 void ESCM_EventLog::add(uint16_t address)
 {
-    
-    
     ESCM_Event newEvent;
     newEvent.address = address;
     newEvent.port    = 0;
     newEvent.setTimeNow();
-    //sprintf ( newEvent.timeStr , "%s", "===");
-    
     put( newEvent );
-    
 }
 
-void ESCM_EventLog::add(uint16_t address, char* timeStamp)
-{
-    ESCM_Event newEvent;
-    newEvent.address = address;
-    newEvent.setTimeNow();
-    //sprintf ( newEvent.timeStr , "%-16s", timeStamp);
-    
-    put( newEvent );
-    
-}
-
+/******************************************************************************/
 ESCM_Event * ESCM_EventLog::index (int pos ){
 
-
     ESCM_Event * result = NULL;
-    
     if (!empty())
     {
         if (pos < size() || pos >= 0 ) {
@@ -260,7 +271,5 @@
             result = &(events[index]); 
         }
     }
-    
     return result;
-    
 }
\ No newline at end of file