Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
gert_lauritsen
Date:
Thu Jun 20 07:13:18 2019 +0000
Revision:
56:df9052e3808c
change date stamp from gps, reads serialnumb from file.

Who changed what in which revision?

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