Data collection core that utilises the nine ring buffer. Intended as the base of other data collection projects

ninelocks_logger_core/ninelocks_logger_core.cpp

Committer:
jont
Date:
2015-07-10
Revision:
0:79615902fcb0
Child:
1:d22102484ea1

File content as of revision 0:79615902fcb0:

#include "ninelocks_logger_core.h"
#include "record_ring.h"
#include "mbed.h"
#include "SDFileSystem.h"  



/*
NineLoggerCore::NineLoggerCore( ){
     
    }*/

NineLoggerCore::NineLoggerCore(Serial * pc){
     _pc = pc;
      
    //_pin = 0;
    }
/*=====================================================================================*/
// Functions to do with data logging
/*=====================================================================================*/
/*
in
long seconds - a timestamp of some sort
char entryType an arbritry flag to label records with
*/
void NineLoggerCore::do_journal(long seconds, char entryType)
{
    log_record logrec;
    logrec.ad1 = 0;
    logrec.ad2 = 0;
    logrec.ad3 = 0;
    logrec.ad4 =0;
    logrec.ad5 = 0;
    logrec.ad6 = 0;
    logrec.count = 0;
    logrec.count2 = 0;
  //  logrec.count = counter_reed.read();
  //  logrec.count2 = counter_reed2.read();
  //  logrec.temperature = read_thermo();
    logrec.timestamp = seconds;
    logrec.record_type = entryType;
    log_buffer.RingWriteToBuffer(logrec);
}

/*=====================================================================================*/
// Write event to the logfile
/*=====================================================================================*/
bool NineLoggerCore::flush_buffered_journal_events()
{
    int remain = 0;
    //char eventlog_filename[255];
 //   if ( rt_info.log_enabled != true) {
 //       return(false);
 //   }
    remain = log_buffer.ring_count();
    if (remain <=0 ) {
        return false;
    }

  
    FILE *fp = fopen(journallog_filename, "a");
    if(!fp) {
        fprintf(stderr, "File %s could not be opened!\n",journallog_filename);
        return false;
    }
    //now write allthose events into the logile
    log_record rec;
    while (log_buffer.ring_count() >0) {
        long z = log_buffer.get_next_record(&rec);
        if (z <0) {
            //uh oh
        } else {
            //write_event(z);
        //    printf("Writing DataJournal...\n");
            fprintf(fp, "%i,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%i,%i,%.1f,%c\n", rec.timestamp,rec.ad1, rec.ad2,rec.ad3, rec.ad4, rec.ad5, rec.ad6,rec.count, rec.count2, rec.temperature,rec.record_type);
          //  time_t seconds =(time_t) rec.timestamp;
           // fprintf(fp,"%s",ctime(&seconds));
        }
    }
   // printf("Closing File...\n");
    fclose(fp);
    return true;
}
 
 
 void NineLoggerCore::setfilename(char* filename){
     
      if(_pc != NULL) {
          //  _pc->printf("called set filename");
        }
     strncpy(journallog_filename, filename,nl_name_length);
     
       if(_pc != NULL) {
          //  _pc->printf("set fname to %s \r\n", filename);
        }
     }