CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Sun Nov 04 18:44:51 2018 -0700
Revision:
0:aa13e1c335cd
create new repo for show2me

Who changed what in which revision?

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