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