Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MBed_Adafruit-GPS-Library SDFileSystem mbed GSM_Library
Fork of DCS by
gsmqueue.cpp@1:8614e190908b, 2015-03-06 (annotated)
- Committer:
- bjcrofts
- Date:
- Fri Mar 06 22:36:44 2015 +0000
- Revision:
- 1:8614e190908b
- Child:
- 2:5c0513ab856e
hoy
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bjcrofts | 1:8614e190908b | 1 | #include "gsmqueue.h" |
| bjcrofts | 1:8614e190908b | 2 | #include "mbed.h" |
| bjcrofts | 1:8614e190908b | 3 | /* queue.cpp |
| bjcrofts | 1:8614e190908b | 4 | * Contains functions to read from the DMA buffer in a queue fashion |
| bjcrofts | 1:8614e190908b | 5 | */ |
| bjcrofts | 1:8614e190908b | 6 | extern Serial pc; |
| bjcrofts | 1:8614e190908b | 7 | |
| bjcrofts | 1:8614e190908b | 8 | char buffer[BUFFER_LENGTH]; |
| bjcrofts | 1:8614e190908b | 9 | char* queueHead; |
| bjcrofts | 1:8614e190908b | 10 | |
| bjcrofts | 1:8614e190908b | 11 | |
| bjcrofts | 1:8614e190908b | 12 | //Initialize variables |
| bjcrofts | 1:8614e190908b | 13 | void queueInit() |
| bjcrofts | 1:8614e190908b | 14 | { |
| bjcrofts | 1:8614e190908b | 15 | //The buffer is initialized in init.cpp |
| bjcrofts | 1:8614e190908b | 16 | queueHead = QUEUETAIL; |
| bjcrofts | 1:8614e190908b | 17 | } |
| bjcrofts | 1:8614e190908b | 18 | |
| bjcrofts | 1:8614e190908b | 19 | //Find an occurrence of the given string in the buffer. |
| bjcrofts | 1:8614e190908b | 20 | //Only advance queueHead until a matching string is found. |
| bjcrofts | 1:8614e190908b | 21 | //The given string terminates in NULL (\0) |
| bjcrofts | 1:8614e190908b | 22 | bool findInQueue(char* str) |
| bjcrofts | 1:8614e190908b | 23 | { |
| bjcrofts | 1:8614e190908b | 24 | //Check that string to find is not empty |
| bjcrofts | 1:8614e190908b | 25 | if (*str == NULL) return false; |
| bjcrofts | 1:8614e190908b | 26 | |
| bjcrofts | 1:8614e190908b | 27 | while (queueHead != QUEUETAIL) |
| bjcrofts | 1:8614e190908b | 28 | { |
| bjcrofts | 1:8614e190908b | 29 | //Does the character match the begin char? |
| bjcrofts | 1:8614e190908b | 30 | if (*queueHead == *str){ |
| bjcrofts | 1:8614e190908b | 31 | //Check the remaining characters |
| bjcrofts | 1:8614e190908b | 32 | char* sPos = str; |
| bjcrofts | 1:8614e190908b | 33 | char* qPos = 0; |
| bjcrofts | 1:8614e190908b | 34 | for (qPos = queueHead; qPos != QUEUETAIL; qPos = incrementIndex(qPos)){ |
| bjcrofts | 1:8614e190908b | 35 | //Compare the next char |
| bjcrofts | 1:8614e190908b | 36 | if (*qPos == *sPos) |
| bjcrofts | 1:8614e190908b | 37 | { |
| bjcrofts | 1:8614e190908b | 38 | ++sPos; //Increment index (prefix incrementation). |
| bjcrofts | 1:8614e190908b | 39 | if (*sPos == NULL) //If finished, update queueHead, return true. |
| bjcrofts | 1:8614e190908b | 40 | { |
| bjcrofts | 1:8614e190908b | 41 | queueHead = incrementIndex(qPos); |
| bjcrofts | 1:8614e190908b | 42 | return true; |
| bjcrofts | 1:8614e190908b | 43 | } |
| bjcrofts | 1:8614e190908b | 44 | } |
| bjcrofts | 1:8614e190908b | 45 | else //Not equal, so exit for loop and try again at a different location |
| bjcrofts | 1:8614e190908b | 46 | break; |
| bjcrofts | 1:8614e190908b | 47 | } |
| bjcrofts | 1:8614e190908b | 48 | } |
| bjcrofts | 1:8614e190908b | 49 | //Increment queue index for next iteration |
| bjcrofts | 1:8614e190908b | 50 | queueHead = incrementIndex(queueHead); |
| bjcrofts | 1:8614e190908b | 51 | } |
| bjcrofts | 1:8614e190908b | 52 | //We never finished, so return false |
| bjcrofts | 1:8614e190908b | 53 | return false; |
| bjcrofts | 1:8614e190908b | 54 | } |
| bjcrofts | 1:8614e190908b | 55 | |
| bjcrofts | 1:8614e190908b | 56 | //Increment queue index by 1 |
| bjcrofts | 1:8614e190908b | 57 | char* incrementIndex(char* pointerToIncrement) |
| bjcrofts | 1:8614e190908b | 58 | { |
| bjcrofts | 1:8614e190908b | 59 | if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH)) |
| bjcrofts | 1:8614e190908b | 60 | return (pointerToIncrement + 1); |
| bjcrofts | 1:8614e190908b | 61 | else |
| bjcrofts | 1:8614e190908b | 62 | return buffer; |
| bjcrofts | 1:8614e190908b | 63 | } |
| bjcrofts | 1:8614e190908b | 64 | |
| bjcrofts | 1:8614e190908b | 65 | //clear queue |
| bjcrofts | 1:8614e190908b | 66 | void flushQueue() |
| bjcrofts | 1:8614e190908b | 67 | { |
| bjcrofts | 1:8614e190908b | 68 | queueHead = QUEUETAIL; |
| bjcrofts | 1:8614e190908b | 69 | } |
| bjcrofts | 1:8614e190908b | 70 | |
| bjcrofts | 1:8614e190908b | 71 | //$debug - print queue elements |
| bjcrofts | 1:8614e190908b | 72 | void printQueue() |
| bjcrofts | 1:8614e190908b | 73 | { |
| bjcrofts | 1:8614e190908b | 74 | char* qPos = queueHead; |
| bjcrofts | 1:8614e190908b | 75 | pc.printf("Queue:"); |
| bjcrofts | 1:8614e190908b | 76 | while (qPos != QUEUETAIL) |
| bjcrofts | 1:8614e190908b | 77 | { |
| bjcrofts | 1:8614e190908b | 78 | //Print the current character |
| bjcrofts | 1:8614e190908b | 79 | if (*qPos == '\n') |
| bjcrofts | 1:8614e190908b | 80 | pc.printf("\\n"); |
| bjcrofts | 1:8614e190908b | 81 | else if (*qPos == '\r') |
| bjcrofts | 1:8614e190908b | 82 | pc.printf("\\r"); |
| bjcrofts | 1:8614e190908b | 83 | else |
| bjcrofts | 1:8614e190908b | 84 | pc.printf("%C",*qPos); |
| bjcrofts | 1:8614e190908b | 85 | |
| bjcrofts | 1:8614e190908b | 86 | |
| bjcrofts | 1:8614e190908b | 87 | //Increment index |
| bjcrofts | 1:8614e190908b | 88 | qPos = incrementIndex(qPos); |
| bjcrofts | 1:8614e190908b | 89 | } |
| bjcrofts | 1:8614e190908b | 90 | pc.printf("\n\r"); |
| bjcrofts | 1:8614e190908b | 91 | } |
| bjcrofts | 1:8614e190908b | 92 | |
| bjcrofts | 1:8614e190908b | 93 | //Parse through characters until first integer is found |
| bjcrofts | 1:8614e190908b | 94 | //Advance qHead until you reach the next non-numeric character |
| bjcrofts | 1:8614e190908b | 95 | //Does not read negative integers; returns -1 if unsuccessful |
| bjcrofts | 1:8614e190908b | 96 | int parseInt() |
| bjcrofts | 1:8614e190908b | 97 | { |
| bjcrofts | 1:8614e190908b | 98 | //Check if queue is empty first |
| bjcrofts | 1:8614e190908b | 99 | if (queueHead == QUEUETAIL) return -1; |
| bjcrofts | 1:8614e190908b | 100 | |
| bjcrofts | 1:8614e190908b | 101 | //Advance to first numeric character |
| bjcrofts | 1:8614e190908b | 102 | while (!isNumeric(queueHead)) |
| bjcrofts | 1:8614e190908b | 103 | { |
| bjcrofts | 1:8614e190908b | 104 | queueHead = incrementIndex(queueHead); |
| bjcrofts | 1:8614e190908b | 105 | if (queueHead == QUEUETAIL) return -1; |
| bjcrofts | 1:8614e190908b | 106 | } |
| bjcrofts | 1:8614e190908b | 107 | |
| bjcrofts | 1:8614e190908b | 108 | //Continue until first non-numeric character |
| bjcrofts | 1:8614e190908b | 109 | int val = 0; |
| bjcrofts | 1:8614e190908b | 110 | while (queueHead != QUEUETAIL && isNumeric(queueHead)) |
| bjcrofts | 1:8614e190908b | 111 | { |
| bjcrofts | 1:8614e190908b | 112 | val *= 10; |
| bjcrofts | 1:8614e190908b | 113 | val += (int)(*queueHead - '0'); |
| bjcrofts | 1:8614e190908b | 114 | queueHead = incrementIndex(queueHead); |
| bjcrofts | 1:8614e190908b | 115 | } |
| bjcrofts | 1:8614e190908b | 116 | return val; |
| bjcrofts | 1:8614e190908b | 117 | } |
| bjcrofts | 1:8614e190908b | 118 | |
| bjcrofts | 1:8614e190908b | 119 | //Returns true if the character is numeric |
| bjcrofts | 1:8614e190908b | 120 | bool isNumeric(char* qPos) |
| bjcrofts | 1:8614e190908b | 121 | { |
| bjcrofts | 1:8614e190908b | 122 | return ('0' <= *qPos && *qPos <= '9'); |
| bjcrofts | 1:8614e190908b | 123 | } |
| bjcrofts | 1:8614e190908b | 124 | |
| bjcrofts | 1:8614e190908b | 125 | //Reset the GSM DMA idle bit to 0 |
| bjcrofts | 1:8614e190908b | 126 | void resetGSMIdleBit() |
| bjcrofts | 1:8614e190908b | 127 | { |
| bjcrofts | 1:8614e190908b | 128 | UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK; |
| bjcrofts | 1:8614e190908b | 129 | } |
| bjcrofts | 1:8614e190908b | 130 | |
| bjcrofts | 1:8614e190908b | 131 | //Get the GSM DMA idle bit (if 1, indicates we already received a response) |
| bjcrofts | 1:8614e190908b | 132 | bool getGSMIdleBit() |
| bjcrofts | 1:8614e190908b | 133 | { |
| bjcrofts | 1:8614e190908b | 134 | return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT; |
| bjcrofts | 1:8614e190908b | 135 | } |
