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 21:50:30 2015 +0000
Revision:
6:3ccc86304c2c
Parent:
5:320d2babfb41
Child:
8:1d8623e25fa6
Child:
9:ee10bf2795fc
Jo v2.1

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 3:dac922a18af6 39 if (*sPos == NULL) //If finished, return true.
danilob 0:41904adca656 40 return true;
danilob 0:41904adca656 41 }
danilob 0:41904adca656 42 else //Not equal, so exit for loop and try again at a different location
danilob 0:41904adca656 43 break;
danilob 0:41904adca656 44 }
danilob 0:41904adca656 45 }
danilob 0:41904adca656 46 //Increment queue index for next iteration
danilob 0:41904adca656 47 queueHead = incrementIndex(queueHead);
danilob 0:41904adca656 48 }
danilob 0:41904adca656 49 //We never finished, so return false
danilob 0:41904adca656 50 return false;
danilob 0:41904adca656 51 }
danilob 0:41904adca656 52
danilob 0:41904adca656 53 //Increment queue index by 1
danilob 0:41904adca656 54 char* incrementIndex(char* pointerToIncrement)
danilob 0:41904adca656 55 {
danilob 0:41904adca656 56 if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH))
danilob 0:41904adca656 57 return (pointerToIncrement + 1);
danilob 0:41904adca656 58 else
danilob 0:41904adca656 59 return buffer;
danilob 0:41904adca656 60 }
danilob 0:41904adca656 61
danilob 0:41904adca656 62 //clear queue
danilob 0:41904adca656 63 void flushQueue()
danilob 0:41904adca656 64 {
danilob 0:41904adca656 65 queueHead = QUEUETAIL;
danilob 0:41904adca656 66 }
danilob 0:41904adca656 67
es_marble 3:dac922a18af6 68 //$debug - print queue elements
danilob 0:41904adca656 69 void printQueue()
danilob 0:41904adca656 70 {
danilob 0:41904adca656 71 char* qPos = queueHead;
es_marble 6:3ccc86304c2c 72 pc.printf("Queue:");
danilob 0:41904adca656 73 while (qPos != QUEUETAIL)
danilob 0:41904adca656 74 {
danilob 0:41904adca656 75 //Print the current character
es_marble 6:3ccc86304c2c 76 if (*qPos == '\n')
es_marble 6:3ccc86304c2c 77 pc.printf("\\n");
es_marble 6:3ccc86304c2c 78 else if (*qPos == '\r')
es_marble 6:3ccc86304c2c 79 pc.printf("\\r");
es_marble 6:3ccc86304c2c 80 else
es_marble 6:3ccc86304c2c 81 pc.printf("%C",*qPos);
es_marble 6:3ccc86304c2c 82
danilob 0:41904adca656 83
danilob 0:41904adca656 84 //Increment index
danilob 0:41904adca656 85 qPos = incrementIndex(qPos);
danilob 0:41904adca656 86 }
es_marble 6:3ccc86304c2c 87 pc.printf("\n\r");
danilob 0:41904adca656 88 }
danilob 0:41904adca656 89
danilob 0:41904adca656 90 //Parse through characters until first integer is found
es_marble 1:c1458b739eb6 91 //Advance qHead until you reach the next non-numeric character
es_marble 3:dac922a18af6 92 //Does not read negative integers; returns -1 if unsuccessful
danilob 0:41904adca656 93 int parseInt()
danilob 0:41904adca656 94 {
danilob 0:41904adca656 95 char* qPos = queueHead;
es_marble 3:dac922a18af6 96 //Advance to first numeric character
es_marble 3:dac922a18af6 97 while (!isNumeric(qPos))
danilob 0:41904adca656 98 {
es_marble 3:dac922a18af6 99 qPos = incrementIndex(qPos);
es_marble 3:dac922a18af6 100 if (qPos == QUEUETAIL) return -1;
es_marble 3:dac922a18af6 101 }
es_marble 3:dac922a18af6 102
es_marble 3:dac922a18af6 103 //Continue until first non-numeric character
es_marble 3:dac922a18af6 104 int val = 0;
es_marble 3:dac922a18af6 105 while (qPos != QUEUETAIL && isNumeric(qPos))
es_marble 3:dac922a18af6 106 {
es_marble 3:dac922a18af6 107 val *= 10;
es_marble 3:dac922a18af6 108 val += (int)(qPos - '0');
danilob 0:41904adca656 109 qPos = incrementIndex(qPos);
danilob 0:41904adca656 110 }
es_marble 3:dac922a18af6 111 return val;
es_marble 1:c1458b739eb6 112 }
es_marble 1:c1458b739eb6 113
es_marble 3:dac922a18af6 114 //Returns true if the character is numeric
es_marble 3:dac922a18af6 115 bool isNumeric(char* qPos)
es_marble 3:dac922a18af6 116 {
es_marble 3:dac922a18af6 117 return ('0' < *qPos && *qPos < '9');
es_marble 3:dac922a18af6 118 }
es_marble 3:dac922a18af6 119
es_marble 1:c1458b739eb6 120 //Reset the GSM DMA idle bit to 0
es_marble 1:c1458b739eb6 121 void resetGSMIdleBit()
es_marble 1:c1458b739eb6 122 {
es_marble 1:c1458b739eb6 123 UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK;
es_marble 1:c1458b739eb6 124 }
es_marble 1:c1458b739eb6 125
es_marble 1:c1458b739eb6 126 //Get the GSM DMA idle bit (if 1, indicates we already received a response)
es_marble 1:c1458b739eb6 127 bool getGSMIdleBit()
es_marble 1:c1458b739eb6 128 {
es_marble 1:c1458b739eb6 129 return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT;
danilob 0:41904adca656 130 }