Baseline for testing
AddressMap.cpp@0:e24f6400a8c8, 2019-07-25 (annotated)
- Committer:
- foxbrianr
- Date:
- Thu Jul 25 00:43:23 2019 +0000
- Revision:
- 0:e24f6400a8c8
- Child:
- 1:a2073f60d3a6
Baseline for testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
foxbrianr | 0:e24f6400a8c8 | 1 | |
foxbrianr | 0:e24f6400a8c8 | 2 | #include "mbed.h" |
foxbrianr | 0:e24f6400a8c8 | 3 | |
foxbrianr | 0:e24f6400a8c8 | 4 | #include "AddressMap.h" |
foxbrianr | 0:e24f6400a8c8 | 5 | |
foxbrianr | 0:e24f6400a8c8 | 6 | #include "stdio.h" |
foxbrianr | 0:e24f6400a8c8 | 7 | #include "stdlib.h" |
foxbrianr | 0:e24f6400a8c8 | 8 | |
foxbrianr | 0:e24f6400a8c8 | 9 | #define TEXT_FILE 1 |
foxbrianr | 0:e24f6400a8c8 | 10 | |
foxbrianr | 0:e24f6400a8c8 | 11 | #if TEXT_FILE |
foxbrianr | 0:e24f6400a8c8 | 12 | const char * filePath = "/local/addresses.txt"; |
foxbrianr | 0:e24f6400a8c8 | 13 | #else |
foxbrianr | 0:e24f6400a8c8 | 14 | const char * filePath = "/local/addresses.bin"; |
foxbrianr | 0:e24f6400a8c8 | 15 | #endif |
foxbrianr | 0:e24f6400a8c8 | 16 | |
foxbrianr | 0:e24f6400a8c8 | 17 | |
foxbrianr | 0:e24f6400a8c8 | 18 | void AddressMap::init() |
foxbrianr | 0:e24f6400a8c8 | 19 | { |
foxbrianr | 0:e24f6400a8c8 | 20 | int i=0; |
foxbrianr | 0:e24f6400a8c8 | 21 | printf("Initializing Default Data\n\r"); |
foxbrianr | 0:e24f6400a8c8 | 22 | |
foxbrianr | 0:e24f6400a8c8 | 23 | addresses[i].address = i; |
foxbrianr | 0:e24f6400a8c8 | 24 | sprintf(addresses[i].description, "Basement %d" , i); |
foxbrianr | 0:e24f6400a8c8 | 25 | |
foxbrianr | 0:e24f6400a8c8 | 26 | for (int i=1;i<100;i++) |
foxbrianr | 0:e24f6400a8c8 | 27 | { |
foxbrianr | 0:e24f6400a8c8 | 28 | addresses[i].address = i; |
foxbrianr | 0:e24f6400a8c8 | 29 | sprintf(addresses[i].description, "Floor %d" , i); |
foxbrianr | 0:e24f6400a8c8 | 30 | } |
foxbrianr | 0:e24f6400a8c8 | 31 | } |
foxbrianr | 0:e24f6400a8c8 | 32 | |
foxbrianr | 0:e24f6400a8c8 | 33 | void trim(const char *input, char *result) |
foxbrianr | 0:e24f6400a8c8 | 34 | { |
foxbrianr | 0:e24f6400a8c8 | 35 | int i, j, k = 0; |
foxbrianr | 0:e24f6400a8c8 | 36 | |
foxbrianr | 0:e24f6400a8c8 | 37 | j = strlen(input)-1; |
foxbrianr | 0:e24f6400a8c8 | 38 | |
foxbrianr | 0:e24f6400a8c8 | 39 | while (input[i] == 0x20 && input[i] != '\0' ) i++; |
foxbrianr | 0:e24f6400a8c8 | 40 | while (input[j] == 0x20 && j>0 ) j--; |
foxbrianr | 0:e24f6400a8c8 | 41 | |
foxbrianr | 0:e24f6400a8c8 | 42 | for (i = 0; i<j; i++) { |
foxbrianr | 0:e24f6400a8c8 | 43 | result[k++] = input[i]; |
foxbrianr | 0:e24f6400a8c8 | 44 | } |
foxbrianr | 0:e24f6400a8c8 | 45 | } |
foxbrianr | 0:e24f6400a8c8 | 46 | |
foxbrianr | 0:e24f6400a8c8 | 47 | int AddressMap::load() |
foxbrianr | 0:e24f6400a8c8 | 48 | { |
foxbrianr | 0:e24f6400a8c8 | 49 | int success = 0; |
foxbrianr | 0:e24f6400a8c8 | 50 | |
foxbrianr | 0:e24f6400a8c8 | 51 | #if TEXT_FILE |
foxbrianr | 0:e24f6400a8c8 | 52 | FILE *fp = fopen (filePath,"r"); |
foxbrianr | 0:e24f6400a8c8 | 53 | |
foxbrianr | 0:e24f6400a8c8 | 54 | if (fp != NULL) { |
foxbrianr | 0:e24f6400a8c8 | 55 | |
foxbrianr | 0:e24f6400a8c8 | 56 | int i=0; |
foxbrianr | 0:e24f6400a8c8 | 57 | char line[128]; |
foxbrianr | 0:e24f6400a8c8 | 58 | while (!feof(fp)) { |
foxbrianr | 0:e24f6400a8c8 | 59 | |
foxbrianr | 0:e24f6400a8c8 | 60 | memset(line,0,sizeof(line)); |
foxbrianr | 0:e24f6400a8c8 | 61 | fgets(line,sizeof(line),fp); |
foxbrianr | 0:e24f6400a8c8 | 62 | |
foxbrianr | 0:e24f6400a8c8 | 63 | int t = 0; |
foxbrianr | 0:e24f6400a8c8 | 64 | char * p = strtok (line,",:"); |
foxbrianr | 0:e24f6400a8c8 | 65 | while (p!= NULL) |
foxbrianr | 0:e24f6400a8c8 | 66 | { |
foxbrianr | 0:e24f6400a8c8 | 67 | if (t==0) { |
foxbrianr | 0:e24f6400a8c8 | 68 | addresses[i].address = atoi(p); |
foxbrianr | 0:e24f6400a8c8 | 69 | } else if (t==1){ |
foxbrianr | 0:e24f6400a8c8 | 70 | trim(p,addresses[i].description); |
foxbrianr | 0:e24f6400a8c8 | 71 | } else { |
foxbrianr | 0:e24f6400a8c8 | 72 | } |
foxbrianr | 0:e24f6400a8c8 | 73 | p = strtok (NULL, ",:"); |
foxbrianr | 0:e24f6400a8c8 | 74 | t++; |
foxbrianr | 0:e24f6400a8c8 | 75 | } |
foxbrianr | 0:e24f6400a8c8 | 76 | |
foxbrianr | 0:e24f6400a8c8 | 77 | i++; |
foxbrianr | 0:e24f6400a8c8 | 78 | } |
foxbrianr | 0:e24f6400a8c8 | 79 | fclose(fp); |
foxbrianr | 0:e24f6400a8c8 | 80 | } |
foxbrianr | 0:e24f6400a8c8 | 81 | else |
foxbrianr | 0:e24f6400a8c8 | 82 | { |
foxbrianr | 0:e24f6400a8c8 | 83 | init(); |
foxbrianr | 0:e24f6400a8c8 | 84 | } |
foxbrianr | 0:e24f6400a8c8 | 85 | |
foxbrianr | 0:e24f6400a8c8 | 86 | #else |
foxbrianr | 0:e24f6400a8c8 | 87 | |
foxbrianr | 0:e24f6400a8c8 | 88 | FILE *input = fopen(filePath, "rb"); |
foxbrianr | 0:e24f6400a8c8 | 89 | if(input){ |
foxbrianr | 0:e24f6400a8c8 | 90 | |
foxbrianr | 0:e24f6400a8c8 | 91 | printf("Reading %s\n\r",filePath ); |
foxbrianr | 0:e24f6400a8c8 | 92 | int size = MAX_ADDRESSES; |
foxbrianr | 0:e24f6400a8c8 | 93 | |
foxbrianr | 0:e24f6400a8c8 | 94 | fread(&size, sizeof(uint32_t),1,input); |
foxbrianr | 0:e24f6400a8c8 | 95 | |
foxbrianr | 0:e24f6400a8c8 | 96 | for (int i=0;i<100;i++) |
foxbrianr | 0:e24f6400a8c8 | 97 | { |
foxbrianr | 0:e24f6400a8c8 | 98 | printf("." ); |
foxbrianr | 0:e24f6400a8c8 | 99 | fread( &addresses[i].address, sizeof(uint32_t),1 , input); |
foxbrianr | 0:e24f6400a8c8 | 100 | fread( addresses[i].description, sizeof(char) ,40, input); |
foxbrianr | 0:e24f6400a8c8 | 101 | } |
foxbrianr | 0:e24f6400a8c8 | 102 | fclose(input); |
foxbrianr | 0:e24f6400a8c8 | 103 | success = 1; |
foxbrianr | 0:e24f6400a8c8 | 104 | } |
foxbrianr | 0:e24f6400a8c8 | 105 | else |
foxbrianr | 0:e24f6400a8c8 | 106 | { |
foxbrianr | 0:e24f6400a8c8 | 107 | printf("Could not Read %s\n\r",filename ); |
foxbrianr | 0:e24f6400a8c8 | 108 | } |
foxbrianr | 0:e24f6400a8c8 | 109 | #endif |
foxbrianr | 0:e24f6400a8c8 | 110 | |
foxbrianr | 0:e24f6400a8c8 | 111 | return success; |
foxbrianr | 0:e24f6400a8c8 | 112 | } |
foxbrianr | 0:e24f6400a8c8 | 113 | |
foxbrianr | 0:e24f6400a8c8 | 114 | |
foxbrianr | 0:e24f6400a8c8 | 115 | |
foxbrianr | 0:e24f6400a8c8 | 116 | void AddressMap::save() |
foxbrianr | 0:e24f6400a8c8 | 117 | { |
foxbrianr | 0:e24f6400a8c8 | 118 | printf("Saving %s\n\r",filePath ); |
foxbrianr | 0:e24f6400a8c8 | 119 | |
foxbrianr | 0:e24f6400a8c8 | 120 | #if TEXT_FILE |
foxbrianr | 0:e24f6400a8c8 | 121 | FILE *output = fopen(filePath, "w"); |
foxbrianr | 0:e24f6400a8c8 | 122 | |
foxbrianr | 0:e24f6400a8c8 | 123 | for (int i=0;i<100;i++) |
foxbrianr | 0:e24f6400a8c8 | 124 | { |
foxbrianr | 0:e24f6400a8c8 | 125 | fprintf(output, "%02d:%s\n\r" , |
foxbrianr | 0:e24f6400a8c8 | 126 | addresses[i].address, addresses[i].description); |
foxbrianr | 0:e24f6400a8c8 | 127 | } |
foxbrianr | 0:e24f6400a8c8 | 128 | fflush(output); |
foxbrianr | 0:e24f6400a8c8 | 129 | fclose(output); |
foxbrianr | 0:e24f6400a8c8 | 130 | #else |
foxbrianr | 0:e24f6400a8c8 | 131 | |
foxbrianr | 0:e24f6400a8c8 | 132 | FILE *output = fopen(filePath, "wb"); |
foxbrianr | 0:e24f6400a8c8 | 133 | |
foxbrianr | 0:e24f6400a8c8 | 134 | int size = MAX_ADDRESSES; |
foxbrianr | 0:e24f6400a8c8 | 135 | |
foxbrianr | 0:e24f6400a8c8 | 136 | fwrite(&size, sizeof(uint32_t),1,output); |
foxbrianr | 0:e24f6400a8c8 | 137 | |
foxbrianr | 0:e24f6400a8c8 | 138 | for (int i=0;i<100;i++) |
foxbrianr | 0:e24f6400a8c8 | 139 | { |
foxbrianr | 0:e24f6400a8c8 | 140 | printf("." ); |
foxbrianr | 0:e24f6400a8c8 | 141 | fwrite( &addresses[i].address, sizeof(uint32_t),1 , output); |
foxbrianr | 0:e24f6400a8c8 | 142 | fwrite( addresses[i].description, sizeof(char) ,40, output); |
foxbrianr | 0:e24f6400a8c8 | 143 | } |
foxbrianr | 0:e24f6400a8c8 | 144 | fflush(output); |
foxbrianr | 0:e24f6400a8c8 | 145 | fclose(output); |
foxbrianr | 0:e24f6400a8c8 | 146 | |
foxbrianr | 0:e24f6400a8c8 | 147 | #endif |
foxbrianr | 0:e24f6400a8c8 | 148 | |
foxbrianr | 0:e24f6400a8c8 | 149 | printf("Done \n\r" ); |
foxbrianr | 0:e24f6400a8c8 | 150 | } |
foxbrianr | 0:e24f6400a8c8 | 151 | |
foxbrianr | 0:e24f6400a8c8 | 152 | void AddressMap::display(Serial *pc) |
foxbrianr | 0:e24f6400a8c8 | 153 | { |
foxbrianr | 0:e24f6400a8c8 | 154 | pc->printf("\n\r"); |
foxbrianr | 0:e24f6400a8c8 | 155 | pc->printf("-----------------------------\n\r"); |
foxbrianr | 0:e24f6400a8c8 | 156 | pc->printf("-- Addresses \n\r"); |
foxbrianr | 0:e24f6400a8c8 | 157 | pc->printf("-----------------------------\n\r"); |
foxbrianr | 0:e24f6400a8c8 | 158 | for (int i=0;i<MAX_ADDRESSES;i++) |
foxbrianr | 0:e24f6400a8c8 | 159 | { |
foxbrianr | 0:e24f6400a8c8 | 160 | pc->printf("%02d : [%s]\n\r", |
foxbrianr | 0:e24f6400a8c8 | 161 | addresses[i].address, addresses[i].description); |
foxbrianr | 0:e24f6400a8c8 | 162 | } |
foxbrianr | 0:e24f6400a8c8 | 163 | } |
foxbrianr | 0:e24f6400a8c8 | 164 | |
foxbrianr | 0:e24f6400a8c8 | 165 | |
foxbrianr | 0:e24f6400a8c8 | 166 | void AddressMap::play(Serial *pc, int address) |
foxbrianr | 0:e24f6400a8c8 | 167 | { |
foxbrianr | 0:e24f6400a8c8 | 168 | if (address>=0&&address<MAX_ADDRESSES){ |
foxbrianr | 0:e24f6400a8c8 | 169 | pc->printf("%02d : %s is open\n\r", address, |
foxbrianr | 0:e24f6400a8c8 | 170 | addresses[address].description); |
foxbrianr | 0:e24f6400a8c8 | 171 | } |
foxbrianr | 0:e24f6400a8c8 | 172 | else |
foxbrianr | 0:e24f6400a8c8 | 173 | { |
foxbrianr | 0:e24f6400a8c8 | 174 | pc->printf("%02d : error \n\r", address); |
foxbrianr | 0:e24f6400a8c8 | 175 | } |
foxbrianr | 0:e24f6400a8c8 | 176 | } |
foxbrianr | 0:e24f6400a8c8 | 177 | |
foxbrianr | 0:e24f6400a8c8 | 178 | |
foxbrianr | 0:e24f6400a8c8 | 179 | char* AddressMap::getString(unsigned char idx ) |
foxbrianr | 0:e24f6400a8c8 | 180 | { |
foxbrianr | 0:e24f6400a8c8 | 181 | if (idx < MAX_ADDRESSES ) |
foxbrianr | 0:e24f6400a8c8 | 182 | return addresses[idx].description; |
foxbrianr | 0:e24f6400a8c8 | 183 | else |
foxbrianr | 0:e24f6400a8c8 | 184 | return NULL; |
foxbrianr | 0:e24f6400a8c8 | 185 | } |