Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
MenuClasses/Stats/SDFileSystem/FATFileSystem/MemFileSystem.h@51:387249f9b333, 2019-04-26 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Fri Apr 26 18:30:25 2019 +0000
- Revision:
- 51:387249f9b333
- Parent:
- SDFileSystem/FATFileSystem/MemFileSystem.h@33:249cf423fb18
Sorted my game classes into folders to make it more organized
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 33:249cf423fb18 | 1 | /* mbed Microcontroller Library - MemFileSystem |
AhmedPlaymaker | 33:249cf423fb18 | 2 | * Copyright (c) 2008, sford |
AhmedPlaymaker | 33:249cf423fb18 | 3 | */ |
AhmedPlaymaker | 33:249cf423fb18 | 4 | |
AhmedPlaymaker | 33:249cf423fb18 | 5 | |
AhmedPlaymaker | 33:249cf423fb18 | 6 | #ifndef MBED_MEMFILESYSTEM_H |
AhmedPlaymaker | 33:249cf423fb18 | 7 | #define MBED_MEMFILESYSTEM_H |
AhmedPlaymaker | 33:249cf423fb18 | 8 | |
AhmedPlaymaker | 33:249cf423fb18 | 9 | #include "FATFileSystem.h" |
AhmedPlaymaker | 33:249cf423fb18 | 10 | |
AhmedPlaymaker | 33:249cf423fb18 | 11 | namespace mbed |
AhmedPlaymaker | 33:249cf423fb18 | 12 | { |
AhmedPlaymaker | 33:249cf423fb18 | 13 | |
AhmedPlaymaker | 33:249cf423fb18 | 14 | class MemFileSystem : public FATFileSystem |
AhmedPlaymaker | 33:249cf423fb18 | 15 | { |
AhmedPlaymaker | 33:249cf423fb18 | 16 | public: |
AhmedPlaymaker | 33:249cf423fb18 | 17 | |
AhmedPlaymaker | 33:249cf423fb18 | 18 | // 2000 sectors, each 512 bytes (malloced as required) |
AhmedPlaymaker | 33:249cf423fb18 | 19 | char *sectors[2000]; |
AhmedPlaymaker | 33:249cf423fb18 | 20 | |
AhmedPlaymaker | 33:249cf423fb18 | 21 | MemFileSystem(const char* name) : FATFileSystem(name) { |
AhmedPlaymaker | 33:249cf423fb18 | 22 | memset(sectors, 0, sizeof(sectors)); |
AhmedPlaymaker | 33:249cf423fb18 | 23 | } |
AhmedPlaymaker | 33:249cf423fb18 | 24 | |
AhmedPlaymaker | 33:249cf423fb18 | 25 | virtual ~MemFileSystem() { |
AhmedPlaymaker | 33:249cf423fb18 | 26 | for(int i = 0; i < 2000; i++) { |
AhmedPlaymaker | 33:249cf423fb18 | 27 | if(sectors[i]) { |
AhmedPlaymaker | 33:249cf423fb18 | 28 | free(sectors[i]); |
AhmedPlaymaker | 33:249cf423fb18 | 29 | } |
AhmedPlaymaker | 33:249cf423fb18 | 30 | } |
AhmedPlaymaker | 33:249cf423fb18 | 31 | } |
AhmedPlaymaker | 33:249cf423fb18 | 32 | |
AhmedPlaymaker | 33:249cf423fb18 | 33 | // read a sector in to the buffer, return 0 if ok |
AhmedPlaymaker | 33:249cf423fb18 | 34 | virtual int disk_read(char *buffer, int sector) { |
AhmedPlaymaker | 33:249cf423fb18 | 35 | if(sectors[sector] == 0) { |
AhmedPlaymaker | 33:249cf423fb18 | 36 | // nothing allocated means sector is empty |
AhmedPlaymaker | 33:249cf423fb18 | 37 | memset(buffer, 0, 512); |
AhmedPlaymaker | 33:249cf423fb18 | 38 | } else { |
AhmedPlaymaker | 33:249cf423fb18 | 39 | memcpy(buffer, sectors[sector], 512); |
AhmedPlaymaker | 33:249cf423fb18 | 40 | } |
AhmedPlaymaker | 33:249cf423fb18 | 41 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 42 | } |
AhmedPlaymaker | 33:249cf423fb18 | 43 | |
AhmedPlaymaker | 33:249cf423fb18 | 44 | // write a sector from the buffer, return 0 if ok |
AhmedPlaymaker | 33:249cf423fb18 | 45 | virtual int disk_write(const char *buffer, int sector) { |
AhmedPlaymaker | 33:249cf423fb18 | 46 | // if buffer is zero deallocate sector |
AhmedPlaymaker | 33:249cf423fb18 | 47 | char zero[512]; |
AhmedPlaymaker | 33:249cf423fb18 | 48 | memset(zero, 0, 512); |
AhmedPlaymaker | 33:249cf423fb18 | 49 | if(memcmp(zero, buffer, 512)==0) { |
AhmedPlaymaker | 33:249cf423fb18 | 50 | if(sectors[sector] != 0) { |
AhmedPlaymaker | 33:249cf423fb18 | 51 | free(sectors[sector]); |
AhmedPlaymaker | 33:249cf423fb18 | 52 | sectors[sector] = 0; |
AhmedPlaymaker | 33:249cf423fb18 | 53 | } |
AhmedPlaymaker | 33:249cf423fb18 | 54 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 55 | } |
AhmedPlaymaker | 33:249cf423fb18 | 56 | // else allocate a sector if needed, and write |
AhmedPlaymaker | 33:249cf423fb18 | 57 | if(sectors[sector] == 0) { |
AhmedPlaymaker | 33:249cf423fb18 | 58 | char *sec = (char*)malloc(512); |
AhmedPlaymaker | 33:249cf423fb18 | 59 | if(sec==0) { |
AhmedPlaymaker | 33:249cf423fb18 | 60 | return 1; // out of memory |
AhmedPlaymaker | 33:249cf423fb18 | 61 | } |
AhmedPlaymaker | 33:249cf423fb18 | 62 | sectors[sector] = sec; |
AhmedPlaymaker | 33:249cf423fb18 | 63 | } |
AhmedPlaymaker | 33:249cf423fb18 | 64 | memcpy(sectors[sector], buffer, 512); |
AhmedPlaymaker | 33:249cf423fb18 | 65 | return 0; |
AhmedPlaymaker | 33:249cf423fb18 | 66 | } |
AhmedPlaymaker | 33:249cf423fb18 | 67 | |
AhmedPlaymaker | 33:249cf423fb18 | 68 | // return the number of sectors |
AhmedPlaymaker | 33:249cf423fb18 | 69 | virtual int disk_sectors() { |
AhmedPlaymaker | 33:249cf423fb18 | 70 | return sizeof(sectors)/sizeof(sectors[0]); |
AhmedPlaymaker | 33:249cf423fb18 | 71 | } |
AhmedPlaymaker | 33:249cf423fb18 | 72 | |
AhmedPlaymaker | 33:249cf423fb18 | 73 | }; |
AhmedPlaymaker | 33:249cf423fb18 | 74 | |
AhmedPlaymaker | 33:249cf423fb18 | 75 | } |
AhmedPlaymaker | 33:249cf423fb18 | 76 | |
AhmedPlaymaker | 33:249cf423fb18 | 77 | #endif |