SD card data log
FATFileSystem/MemFileSystem.h@0:18d80d7403d3, 2019-11-03 (annotated)
- Committer:
- kotachi
- Date:
- Sun Nov 03 07:28:09 2019 +0000
- Revision:
- 0:18d80d7403d3
SD card logging system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kotachi | 0:18d80d7403d3 | 1 | /* mbed Microcontroller Library - MemFileSystem |
kotachi | 0:18d80d7403d3 | 2 | * Copyright (c) 2008, sford |
kotachi | 0:18d80d7403d3 | 3 | */ |
kotachi | 0:18d80d7403d3 | 4 | |
kotachi | 0:18d80d7403d3 | 5 | |
kotachi | 0:18d80d7403d3 | 6 | #ifndef MBED_MEMFILESYSTEM_H |
kotachi | 0:18d80d7403d3 | 7 | #define MBED_MEMFILESYSTEM_H |
kotachi | 0:18d80d7403d3 | 8 | |
kotachi | 0:18d80d7403d3 | 9 | #include "FATFileSystem.h" |
kotachi | 0:18d80d7403d3 | 10 | |
kotachi | 0:18d80d7403d3 | 11 | namespace mbed |
kotachi | 0:18d80d7403d3 | 12 | { |
kotachi | 0:18d80d7403d3 | 13 | |
kotachi | 0:18d80d7403d3 | 14 | class MemFileSystem : public FATFileSystem |
kotachi | 0:18d80d7403d3 | 15 | { |
kotachi | 0:18d80d7403d3 | 16 | public: |
kotachi | 0:18d80d7403d3 | 17 | |
kotachi | 0:18d80d7403d3 | 18 | // 2000 sectors, each 512 bytes (malloced as required) |
kotachi | 0:18d80d7403d3 | 19 | char *sectors[2000]; |
kotachi | 0:18d80d7403d3 | 20 | |
kotachi | 0:18d80d7403d3 | 21 | MemFileSystem(const char* name) : FATFileSystem(name) { |
kotachi | 0:18d80d7403d3 | 22 | memset(sectors, 0, sizeof(sectors)); |
kotachi | 0:18d80d7403d3 | 23 | } |
kotachi | 0:18d80d7403d3 | 24 | |
kotachi | 0:18d80d7403d3 | 25 | virtual ~MemFileSystem() { |
kotachi | 0:18d80d7403d3 | 26 | for(int i = 0; i < 2000; i++) { |
kotachi | 0:18d80d7403d3 | 27 | if(sectors[i]) { |
kotachi | 0:18d80d7403d3 | 28 | free(sectors[i]); |
kotachi | 0:18d80d7403d3 | 29 | } |
kotachi | 0:18d80d7403d3 | 30 | } |
kotachi | 0:18d80d7403d3 | 31 | } |
kotachi | 0:18d80d7403d3 | 32 | |
kotachi | 0:18d80d7403d3 | 33 | // read a sector in to the buffer, return 0 if ok |
kotachi | 0:18d80d7403d3 | 34 | virtual int disk_read(char *buffer, int sector) { |
kotachi | 0:18d80d7403d3 | 35 | if(sectors[sector] == 0) { |
kotachi | 0:18d80d7403d3 | 36 | // nothing allocated means sector is empty |
kotachi | 0:18d80d7403d3 | 37 | memset(buffer, 0, 512); |
kotachi | 0:18d80d7403d3 | 38 | } else { |
kotachi | 0:18d80d7403d3 | 39 | memcpy(buffer, sectors[sector], 512); |
kotachi | 0:18d80d7403d3 | 40 | } |
kotachi | 0:18d80d7403d3 | 41 | return 0; |
kotachi | 0:18d80d7403d3 | 42 | } |
kotachi | 0:18d80d7403d3 | 43 | |
kotachi | 0:18d80d7403d3 | 44 | // write a sector from the buffer, return 0 if ok |
kotachi | 0:18d80d7403d3 | 45 | virtual int disk_write(const char *buffer, int sector) { |
kotachi | 0:18d80d7403d3 | 46 | // if buffer is zero deallocate sector |
kotachi | 0:18d80d7403d3 | 47 | char zero[512]; |
kotachi | 0:18d80d7403d3 | 48 | memset(zero, 0, 512); |
kotachi | 0:18d80d7403d3 | 49 | if(memcmp(zero, buffer, 512)==0) { |
kotachi | 0:18d80d7403d3 | 50 | if(sectors[sector] != 0) { |
kotachi | 0:18d80d7403d3 | 51 | free(sectors[sector]); |
kotachi | 0:18d80d7403d3 | 52 | sectors[sector] = 0; |
kotachi | 0:18d80d7403d3 | 53 | } |
kotachi | 0:18d80d7403d3 | 54 | return 0; |
kotachi | 0:18d80d7403d3 | 55 | } |
kotachi | 0:18d80d7403d3 | 56 | // else allocate a sector if needed, and write |
kotachi | 0:18d80d7403d3 | 57 | if(sectors[sector] == 0) { |
kotachi | 0:18d80d7403d3 | 58 | char *sec = (char*)malloc(512); |
kotachi | 0:18d80d7403d3 | 59 | if(sec==0) { |
kotachi | 0:18d80d7403d3 | 60 | return 1; // out of memory |
kotachi | 0:18d80d7403d3 | 61 | } |
kotachi | 0:18d80d7403d3 | 62 | sectors[sector] = sec; |
kotachi | 0:18d80d7403d3 | 63 | } |
kotachi | 0:18d80d7403d3 | 64 | memcpy(sectors[sector], buffer, 512); |
kotachi | 0:18d80d7403d3 | 65 | return 0; |
kotachi | 0:18d80d7403d3 | 66 | } |
kotachi | 0:18d80d7403d3 | 67 | |
kotachi | 0:18d80d7403d3 | 68 | // return the number of sectors |
kotachi | 0:18d80d7403d3 | 69 | virtual int disk_sectors() { |
kotachi | 0:18d80d7403d3 | 70 | return sizeof(sectors)/sizeof(sectors[0]); |
kotachi | 0:18d80d7403d3 | 71 | } |
kotachi | 0:18d80d7403d3 | 72 | |
kotachi | 0:18d80d7403d3 | 73 | }; |
kotachi | 0:18d80d7403d3 | 74 | |
kotachi | 0:18d80d7403d3 | 75 | } |
kotachi | 0:18d80d7403d3 | 76 | |
kotachi | 0:18d80d7403d3 | 77 | #endif |