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
gsmqueue.cpp@3:dac922a18af6, 2015-03-05 (annotated)
- Committer:
- es_marble
- Date:
- Thu Mar 05 21:24:56 2015 +0000
- Revision:
- 3:dac922a18af6
- Parent:
- 1:c1458b739eb6
- Child:
- 5:320d2babfb41
v3 Jo
Who changed what in which revision?
User | Revision | Line number | New 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) |
danilob | 0:41904adca656 | 22 | bool findInQueue(char* str, int strLen) |
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; |
danilob | 0:41904adca656 | 72 | while (qPos != QUEUETAIL) |
danilob | 0:41904adca656 | 73 | { |
danilob | 0:41904adca656 | 74 | //Print the current character |
danilob | 0:41904adca656 | 75 | pc.printf("%C",*qPos); |
danilob | 0:41904adca656 | 76 | |
danilob | 0:41904adca656 | 77 | //Increment index |
danilob | 0:41904adca656 | 78 | qPos = incrementIndex(qPos); |
danilob | 0:41904adca656 | 79 | } |
danilob | 0:41904adca656 | 80 | } |
danilob | 0:41904adca656 | 81 | |
danilob | 0:41904adca656 | 82 | //Parse through characters until first integer is found |
es_marble | 1:c1458b739eb6 | 83 | //Advance qHead until you reach the next non-numeric character |
es_marble | 3:dac922a18af6 | 84 | //Does not read negative integers; returns -1 if unsuccessful |
danilob | 0:41904adca656 | 85 | int parseInt() |
danilob | 0:41904adca656 | 86 | { |
danilob | 0:41904adca656 | 87 | char* qPos = queueHead; |
es_marble | 3:dac922a18af6 | 88 | //Advance to first numeric character |
es_marble | 3:dac922a18af6 | 89 | while (!isNumeric(qPos)) |
danilob | 0:41904adca656 | 90 | { |
es_marble | 3:dac922a18af6 | 91 | qPos = incrementIndex(qPos); |
es_marble | 3:dac922a18af6 | 92 | if (qPos == QUEUETAIL) return -1; |
es_marble | 3:dac922a18af6 | 93 | } |
es_marble | 3:dac922a18af6 | 94 | |
es_marble | 3:dac922a18af6 | 95 | //Continue until first non-numeric character |
es_marble | 3:dac922a18af6 | 96 | int val = 0; |
es_marble | 3:dac922a18af6 | 97 | while (qPos != QUEUETAIL && isNumeric(qPos)) |
es_marble | 3:dac922a18af6 | 98 | { |
es_marble | 3:dac922a18af6 | 99 | val *= 10; |
es_marble | 3:dac922a18af6 | 100 | val += (int)(qPos - '0'); |
danilob | 0:41904adca656 | 101 | qPos = incrementIndex(qPos); |
danilob | 0:41904adca656 | 102 | } |
es_marble | 3:dac922a18af6 | 103 | return val; |
es_marble | 1:c1458b739eb6 | 104 | } |
es_marble | 1:c1458b739eb6 | 105 | |
es_marble | 3:dac922a18af6 | 106 | //Returns true if the character is numeric |
es_marble | 3:dac922a18af6 | 107 | bool isNumeric(char* qPos) |
es_marble | 3:dac922a18af6 | 108 | { |
es_marble | 3:dac922a18af6 | 109 | return ('0' < *qPos && *qPos < '9'); |
es_marble | 3:dac922a18af6 | 110 | } |
es_marble | 3:dac922a18af6 | 111 | |
es_marble | 1:c1458b739eb6 | 112 | //Reset the GSM DMA idle bit to 0 |
es_marble | 1:c1458b739eb6 | 113 | void resetGSMIdleBit() |
es_marble | 1:c1458b739eb6 | 114 | { |
es_marble | 1:c1458b739eb6 | 115 | UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK; |
es_marble | 1:c1458b739eb6 | 116 | } |
es_marble | 1:c1458b739eb6 | 117 | |
es_marble | 1:c1458b739eb6 | 118 | //Get the GSM DMA idle bit (if 1, indicates we already received a response) |
es_marble | 1:c1458b739eb6 | 119 | bool getGSMIdleBit() |
es_marble | 1:c1458b739eb6 | 120 | { |
es_marble | 1:c1458b739eb6 | 121 | return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT; |
danilob | 0:41904adca656 | 122 | } |