uses pushing box to publish to google spreadsheets with a state machine instead of a while loop
Fork of GSM_PUSHING_BOX_STATE_MACHINE by
Diff: GSMLibrary.cpp
- Revision:
- 16:6807d437cd48
- Parent:
- 15:19ae032e2e54
- Child:
- 17:360afa1e6809
diff -r 19ae032e2e54 -r 6807d437cd48 GSMLibrary.cpp --- a/GSMLibrary.cpp Fri Mar 06 02:45:22 2015 +0000 +++ b/GSMLibrary.cpp Fri Mar 06 22:14:50 2015 +0000 @@ -15,7 +15,7 @@ #define AT_CMGS "AT+CMGS=" RECEIVER_PHONE_NUMBER #define MESSAGE_BODY "stress test\32\32" -//Definition for at repsonses +//Definition for AT repsonses //Please notice that after ":" the gsm will usually send aditional information #define AT_OK_RESPONSE "OK" //Response after sending "AT" message #define AT_CSQ_RESPONSE "+CSQ:" //+CSQ: <arg1>,<arg2> where <arg1> is signal strength arg1 = 0-30 where a number below 10 means low signal strength and 99 is not knwn or detectable signal and arg2 is bit error rate form 0-7, 99 will represent error @@ -25,196 +25,162 @@ #define AT_SENDSMS_RESPONSE ">" // +CMGS: <id> this will include the message id. CMGS ERROR for error and #define AT_SUCCESS_REPSONSE "+CMGS:" - +//External variables extern Serial pc; extern Serial gsm; extern uint8_t buffer[BUFFER_LENGTH];//buffer storing char + +//Internal variables gsm_states gsm_current_state = GSM_INITIALIZE; - char send = 0; int timeout_count = 0; -char timeout_limit = TIMEOUTLIMIT; -void gsm_tick(){ - if(++timeout_count >= timeout_limit){ +void gsm_tick() +{ + if (getGSMIdleBit() || gsm_timeOut() || (send && gsm_current_state == GSM_INITIALIZE)) //question with send... + { + resetGSMIdleBit(); //reset GSM idle bit + gsm_nextStateLogic(); //Next state + gsm_mealyOutputs(); //Mealy outputs + flushQueue(); //Flush the queue + } +} + + + +//Advance timeout counter; if timeout, return true +bool gsm_timeOut() +{ + if(++timeout_count >= TIMEOUTLIMIT){ timeout_count=0; gsm_current_state = GSM_INITIALIZE; + return true; } - switch(gsm_current_state){ - //when send flag is on , send AT_OK message to gsm. + else + return false; +} + +//Next state logic ----------------------------------------------------- +void gsm_nextStateLogic() +{ + printQueue(); //$debug + + switch(gsm_current_state) + { case GSM_INITIALIZE: - pc.printf("gsm_initilize state\r\n");//&debug + pc.printf("gsm_initialize state\r\n");//&debug timeout_count = 0; - if(send){ //send first at_ok message - resetGSMIdleBit(); - pc.printf("sending AT_OK\r\n");//&debug - gsm.puts(AT_OK); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_OK; - } - else - gsm_current_state = GSM_INITIALIZE; + if (send) + gsm_current_state = GSM_AT_OK; //unconditional (check it) break; - - // check for repsonse to AT and if correct send AT+CSQ message case GSM_AT_OK: pc.printf("inside AT_OK state\r\n");//&debug - if(getGSMIdleBit()){ - printQueue(); //$debug - if(findInQueue(AT_OK_RESPONSE)){ - resetGSMIdleBit(); - pc.printf("sending AT_CSQ\r\n");//&debug - gsm.puts(AT_CSQ); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_CSQ; - } - else{ - resetGSMIdleBit(); - gsm.puts(AT_OK); - gsm.puts("\r\n"); - } - } + if (findInQueue(AT_OK_RESPONSE)) + gsm_current_state = GSM_AT_CSQ; break; - - //CHECK FOR RESPOSE TO at+csq AND SEND at+creg case GSM_AT_CSQ: pc.printf("inside AT_CSQ state \r\n");//&debug - if(getGSMIdleBit()){ - printQueue(); //$debug - if(findInQueue(AT_CSQ_RESPONSE)){ - if(parseInt() > 9){ - resetGSMIdleBit(); - pc.printf("sending AT_CREG\r\n");//&debug - gsm.puts(AT_CREG); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_CREG; - } - } - else{ - resetGSMIdleBit(); - gsm.puts(AT_CSQ); - gsm.puts("\r\n"); - } - } + if(findInQueue(AT_CSQ_RESPONSE)) + gsm_current_state = GSM_AT_CREG; break; - - //check for AT creg and if correct send AT+CMGF case GSM_AT_CREG: pc.printf("gsm_creg state\r\n");//&debug - if(getGSMIdleBit()){ - printQueue(); //$debug - if(findInQueue(AT_CREG_RESPONSE)){ - pc.printf("creg parse Int1: %d\r\n",parseInt());//&debug - int q = parseInt(); - pc.printf("creg parse Int2: %d\r\n",q);//&debug - if(q == 1){ - resetGSMIdleBit(); - pc.printf("sending AT_CMGF\r\n");//&debug - gsm.puts(AT_CMGF); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_CMGF; - } - } - if(gsm_current_state == GSM_AT_CREG){ - resetGSMIdleBit(); - gsm.puts(AT_CREG); - gsm.puts("\r\n"); - } + if(findInQueue(AT_CREG_RESPONSE)) + { + pc.printf("creg parse Int1: %d\r\n",parseInt());//&debug + int q = parseInt(); + pc.printf("creg parse Int2: %d\r\n",q);//&debug + if(q == 1) + gsm_current_state = GSM_AT_CMGF; } break; - - //check for cmgf esponse and if correct go to at_cmgs case GSM_AT_CMGF: pc.printf("gsm_cmgf state\r\n");//&debug - if(getGSMIdleBit()){ - printQueue(); //$debug - if(findInQueue(AT_CMGF_RESPONSE)){ - resetGSMIdleBit(); - pc.printf("sending AT_CMGS\r\n");//&debug - gsm.puts(AT_CMGS); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_CMGS; - } - else{ - resetGSMIdleBit(); - gsm.puts(AT_CMGF); - gsm.puts("\r\n"); - } - } + if(findInQueue(AT_CMGF_RESPONSE)) + gsm_current_state = GSM_AT_CMGS; break; - - //check cmgs response if correct send SMS case GSM_AT_CMGS: - pc.printf("gsm_cmgs state\r\n");//&debug - if(getGSMIdleBit()){ - printQueue(); //$debug - if(findInQueue(AT_CMGS_RESPONSE)){ - resetGSMIdleBit(); - pc.printf("sending MESSAGE_BODY\r\n");//&debug - gsm.puts(MESSAGE_BODY); //substitute - gsm.puts("\r\n"); - flushQueue(); - gsm_current_state = GSM_AT_SENDSMS; - } - else - { - resetGSMIdleBit(); - gsm.puts(AT_CMGS); - gsm.puts("\r\n"); - } - } + pc.printf("gsm_cmgs state\r\n");//&debug + if(findInQueue(AT_CMGS_RESPONSE)) + gsm_current_state = GSM_AT_SENDSMS; break; - - //check if message was sent correctly and if so case GSM_AT_SENDSMS: pc.printf("gsm_send_sms state\r\n");//&debug - if(getGSMIdleBit()){ - printQueue(); //$debug - - if(findInQueue(AT_SENDSMS_RESPONSE)){ - resetGSMIdleBit(); - gsm_current_state = GSM_SUCCESS; - } - else - { - resetGSMIdleBit(); - gsm.puts(AT_CMGF); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_CMGS; - } - } + if(findInQueue(AT_SENDSMS_RESPONSE)) + gsm_current_state = GSM_SUCCESS; + else + gsm_current_state = GSM_AT_CMGS; //The only spot we can go backwards break; - - case GSM_SUCCESS: pc.printf("gsm_success state\r\n");//&debug - if(findInQueue(AT_SENDSMS_RESPONSE)){ - pc.printf("sending message ID: %iY\r\n",parseInt());//&debug - pc.printf("Message SENT!\r\n");//&debug - } - resetGSMIdleBit(); - gsm.puts(AT_CMGS); - gsm.puts("\r\n"); - gsm_current_state = GSM_AT_CMGS; + if(findInQueue(AT_SENDSMS_RESPONSE)) //This appears to be a bug. It was in Danilo's original code as well. + pc.printf("Message SENT! msgID: %iY\r\n",parseInt());//&debug + gsm_current_state = GSM_AT_CMGS; //Confusing part. Do we always go backwards here? break; default: pc.printf("This is a state error"); } - } + +//Mealy output logic ------------------------------------------------------ +void gsm_mealyOutputs() +{ + switch(gsm_current_state) + { + case GSM_INITIALIZE: + pc.printf("No Mealy initialize state output\r\n");//&debug + break; + case GSM_AT_OK: + pc.printf("sending AT_OK\r\n");//&debug + gsm.puts(AT_OK); + gsm.puts("\r\n"); + break; + case GSM_AT_CSQ: + pc.printf("sending AT_CSQ\r\n");//&debug + gsm.puts(AT_CSQ); + gsm.puts("\r\n"); + break; + case GSM_AT_CREG: + pc.printf("sending AT_CREG\r\n");//&debug + gsm.puts(AT_CREG); + gsm.puts("\r\n"); + break; + case GSM_AT_CMGF: + pc.printf("sending AT_CMGF\r\n");//&debug + gsm.puts(AT_CMGF); + gsm.puts("\r\n"); + break; + case GSM_AT_CMGS: + pc.printf("sending AT_CMGS\r\n");//&debug + gsm.puts(AT_CMGS); + gsm.puts("\r\n"); + break; + case GSM_AT_SENDSMS: + pc.printf("sending MESSAGE_BODY\r\n");//&debug + gsm.puts(MESSAGE_BODY); //substitute char included + gsm.puts("\r\n"); + break; + case GSM_SUCCESS: + pc.printf("No Mealy success state output\r\n");//&debug + default: + pc.printf("This is a state error"); + } +} + //set send falg on void gsm_send_sms(){ send = 1; } + // void gsm_reset(); // -void gsm_initialize(){ +void gsm_initialize(){ SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; //enabling dmamux clock SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; // enebaling dma clock - pc.printf("initializing tregisters...!\r\n"); + pc.printf("initializing registers...!\r\n"); // control register mux, enabling uart3 receive DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK|DMAMUX_CHCFG_SOURCE(8); @@ -243,8 +209,8 @@ DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(BUFFER_LENGTH); // Adjustment value used to restore the source and destiny address to the initial value // After reading 'len' number of times, the DMA goes back to the beginning by subtracting len*2 from the address (going back to the original address) - DMA_TCD0_SLAST = 0; // Source address adjustment - DMA_TCD0_DLASTSGA = -BUFFER_LENGTH; // Destination address adjustment + DMA_TCD0_SLAST = 0; // Source address adjustment + DMA_TCD0_DLASTSGA = -BUFFER_LENGTH; // Destination address adjustment // Setup control and status register DMA_TCD0_CSR = 0; @@ -293,14 +259,14 @@ pc.printf("UART_C1_REG: %08x\r\n",UART_C1_REG(UART3)); pc.printf("UART_C2_REG: %08x\r\n",UART_C2_REG(UART3)); pc.printf("UART_S1_REG: %08x\r\n",UART_S1_REG(UART3)); - pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3)); + pc.printf("UART_s2_REG: %08x\r\n",UART_S2_REG(UART3)); pc.printf("UART_C3_REG: %08x\r\n",UART_C3_REG(UART3)); pc.printf("UART_D_REG: %08x\r\n",UART_D_REG(UART3)); pc.printf("UART_MA1_REG: %08x\r\n",UART_MA1_REG(UART3)); pc.printf("UART_MA2_REG: %08x\r\n",UART_MA2_REG(UART3)); pc.printf("UART_C4_REG: %08x\r\n",UART_C4_REG(UART3)); pc.printf("UART_C5_REG: %08x\r\n",UART_C5_REG(UART3)); - pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3)); + pc.printf("UART_ED_REG: %08x\r\n",UART_ED_REG(UART3)); pc.printf("UART_MODEM_REG: %08x\r\n",UART_MODEM_REG(UART3)); pc.printf("UART_IR_REG: %08x\r\n",UART_IR_REG(UART3)); pc.printf("UART_PFIFO_REG: %08x\r\n",UART_PFIFO_REG(UART3));