LCD1289Serial_Ethenet

Dependencies:   EthernetInterface FatFileSystemCpp SDFileSystem mbed-rtos mbed

Committer:
shindo
Date:
Wed Nov 07 06:42:34 2012 +0000
Revision:
0:a5367e4d8591
LCD1289Serial_Ethenet

Who changed what in which revision?

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