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@13:9ac5ff131214, 2015-03-06 (annotated)
- Committer:
- danilob
- Date:
- Fri Mar 06 01:11:58 2015 +0000
- Revision:
- 13:9ac5ff131214
- Parent:
- 10:32d5604accfd
- Child:
- 24:7d2ff444d6d8
yes!
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) |
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 | 13:9ac5ff131214 | 32 | char* sPos = str; |
es_marble | 3:dac922a18af6 | 33 | char* qPos = 0; |
danilob | 13:9ac5ff131214 | 34 | for (qPos = 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 | 13:9ac5ff131214 | 98 | //Check if queue is empty first |
danilob | 13:9ac5ff131214 | 99 | if (queueHead == QUEUETAIL) return -1; |
danilob | 13:9ac5ff131214 | 100 | |
es_marble | 3:dac922a18af6 | 101 | //Advance to first numeric character |
danilob | 13:9ac5ff131214 | 102 | while (!isNumeric(queueHead)) |
danilob | 0:41904adca656 | 103 | { |
danilob | 13:9ac5ff131214 | 104 | queueHead = incrementIndex(queueHead); |
danilob | 13:9ac5ff131214 | 105 | if (queueHead == QUEUETAIL) return -1; |
es_marble | 3:dac922a18af6 | 106 | } |
es_marble | 3:dac922a18af6 | 107 | |
es_marble | 3:dac922a18af6 | 108 | //Continue until first non-numeric character |
es_marble | 3:dac922a18af6 | 109 | int val = 0; |
danilob | 13:9ac5ff131214 | 110 | while (queueHead != QUEUETAIL && isNumeric(queueHead)) |
es_marble | 3:dac922a18af6 | 111 | { |
es_marble | 3:dac922a18af6 | 112 | val *= 10; |
danilob | 13:9ac5ff131214 | 113 | val += (int)(*queueHead - '0'); |
danilob | 13:9ac5ff131214 | 114 | queueHead = incrementIndex(queueHead); |
danilob | 0:41904adca656 | 115 | } |
es_marble | 3:dac922a18af6 | 116 | return val; |
es_marble | 1:c1458b739eb6 | 117 | } |
es_marble | 1:c1458b739eb6 | 118 | |
es_marble | 3:dac922a18af6 | 119 | //Returns true if the character is numeric |
es_marble | 3:dac922a18af6 | 120 | bool isNumeric(char* qPos) |
es_marble | 3:dac922a18af6 | 121 | { |
danilob | 13:9ac5ff131214 | 122 | return ('0' <= *qPos && *qPos <= '9'); |
es_marble | 3:dac922a18af6 | 123 | } |
es_marble | 3:dac922a18af6 | 124 | |
es_marble | 1:c1458b739eb6 | 125 | //Reset the GSM DMA idle bit to 0 |
es_marble | 1:c1458b739eb6 | 126 | void resetGSMIdleBit() |
es_marble | 1:c1458b739eb6 | 127 | { |
es_marble | 1:c1458b739eb6 | 128 | UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK; |
es_marble | 1:c1458b739eb6 | 129 | } |
es_marble | 1:c1458b739eb6 | 130 | |
es_marble | 1:c1458b739eb6 | 131 | //Get the GSM DMA idle bit (if 1, indicates we already received a response) |
es_marble | 1:c1458b739eb6 | 132 | bool getGSMIdleBit() |
es_marble | 1:c1458b739eb6 | 133 | { |
es_marble | 1:c1458b739eb6 | 134 | return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT; |
danilob | 0:41904adca656 | 135 | } |