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

Committer:
jont
Date:
Mon Jul 13 08:02:01 2015 +0000
Revision:
3:ba7863bdf478
Parent:
2:8587554a1f52
First pre-release without proper docs as the build system seems to be broken

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jont 0:79615902fcb0 1 #include "ninelocks_logger_core.h"
jont 0:79615902fcb0 2 #include "record_ring.h"
jont 0:79615902fcb0 3 #include "mbed.h"
jont 0:79615902fcb0 4 #include "SDFileSystem.h"
jont 0:79615902fcb0 5
jont 0:79615902fcb0 6
jont 0:79615902fcb0 7
jont 0:79615902fcb0 8 /*
jont 0:79615902fcb0 9 NineLoggerCore::NineLoggerCore( ){
jont 0:79615902fcb0 10
jont 0:79615902fcb0 11 }*/
jont 0:79615902fcb0 12
jont 0:79615902fcb0 13 NineLoggerCore::NineLoggerCore(Serial * pc){
jont 0:79615902fcb0 14 _pc = pc;
jont 0:79615902fcb0 15
jont 0:79615902fcb0 16 //_pin = 0;
jont 0:79615902fcb0 17 }
jont 0:79615902fcb0 18 /*=====================================================================================*/
jont 0:79615902fcb0 19 // Functions to do with data logging
jont 0:79615902fcb0 20 /*=====================================================================================*/
jont 0:79615902fcb0 21 /*
jont 0:79615902fcb0 22 in
jont 0:79615902fcb0 23 long seconds - a timestamp of some sort
jont 0:79615902fcb0 24 char entryType an arbritry flag to label records with
jont 2:8587554a1f52 25
jont 2:8587554a1f52 26 You implement version in your own class
jont 0:79615902fcb0 27 */
jont 0:79615902fcb0 28 void NineLoggerCore::do_journal(long seconds, char entryType)
jont 0:79615902fcb0 29 {
jont 0:79615902fcb0 30 log_record logrec;
jont 0:79615902fcb0 31 logrec.ad1 = 0;
jont 0:79615902fcb0 32 logrec.ad2 = 0;
jont 0:79615902fcb0 33 logrec.ad3 = 0;
jont 2:8587554a1f52 34 logrec.ad4 = 0;
jont 0:79615902fcb0 35 logrec.ad5 = 0;
jont 0:79615902fcb0 36 logrec.ad6 = 0;
jont 0:79615902fcb0 37 logrec.count = 0;
jont 0:79615902fcb0 38 logrec.count2 = 0;
jont 2:8587554a1f52 39
jont 2:8587554a1f52 40 logrec.temperature = 0;;
jont 0:79615902fcb0 41 logrec.timestamp = seconds;
jont 0:79615902fcb0 42 logrec.record_type = entryType;
jont 0:79615902fcb0 43 log_buffer.RingWriteToBuffer(logrec);
jont 0:79615902fcb0 44 }
jont 0:79615902fcb0 45
jont 0:79615902fcb0 46 /*=====================================================================================*/
jont 0:79615902fcb0 47 // Write event to the logfile
jont 0:79615902fcb0 48 /*=====================================================================================*/
jont 2:8587554a1f52 49 /*
jont 2:8587554a1f52 50 in reality whats here is a reminder, you override this in your own class
jont 2:8587554a1f52 51 */
jont 0:79615902fcb0 52 bool NineLoggerCore::flush_buffered_journal_events()
jont 0:79615902fcb0 53 {
jont 2:8587554a1f52 54 /*
jont 0:79615902fcb0 55 int remain = 0;
jont 0:79615902fcb0 56 remain = log_buffer.ring_count();
jont 0:79615902fcb0 57 if (remain <=0 ) {
jont 0:79615902fcb0 58 return false;
jont 0:79615902fcb0 59 }
jont 1:d22102484ea1 60
jont 1:d22102484ea1 61 if (remain > buff_size) {
jont 1:d22102484ea1 62 if(_pc != NULL) {
jont 1:d22102484ea1 63 _pc->printf("Buffer Overfill %i\r\n", remain);
jont 1:d22102484ea1 64 }
jont 1:d22102484ea1 65 overrun_count++;
jont 1:d22102484ea1 66 }
jont 0:79615902fcb0 67
jont 0:79615902fcb0 68 FILE *fp = fopen(journallog_filename, "a");
jont 0:79615902fcb0 69 if(!fp) {
jont 0:79615902fcb0 70 fprintf(stderr, "File %s could not be opened!\n",journallog_filename);
jont 0:79615902fcb0 71 return false;
jont 0:79615902fcb0 72 }
jont 0:79615902fcb0 73 //now write allthose events into the logile
jont 0:79615902fcb0 74 log_record rec;
jont 0:79615902fcb0 75 while (log_buffer.ring_count() >0) {
jont 0:79615902fcb0 76 long z = log_buffer.get_next_record(&rec);
jont 0:79615902fcb0 77 if (z <0) {
jont 0:79615902fcb0 78 //uh oh
jont 0:79615902fcb0 79 } else {
jont 0:79615902fcb0 80 //write_event(z);
jont 0:79615902fcb0 81 // printf("Writing DataJournal...\n");
jont 0:79615902fcb0 82 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);
jont 0:79615902fcb0 83 // time_t seconds =(time_t) rec.timestamp;
jont 0:79615902fcb0 84 // fprintf(fp,"%s",ctime(&seconds));
jont 0:79615902fcb0 85 }
jont 0:79615902fcb0 86 }
jont 0:79615902fcb0 87 // printf("Closing File...\n");
jont 0:79615902fcb0 88 fclose(fp);
jont 2:8587554a1f52 89 */
jont 0:79615902fcb0 90 return true;
jont 0:79615902fcb0 91 }
jont 0:79615902fcb0 92
jont 0:79615902fcb0 93
jont 0:79615902fcb0 94 void NineLoggerCore::setfilename(char* filename){
jont 0:79615902fcb0 95
jont 0:79615902fcb0 96 if(_pc != NULL) {
jont 0:79615902fcb0 97 // _pc->printf("called set filename");
jont 0:79615902fcb0 98 }
jont 0:79615902fcb0 99 strncpy(journallog_filename, filename,nl_name_length);
jont 0:79615902fcb0 100
jont 0:79615902fcb0 101 if(_pc != NULL) {
jont 0:79615902fcb0 102 // _pc->printf("set fname to %s \r\n", filename);
jont 0:79615902fcb0 103 }
jont 1:d22102484ea1 104 }
jont 1:d22102484ea1 105
jont 1:d22102484ea1 106
jont 1:d22102484ea1 107 /*=====================================================================*/
jont 1:d22102484ea1 108 // see how many times we have overrun the buffer
jont 1:d22102484ea1 109 /*=====================================================================*/
jont 1:d22102484ea1 110 int NineLoggerCore::get_overrun_count(){
jont 1:d22102484ea1 111 return overrun_count;
jont 1:d22102484ea1 112
jont 2:8587554a1f52 113 }
jont 2:8587554a1f52 114
jont 2:8587554a1f52 115 /*=====================================================================*/
jont 2:8587554a1f52 116 // see how many times we have had write erros
jont 2:8587554a1f52 117 /*=====================================================================*/
jont 2:8587554a1f52 118 int NineLoggerCore::get_write_error_count(){
jont 2:8587554a1f52 119 return write_error_count;
jont 2:8587554a1f52 120
jont 2:8587554a1f52 121 }
jont 2:8587554a1f52 122
jont 2:8587554a1f52 123 /*=====================================================================*/
jont 2:8587554a1f52 124 // see how much buffer was used
jont 2:8587554a1f52 125 /*=====================================================================*/
jont 2:8587554a1f52 126 int NineLoggerCore::get_max_fill(){
jont 2:8587554a1f52 127 return max_buffer_use;
jont 2:8587554a1f52 128
jont 2:8587554a1f52 129 }