Baseline for testing
Diff: AddressMap.cpp
- Revision:
- 0:e24f6400a8c8
- Child:
- 1:a2073f60d3a6
diff -r 000000000000 -r e24f6400a8c8 AddressMap.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AddressMap.cpp Thu Jul 25 00:43:23 2019 +0000 @@ -0,0 +1,185 @@ + +#include "mbed.h" + +#include "AddressMap.h" + +#include "stdio.h" +#include "stdlib.h" + +#define TEXT_FILE 1 + +#if TEXT_FILE +const char * filePath = "/local/addresses.txt"; +#else +const char * filePath = "/local/addresses.bin"; +#endif + + +void AddressMap::init() +{ + int i=0; + printf("Initializing Default Data\n\r"); + + addresses[i].address = i; + sprintf(addresses[i].description, "Basement %d" , i); + + for (int i=1;i<100;i++) + { + addresses[i].address = i; + sprintf(addresses[i].description, "Floor %d" , i); + } +} + +void trim(const char *input, char *result) +{ + int i, j, k = 0; + + j = strlen(input)-1; + + while (input[i] == 0x20 && input[i] != '\0' ) i++; + while (input[j] == 0x20 && j>0 ) j--; + + for (i = 0; i<j; i++) { + result[k++] = input[i]; + } +} + +int AddressMap::load() +{ + int success = 0; + +#if TEXT_FILE + FILE *fp = fopen (filePath,"r"); + + if (fp != NULL) { + + int i=0; + char line[128]; + while (!feof(fp)) { + + memset(line,0,sizeof(line)); + fgets(line,sizeof(line),fp); + + int t = 0; + char * p = strtok (line,",:"); + while (p!= NULL) + { + if (t==0) { + addresses[i].address = atoi(p); + } else if (t==1){ + trim(p,addresses[i].description); + } else { + } + p = strtok (NULL, ",:"); + t++; + } + + i++; + } + fclose(fp); + } + else + { + init(); + } + +#else + + FILE *input = fopen(filePath, "rb"); + if(input){ + + printf("Reading %s\n\r",filePath ); + int size = MAX_ADDRESSES; + + fread(&size, sizeof(uint32_t),1,input); + + for (int i=0;i<100;i++) + { + printf("." ); + fread( &addresses[i].address, sizeof(uint32_t),1 , input); + fread( addresses[i].description, sizeof(char) ,40, input); + } + fclose(input); + success = 1; + } + else + { + printf("Could not Read %s\n\r",filename ); + } +#endif + + return success; +} + + + +void AddressMap::save() +{ + printf("Saving %s\n\r",filePath ); + +#if TEXT_FILE + FILE *output = fopen(filePath, "w"); + + for (int i=0;i<100;i++) + { + fprintf(output, "%02d:%s\n\r" , + addresses[i].address, addresses[i].description); + } + fflush(output); + fclose(output); +#else + + FILE *output = fopen(filePath, "wb"); + + int size = MAX_ADDRESSES; + + fwrite(&size, sizeof(uint32_t),1,output); + + for (int i=0;i<100;i++) + { + printf("." ); + fwrite( &addresses[i].address, sizeof(uint32_t),1 , output); + fwrite( addresses[i].description, sizeof(char) ,40, output); + } + fflush(output); + fclose(output); + +#endif + + printf("Done \n\r" ); +} + +void AddressMap::display(Serial *pc) +{ + pc->printf("\n\r"); + pc->printf("-----------------------------\n\r"); + pc->printf("-- Addresses \n\r"); + pc->printf("-----------------------------\n\r"); + for (int i=0;i<MAX_ADDRESSES;i++) + { + pc->printf("%02d : [%s]\n\r", + addresses[i].address, addresses[i].description); + } +} + + +void AddressMap::play(Serial *pc, int address) +{ + if (address>=0&&address<MAX_ADDRESSES){ + pc->printf("%02d : %s is open\n\r", address, + addresses[address].description); + } + else + { + pc->printf("%02d : error \n\r", address); + } +} + + +char* AddressMap::getString(unsigned char idx ) +{ + if (idx < MAX_ADDRESSES ) + return addresses[idx].description; + else + return NULL; +}