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