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@0:41904adca656, 2015-03-05 (annotated)
- Committer:
- danilob
- Date:
- Thu Mar 05 20:06:41 2015 +0000
- Revision:
- 0:41904adca656
- Child:
- 1:c1458b739eb6
new version
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 | */ |
danilob | 0:41904adca656 | 6 | |
danilob | 0:41904adca656 | 7 | char buffer[BUFFER_LENGTH]; |
danilob | 0:41904adca656 | 8 | char* queueHead; |
danilob | 0:41904adca656 | 9 | |
danilob | 0:41904adca656 | 10 | |
danilob | 0:41904adca656 | 11 | //Initialize variables |
danilob | 0:41904adca656 | 12 | void queueInit() |
danilob | 0:41904adca656 | 13 | { |
danilob | 0:41904adca656 | 14 | //The buffer is initialized in init.cpp |
danilob | 0:41904adca656 | 15 | queueHead = QUEUETAIL; |
danilob | 0:41904adca656 | 16 | } |
danilob | 0:41904adca656 | 17 | |
danilob | 0:41904adca656 | 18 | //Find an occurrence of the given string in the buffer. |
danilob | 0:41904adca656 | 19 | //Only advance queueHead until a matching string is found. |
danilob | 0:41904adca656 | 20 | bool findInQueue(char* str, int strLen) |
danilob | 0:41904adca656 | 21 | { |
danilob | 0:41904adca656 | 22 | while (queueHead != QUEUETAIL) |
danilob | 0:41904adca656 | 23 | { |
danilob | 0:41904adca656 | 24 | //Does the character match the begin char? |
danilob | 0:41904adca656 | 25 | if (*queueHead == *str){ |
danilob | 0:41904adca656 | 26 | //Check the remaining characters |
danilob | 0:41904adca656 | 27 | char* sPos = str + 1; |
danilob | 0:41904adca656 | 28 | char * qPos = 0; |
danilob | 0:41904adca656 | 29 | for (qPos = incrementIndex(queueHead); qPos != QUEUETAIL; qPos = incrementIndex(qPos)){ |
danilob | 0:41904adca656 | 30 | //Compare the next char |
danilob | 0:41904adca656 | 31 | if (*qPos == *sPos) |
danilob | 0:41904adca656 | 32 | { |
danilob | 0:41904adca656 | 33 | //Increment index (prefix incrementation). If finished, return true. |
danilob | 0:41904adca656 | 34 | if (++sPos == str + strLen) |
danilob | 0:41904adca656 | 35 | return true; |
danilob | 0:41904adca656 | 36 | } |
danilob | 0:41904adca656 | 37 | else //Not equal, so exit for loop and try again at a different location |
danilob | 0:41904adca656 | 38 | break; |
danilob | 0:41904adca656 | 39 | } |
danilob | 0:41904adca656 | 40 | } |
danilob | 0:41904adca656 | 41 | //Increment queue index for next iteration |
danilob | 0:41904adca656 | 42 | queueHead = incrementIndex(queueHead); |
danilob | 0:41904adca656 | 43 | } |
danilob | 0:41904adca656 | 44 | //We never finished, so return false |
danilob | 0:41904adca656 | 45 | return false; |
danilob | 0:41904adca656 | 46 | } |
danilob | 0:41904adca656 | 47 | |
danilob | 0:41904adca656 | 48 | //Increment queue index by 1 |
danilob | 0:41904adca656 | 49 | char* incrementIndex(char* pointerToIncrement) |
danilob | 0:41904adca656 | 50 | { |
danilob | 0:41904adca656 | 51 | if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH)) |
danilob | 0:41904adca656 | 52 | return (pointerToIncrement + 1); |
danilob | 0:41904adca656 | 53 | else |
danilob | 0:41904adca656 | 54 | return buffer; |
danilob | 0:41904adca656 | 55 | } |
danilob | 0:41904adca656 | 56 | |
danilob | 0:41904adca656 | 57 | //clear queue |
danilob | 0:41904adca656 | 58 | void flushQueue() |
danilob | 0:41904adca656 | 59 | { |
danilob | 0:41904adca656 | 60 | queueHead = QUEUETAIL; |
danilob | 0:41904adca656 | 61 | } |
danilob | 0:41904adca656 | 62 | |
danilob | 0:41904adca656 | 63 | //print queue elements, (will help debug) |
danilob | 0:41904adca656 | 64 | void printQueue() |
danilob | 0:41904adca656 | 65 | { |
danilob | 0:41904adca656 | 66 | char* qPos = queueHead; |
danilob | 0:41904adca656 | 67 | while (qPos != QUEUETAIL) |
danilob | 0:41904adca656 | 68 | { |
danilob | 0:41904adca656 | 69 | //Print the current character |
danilob | 0:41904adca656 | 70 | pc.printf("%C",*qPos); |
danilob | 0:41904adca656 | 71 | |
danilob | 0:41904adca656 | 72 | //Increment index |
danilob | 0:41904adca656 | 73 | qPos = incrementIndex(qPos); |
danilob | 0:41904adca656 | 74 | } |
danilob | 0:41904adca656 | 75 | } |
danilob | 0:41904adca656 | 76 | |
danilob | 0:41904adca656 | 77 | //Parse through characters until first integer is found |
danilob | 0:41904adca656 | 78 | // |
danilob | 0:41904adca656 | 79 | int parseInt() |
danilob | 0:41904adca656 | 80 | { |
danilob | 0:41904adca656 | 81 | char* qPos = queueHead; |
danilob | 0:41904adca656 | 82 | while (qPos != QUEUETAIL) |
danilob | 0:41904adca656 | 83 | { |
danilob | 0:41904adca656 | 84 | //Print the current character |
danilob | 0:41904adca656 | 85 | pc.printf("%C",*qPos); |
danilob | 0:41904adca656 | 86 | |
danilob | 0:41904adca656 | 87 | //Increment index |
danilob | 0:41904adca656 | 88 | qPos = incrementIndex(qPos); |
danilob | 0:41904adca656 | 89 | } |
danilob | 0:41904adca656 | 90 | } |