Log system based on external FRAM storage. Allow to collect float values in FRAM, and then store whole collection into file on flash.
Sample code
#include "mbed.h" #include "FRAMLog.h" SPI spi(p5,p6,p7); FRAMLog flog(spi, p8, 4, 2048); Serial pc(USBTX, USBRX); int main() { for( int i=0; i<2048; i++){ flog.log(i/4096.0); flog.log(i/(4096.0+1)); flog.log(i/(4096.0+2)); flog.log(i/(4096.0+3)); } flog.save(); }
FileLog.cpp@0:f211f4870371, 2012-10-15 (annotated)
- Committer:
- Decimus
- Date:
- Mon Oct 15 17:04:49 2012 +0000
- Revision:
- 0:f211f4870371
Logging on internal Flash
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Decimus | 0:f211f4870371 | 1 | /* mbed File Log Library |
Decimus | 0:f211f4870371 | 2 | * Copyright (c) 2012 Oleg Evsegneev |
Decimus | 0:f211f4870371 | 3 | * |
Decimus | 0:f211f4870371 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Decimus | 0:f211f4870371 | 5 | * of this software and associated documentation files (the "Software"), to deal |
Decimus | 0:f211f4870371 | 6 | * in the Software without restriction, including without limitation the rights |
Decimus | 0:f211f4870371 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Decimus | 0:f211f4870371 | 8 | * copies of the Software, and to permit persons to whom the Software is |
Decimus | 0:f211f4870371 | 9 | * furnished to do so, subject to the following conditions: |
Decimus | 0:f211f4870371 | 10 | * |
Decimus | 0:f211f4870371 | 11 | * The above copyright notice and this permission notice shall be included in |
Decimus | 0:f211f4870371 | 12 | * all copies or substantial portions of the Software. |
Decimus | 0:f211f4870371 | 13 | * |
Decimus | 0:f211f4870371 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Decimus | 0:f211f4870371 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Decimus | 0:f211f4870371 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Decimus | 0:f211f4870371 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Decimus | 0:f211f4870371 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Decimus | 0:f211f4870371 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Decimus | 0:f211f4870371 | 20 | * THE SOFTWARE. |
Decimus | 0:f211f4870371 | 21 | */ |
Decimus | 0:f211f4870371 | 22 | |
Decimus | 0:f211f4870371 | 23 | #include "mbed.h" |
Decimus | 0:f211f4870371 | 24 | #include "FileLog.h" |
Decimus | 0:f211f4870371 | 25 | |
Decimus | 0:f211f4870371 | 26 | FileLog::FileLog( short cols, short rows ): _local("local") { |
Decimus | 0:f211f4870371 | 27 | _cols = cols; |
Decimus | 0:f211f4870371 | 28 | _rows = rows; |
Decimus | 0:f211f4870371 | 29 | _data_idx = 0; |
Decimus | 0:f211f4870371 | 30 | _data = new float[cols*rows]; |
Decimus | 0:f211f4870371 | 31 | } |
Decimus | 0:f211f4870371 | 32 | |
Decimus | 0:f211f4870371 | 33 | void FileLog::log( float value ) { |
Decimus | 0:f211f4870371 | 34 | _data[_data_idx++] = value; |
Decimus | 0:f211f4870371 | 35 | } |
Decimus | 0:f211f4870371 | 36 | |
Decimus | 0:f211f4870371 | 37 | void FileLog::save() { |
Decimus | 0:f211f4870371 | 38 | FILE *fp = fopen("/local/log.txt", "w"); |
Decimus | 0:f211f4870371 | 39 | for( short i=0; i<_data_idx; i++){ |
Decimus | 0:f211f4870371 | 40 | if( i && (i % _cols == 0)) |
Decimus | 0:f211f4870371 | 41 | fprintf( fp, "\r\n"); |
Decimus | 0:f211f4870371 | 42 | else if( i && i != _data_idx-1 ) |
Decimus | 0:f211f4870371 | 43 | fprintf( fp, "\t"); |
Decimus | 0:f211f4870371 | 44 | fprintf( fp, "%.4f", _data[i]); |
Decimus | 0:f211f4870371 | 45 | } |
Decimus | 0:f211f4870371 | 46 | fclose(fp); |
Decimus | 0:f211f4870371 | 47 | |
Decimus | 0:f211f4870371 | 48 | delete [] _data; |
Decimus | 0:f211f4870371 | 49 | _data = NULL; |
Decimus | 0:f211f4870371 | 50 | } |