GSMA version

Dependencies:   FXOS8700CQ mbed

Fork of AvnetATT_shape_hackathon by Rick McConney

Revision:
44:60008ebffdd4
Parent:
42:8500f0cb2ea5
Child:
45:fe90f1fcb4e0
--- a/main.cpp	Mon Oct 10 16:29:34 2016 +0000
+++ b/main.cpp	Wed Oct 26 18:40:40 2016 +0000
@@ -9,16 +9,22 @@
 #include "Wnc.h"
 
 #include "hardware.h"
+#include "att160826.h"
 
 extern SerialBuffered mdm;
 Wnc wnc;
 
 I2C i2c(PTC11, PTC10);    //SDA, SCL -- define the I2C pins being used
+I2C *myi2c;
 
 #define PROXIMITYON 1
 
 #if PROXIMITYON == 1
-Proximity proximityStrip;
+att160826_data_t strip_data[2];
+att160826_data_t last_data[2];
+ATT160826 *strips[2];
+#elif PROXIMITYON == 2
+Proximity proximityStrip[2];
 #endif
 
 // comment out the following line if color is not supported on the terminal
@@ -48,8 +54,8 @@
 #define MDM_DBG_OFF                             0
 #define MDM_DBG_AT_CMDS                         (1 << 0)
 
-#define MUXADDRESS 0x70
-#define PROXIMITYADDRESS  0x39
+
+
 
 
 
@@ -65,6 +71,7 @@
 
 
 
+
 DigitalIn   slot1(PTB3,PullUp);
 DigitalIn   slot2(PTB10,PullUp);
 DigitalIn   slot3(PTB11,PullUp);
@@ -81,8 +88,6 @@
 
 #define MAX_AT_RSP_LEN                          255
 
-bool proximityChange = false;
-
 bool toggleLed = false;
 int seqNum;
 
@@ -146,17 +151,54 @@
     .Humidity_Si7020    = "0"
 };
 
