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

Committer:
es_marble
Date:
Thu Mar 05 23:23:33 2015 +0000
Revision:
10:32d5604accfd
Parent:
9:ee10bf2795fc
Parent:
8:1d8623e25fa6
Child:
13:9ac5ff131214
z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
danilob 0:41904adca656 1 #include "gsmqueue.h"
danilob 0:41904adca656 2 #include "mbed.h"
danilob 0:41904adca656 3 /* queue.cpp
danilob 0:41904adca656 4 * Contains functions to read from the DMA buffer in a queue fashion
danilob 0:41904adca656 5 */
es_marble 3:dac922a18af6 6 extern Serial pc;
danilob 0:41904adca656 7
danilob 0:41904adca656 8 char buffer[BUFFER_LENGTH];
danilob 0:41904adca656 9 char* queueHead;
danilob 0:41904adca656 10
danilob 0:41904adca656 11
danilob 0:41904adca656 12 //Initialize variables
danilob 0:41904adca656 13 void queueInit()
danilob 0:41904adca656 14 {
danilob 0:41904adca656 15 //The buffer is initialized in init.cpp
danilob 0:41904adca656 16 queueHead = QUEUETAIL;
danilob 0:41904adca656 17 }
danilob 0:41904adca656 18
danilob 0:41904adca656 19 //Find an occurrence of the given string in the buffer.
danilob 0:41904adca656 20 //Only advance queueHead until a matching string is found.
es_marble 3:dac922a18af6 21 //The given string terminates in NULL (\0)
es_marble 5:320d2babfb41 22 bool findInQueue(char* str)
danilob 0:41904adca656 23 {
es_marble 3:dac922a18af6 24 //Check that string to find is not empty
es_marble 3:dac922a18af6 25 if (*str == NULL) return false;
es_marble 3:dac922a18af6 26
danilob 0:41904adca656 27 while (queueHead != QUEUETAIL)
danilob 0:41904adca656 28 {
danilob 0:41904adca656 29 //Does the character match the begin char?
danilob 0:41904adca656 30 if (*queueHead == *str){
danilob 0:41904adca656 31 //Check the remaining characters
danilob 0:41904adca656 32 char* sPos = str + 1;
es_marble 3:dac922a18af6 33 char* qPos = 0;
danilob 0:41904adca656 34 for (qPos = incrementIndex(queueHead); qPos != QUEUETAIL; qPos = incrementIndex(qPos)){
danilob 0:41904adca656 35 //Compare the next char
danilob 0:41904adca656 36 if (*qPos == *sPos)
danilob 0:41904adca656 37 {
es_marble 3:dac922a18af6 38 ++sPos; //Increment index (prefix incrementation).
es_marble 9:ee10bf2795fc 39 if (*sPos == NULL) //If finished, update queueHead, return true.
es_marble 8:1d8623e25fa6 40 {
es_marble 9:ee10bf2795fc 41 queueHead = incrementIndex(qPos);
danilob 0:41904adca656 42 return true;
es_marble 8:1d8623e25fa6 43 }
danilob 0:41904adca656 44 }
danilob 0:41904adca656 45 else //Not equal, so exit for loop and try again at a different location
danilob 0:41904adca656 46 break;
danilob 0:41904adca656 47 }
danilob 0:41904adca656 48 }
danilob 0:41904adca656 49 //Increment queue index for next iteration
danilob 0:41904adca656 50 queueHead = incrementIndex(queueHead);
danilob 0:41904adca656 51 }
danilob 0:41904adca656 52 //We never finished, so return false
danilob 0:41904adca656 53 return false;
danilob 0:41904adca656 54 }
danilob 0:41904adca656 55
danilob 0:41904adca656 56 //Increment queue index by 1
danilob 0:41904adca656 57 char* incrementIndex(char* pointerToIncrement)
danilob 0:41904adca656 58 {
danilob 0:41904adca656 59 if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH))
danilob 0:41904adca656 60 return (pointerToIncrement + 1);
danilob 0:41904adca656 61 else
danilob 0:41904adca656 62 return buffer;
danilob 0:41904adca656 63 }
danilob 0:41904adca656 64
danilob 0:41904adca656 65 //clear queue
danilob 0:41904adca656 66 void flushQueue()
danilob 0:41904adca656 67 {
danilob 0:41904adca656 68 queueHead = QUEUETAIL;
danilob 0:41904adca656 69 }
danilob 0:41904adca656 70
es_marble 3:dac922a18af6 71 //$debug - print queue elements
danilob 0:41904adca656 72 void printQueue()
danilob 0:41904adca656 73 {
danilob 0:41904adca656 74 char* qPos = queueHead;
es_marble 6:3ccc86304c2c 75 pc.printf("Queue:");
danilob 0:41904adca656 76 while (qPos != QUEUETAIL)
danilob 0:41904adca656 77 {
danilob 0:41904adca656 78 //Print the current character
es_marble 6:3ccc86304c2c 79 if (*qPos == '\n')
es_marble 6:3ccc86304c2c 80 pc.printf("\\n");
es_marble 6:3ccc86304c2c 81 else if (*qPos == '\r')
es_marble 6:3ccc86304c2c 82 pc.printf("\\r");
es_marble 6:3ccc86304c2c 83 else
es_marble 6:3ccc86304c2c 84 pc.printf("%C",*qPos);
es_marble 6:3ccc86304c2c 85
danilob 0:41904adca656 86
danilob 0:41904adca656 87 //Increment index
danilob 0:41904adca656 88 qPos = incrementIndex(qPos);
danilob 0:41904adca656 89 }
es_marble 6:3ccc86304c2c 90 pc.printf("\n\r");
danilob 0:41904adca656 91 }
danilob 0:41904adca656 92
danilob 0:41904adca656 93 //Parse through characters until first integer is found
es_marble 1:c1458b739eb6 94 //Advance qHead until you reach the next non-numeric character
es_marble 3:dac922a18af6 95 //Does not read negative integers; returns -1 if unsuccessful
danilob 0:41904adca656 96 int parseInt()
danilob 0:41904adca656 97 {
danilob 0:41904adca656 98 char* qPos = queueHead;
es_marble 3:dac922a18af6 99 //Advance to first numeric character
es_marble 3:dac922a18af6 100 while (!isNumeric(qPos))
danilob 0:41904adca656 101 {
es_marble 3:dac922a18af6 102 qPos = incrementIndex(qPos);
es_marble 3:dac922a18af6 103 if (qPos == QUEUETAIL) return -1;
es_marble 3:dac922a18af6 104 }
es_marble 3:dac922a18af6 105
es_marble 3:dac922a18af6 106 //Continue until first non-numeric character
es_marble 3:dac922a18af6 107 int val = 0;
es_marble 3:dac922a18af6 108 while (qPos != QUEUETAIL && isNumeric(qPos))
es_marble 3:dac922a18af6 109 {
es_marble 3:dac922a18af6 110 val *= 10;
es_marble 3:dac922a18af6 111 val += (int)(qPos - '0');
danilob 0:41904adca656 112 qPos = incrementIndex(qPos);
danilob 0:41904adca656 113 }
es_marble 3:dac922a18af6 114 return val;
es_marble 1:c1458b739eb6 115 }
es_marble 1:c1458b739eb6 116
es_marble 3:dac922a18af6 117 //Returns true if the character is numeric
es_marble 3:dac922a18af6 118 bool isNumeric(char* qPos)
es_marble 3:dac922a18af6 119 {
es_marble 3:dac922a18af6 120 return ('0' < *qPos && *qPos < '9');
es_marble 3:dac922a18af6 121 }
es_marble 3:dac922a18af6 122
es_marble 1:c1458b739eb6 123 //Reset the GSM DMA idle bit to 0
es_marble 1:c1458b739eb6 124 void resetGSMIdleBit()
es_marble 1:c1458b739eb6 125 {
es_marble 1:c1458b739eb6 126 UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK;
es_marble 1:c1458b739eb6 127 }
es_marble 1:c1458b739eb6 128
es_marble 1:c1458b739eb6 129 //Get the GSM DMA idle bit (if 1, indicates we already received a response)
es_marble 1:c1458b739eb6 130 bool getGSMIdleBit()
es_marble 1:c1458b739eb6 131 {
es_marble 1:c1458b739eb6 132 return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT;
danilob 0:41904adca656 133 }