Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Wed Sep 21 15:42:01 2016 +0000
Revision:
36:f8d96ff1dd1b
Parent:
35:2e864bae3af0
Child:
37:ee01f752524a
v2

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 2:0e2ef866af95 6 #include "config_me.h"
JMF 2:0e2ef866af95 7 #include "wnc_control.h"
stefanrousseau 4:f83bedd9cab4 8 #include "sensors.h"
JMF 0:9d5134074d84 9
stefanrousseau 11:e6602513730f 10 #include "hardware.h"
stefanrousseau 11:e6602513730f 11 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
elmkom 35:2e864bae3af0 12 I2C proximityi2c(PTE25, PTE24);
elmkom 36:f8d96ff1dd1b 13
JMF 0:9d5134074d84 14 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 15 #define USE_COLOR
JMF 0:9d5134074d84 16 #ifdef USE_COLOR
JMF 0:9d5134074d84 17 #define BLK "\033[30m"
JMF 0:9d5134074d84 18 #define RED "\033[31m"
JMF 0:9d5134074d84 19 #define GRN "\033[32m"
JMF 0:9d5134074d84 20 #define YEL "\033[33m"
JMF 0:9d5134074d84 21 #define BLU "\033[34m"
JMF 0:9d5134074d84 22 #define MAG "\033[35m"
JMF 0:9d5134074d84 23 #define CYN "\033[36m"
JMF 0:9d5134074d84 24 #define WHT "\033[37m"
JMF 0:9d5134074d84 25 #define DEF "\033[39m"
JMF 0:9d5134074d84 26 #else
JMF 0:9d5134074d84 27 #define BLK
JMF 0:9d5134074d84 28 #define RED
JMF 0:9d5134074d84 29 #define GRN
JMF 0:9d5134074d84 30 #define YEL
JMF 0:9d5134074d84 31 #define BLU
JMF 0:9d5134074d84 32 #define MAG
JMF 0:9d5134074d84 33 #define CYN
JMF 0:9d5134074d84 34 #define WHT
JMF 0:9d5134074d84 35 #define DEF
JMF 0:9d5134074d84 36 #endif
JMF 0:9d5134074d84 37
JMF 0:9d5134074d84 38 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 39 #define MDM_DBG_AT_CMDS (1 << 0)
elmkom 36:f8d96ff1dd1b 40
elmkom 36:f8d96ff1dd1b 41 #define MUXADDRESS 0x70
elmkom 36:f8d96ff1dd1b 42 #define PROXIMITYADDRESS 0x39
elmkom 36:f8d96ff1dd1b 43
JMF 0:9d5134074d84 44 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 45
JMF 0:9d5134074d84 46 Serial pc(USBTX, USBRX);
JMF 0:9d5134074d84 47 SerialBuffered mdm(PTD3, PTD2, 128);
stefanrousseau 16:17c5916f2d12 48 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 49 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 50 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 51
JMF 0:9d5134074d84 52 DigitalOut mdm_uart2_rx_boot_mode_sel(PTC17); // on powerup, 0 = boot mode, 1 = normal boot
JMF 0:9d5134074d84 53 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 54 DigitalOut mdm_wakeup_in(PTC2); // 0 = let modem sleep, 1 = keep modem awake -- Note: pulled high on shield
JMF 0:9d5134074d84 55
fkellermavnet 14:0c353e212296 56 DigitalOut mdm_reset(PTC12); // active high
fkellermavnet 14:0c353e212296 57
JMF 0:9d5134074d84 58 DigitalOut shield_3v3_1v8_sig_trans_ena(PTC4); // 0 = disabled (all signals high impedence, 1 = translation active
JMF 0:9d5134074d84 59 DigitalOut mdm_uart1_cts(PTD0);
JMF 0:9d5134074d84 60
JMF 0:9d5134074d84 61 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 62
JMF 0:9d5134074d84 63 const char ok_str[] = "OK";
JMF 0:9d5134074d84 64 const char error_str[] = "ERROR";
JMF 0:9d5134074d84 65
JMF 0:9d5134074d84 66 #define MDM_OK 0
JMF 0:9d5134074d84 67 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 68
JMF 0:9d5134074d84 69 #define MAX_AT_RSP_LEN 255
elmkom 36:f8d96ff1dd1b 70 #define NUM_PROXIMIY_SENSORS 8
elmkom 36:f8d96ff1dd1b 71 short proximityData [NUM_PROXIMIY_SENSORS][3];
elmkom 36:f8d96ff1dd1b 72 short lastProximityData [NUM_PROXIMIY_SENSORS][3];
elmkom 36:f8d96ff1dd1b 73 bool proximityChange = false;
elmkom 35:2e864bae3af0 74
elmkom 35:2e864bae3af0 75 void prox_write_reg(char address,char reg, char cmd)
elmkom 35:2e864bae3af0 76 {
elmkom 35:2e864bae3af0 77 char txbuffer [2];
elmkom 35:2e864bae3af0 78 txbuffer[0] = reg;
elmkom 35:2e864bae3af0 79 txbuffer[1] = cmd;
elmkom 35:2e864bae3af0 80 proximityi2c.write(address<<1, txbuffer, 2,false );
elmkom 35:2e864bae3af0 81 }
elmkom 35:2e864bae3af0 82
elmkom 35:2e864bae3af0 83 void prox_write(char address, char cmd)
elmkom 35:2e864bae3af0 84 {
elmkom 35:2e864bae3af0 85 char txbuffer [1];
elmkom 35:2e864bae3af0 86 txbuffer[0] = cmd;
elmkom 35:2e864bae3af0 87 proximityi2c.write(address<<1, txbuffer, 1,false );
elmkom 35:2e864bae3af0 88 }
elmkom 35:2e864bae3af0 89
elmkom 35:2e864bae3af0 90 unsigned char prox_read_reg(char address,char reg)
elmkom 35:2e864bae3af0 91 {
elmkom 35:2e864bae3af0 92 char txbuffer [1];
elmkom 35:2e864bae3af0 93 char rxbuffer [1];
elmkom 35:2e864bae3af0 94 rxbuffer[0] = 0;
elmkom 35:2e864bae3af0 95 txbuffer[0] = reg;
elmkom 35:2e864bae3af0 96 proximityi2c.write(address<<1, txbuffer, 1,false );
elmkom 35:2e864bae3af0 97 proximityi2c.read(address<<1, rxbuffer, 1 );
elmkom 35:2e864bae3af0 98 return (unsigned char)rxbuffer[0];
elmkom 35:2e864bae3af0 99 }
elmkom 36:f8d96ff1dd1b 100 void proximity_sensor_off(int sensor)
elmkom 36:f8d96ff1dd1b 101 {
elmkom 36:f8d96ff1dd1b 102 prox_write(MUXADDRESS,1<<sensor);
elmkom 36:f8d96ff1dd1b 103 prox_write_reg(PROXIMITYADDRESS,0x41,0x00);
elmkom 36:f8d96ff1dd1b 104 }
elmkom 35:2e864bae3af0 105
elmkom 36:f8d96ff1dd1b 106 void proximity_sensor_on(int sensor)
elmkom 35:2e864bae3af0 107 {
elmkom 35:2e864bae3af0 108 char C25ma = 0x00;
elmkom 35:2e864bae3af0 109 char C50ma = 0x01;
elmkom 35:2e864bae3af0 110 char C100m1 = 0x02;
elmkom 35:2e864bae3af0 111 char C200ma = 0x03;
elmkom 35:2e864bae3af0 112
elmkom 35:2e864bae3af0 113 char GainAls1Ir1 = 0x00<<2;
elmkom 35:2e864bae3af0 114 char GainAls2Ir1 = 0x04<<2;
elmkom 35:2e864bae3af0 115 char GainAls2Ir2 = 0x05<<2;
elmkom 35:2e864bae3af0 116 char GainAls64Ir64 = 0x0A<<2;
elmkom 35:2e864bae3af0 117 char GainAls128Ir64 = 0x0D<<2;
elmkom 35:2e864bae3af0 118 char GainAls128Ir128 = 0x0F<<2;
elmkom 35:2e864bae3af0 119
elmkom 35:2e864bae3af0 120 char Als0Ps0 = 0x00;
elmkom 35:2e864bae3af0 121 char Als0Ps10 = 0x01;
elmkom 35:2e864bae3af0 122 char Als0Ps40 = 0x02;
elmkom 35:2e864bae3af0 123 char Als0Ps100 = 0x03;
elmkom 35:2e864bae3af0 124 char Als0Ps400 = 0x04;
elmkom 35:2e864bae3af0 125
elmkom 35:2e864bae3af0 126 char Als100Ps0 = 0x05;
elmkom 35:2e864bae3af0 127 char Als100Ps100 = 0x06;
elmkom 35:2e864bae3af0 128 char Als100Ps400 = 0x07;
elmkom 35:2e864bae3af0 129
elmkom 35:2e864bae3af0 130 char Als401Ps0 = 0x08;
elmkom 35:2e864bae3af0 131 char Als401Ps100 = 0x09;
elmkom 35:2e864bae3af0 132 char Als400Ps0 = 0x0A;
elmkom 35:2e864bae3af0 133 char Als400Ps400 = 0x0B;
elmkom 35:2e864bae3af0 134
elmkom 35:2e864bae3af0 135 char Als50Ps50 = 0x0C;
elmkom 35:2e864bae3af0 136
elmkom 36:f8d96ff1dd1b 137
elmkom 36:f8d96ff1dd1b 138 prox_write(MUXADDRESS,1<<sensor);
elmkom 36:f8d96ff1dd1b 139 prox_write_reg(PROXIMITYADDRESS,0x41,Als400Ps400); // initiate ALS: and PS
elmkom 36:f8d96ff1dd1b 140 prox_write_reg(PROXIMITYADDRESS,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma
elmkom 36:f8d96ff1dd1b 141 wait(0.5);
elmkom 35:2e864bae3af0 142 }
elmkom 35:2e864bae3af0 143
elmkom 36:f8d96ff1dd1b 144 short* read_proximity(int sensor)
elmkom 35:2e864bae3af0 145 {
elmkom 36:f8d96ff1dd1b 146 short readings[3];
elmkom 36:f8d96ff1dd1b 147
elmkom 36:f8d96ff1dd1b 148 prox_write(MUXADDRESS,1<<sensor);
elmkom 36:f8d96ff1dd1b 149 //proximity_sensor_on(sensor);
elmkom 36:f8d96ff1dd1b 150 unsigned char prox_lsb = prox_read_reg(PROXIMITYADDRESS,0x44);
elmkom 36:f8d96ff1dd1b 151 unsigned char prox_msb = prox_read_reg(PROXIMITYADDRESS,0x45);
elmkom 36:f8d96ff1dd1b 152 unsigned char ALS_lsb = prox_read_reg(PROXIMITYADDRESS,0x46);
elmkom 36:f8d96ff1dd1b 153 unsigned char ALS_msb = prox_read_reg(PROXIMITYADDRESS,0x47);
elmkom 36:f8d96ff1dd1b 154 unsigned char IR_lsb = prox_read_reg(PROXIMITYADDRESS,0x48);
elmkom 36:f8d96ff1dd1b 155 unsigned char IR_msb = prox_read_reg(PROXIMITYADDRESS,0x49);
elmkom 35:2e864bae3af0 156
elmkom 35:2e864bae3af0 157 short proximity = prox_msb*256+prox_lsb;
elmkom 35:2e864bae3af0 158 short ALS = ALS_msb*256+ALS_lsb;
elmkom 35:2e864bae3af0 159 short IR = IR_msb*256+IR_lsb;
elmkom 36:f8d96ff1dd1b 160 readings[0] = proximity;
elmkom 36:f8d96ff1dd1b 161 readings[1] = ALS;
elmkom 36:f8d96ff1dd1b 162 readings[2] = IR;
elmkom 36:f8d96ff1dd1b 163 //proximity_sensor_off(sensor);
elmkom 36:f8d96ff1dd1b 164 //pc.printf(GRN "Sensor %d = %d, %d, %d\n\r",sensor,proximity,ALS,IR);
elmkom 36:f8d96ff1dd1b 165
elmkom 36:f8d96ff1dd1b 166
elmkom 36:f8d96ff1dd1b 167 return readings;
elmkom 35:2e864bae3af0 168 }
elmkom 35:2e864bae3af0 169
elmkom 35:2e864bae3af0 170
elmkom 36:f8d96ff1dd1b 171
elmkom 36:f8d96ff1dd1b 172
elmkom 36:f8d96ff1dd1b 173
elmkom 36:f8d96ff1dd1b 174 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 175 //* Set the RGB LED's Color
elmkom 36:f8d96ff1dd1b 176 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
elmkom 36:f8d96ff1dd1b 177 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 178 void SetLedColor(unsigned char ucColor)
elmkom 35:2e864bae3af0 179 {
elmkom 36:f8d96ff1dd1b 180 //Note that when an LED is on, you write a 0 to it:
elmkom 36:f8d96ff1dd1b 181 led_red = !(ucColor & 0x1); //bit 0
elmkom 36:f8d96ff1dd1b 182 led_green = !(ucColor & 0x2); //bit 1
elmkom 36:f8d96ff1dd1b 183 led_blue = !(ucColor & 0x4); //bit 2
elmkom 36:f8d96ff1dd1b 184 } //SetLedColor()
elmkom 35:2e864bae3af0 185
JMF 0:9d5134074d84 186 ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) {
JMF 0:9d5134074d84 187 int cin = -1;
JMF 0:9d5134074d84 188 int cin_last;
JMF 0:9d5134074d84 189
JMF 0:9d5134074d84 190 if (NULL == buff || size == 0) {
JMF 0:9d5134074d84 191 return -1;
JMF 0:9d5134074d84 192 }
JMF 0:9d5134074d84 193
JMF 0:9d5134074d84 194 size_t len = 0;
JMF 0:9d5134074d84 195 Timer timer;
JMF 0:9d5134074d84 196 timer.start();
JMF 0:9d5134074d84 197 while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) {
JMF 0:9d5134074d84 198 if (mdm.readable()) {
JMF 0:9d5134074d84 199 cin_last = cin;
JMF 0:9d5134074d84 200 cin = mdm.getc();
JMF 0:9d5134074d84 201 if (isprint(cin)) {
JMF 0:9d5134074d84 202 buff[len++] = (char)cin;
JMF 0:9d5134074d84 203 continue;
JMF 0:9d5134074d84 204 } else if (('\r' == cin_last) && ('\n' == cin)) {
JMF 0:9d5134074d84 205 break;
JMF 0:9d5134074d84 206 }
JMF 0:9d5134074d84 207 }
JMF 0:9d5134074d84 208 wait_ms(1);
JMF 0:9d5134074d84 209 }
JMF 2:0e2ef866af95 210 buff[len] = (char)NULL;
JMF 0:9d5134074d84 211
JMF 0:9d5134074d84 212 return len;
JMF 0:9d5134074d84 213 }
JMF 0:9d5134074d84 214
JMF 0:9d5134074d84 215 int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) {
JMF 0:9d5134074d84 216 if (cmd && strlen(cmd) > 0) {
JMF 0:9d5134074d84 217 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 218 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 0:9d5134074d84 219 }
JMF 0:9d5134074d84 220 mdm.printf("%s\r\n", cmd);
JMF 0:9d5134074d84 221 }
JMF 0:9d5134074d84 222
JMF 0:9d5134074d84 223 if (rsp_list) {
JMF 0:9d5134074d84 224 Timer timer;
JMF 0:9d5134074d84 225 char rsp[MAX_AT_RSP_LEN+1];
JMF 0:9d5134074d84 226 int len;
JMF 0:9d5134074d84 227
JMF 0:9d5134074d84 228 timer.start();
JMF 0:9d5134074d84 229 while (timer.read_ms() < timeout_ms) {
JMF 0:9d5134074d84 230 len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms());
JMF 0:9d5134074d84 231
JMF 0:9d5134074d84 232 if (len < 0)
JMF 0:9d5134074d84 233 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 234
JMF 0:9d5134074d84 235 if (len == 0)
JMF 0:9d5134074d84 236 continue;
JMF 0:9d5134074d84 237
JMF 0:9d5134074d84 238 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 0:9d5134074d84 239 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp);
JMF 0:9d5134074d84 240 }
JMF 0:9d5134074d84 241
JMF 0:9d5134074d84 242 if (rsp_list) {
JMF 0:9d5134074d84 243 int rsp_idx = 0;
JMF 0:9d5134074d84 244 while (rsp_list[rsp_idx]) {
JMF 0:9d5134074d84 245 if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) {
JMF 0:9d5134074d84 246 return rsp_idx;
JMF 0:9d5134074d84 247 }
JMF 0:9d5134074d84 248 rsp_idx++;
JMF 0:9d5134074d84 249 }
JMF 0:9d5134074d84 250 }
JMF 0:9d5134074d84 251 }
JMF 0:9d5134074d84 252 return MDM_ERR_TIMEOUT;
JMF 0:9d5134074d84 253 }
JMF 0:9d5134074d84 254 return MDM_OK;
JMF 0:9d5134074d84 255 }
elmkom 35:2e864bae3af0 256 int mdm_init(void) {
elmkom 35:2e864bae3af0 257 // disable signal level translator (necessary
elmkom 35:2e864bae3af0 258 // for the modem to boot properly)
elmkom 35:2e864bae3af0 259 shield_3v3_1v8_sig_trans_ena = 0;
JMF 0:9d5134074d84 260
elmkom 35:2e864bae3af0 261 // Hard reset the modem (doesn't go through
elmkom 35:2e864bae3af0 262 // the signal level translator)
elmkom 35:2e864bae3af0 263 mdm_reset = 1;
elmkom 35:2e864bae3af0 264
elmkom 35:2e864bae3af0 265 // wait a moment for the modem to react
elmkom 35:2e864bae3af0 266 wait_ms(10);
elmkom 35:2e864bae3af0 267
elmkom 35:2e864bae3af0 268 // Let modem boot
elmkom 35:2e864bae3af0 269 mdm_reset = 0;
elmkom 35:2e864bae3af0 270
elmkom 35:2e864bae3af0 271 // wait a moment for the modem to react
elmkom 35:2e864bae3af0 272 wait(1.0);
elmkom 35:2e864bae3af0 273
elmkom 35:2e864bae3af0 274 // power modem on //off
elmkom 35:2e864bae3af0 275 mdm_power_on = 0; //1;
elmkom 35:2e864bae3af0 276
elmkom 35:2e864bae3af0 277 // insure modem boots into normal operating mode
elmkom 35:2e864bae3af0 278 // and does not go to sleep when powered on
elmkom 35:2e864bae3af0 279 mdm_uart2_rx_boot_mode_sel = 1;
elmkom 35:2e864bae3af0 280 mdm_wakeup_in = 1;
elmkom 35:2e864bae3af0 281
elmkom 35:2e864bae3af0 282 // initialze comm with the modem
elmkom 35:2e864bae3af0 283 mdm.baud(115200);
elmkom 35:2e864bae3af0 284 // clear out potential garbage
elmkom 35:2e864bae3af0 285 while (mdm.readable())
elmkom 35:2e864bae3af0 286 mdm.getc();
elmkom 35:2e864bae3af0 287
elmkom 35:2e864bae3af0 288 mdm_uart1_cts = 0;
elmkom 35:2e864bae3af0 289
elmkom 35:2e864bae3af0 290 // wait a moment for the modem to react to signal
elmkom 35:2e864bae3af0 291 // conditions while the level translator is disabled
elmkom 35:2e864bae3af0 292 // (sorry, don't have enough information to know
elmkom 35:2e864bae3af0 293 // what exactly the modem is doing with the current
elmkom 35:2e864bae3af0 294 // pin settings)
elmkom 35:2e864bae3af0 295 wait(1.0);
elmkom 35:2e864bae3af0 296
elmkom 35:2e864bae3af0 297 // enable the signal level translator to start
elmkom 35:2e864bae3af0 298 // modem reset process (modem will be powered down)
elmkom 35:2e864bae3af0 299 shield_3v3_1v8_sig_trans_ena = 1;
elmkom 35:2e864bae3af0 300
elmkom 35:2e864bae3af0 301 // Give the modem 60 secons to start responding by
elmkom 35:2e864bae3af0 302 // sending simple 'AT' commands to modem once per second.
elmkom 35:2e864bae3af0 303 Timer timer;
elmkom 35:2e864bae3af0 304 timer.start();
elmkom 35:2e864bae3af0 305 while (timer.read() < 60) {
elmkom 36:f8d96ff1dd1b 306 SetLedColor(0x1); //red
elmkom 35:2e864bae3af0 307 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 35:2e864bae3af0 308 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 35:2e864bae3af0 309 if (rc == 0)
elmkom 35:2e864bae3af0 310 return true; //timer.read();
elmkom 36:f8d96ff1dd1b 311 SetLedColor(0); //off
elmkom 36:f8d96ff1dd1b 312 wait_ms(1000 - (timer.read_ms() % 1000));
elmkom 36:f8d96ff1dd1b 313 pc.printf("\r%d",timer.read_ms()/1000);
elmkom 36:f8d96ff1dd1b 314
elmkom 36:f8d96ff1dd1b 315 }
elmkom 36:f8d96ff1dd1b 316 return false;
elmkom 36:f8d96ff1dd1b 317 }
elmkom 36:f8d96ff1dd1b 318
elmkom 36:f8d96ff1dd1b 319 bool oldwakeModem()
elmkom 36:f8d96ff1dd1b 320 {
elmkom 36:f8d96ff1dd1b 321 Timer timer;
elmkom 36:f8d96ff1dd1b 322 timer.start();
elmkom 36:f8d96ff1dd1b 323 while (timer.read() < 60) {
elmkom 36:f8d96ff1dd1b 324 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 36:f8d96ff1dd1b 325 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 36:f8d96ff1dd1b 326 if (rc == 0)
elmkom 36:f8d96ff1dd1b 327 return true;
elmkom 35:2e864bae3af0 328 wait_ms(1000 - (timer.read_ms() % 1000));
elmkom 35:2e864bae3af0 329 pc.printf("\r%d",timer.read_ms()/1000);
elmkom 35:2e864bae3af0 330 }
elmkom 36:f8d96ff1dd1b 331 return false;
elmkom 35:2e864bae3af0 332 }
elmkom 36:f8d96ff1dd1b 333
elmkom 36:f8d96ff1dd1b 334 bool wakeModem()
elmkom 36:f8d96ff1dd1b 335 {
elmkom 36:f8d96ff1dd1b 336 const char * rsp_lst[] = { ok_str, error_str, NULL };
elmkom 36:f8d96ff1dd1b 337 int tries = 60;
elmkom 36:f8d96ff1dd1b 338 pc.printf("wake ");
elmkom 36:f8d96ff1dd1b 339 while (tries > 0) {
elmkom 36:f8d96ff1dd1b 340 tries--;
elmkom 36:f8d96ff1dd1b 341 pc.printf(".");
elmkom 36:f8d96ff1dd1b 342 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
elmkom 36:f8d96ff1dd1b 343 if (rc == 0)
elmkom 36:f8d96ff1dd1b 344 {
elmkom 36:f8d96ff1dd1b 345 pc.printf("\r\n");
elmkom 36:f8d96ff1dd1b 346 return true;
elmkom 36:f8d96ff1dd1b 347 }
elmkom 36:f8d96ff1dd1b 348 wait(1.0);
elmkom 36:f8d96ff1dd1b 349 }
elmkom 36:f8d96ff1dd1b 350 return false;
elmkom 36:f8d96ff1dd1b 351 }
elmkom 36:f8d96ff1dd1b 352
elmkom 35:2e864bae3af0 353 int oldmdm_init(void) {
fkellermavnet 14:0c353e212296 354 // Hard reset the modem (doesn't go through
fkellermavnet 14:0c353e212296 355 // the signal level translator)
fkellermavnet 14:0c353e212296 356 mdm_reset = 0;
JMF 17:38a8cc0c6ba5 357
JMF 17:38a8cc0c6ba5 358 // disable signal level translator (necessary
JMF 17:38a8cc0c6ba5 359 // for the modem to boot properly). All signals
JMF 17:38a8cc0c6ba5 360 // except mdm_reset go through the level translator
JMF 17:38a8cc0c6ba5 361 // and have internal pull-up/down in the module. While
JMF 17:38a8cc0c6ba5 362 // the level translator is disabled, these pins will
JMF 17:38a8cc0c6ba5 363 // be in the correct state.
JMF 17:38a8cc0c6ba5 364 shield_3v3_1v8_sig_trans_ena = 0;
JMF 17:38a8cc0c6ba5 365
JMF 17:38a8cc0c6ba5 366 // While the level translator is disabled and ouptut pins
JMF 17:38a8cc0c6ba5 367 // are tristated, make sure the inputs are in the same state
JMF 17:38a8cc0c6ba5 368 // as the WNC Module pins so that when the level translator is
JMF 17:38a8cc0c6ba5 369 // enabled, there are no differences.
JMF 17:38a8cc0c6ba5 370 mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high
JMF 17:38a8cc0c6ba5 371 mdm_power_on = 0; // powr_on should be low
JMF 17:38a8cc0c6ba5 372 mdm_wakeup_in = 1; // wake-up should be high
JMF 17:38a8cc0c6ba5 373 mdm_uart1_cts = 0; // indicate that it is ok to send
JMF 17:38a8cc0c6ba5 374
JMF 17:38a8cc0c6ba5 375 // Now, wait for the WNC Module to perform its initial boot correctly
fkellermavnet 14:0c353e212296 376 wait(1.0);
JMF 17:38a8cc0c6ba5 377
JMF 17:38a8cc0c6ba5 378 // The WNC module initializes comms at 115200 8N1 so set it up
JMF 17:38a8cc0c6ba5 379 mdm.baud(115200);
JMF 0:9d5134074d84 380
JMF 17:38a8cc0c6ba5 381 //Now, enable the level translator, the input pins should now be the
JMF 17:38a8cc0c6ba5 382 //same as how the M14A module is driving them with internal pull ups/downs.
JMF 17:38a8cc0c6ba5 383 //When enabled, there will be no changes in these 4 pins...
JMF 17:38a8cc0c6ba5 384 shield_3v3_1v8_sig_trans_ena = 1;
JMF 2:0e2ef866af95 385
JMF 17:38a8cc0c6ba5 386 // Now, give the modem 60 secons to start responding by
JMF 0:9d5134074d84 387 // sending simple 'AT' commands to modem once per second.
JMF 0:9d5134074d84 388 Timer timer;
JMF 0:9d5134074d84 389 timer.start();
JMF 0:9d5134074d84 390 while (timer.read() < 60) {
JMF 0:9d5134074d84 391 const char * rsp_lst[] = { ok_str, error_str, NULL };
JMF 0:9d5134074d84 392 int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
JMF 0:9d5134074d84 393 if (rc == 0)
fkellermavnet 14:0c353e212296 394 return true; //timer.read();
JMF 0:9d5134074d84 395 wait_ms(1000 - (timer.read_ms() % 1000));
JMF 0:9d5134074d84 396 pc.printf("\r%d",timer.read_ms()/1000);
JMF 0:9d5134074d84 397 }
JMF 0:9d5134074d84 398 return false;
JMF 0:9d5134074d84 399 }
JMF 0:9d5134074d84 400
JMF 2:0e2ef866af95 401 int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) {
JMF 2:0e2ef866af95 402 static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars)
fkellermavnet 6:713b4cbf1a7d 403 size_t n = strlen(cmd);
fkellermavnet 6:713b4cbf1a7d 404 if (cmd && n > 0) {
JMF 2:0e2ef866af95 405 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 406 printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd);
JMF 2:0e2ef866af95 407 }
fkellermavnet 6:713b4cbf1a7d 408 while (n--) {
fkellermavnet 6:713b4cbf1a7d 409 mdm.putc(*cmd++);
fkellermavnet 6:713b4cbf1a7d 410 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 411 };
fkellermavnet 6:713b4cbf1a7d 412 mdm.putc('\r');
fkellermavnet 6:713b4cbf1a7d 413 wait_ms(1);
fkellermavnet 6:713b4cbf1a7d 414 mdm.putc('\n');
fkellermavnet 6:713b4cbf1a7d 415 wait_ms(1);
JMF 2:0e2ef866af95 416 }
JMF 2:0e2ef866af95 417
JMF 2:0e2ef866af95 418 if (rsp_list) {
JMF 2:0e2ef866af95 419 rsp->erase(); // Clean up from prior cmd response
JMF 2:0e2ef866af95 420 *len = 0;
JMF 2:0e2ef866af95 421 Timer timer;
JMF 2:0e2ef866af95 422 timer.start();
JMF 2:0e2ef866af95 423 while (timer.read_ms() < timeout_ms) {
JMF 2:0e2ef866af95 424 int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms());
JMF 2:0e2ef866af95 425
JMF 2:0e2ef866af95 426 if (lenCmd == 0)
JMF 2:0e2ef866af95 427 continue;
JMF 2:0e2ef866af95 428
JMF 2:0e2ef866af95 429 if (lenCmd < 0)
JMF 2:0e2ef866af95 430 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 431 else {
JMF 2:0e2ef866af95 432 *len += lenCmd;
JMF 2:0e2ef866af95 433 *rsp += cmd_buf;
JMF 2:0e2ef866af95 434 }
JMF 2:0e2ef866af95 435
JMF 2:0e2ef866af95 436 if (mdm_dbgmask & MDM_DBG_AT_CMDS) {
JMF 2:0e2ef866af95 437 printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf);
JMF 2:0e2ef866af95 438 }
JMF 2:0e2ef866af95 439
JMF 2:0e2ef866af95 440 int rsp_idx = 0;
JMF 2:0e2ef866af95 441 while (rsp_list[rsp_idx]) {
JMF 2:0e2ef866af95 442 if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) {
JMF 2:0e2ef866af95 443 return rsp_idx;
JMF 2:0e2ef866af95 444 }
JMF 2:0e2ef866af95 445 rsp_idx++;
JMF 2:0e2ef866af95 446 }
JMF 2:0e2ef866af95 447 }
JMF 2:0e2ef866af95 448 return MDM_ERR_TIMEOUT;
JMF 2:0e2ef866af95 449 }
JMF 2:0e2ef866af95 450 pc.printf("D %s",rsp);
JMF 2:0e2ef866af95 451 return MDM_OK;
JMF 2:0e2ef866af95 452 }
elmkom 36:f8d96ff1dd1b 453 void system_reset()
elmkom 36:f8d96ff1dd1b 454 {
elmkom 36:f8d96ff1dd1b 455 printf(RED "\n\rSystem resetting..." DEF "\n");
elmkom 36:f8d96ff1dd1b 456 NVIC_SystemReset();
elmkom 36:f8d96ff1dd1b 457 }
JMF 2:0e2ef866af95 458 void reinitialize_mdm(void)
JMF 2:0e2ef866af95 459 {
elmkom 36:f8d96ff1dd1b 460 system_reset();
elmkom 36:f8d96ff1dd1b 461 /*
JMF 2:0e2ef866af95 462 // Initialize the modem
JMF 2:0e2ef866af95 463 printf(GRN "Modem RE-initializing..." DEF "\r\n");
JMF 2:0e2ef866af95 464 if (!mdm_init()) {
elmkom 36:f8d96ff1dd1b 465
elmkom 36:f8d96ff1dd1b 466 system_reset();
JMF 2:0e2ef866af95 467 }
JMF 2:0e2ef866af95 468 printf("\r\n");
elmkom 36:f8d96ff1dd1b 469 */
JMF 2:0e2ef866af95 470 }
JMF 2:0e2ef866af95 471 // These are built on the fly
JMF 2:0e2ef866af95 472 string MyServerIpAddress;
JMF 2:0e2ef866af95 473 string MySocketData;
JMF 2:0e2ef866af95 474
JMF 2:0e2ef866af95 475 // These are to be built on the fly
JMF 2:0e2ef866af95 476 string my_temp;
JMF 2:0e2ef866af95 477 string my_humidity;
JMF 2:0e2ef866af95 478
JMF 0:9d5134074d84 479 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 480
stefanrousseau 3:26b3cc155f39 481 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 482 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 483 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 484 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 485 {
stefanrousseau 12:7c94ec5069dc 486 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 487 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 488 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 489 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 490 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 491 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 492 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 493 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 494 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 495 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 496 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 497 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 498 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 499 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 500 };
stefanrousseau 12:7c94ec5069dc 501
stefanrousseau 3:26b3cc155f39 502 void GenerateModemString(char * modem_string)
stefanrousseau 3:26b3cc155f39 503 {
stefanrousseau 12:7c94ec5069dc 504 switch(iSensorsToReport)
stefanrousseau 12:7c94ec5069dc 505 {
elmkom 35:2e864bae3af0 506 case PROXIMITY:
elmkom 35:2e864bae3af0 507 {
elmkom 36:f8d96ff1dd1b 508 char dataStr[NUM_PROXIMIY_SENSORS*32];
elmkom 36:f8d96ff1dd1b 509 int i=0;
elmkom 36:f8d96ff1dd1b 510 int index = 0;
elmkom 36:f8d96ff1dd1b 511 for (i=0; i<NUM_PROXIMIY_SENSORS; i++)
elmkom 36:f8d96ff1dd1b 512 {
elmkom 36:f8d96ff1dd1b 513 if(i<NUM_PROXIMIY_SENSORS-1)
elmkom 36:f8d96ff1dd1b 514 index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]);
elmkom 36:f8d96ff1dd1b 515 else
elmkom 36:f8d96ff1dd1b 516 index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]);
elmkom 36:f8d96ff1dd1b 517 }
elmkom 36:f8d96ff1dd1b 518 sprintf(modem_string, "GET %s%s?serial=%s&data=[%s] %s%s\r\n\r\n",
elmkom 36:f8d96ff1dd1b 519 FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, dataStr, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 36:f8d96ff1dd1b 520
elmkom 35:2e864bae3af0 521 break;
elmkom 35:2e864bae3af0 522 }
stefanrousseau 12:7c94ec5069dc 523 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 524 {
stefanrousseau 12:7c94ec5069dc 525 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 526 break;
stefanrousseau 12:7c94ec5069dc 527 }
stefanrousseau 12:7c94ec5069dc 528 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 529 {
stefanrousseau 12:7c94ec5069dc 530 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 531 break;
stefanrousseau 12:7c94ec5069dc 532 }
stefanrousseau 12:7c94ec5069dc 533 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 534 {
stefanrousseau 12:7c94ec5069dc 535 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 536 break;
stefanrousseau 12:7c94ec5069dc 537 }
stefanrousseau 12:7c94ec5069dc 538 default:
stefanrousseau 12:7c94ec5069dc 539 {
stefanrousseau 12:7c94ec5069dc 540 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 541 break;
stefanrousseau 12:7c94ec5069dc 542 }
stefanrousseau 16:17c5916f2d12 543 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 544 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 545
stefanrousseau 3:26b3cc155f39 546
stefanrousseau 3:26b3cc155f39 547 //Periodic timer
stefanrousseau 3:26b3cc155f39 548 Ticker OneMsTicker;
stefanrousseau 3:26b3cc155f39 549 volatile bool bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 550 int OneMsTicks = 0;
stefanrousseau 3:26b3cc155f39 551 int iTimer1Interval_ms = 1000;
stefanrousseau 3:26b3cc155f39 552 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 553 //* Periodic 1ms timer tick
stefanrousseau 3:26b3cc155f39 554 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 555 void OneMsFunction()
stefanrousseau 3:26b3cc155f39 556 {
stefanrousseau 3:26b3cc155f39 557 OneMsTicks++;
stefanrousseau 3:26b3cc155f39 558 if ((OneMsTicks % iTimer1Interval_ms) == 0)
stefanrousseau 3:26b3cc155f39 559 {
stefanrousseau 3:26b3cc155f39 560 bTimerExpiredFlag = true;
stefanrousseau 3:26b3cc155f39 561 }
stefanrousseau 3:26b3cc155f39 562 } //OneMsFunction()
stefanrousseau 3:26b3cc155f39 563
elmkom 36:f8d96ff1dd1b 564
stefanrousseau 16:17c5916f2d12 565
stefanrousseau 16:17c5916f2d12 566 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 567 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 568 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 569 bool extract_JSON(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 570 {
stefanrousseau 16:17c5916f2d12 571 char* beginquote;
stefanrousseau 16:17c5916f2d12 572 char* endquote;
stefanrousseau 16:17c5916f2d12 573 beginquote = strchr(search_field, '{'); //start of JSON
stefanrousseau 16:17c5916f2d12 574 endquote = strchr(search_field, '}'); //end of JSON
stefanrousseau 16:17c5916f2d12 575 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 576 {
stefanrousseau 16:17c5916f2d12 577 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 578 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 579 {
stefanrousseau 16:17c5916f2d12 580 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 581 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 582 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 583 return true;
stefanrousseau 16:17c5916f2d12 584 }
stefanrousseau 16:17c5916f2d12 585 else
stefanrousseau 16:17c5916f2d12 586 {
stefanrousseau 16:17c5916f2d12 587 endquote = strchr(search_field, '\0'); //end of string... sometimes the end bracket is missing
stefanrousseau 16:17c5916f2d12 588 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 589 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 590 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 591 return false;
stefanrousseau 16:17c5916f2d12 592 }
stefanrousseau 16:17c5916f2d12 593 }
stefanrousseau 16:17c5916f2d12 594 else
stefanrousseau 16:17c5916f2d12 595 {
stefanrousseau 16:17c5916f2d12 596 return false;
stefanrousseau 16:17c5916f2d12 597 }
stefanrousseau 16:17c5916f2d12 598 } //extract_JSON
stefanrousseau 16:17c5916f2d12 599
stefanrousseau 16:17c5916f2d12 600 bool parse_JSON(char* json_string)
stefanrousseau 16:17c5916f2d12 601 {
stefanrousseau 16:17c5916f2d12 602 char* beginquote;
stefanrousseau 16:17c5916f2d12 603 char token[] = "\"LED\":\"";
stefanrousseau 16:17c5916f2d12 604 beginquote = strstr(json_string, token );
stefanrousseau 16:17c5916f2d12 605 if ((beginquote != 0))
stefanrousseau 16:17c5916f2d12 606 {
stefanrousseau 16:17c5916f2d12 607 char cLedColor = beginquote[strlen(token)];
stefanrousseau 16:17c5916f2d12 608 printf(GRN "LED Found : %c" DEF "\r\n", cLedColor);
stefanrousseau 16:17c5916f2d12 609 switch(cLedColor)
stefanrousseau 16:17c5916f2d12 610 {
stefanrousseau 16:17c5916f2d12 611 case 'O':
stefanrousseau 16:17c5916f2d12 612 { //Off
stefanrousseau 16:17c5916f2d12 613 SetLedColor(0);
stefanrousseau 16:17c5916f2d12 614 break;
stefanrousseau 16:17c5916f2d12 615 }
stefanrousseau 16:17c5916f2d12 616 case 'R':
stefanrousseau 16:17c5916f2d12 617 { //Red
stefanrousseau 16:17c5916f2d12 618 SetLedColor(1);
stefanrousseau 16:17c5916f2d12 619 break;
stefanrousseau 16:17c5916f2d12 620 }
stefanrousseau 16:17c5916f2d12 621 case 'G':
stefanrousseau 16:17c5916f2d12 622 { //Green
stefanrousseau 16:17c5916f2d12 623 SetLedColor(2);
stefanrousseau 16:17c5916f2d12 624 break;
stefanrousseau 16:17c5916f2d12 625 }
stefanrousseau 16:17c5916f2d12 626 case 'Y':
stefanrousseau 16:17c5916f2d12 627 { //Yellow
stefanrousseau 16:17c5916f2d12 628 SetLedColor(3);
stefanrousseau 16:17c5916f2d12 629 break;
stefanrousseau 16:17c5916f2d12 630 }
stefanrousseau 16:17c5916f2d12 631 case 'B':
stefanrousseau 16:17c5916f2d12 632 { //Blue
stefanrousseau 16:17c5916f2d12 633 SetLedColor(4);
stefanrousseau 16:17c5916f2d12 634 break;
stefanrousseau 16:17c5916f2d12 635 }
stefanrousseau 16:17c5916f2d12 636 case 'M':
stefanrousseau 16:17c5916f2d12 637 { //Magenta
stefanrousseau 16:17c5916f2d12 638 SetLedColor(5);
stefanrousseau 16:17c5916f2d12 639 break;
stefanrousseau 16:17c5916f2d12 640 }
stefanrousseau 16:17c5916f2d12 641 case 'T':
stefanrousseau 16:17c5916f2d12 642 { //Turquoise
stefanrousseau 16:17c5916f2d12 643 SetLedColor(6);
stefanrousseau 16:17c5916f2d12 644 break;
stefanrousseau 16:17c5916f2d12 645 }
stefanrousseau 16:17c5916f2d12 646 case 'W':
stefanrousseau 16:17c5916f2d12 647 { //White
stefanrousseau 16:17c5916f2d12 648 SetLedColor(7);
stefanrousseau 16:17c5916f2d12 649 break;
stefanrousseau 16:17c5916f2d12 650 }
stefanrousseau 16:17c5916f2d12 651 default:
stefanrousseau 16:17c5916f2d12 652 {
stefanrousseau 16:17c5916f2d12 653 break;
stefanrousseau 16:17c5916f2d12 654 }
stefanrousseau 16:17c5916f2d12 655 } //switch(cLedColor)
stefanrousseau 16:17c5916f2d12 656 return true;
stefanrousseau 16:17c5916f2d12 657 }
stefanrousseau 16:17c5916f2d12 658 else
stefanrousseau 16:17c5916f2d12 659 {
stefanrousseau 16:17c5916f2d12 660 return false;
stefanrousseau 16:17c5916f2d12 661 }
stefanrousseau 16:17c5916f2d12 662 } //parse_JSON
stefanrousseau 16:17c5916f2d12 663
JMF 0:9d5134074d84 664 int main() {
elmkom 36:f8d96ff1dd1b 665
JMF 2:0e2ef866af95 666 int i;
elmkom 36:f8d96ff1dd1b 667
JMF 0:9d5134074d84 668 HTS221 hts221;
JMF 0:9d5134074d84 669 pc.baud(115200);
elmkom 35:2e864bae3af0 670 proximityi2c.frequency(400000);
elmkom 36:f8d96ff1dd1b 671
JMF 0:9d5134074d84 672
JMF 0:9d5134074d84 673 void hts221_init(void);
JMF 0:9d5134074d84 674
fkellermavnet 20:27a4f27254d0 675 // Set LED to RED until init finishes
fkellermavnet 20:27a4f27254d0 676 SetLedColor(0x1);
fkellermavnet 20:27a4f27254d0 677
JMF 1:af7a42f7d465 678 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 679 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 680
JMF 0:9d5134074d84 681 i = hts221.begin();
JMF 0:9d5134074d84 682 if( i )
JMF 0:9d5134074d84 683 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 684 else
JMF 0:9d5134074d84 685 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 686
JMF 0:9d5134074d84 687 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 688 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
JMF 0:9d5134074d84 689
elmkom 36:f8d96ff1dd1b 690
elmkom 36:f8d96ff1dd1b 691 //test
elmkom 36:f8d96ff1dd1b 692 /*
elmkom 36:f8d96ff1dd1b 693 int count = 0;
elmkom 36:f8d96ff1dd1b 694 while(count < 4)
elmkom 36:f8d96ff1dd1b 695 {
elmkom 36:f8d96ff1dd1b 696 if(count == 0)
elmkom 36:f8d96ff1dd1b 697 proximity_sensor_on(i);
elmkom 36:f8d96ff1dd1b 698 for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++)
elmkom 36:f8d96ff1dd1b 699 {
elmkom 36:f8d96ff1dd1b 700
elmkom 36:f8d96ff1dd1b 701 short* readings = read_proximity(i);
elmkom 36:f8d96ff1dd1b 702 proximityData[i][0] = readings[0];
elmkom 36:f8d96ff1dd1b 703 proximityData[i][1] = readings[1];
elmkom 36:f8d96ff1dd1b 704 proximityData[i][2] = readings[2];
elmkom 36:f8d96ff1dd1b 705 }
elmkom 36:f8d96ff1dd1b 706 proximity_sensor_off(i);
elmkom 36:f8d96ff1dd1b 707 wait(2);
elmkom 36:f8d96ff1dd1b 708 count++;
elmkom 36:f8d96ff1dd1b 709 }
elmkom 36:f8d96ff1dd1b 710 */
elmkom 35:2e864bae3af0 711
elmkom 36:f8d96ff1dd1b 712 for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++)
elmkom 36:f8d96ff1dd1b 713 {
elmkom 36:f8d96ff1dd1b 714 proximity_sensor_on(i);
elmkom 36:f8d96ff1dd1b 715 lastProximityData[i][0] = 1000;
elmkom 36:f8d96ff1dd1b 716 }
elmkom 36:f8d96ff1dd1b 717
stefanrousseau 11:e6602513730f 718 sensors_init();
stefanrousseau 12:7c94ec5069dc 719 read_sensors();
stefanrousseau 11:e6602513730f 720
JMF 0:9d5134074d84 721 // Initialize the modem
JMF 0:9d5134074d84 722 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 723 do {
fkellermavnet 14:0c353e212296 724 i=mdm_init();
fkellermavnet 14:0c353e212296 725 if (!i) {
fkellermavnet 14:0c353e212296 726 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 727 }
fkellermavnet 14:0c353e212296 728 } while (!i);
JMF 0:9d5134074d84 729
elmkom 36:f8d96ff1dd1b 730
elmkom 36:f8d96ff1dd1b 731
JMF 2:0e2ef866af95 732 //Software init
JMF 2:0e2ef866af95 733 software_init_mdm();
fkellermavnet 19:f89baed3bd6f 734
JMF 2:0e2ef866af95 735 // Resolve URL to IP address to connect to
JMF 2:0e2ef866af95 736 resolve_mdm();
JMF 0:9d5134074d84 737
stefanrousseau 3:26b3cc155f39 738 //Create a 1ms timer tick function:
elmkom 36:f8d96ff1dd1b 739 //OneMsTicker.attach(OneMsFunction, 0.001f) ;
fkellermavnet 26:8d6e7e7cdcae 740
elmkom 36:f8d96ff1dd1b 741 // iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
stefanrousseau 3:26b3cc155f39 742
fkellermavnet 26:8d6e7e7cdcae 743 // Open the socket (connect to the server)
fkellermavnet 25:e7996d22a7e6 744 sockopen_mdm();
stefanrousseau 3:26b3cc155f39 745
fkellermavnet 20:27a4f27254d0 746 // Set LED BLUE for partial init
fkellermavnet 20:27a4f27254d0 747 SetLedColor(0x4);
elmkom 36:f8d96ff1dd1b 748
JMF 2:0e2ef866af95 749 // Send and receive data perpetually
JMF 2:0e2ef866af95 750 while(1) {
fkellermavnet 20:27a4f27254d0 751 static unsigned ledOnce = 0;
elmkom 36:f8d96ff1dd1b 752 if (true || bTimerExpiredFlag)
stefanrousseau 3:26b3cc155f39 753 {
elmkom 36:f8d96ff1dd1b 754
stefanrousseau 3:26b3cc155f39 755 bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 756 sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
stefanrousseau 3:26b3cc155f39 757 sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
stefanrousseau 4:f83bedd9cab4 758 read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
fkellermavnet 20:27a4f27254d0 759
elmkom 36:f8d96ff1dd1b 760 SetLedColor(0x2); //green
elmkom 36:f8d96ff1dd1b 761 for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++)
elmkom 36:f8d96ff1dd1b 762 {
elmkom 36:f8d96ff1dd1b 763
elmkom 36:f8d96ff1dd1b 764 short* readings = read_proximity(i);
elmkom 36:f8d96ff1dd1b 765 proximityData[i][0] = readings[0];
elmkom 36:f8d96ff1dd1b 766 proximityData[i][1] = readings[1];
elmkom 36:f8d96ff1dd1b 767 proximityData[i][2] = readings[2];
elmkom 36:f8d96ff1dd1b 768 if(abs(proximityData[i][0] - lastProximityData[i][0]) > 5)
elmkom 36:f8d96ff1dd1b 769 {
elmkom 36:f8d96ff1dd1b 770 proximityChange = true;
elmkom 36:f8d96ff1dd1b 771 }
elmkom 36:f8d96ff1dd1b 772 lastProximityData[i][0] = proximityData[i][0];
elmkom 36:f8d96ff1dd1b 773
elmkom 36:f8d96ff1dd1b 774
fkellermavnet 20:27a4f27254d0 775 }
elmkom 36:f8d96ff1dd1b 776
elmkom 36:f8d96ff1dd1b 777
elmkom 36:f8d96ff1dd1b 778 SetLedColor(0); //off
fkellermavnet 20:27a4f27254d0 779
elmkom 36:f8d96ff1dd1b 780 if(proximityChange)
stefanrousseau 16:17c5916f2d12 781 {
elmkom 36:f8d96ff1dd1b 782 SetLedColor(0x04); //blue
elmkom 36:f8d96ff1dd1b 783 proximityChange = false;
elmkom 36:f8d96ff1dd1b 784 char modem_string[512];
elmkom 36:f8d96ff1dd1b 785 GenerateModemString(&modem_string[0]);
elmkom 36:f8d96ff1dd1b 786 printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
elmkom 36:f8d96ff1dd1b 787 wakeModem();
elmkom 36:f8d96ff1dd1b 788 sockwrite_mdm(modem_string);
elmkom 36:f8d96ff1dd1b 789 sockread_mdm(&MySocketData, 1024, 20);
elmkom 36:f8d96ff1dd1b 790
elmkom 36:f8d96ff1dd1b 791 // If any non-zero response from server, make it GREEN one-time
elmkom 36:f8d96ff1dd1b 792 // then the actual FLOW responses will set the color.
elmkom 36:f8d96ff1dd1b 793 if (MySocketData.length() > 0)
elmkom 36:f8d96ff1dd1b 794 {
elmkom 36:f8d96ff1dd1b 795 SetLedColor(0x2); // green
elmkom 36:f8d96ff1dd1b 796 printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]);
elmkom 36:f8d96ff1dd1b 797 char myJsonResponse[512];
elmkom 36:f8d96ff1dd1b 798 if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
elmkom 36:f8d96ff1dd1b 799 {
elmkom 36:f8d96ff1dd1b 800 printf(GRN "JSON : %s" DEF "\r\n", &myJsonResponse[0]);
elmkom 36:f8d96ff1dd1b 801 //parse_JSON(&myJsonResponse[0]);
elmkom 36:f8d96ff1dd1b 802 }
elmkom 36:f8d96ff1dd1b 803 else
elmkom 36:f8d96ff1dd1b 804 {
elmkom 36:f8d96ff1dd1b 805 printf(RED "JSON : %s" DEF "\r\n", &myJsonResponse[0]); //most likely an incomplete JSON string
elmkom 36:f8d96ff1dd1b 806 //parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
elmkom 36:f8d96ff1dd1b 807 }
elmkom 36:f8d96ff1dd1b 808 SetLedColor(0); // off
elmkom 36:f8d96ff1dd1b 809 }
elmkom 36:f8d96ff1dd1b 810 else
elmkom 36:f8d96ff1dd1b 811 {
elmkom 36:f8d96ff1dd1b 812 SetLedColor(0x1); //red
elmkom 36:f8d96ff1dd1b 813 system_reset();
elmkom 36:f8d96ff1dd1b 814
elmkom 36:f8d96ff1dd1b 815 }
elmkom 36:f8d96ff1dd1b 816
elmkom 36:f8d96ff1dd1b 817
stefanrousseau 16:17c5916f2d12 818 }
elmkom 36:f8d96ff1dd1b 819 wait(0.2);
stefanrousseau 3:26b3cc155f39 820 } //bTimerExpiredFlag
elmkom 36:f8d96ff1dd1b 821
stefanrousseau 3:26b3cc155f39 822 } //forever loop
JMF 0:9d5134074d84 823 }