-void GenerateModemString(char * modem_string,int sensor)
+
+
+void GenerateModemString(char * modem_string,int sensor,int strip)
 {
     switch(sensor)
     {
 #if PROXIMITYON == 1
+
+        case PROXIMITY_ONLY:
+        {
+            if(strip == -1)
+            {
+                seqNum++;
+                sprintf(modem_string, "GET %s%s?ver=cat1&strip=%d&serial=%s&seq=%d&psm=%d&T=%d&t=%d %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", strip, iccid, seqNum, wnc.isPowerSaveOn(),wnc.gett3412Timer(),wnc.gett3324Timer(),FLOW_URL_TYPE, MY_SERVER_URL);
+                break;
+            }
+            int stripIndex = 0;
+            if(strip == 3)
+                stripIndex = 1;
+            char data[256];
+            int i=0;
+            int index = 0;
+            index += sprintf(&data[index],"[");
+            for (i=1; i<SENSORS_PER_STRIP; i++)
+            {
+                short  p = last_data[stripIndex].sensor[i].sample.prox;
+                short l = last_data[stripIndex].sensor[i].sample.als_vis;
+                short r = last_data[stripIndex].sensor[i].sample.als_ir;
+                if(i<SENSORS_PER_STRIP-1)
+                    index += snprintf(&data[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,p,l,r);
+                else
+                    index += snprintf(&data[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}]", i,p,l,r);
+
+            } 
+            
+
+            seqNum++;
+            sprintf(modem_string, "GET %s%s?ver=cat1&strip=%d&serial=%s&seq=%d&psm=%d&T=%d&t=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", strip, iccid, seqNum, wnc.isPowerSaveOn(),wnc.gett3412Timer(),wnc.gett3324Timer(), data,  FLOW_URL_TYPE, MY_SERVER_URL);
+            break;
+        }
+#elif PROXIMITYON == 2
         case PROXIMITY_ONLY:
         {
 
-            char* data = proximityStrip.getDataStr();
+            char* data0 = proximityStrip[0].getDataStr();
+            char* data1 = proximityStrip[1].getDataStr();
             seqNum++;
-            sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", iccid, seqNum, data,  FLOW_URL_TYPE, MY_SERVER_URL);
+            sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s&data1=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", iccid, seqNum, data0,data1,  FLOW_URL_TYPE, MY_SERVER_URL);
             break;
         }
 #endif
@@ -269,6 +311,45 @@
     lastSlot3 = s3;
     return changed;
 }
+#if PROXIMITYON == 1 
+bool scanStrip(int id)
+{
+    bool stripChanged = false;
+    ATT160826&  strip = *strips[id];
+    att160826_data_t *data = &strip_data[id];
+    bool ok = strip.scan(data);
+     
+    if(ok)
+    {  
+       // printf("%d\r\n",id);
+        for (int i = 1; i < SENSORS_PER_STRIP;i++) { // ignore first sensor it is blocked
+         
+          /*  
+            if (strip_data[id].sensor[i].present) {
+                printf(" %3d", strip_data[id].sensor[i].sample.prox);
+            } else {
+                printf(" %3s", "---");
+            }
+            */
+            if (strip_data[id].sensor[i].present) {
+                
+                if (abs(last_data[id].sensor[i].sample.prox - strip_data[id].sensor[i].sample.prox) > 50)
+                {
+                    stripChanged = true; 
+                    last_data[id].sensor[i].sample.prox = strip_data[id].sensor[i].sample.prox;
+                    last_data[id].sensor[i].sample.als_vis = strip_data[id].sensor[i].sample.als_vis;
+                    last_data[id].sensor[i].sample.als_ir = strip_data[id].sensor[i].sample.als_ir;   
+                }
+
+            }
+        }
+        // printf("\r\n");
+    }
+    else
+        printf("scan of strip %d failed",id);
+    return stripChanged;
+}
+#endif
 
 int main() {
 
@@ -305,9 +386,9 @@
     } while (!i);
     
    // wnc.setIn();
-    wnc.send("AT+CPSMS=0",WNC_WAIT_TIME_MS);
+   // wnc.send("AT+CPSMS=0",WNC_WAIT_TIME_MS);
 
-    wnc.toggleWake();   
+   // wnc.toggleWake();   
         
     
     iccid = wnc.getIccid();
@@ -319,12 +400,31 @@
 
     // Set LED BLUE for partial init
     SetLedColor(0x4);
-    
-    wnc.setPowerSave(false,12*60*60,20);
+ 
+    wnc.setPowerSave(true,wnc.gett3412Timer(),wnc.gett3324Timer());
  
-#if PROXIMITYON == 1  
-    proximityStrip.init();  
-    proximityStrip.on();        
+#if PROXIMITYON == 1 
+    bool stripChanged[2];
+    myi2c = new I2C(PTE25, PTE24); 
+    myi2c->frequency(100000);
+    ATT160826 strip0 (myi2c, 0);
+    ATT160826 strip1 (myi2c, 3);
+    strips[0] = &strip0;
+    strips[1] = &strip1;
+    
+#elif PROXIMITYON == 2
+
+    myi2c = new I2C(PTE25, PTE24); 
+    myi2c->frequency(100000);
+    Proximity strip0;
+    Proximity strip1;
+    proximityStrip[0] = strip0;
+    proximityStrip[1] = strip1;
+    proximityStrip[0].init(myi2c, 0x70); 
+    proximityStrip[1].init(myi2c, 0x73);
+    proximityStrip[0].on(); 
+    proximityStrip[1].on(); 
+     
 #endif
 
     int count = 0;
@@ -342,14 +442,43 @@
             SetLedColor(0); //off   
 
 #if PROXIMITYON == 1            
-        proximityStrip.scan();
+    
+     if(stripChanged[0] == false)
+        stripChanged[0] = scanStrip(0);
+     if(stripChanged[1] == false)
+        stripChanged[1] = scanStrip(1); 
+     
+#elif PROXIMITYON == 2
+
+    printf("scan %d\r\n",count);
+    bool stripChanged = false;
+    proximityStrip[0].scan();
+    printf("scan0 done %d\r\n",count);
+    proximityStrip[1].scan();
+    printf("scan1 done %d\r\n",count);
+    stripChanged |= proximityStrip[0].changed(50);
+    printf("scan0 change %d\r\n",stripChanged);
+    stripChanged |= proximityStrip[1].changed(50);
+    printf("scan1 change %d\r\n",stripChanged);
+    
 #else
         bool slotsChanged = checkSlots();
 #endif
-                
+
+     char modem_string[512];                
         
 #if PROXIMITYON == 1            
-        if(count >= 5*60 || proximityStrip.changed(50))
+        if(count >= 5*120)
+        {
+            stripChanged[0] = true;
+            stripChanged[1] = true;
+        }
+        
+        if(stripChanged[0] || stripChanged[1])
+
+        
+#elif PROXIMITYON == 2
+        if(count >= 5*60 || stripChanged )
 #else
         if(count >= 5*60 || slotsChanged)
 #endif
@@ -362,17 +491,32 @@
             count = 0;
             SetLedColor(0x04); //blue
                 
-            char modem_string[512];
+
             
 #if PROXIMITYON == 1
-            GenerateModemString(&modem_string[0],PROXIMITY_ONLY);
+        if(stripChanged[0])
+        {
+            GenerateModemString(&modem_string[0],PROXIMITY_ONLY,0);
+            stripChanged[0] = false;
+        }
+        else if (stripChanged[1])
+        {
+            GenerateModemString(&modem_string[0],PROXIMITY_ONLY,3);
+            stripChanged[1] = false;
+        }
+        else
+            GenerateModemString(&modem_string[0],PROXIMITY_ONLY,-1);
+#elif PROXIMITYON == 2
+            GenerateModemString(&modem_string[0],PROXIMITY_ONLY,0);
 #else
-            GenerateModemString(&modem_string[0],SWITCH_ONLY);
+            GenerateModemString(&modem_string[0],SWITCH_ONLY,0);
 #endif
   
             printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
 
-            if(wnc.connect("108.244.165.22",5005))
+            //if(wnc.connect("108.244.165.22",5005)) // node-red
+            if(wnc.connect("52.21.224.249",80)) // shelf
+            //if(wnc.connect("52.33.231.251",80)) // car
             {
                 char * reply = wnc.writeSocket(modem_string);
            
@@ -385,7 +529,7 @@
                     if (strlen(mydata) > 0)
                         break;
                  
-                    wait(0.2);
+                    wait(0.5);
                 }
             
                 if (strlen(mydata) > 0)