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