External memory log

Dependents:   Q2_Stabi

Files at this revision

API Documentation at this revision

Comitter:
Decimus
Date:
Mon May 30 08:10:08 2016 +0000
Commit message:
[+]

Changed in this revision

MemLog.cpp Show annotated file Show diff for this revision Revisions of this file
MemLog.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MemLog.cpp	Mon May 30 08:10:08 2016 +0000
@@ -0,0 +1,39 @@
+/* Log system for internal memory flash
+* Copyright (c) 2013 Oleg Evsegneev
+* Released under the MIT License: http://mbed.org/license/mit
+*/
+ 
+#include "mbed.h"
+#include "MemLog.h"
+
+MemLog::MemLog( short cols, short rows ): _local("local") {
+    _cols = cols;
+    _rows = rows;
+    _addr = 0;
+    _row = 0;
+}
+
+void MemLog::log( float val ) {
+    if( _addr >= _rows*_cols )
+        return; // can't write
+    _data[_addr++] = val;
+    if( ( _addr % _cols) == 0 )
+        _row++;
+}
+
+void MemLog::save() {
+    FILE *fp = fopen(LOG_FILE, "w");
+    unsigned short addr = 0;
+    
+    for( short i=0; i<_row; i++){
+        for( short k=0; k<_cols; k++){
+            fprintf( fp, "%.4f", _data[addr++]);
+            if( k < _cols-1 )
+                fprintf( fp, "\t");
+        }
+        fprintf( fp, "\r\n");
+    }
+    fclose(fp);
+    _addr = 0;
+    _row = 0;    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MemLog.h	Mon May 30 08:10:08 2016 +0000
@@ -0,0 +1,40 @@
+/* Log system for internal memory flash
+* Copyright (c) 2013 Oleg Evsegneev
+* Released under the MIT License: http://mbed.org/license/mit
+*/
+
+#ifndef MBED_MEMLOG_H
+#define MBED_MEMLOG_H
+  
+#include "mbed.h"
+
+#define LOG_FILE "/local/log.txt"
+
+class MemLog {
+
+public:
+    MemLog( short cols, short rows );
+
+    /** Set new value to log
+     *
+     * @param value Value of log entry.
+     */
+    void log( float val );
+
+    /**  Save log to file on flash
+     *
+     */
+    void save();
+        
+protected:
+    LocalFileSystem _local;
+    float _data[4000];
+    unsigned short _addr;
+    short _cols;
+    short _rows;
+    short _row;
+    void toArray( float val, char *bytes );
+    float fromArray( char *bytes );
+};
+
+#endif
\ No newline at end of file