Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
gert_lauritsen
Date:
Fri Jul 05 12:55:53 2019 +0000
Branch:
MbedBMAGThrRev
Revision:
61:52a6d0ffd8e5
dato rettel og ID i file lagt ind i version 5

Who changed what in which revision?

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