uses pushing box to publish to google spreadsheets with a state machine instead of a while loop

Dependents:   DCS_FINAL_CODE

Fork of GSM_PUSHING_BOX_STATE_MACHINE by DCS_TEAM

Revision:
18:7642909bfcfc
Parent:
17:360afa1e6809
Child:
19:a442b5a0116f
--- a/GSMLibrary.cpp	Sat Mar 07 23:35:12 2015 +0000
+++ b/GSMLibrary.cpp	Sat Mar 07 23:45:00 2015 +0000
@@ -14,6 +14,7 @@
 #define RECEIVER_PHONE_NUMBER "\"+18014722842\""
 #define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER 
 #define MESSAGE_BODY "stress test\32\32"
+#define MSG_SIZE 200
 
 //Definition for AT repsonses
 //Please notice that after ":" the gsm will usually send aditional information
@@ -34,6 +35,7 @@
 gsm_states gsm_current_state = GSM_INITIALIZE;
 char send = 0;
 int timeout_count = 0;
+char gsm_msg[MSG_SIZE + 1]; //1 extra for Ctrl+Z
 
 void gsm_tick()
 {
@@ -60,6 +62,26 @@
         return false;     
 }
 
+//Have the GSM send a message
+void gsm_send_sms(char msg[])
+{
+    send = 1;
+    strcpy(gsm_msg,msg);  //If we need to optimize later we can do that, but this is more robust
+    strcat(gsm_msg,"\x1A");
+}
+
+//Return true if gsm is ready
+bool gsm_ready()
+{
+    return (send == 1) ? true : false;
+}
+
+//Reset the gsm
+void gsm_reset()
+{
+    gsm_current_state = GSM_INITIALIZE;
+}
+
 //Next state logic -----------------------------------------------------
 void gsm_nextStateLogic()
 {
@@ -106,16 +128,31 @@
             break;
         case GSM_AT_SENDSMS:
             pc.printf("gsm_send_sms state\r\n");//&debug
-            if(findInQueue(AT_SENDSMS_RESPONSE))
-                gsm_current_state = GSM_SUCCESS;
+            if(findInQueue(AT_SENDSMS_RESPONSE))    //>
+            {
+                //Check if the "successfully sent" has also already been received (most likely
+                //this won't be the case, but if it has been >500 ms there's a possibility
+                //we've received both the messages during the same call to our state machine.)
+                if(findInQueue(AT_SUCCESS_RESPONSE))
+                {
+                    pc.printf("Message SENT! msgID: %iY\r\n",parseInt());//&debug
+                    send = 0;
+                    gsm_current_state = GSM_INITIALIZE; //Skip success state (we've received both)
+                }
+                else
+                    gsm_current_state = GSM_SUCCESS;    //Go to success state
+            }
             else
-                gsm_current_state = GSM_AT_CMGS;        //The only spot we can go backwards
+                gsm_current_state = GSM_AT_CMGS;        //Try resending the message (until timeout)
             break;
         case GSM_SUCCESS:
             pc.printf("gsm_success state\r\n");//&debug
-            if(findInQueue(AT_SUCCESS_RESPONSE))         //This appears to be a bug. It was in Danilo's original code as well.
+            if(findInQueue(AT_SUCCESS_RESPONSE))
+            {
                 pc.printf("Message SENT! msgID: %iY\r\n",parseInt());//&debug
-            gsm_current_state = GSM_AT_CMGS;                //Confusing part. Do we always go backwards here?
+            }
+            send = 0;
+            gsm_current_state = GSM_INITIALIZE;     //We will restart regardless of whether it worked
             break;
         default:
             pc.printf("This is a state error");
@@ -156,8 +193,8 @@
             gsm.puts("\r\n"); 
             break;
         case GSM_AT_SENDSMS:
-            pc.printf("sending MESSAGE_BODY\r\n");//&debug
-            gsm.puts(MESSAGE_BODY); //substitute char included
+            pc.printf("sending message\r\n");//&debug
+            gsm.puts(gsm_msg); //substitute char included
             gsm.puts("\r\n"); 
             break;
         case GSM_SUCCESS:
@@ -167,20 +204,12 @@
     }
 }
 
-//set send falg on
-void gsm_send_sms(){
-    send = 1;
-}
 
-//
-void gsm_reset();
-
-
-//
+//Initialize the GSM
 void gsm_initialize(){  
       SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock
       SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;  // enebaling dma clock
-      pc.printf("initializing registers...!\r\n");
+      pc.printf("initializing tregisters...!\r\n");
      // control register mux, enabling uart3 receive        
      DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8);