Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
JMF
Date:
Sat Jul 09 00:45:53 2016 +0000
Revision:
1:af7a42f7d465
Parent:
0:9d5134074d84
Child:
2:0e2ef866af95
Adding string encode/decode;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:9d5134074d84 1 #include "mbed.h"
JMF 0:9d5134074d84 2 #include <cctype>
JMF 0:9d5134074d84 3 #include <string>
JMF 0:9d5134074d84 4 #include "SerialBuffered.h"
JMF 0:9d5134074d84 5 #include "HTS221.h"
JMF 0:9d5134074d84 6
JMF 0:9d5134074d84 7 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 8 #define USE_COLOR
JMF 0:9d5134074d84 9 #ifdef USE_COLOR
JMF 0:9d5134074d84 10 #define BLK "\033[30m"
JMF 0:9d5134074d84 11 #define RED "\033[31m"
JMF 0:9d5134074d84 12 #define GRN "\033[32m"
JMF 0:9d5134074d84 13 #define YEL "\033[33m"
JMF 0:9d5134074d84 14 #define BLU "\033[34m"
JMF 0:9d5134074d84 15 #define MAG "\033[35m"
JMF 0:9d5134074d84 16 #define CYN "\033[36m"
JMF 0:9d5134074d84 17 #define WHT "\033[37m"
JMF 0:9d5134074d84 18 #define DEF "\033[39m"
JMF 0:9d5134074d84 19 #else
JMF 0:9d5134074d84 20 #define BLK
JMF 0:9d5134074d84 21 #define RED
JMF 0:9d5134074d84 22 #define GRN
JMF 0:9d5134074d84 23 #define YEL
JMF 0:9d5134074d84 24 #define BLU
JMF 0:9d5134074d84 25 #define MAG
JMF 0:9d5134074d84 26 #define CYN
JMF 0:9d5134074d84 27 #define WHT
JMF 0:9d5134074d84 28 #define DEF
JMF 0:9d5134074d84 29 #endif
JMF 0:9d5134074d84 30
JMF 0:9d5134074d84 31 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 32 #define MDM_DBG_AT_CMDS (1 << 0)
JMF 0:9d5134074d84 33 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 34
JMF 0:9d5134074d84 35 Serial pc(USBTX, USBRX);
JMF 0:9d5134074d84 36 SerialBuffered mdm(PTD3, PTD2, 128);
JMF 0:9d5134074d84 37 DigitalOut led_red(LED_RED);
JMF 0:9d5134074d84 38 DigitalOut led_green(LED_GREEN);
JMF 0:9d5134074d84 39
JMF 0:9d5134074d84 40 DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot
JMF 0:9d5134074d84 41 DigitalOut mdm_power_on(PTB9); // 0 = turn modem on, 1 = turn modem off (should be held high for >5 seconds to cycle modem)
JMF 0:9d5134074d84 42 DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield
JMF 0:9d5134074d84 43
JMF 0:9d5134074d84 44 DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
JMF 0:9d5134074d84 45 DigitalOut mdm_uart1_cts(PTD0);
JMF 0:9d5134074d84 46
JMF 0:9d5134074d84 47 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 48
JMF 0:9d5134074d84 49 const char ok_str[] = "OK";
JMF 0:9d5134074d84 50 const char error_str[] = "ERROR";
JMF 0:9d5134074d84 51
JMF 0:9d5134074d84 52 #define MDM_OK 0
JMF 0:9d5134074d84 53 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 54
JMF 0:9d5134074d84 55 #define MAX_AT_RSP_LEN 255
JMF 0:9d5134074d84 56
JMF 0:9d5134074d84 57 //
JMF 0:9d5134074d84 58 // The modem will return strings of HEX encoded data. This function takes
JMF 0:9d5134074d84 59 // a pointer to a string of HEX ASCII data and converts it into a string
JMF 0:9d5134074d84 60 // of ASCII data. It takes a pointer to the string of HEX ASCII data and
JMF 0:9d5134074d84 61 // a pointer to the destination string. It returns the number of characters
JMF 0:9d5134074d84 62 // it converted.
JMF 0:9d5134074d84 63 //
JMF 0:9d5134074d84 64 int DecodeASCIIstr(string& ins, string& outs) {
JMF 0:9d5134074d84 65 int val, n = 0;
JMF 1:af7a42f7d465 66 char ts[] = {0,0,0};
JMF 0:9d5134074d84 67
JMF 0:9d5134074d84 68 while(n<ins.length()) {
JMF 1:af7a42f7d465 69 ts[0] = ins[n];
JMF 1:af7a42f7d465 70 ts[1] = ins[n+1];
JMF 1:af7a42f7d465 71 sscanf(ts,"%X",&val);
JMF 0:9d5134074d84 72 sprintf(ts,"%c",val);
JMF 0:9d5134074d84 73 outs.append(ts);
JMF 0:9d5134074d84 74 n += 2;
JMF 0:9d5134074d84 75 }
JMF 0:9d5134074d84 76 return outs.length();
JMF 0:9d5134074d84 77 }
JMF 0:9d5134074d84 78
JMF 0:9d5134074d84 79
JMF 0:9d5134074d84 80 //
JMF 0:9d5134074d84 81 // Modem expects data to be passed to it in the form of HEX encoded strings. This
JMF 0:9d5134074d84 82 // function takes a pointer to a users supplied ASCII string, and converts it into
JMF 0:9d5134074d84 83 // an ASCII string of equivelent HEX numbers encoded as a string. The function takes
JMF 0:9d5134074d84 84 // a pointer to the users input string, and a pointer to the output string. The
JMF 0:9d5134074d84 85 // function returns the number of characters converted or 0 if an error occurs or more
JMF 0:9d5134074d84 86 // than 750 characters were converted. The 750 chacter limit is because the modem
JMF 0:9d5134074d84 87 // will only accept up to 1500 characters, and the converted srings will be 2x the
JMF 0:9d5134074d84 88 // input string since the hex representation of 1 character is a two digit hex value.
JMF 0:9d5134074d84 89 //
JMF 1:af7a42f7d465 90 int CreateASCIIstr(string& ins, string& outs) {
JMF 0:9d5134074d84 91 int i = 0;
JMF 0:9d5134074d84 92 char ts[3];
JMF 1:af7a42f7d465 93
JMF 1:af7a42f7d465 94 if( ins.length() > 749 )
JMF 0:9d5134074d84 95 return 0;
JMF 0:9d5134074d84 96
JMF 1:af7a42f7d465 97 while(ins[i] != 0x00) {
JMF 1:af7a42f7d465 98 sprintf(ts,"%02X", ins[i]);
JMF 1:af7a42f7d465 99 outs.append(ts);
JMF 0:9d5134074d84 100 i++;
JMF 0:9d5134074d84 101 }
JMF 1:af7a42f7d465 102 return outs.length();
JMF 0:9d5134074d84 103 }
JMF 0:9d5134074d84 104
JMF 0:9d5134074d84 105
JMF 0:9d5134074d84 106
JMF 0:9d5134074d84 107 ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) {
JMF 0:9d5134074d84 108 int cin = -1;
JMF 0:9d5134074d84 109 int cin_last;
JMF 0:9d5134074d84 110
JMF 0:9d5134074d84 111 if (NULL == buff || size == 0) {
JMF 0:9d5134074d84 112 return -1;
JMF 0:9d5134074d84 113 }
JMF 0:9d5134074d84 114
JMF 0:9d5134074d84 115 size_t len = 0;
JMF 0:9d5134074d84 116 Timer timer;
JMF 0:9d5134074d84 117 timer.start();
JMF 0:9d5134074d84 118 while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) {
JMF 0:9d5134074d84 119 if (mdm.readable()) {
JMF 0:9d5134074d84 120 cin_last = cin;
JMF 0:9d5134074d84 121 cin = mdm.getc();
JMF 0:9d5134074d84 122 if (isprint(cin)) {
JMF 0:9d5134074d84 123 buff[len++] = (char)cin;
JMF 0:9d5134074d84 124 continue;
JMF 0:9d5134074d84 125 } else if (('\r' == cin_last) && ('\n' == cin)) {
JMF 0:9d5134074d84 126 break;
JMF 0:9d5134074d84 127 }
JMF 0:9d5134074d84 128 }
JMF 0:9d5134074d84 129 wait_ms(1);
JMF 0:9d5134074d84 130 }
JMF 0:9d5134074d84 131 buff[len] = NULL;
JMF 0:9d5134074d84 132
JMF 0:9d5134074d84 133 return len;
JMF 0:9d5134074d84 134 }
JMF 0:9d5134074d84 135
JMF 0:9d5134074d84 136 int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) {
JMF 0:9d5134074d84 137 if (cmd && strlen(cmd) > 0) {
JMF 0:9d5134074d84 138 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 139 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 0:9d5134074d84 140 }
JMF 0:9d5134074d84 141 mdm.printf("%s\r\n", cmd);
JMF 0:9d5134074d84 142 }
JMF 0:9d5134074d84 143
JMF 0:9d5134074d84 144 if (rsp_list) {
JMF 0:9d5134074d84 145 Timer timer;
JMF 0:9d5134074d84 146 char rsp[MAX_AT_RSP_LEN+1];
JMF 0:9d5134074d84 147 int len;
JMF 0:9d5134074d84 148
JMF 0:9d5134074d84 149 timer.start();
JMF 0:9d5134074d84 150 while (timer.read_ms() < timeout_ms) {
JMF 0:9d5134074d84 151 len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms());
JMF 0:9d5134074d84 152
JMF 0:9d5134074d84 153 if (len < 0)
JMF 0:9d5134074d84 154 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 155
JMF 0:9d5134074d84 156 if (len == 0)
JMF 0:9d5134074d84 157 continue;
JMF 0:9d5134074d84 158
JMF 0:9d5134074d84 159 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 160 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp);
JMF 0:9d5134074d84 161 }
JMF 0:9d5134074d84 162
JMF 0:9d5134074d84 163 if (rsp_list) {
JMF 0:9d5134074d84 164 int rsp_idx = 0;
JMF 0:9d5134074d84 165 while (rsp_list[rsp_idx]) {
JMF 0:9d5134074d84 166 if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) {
JMF 0:9d5134074d84 167 return rsp_idx;
JMF 0:9d5134074d84 168 }
JMF 0:9d5134074d84 169 rsp_idx++;
JMF 0:9d5134074d84 170 }
JMF 0:9d5134074d84 171 }
JMF 0:9d5134074d84 172 }
JMF 0:9d5134074d84 173 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 174 }
JMF 0:9d5134074d84 175 return MDM_OK;
JMF 0:9d5134074d84 176 }
JMF 0:9d5134074d84 177
JMF 0:9d5134074d84 178 int mdm_init(void) {
JMF 0:9d5134074d84 179 // disable signal level translator
JMF 0:9d5134074d84 180 shield_3v3_1v8_sig_trans_ena = 0;
JMF 0:9d5134074d84 181
JMF 0:9d5134074d84 182 // power modem on //off
JMF 0:9d5134074d84 183 mdm_power_on = 0; //1;
JMF 0:9d5134074d84 184
JMF 0:9d5134074d84 185 // insure modem boots into normal operating mode
JMF 0:9d5134074d84 186 // and does not go to sleep when powered on
JMF 0:9d5134074d84 187 mdm_uart2_rx_boot_mode_sel = 1;
JMF 0:9d5134074d84 188 mdm_wakeup_in = 1;
JMF 0:9d5134074d84 189
JMF 0:9d5134074d84 190 // initialze comm with the modem
JMF 0:9d5134074d84 191 mdm.baud(115200);
JMF 0:9d5134074d84 192 mdm_uart1_cts = 0;
JMF 0:9d5134074d84 193
JMF 0:9d5134074d84 194 // enable the signal level translator to start
JMF 0:9d5134074d84 195 // modem reset process (modem will be powered down)
JMF 0:9d5134074d84 196 shield_3v3_1v8_sig_trans_ena = 1;
JMF 0:9d5134074d84 197
JMF 0:9d5134074d84 198 // Give the modem 60 secons to start responding by
JMF 0:9d5134074d84 199 // sending simple 'AT' commands to modem once per second.
JMF 0:9d5134074d84 200 Timer timer;
JMF 0:9d5134074d84 201 timer.start();
JMF 0:9d5134074d84 202 while (timer.read() < 60) {
JMF 0:9d5134074d84 203 const char * rsp_lst[] = { ok_str, error_str, NULL };
JMF 0:9d5134074d84 204 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
JMF 0:9d5134074d84 205 if (rc == 0)
JMF 0:9d5134074d84 206 return timer.read();
JMF 0:9d5134074d84 207 wait_ms(1000 - (timer.read_ms() % 1000));
JMF 0:9d5134074d84 208 pc.printf("\r%d",timer.read_ms()/1000);
JMF 0:9d5134074d84 209 }
JMF 0:9d5134074d84 210 return false;
JMF 0:9d5134074d84 211 }
JMF 0:9d5134074d84 212
JMF 0:9d5134074d84 213 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 214
JMF 0:9d5134074d84 215 int main() {
JMF 0:9d5134074d84 216 HTS221 hts221;
JMF 0:9d5134074d84 217 pc.baud(115200);
JMF 0:9d5134074d84 218 int i,
JMF 0:9d5134074d84 219 CreateASCIIstr(string& in, string& out),
JMF 0:9d5134074d84 220 DecodeASCIIstr(string& ins, string& outs);
JMF 0:9d5134074d84 221 string ins, outs;
JMF 0:9d5134074d84 222
JMF 0:9d5134074d84 223 void hts221_init(void);
JMF 0:9d5134074d84 224
JMF 1:af7a42f7d465 225 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 226 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 227
JMF 0:9d5134074d84 228 i = hts221.begin();
JMF 0:9d5134074d84 229 if( i )
JMF 0:9d5134074d84 230 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 231 else
JMF 0:9d5134074d84 232 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 233
JMF 0:9d5134074d84 234 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 235 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
JMF 1:af7a42f7d465 236
JMF 1:af7a42f7d465 237 string newstr, outstr, instr = "0123456789aAbBcCdDeEfFxXyYzZ";
JMF 1:af7a42f7d465 238 pc.printf("\n\rTest ASCII String creation: \n\r");
JMF 1:af7a42f7d465 239 i = CreateASCIIstr(instr,outstr);
JMF 1:af7a42f7d465 240 pc.printf(">Initially the string is '%s' (%d long)\n\r>after encode it is '%s' (%d characters long).\n\r",
JMF 1:af7a42f7d465 241 instr.c_str(),instr.length(),outstr.c_str(),i);
JMF 1:af7a42f7d465 242 i = DecodeASCIIstr(outstr, newstr);
JMF 1:af7a42f7d465 243 pc.printf(">after decoding the encoded string, it is '%s' (%d long)\n\r\n\r",newstr.c_str(),i);
JMF 1:af7a42f7d465 244
JMF 0:9d5134074d84 245
JMF 0:9d5134074d84 246 // Initialize the modem
JMF 0:9d5134074d84 247 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
JMF 0:9d5134074d84 248 i=mdm_init();
JMF 0:9d5134074d84 249 if (!i) {
JMF 0:9d5134074d84 250 pc.printf(RED "Modem initialization failed!" DEF "\n");
JMF 0:9d5134074d84 251 while (1);
JMF 0:9d5134074d84 252 }
JMF 0:9d5134074d84 253
JMF 0:9d5134074d84 254 // Now that the modem is up and running, transfer characters
JMF 0:9d5134074d84 255 // between the pc terminal and the modem to give the user
JMF 0:9d5134074d84 256 // a virtual terminal to the modem.
JMF 0:9d5134074d84 257 pc.printf(YEL "\rAT command interface ready, completed in %d seconds. You may now type AT commands" DEF "\r\n",i);
JMF 0:9d5134074d84 258
JMF 0:9d5134074d84 259 while(1) {
JMF 0:9d5134074d84 260 if(pc.readable()) {
JMF 0:9d5134074d84 261 char char_in = TOUPPER(pc.getc());
JMF 0:9d5134074d84 262
JMF 0:9d5134074d84 263 static char last_char_in = 0;
JMF 0:9d5134074d84 264
JMF 0:9d5134074d84 265 if (('\r' == char_in) || ('\n' == char_in))
JMF 0:9d5134074d84 266 {
JMF 0:9d5134074d84 267 if (('\r' == char_in) || ('\r' != last_char_in))
JMF 0:9d5134074d84 268 {
JMF 0:9d5134074d84 269 mdm.puts("\r\n");
JMF 0:9d5134074d84 270 }
JMF 0:9d5134074d84 271 }
JMF 0:9d5134074d84 272 else
JMF 0:9d5134074d84 273 {
JMF 0:9d5134074d84 274 pc.putc(char_in);
JMF 0:9d5134074d84 275 mdm.putc(char_in);
JMF 0:9d5134074d84 276 }
JMF 0:9d5134074d84 277 last_char_in = char_in;
JMF 0:9d5134074d84 278 }
JMF 0:9d5134074d84 279 if(mdm.readable()) {
JMF 0:9d5134074d84 280 char ser_char = mdm.getc();
JMF 0:9d5134074d84 281 pc.putc(ser_char);
JMF 0:9d5134074d84 282 }
JMF 0:9d5134074d84 283 }
JMF 0:9d5134074d84 284 }