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@2:5c0513ab856e, 2015-03-06 (annotated)
- Committer:
- danilob
- Date:
- Fri Mar 06 23:14:13 2015 +0000
- Revision:
- 2:5c0513ab856e
- Parent:
- 1:8614e190908b
GSM library ready
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 | */ |
| danilob | 2:5c0513ab856e | 6 | |
| bjcrofts | 1:8614e190908b | 7 | extern Serial pc; |
| bjcrofts | 1:8614e190908b | 8 | |
| bjcrofts | 1:8614e190908b | 9 | char buffer[BUFFER_LENGTH]; |
| bjcrofts | 1:8614e190908b | 10 | char* queueHead; |
| bjcrofts | 1:8614e190908b | 11 | |
| bjcrofts | 1:8614e190908b | 12 | |
| bjcrofts | 1:8614e190908b | 13 | //Initialize variables |
| bjcrofts | 1:8614e190908b | 14 | void queueInit() |
| bjcrofts | 1:8614e190908b | 15 | { |
| bjcrofts | 1:8614e190908b | 16 | //The buffer is initialized in init.cpp |
| bjcrofts | 1:8614e190908b | 17 | queueHead = QUEUETAIL; |
| bjcrofts | 1:8614e190908b | 18 | } |
| bjcrofts | 1:8614e190908b | 19 | |
| bjcrofts | 1:8614e190908b | 20 | //Find an occurrence of the given string in the buffer. |
| bjcrofts | 1:8614e190908b | 21 | //Only advance queueHead until a matching string is found. |
| bjcrofts | 1:8614e190908b | 22 | //The given string terminates in NULL (\0) |
| bjcrofts | 1:8614e190908b | 23 | bool findInQueue(char* str) |
| bjcrofts | 1:8614e190908b | 24 | { |
| bjcrofts | 1:8614e190908b | 25 | //Check that string to find is not empty |
| bjcrofts | 1:8614e190908b | 26 | if (*str == NULL) return false; |
| bjcrofts | 1:8614e190908b | 27 | |
| bjcrofts | 1:8614e190908b | 28 | while (queueHead != QUEUETAIL) |
| bjcrofts | 1:8614e190908b | 29 | { |
| bjcrofts | 1:8614e190908b | 30 | //Does the character match the begin char? |
| bjcrofts | 1:8614e190908b | 31 | if (*queueHead == *str){ |
| bjcrofts | 1:8614e190908b | 32 | //Check the remaining characters |
| bjcrofts | 1:8614e190908b | 33 | char* sPos = str; |
| bjcrofts | 1:8614e190908b | 34 | char* qPos = 0; |
| bjcrofts | 1:8614e190908b | 35 | for (qPos = queueHead; qPos != QUEUETAIL; qPos = incrementIndex(qPos)){ |
| bjcrofts | 1:8614e190908b | 36 | //Compare the next char |
| bjcrofts | 1:8614e190908b | 37 | if (*qPos == *sPos) |
| bjcrofts | 1:8614e190908b | 38 | { |
| bjcrofts | 1:8614e190908b | 39 | ++sPos; //Increment index (prefix incrementation). |
| bjcrofts | 1:8614e190908b | 40 | if (*sPos == NULL) //If finished, update queueHead, return true. |
| bjcrofts | 1:8614e190908b | 41 | { |
| bjcrofts | 1:8614e190908b | 42 | queueHead = incrementIndex(qPos); |
| bjcrofts | 1:8614e190908b | 43 | return true; |
| bjcrofts | 1:8614e190908b | 44 | } |
| bjcrofts | 1:8614e190908b | 45 | } |
| bjcrofts | 1:8614e190908b | 46 | else //Not equal, so exit for loop and try again at a different location |
| bjcrofts | 1:8614e190908b | 47 | break; |
| bjcrofts | 1:8614e190908b | 48 | } |
| bjcrofts | 1:8614e190908b | 49 | } |
| bjcrofts | 1:8614e190908b | 50 | //Increment queue index for next iteration |
| bjcrofts | 1:8614e190908b | 51 | queueHead = incrementIndex(queueHead); |
| bjcrofts | 1:8614e190908b | 52 | } |
| bjcrofts | 1:8614e190908b | 53 | //We never finished, so return false |
| bjcrofts | 1:8614e190908b | 54 | return false; |
| bjcrofts | 1:8614e190908b | 55 | } |
| bjcrofts | 1:8614e190908b | 56 | |
| bjcrofts | 1:8614e190908b | 57 | //Increment queue index by 1 |
| bjcrofts | 1:8614e190908b | 58 | char* incrementIndex(char* pointerToIncrement) |
| bjcrofts | 1:8614e190908b | 59 | { |
| bjcrofts | 1:8614e190908b | 60 | if((pointerToIncrement + 1) < (buffer + BUFFER_LENGTH)) |
| bjcrofts | 1:8614e190908b | 61 | return (pointerToIncrement + 1); |
| bjcrofts | 1:8614e190908b | 62 | else |
| bjcrofts | 1:8614e190908b | 63 | return buffer; |
| bjcrofts | 1:8614e190908b | 64 | } |
| bjcrofts | 1:8614e190908b | 65 | |
| bjcrofts | 1:8614e190908b | 66 | //clear queue |
| bjcrofts | 1:8614e190908b | 67 | void flushQueue() |
| bjcrofts | 1:8614e190908b | 68 | { |
| bjcrofts | 1:8614e190908b | 69 | queueHead = QUEUETAIL; |
| bjcrofts | 1:8614e190908b | 70 | } |
| bjcrofts | 1:8614e190908b | 71 | |
| bjcrofts | 1:8614e190908b | 72 | //$debug - print queue elements |
| bjcrofts | 1:8614e190908b | 73 | void printQueue() |
| bjcrofts | 1:8614e190908b | 74 | { |
| bjcrofts | 1:8614e190908b | 75 | char* qPos = queueHead; |
| bjcrofts | 1:8614e190908b | 76 | pc.printf("Queue:"); |
| bjcrofts | 1:8614e190908b | 77 | while (qPos != QUEUETAIL) |
| bjcrofts | 1:8614e190908b | 78 | { |
| bjcrofts | 1:8614e190908b | 79 | //Print the current character |
| bjcrofts | 1:8614e190908b | 80 | if (*qPos == '\n') |
| bjcrofts | 1:8614e190908b | 81 | pc.printf("\\n"); |
| bjcrofts | 1:8614e190908b | 82 | else if (*qPos == '\r') |
| bjcrofts | 1:8614e190908b | 83 | pc.printf("\\r"); |
| bjcrofts | 1:8614e190908b | 84 | else |
| bjcrofts | 1:8614e190908b | 85 | pc.printf("%C",*qPos); |
| bjcrofts | 1:8614e190908b | 86 | |
| bjcrofts | 1:8614e190908b | 87 | |
| bjcrofts | 1:8614e190908b | 88 | //Increment index |
| bjcrofts | 1:8614e190908b | 89 | qPos = incrementIndex(qPos); |
| bjcrofts | 1:8614e190908b | 90 | } |
| bjcrofts | 1:8614e190908b | 91 | pc.printf("\n\r"); |
| bjcrofts | 1:8614e190908b | 92 | } |
| bjcrofts | 1:8614e190908b | 93 | |
| bjcrofts | 1:8614e190908b | 94 | //Parse through characters until first integer is found |
| bjcrofts | 1:8614e190908b | 95 | //Advance qHead until you reach the next non-numeric character |
| bjcrofts | 1:8614e190908b | 96 | //Does not read negative integers; returns -1 if unsuccessful |
| bjcrofts | 1:8614e190908b | 97 | int parseInt() |
| bjcrofts | 1:8614e190908b | 98 | { |
| bjcrofts | 1:8614e190908b | 99 | //Check if queue is empty first |
| bjcrofts | 1:8614e190908b | 100 | if (queueHead == QUEUETAIL) return -1; |
| bjcrofts | 1:8614e190908b | 101 | |
| bjcrofts | 1:8614e190908b | 102 | //Advance to first numeric character |
| bjcrofts | 1:8614e190908b | 103 | while (!isNumeric(queueHead)) |
| bjcrofts | 1:8614e190908b | 104 | { |
| bjcrofts | 1:8614e190908b | 105 | queueHead = incrementIndex(queueHead); |
| bjcrofts | 1:8614e190908b | 106 | if (queueHead == QUEUETAIL) return -1; |
| bjcrofts | 1:8614e190908b | 107 | } |
| bjcrofts | 1:8614e190908b | 108 | |
| bjcrofts | 1:8614e190908b | 109 | //Continue until first non-numeric character |
| bjcrofts | 1:8614e190908b | 110 | int val = 0; |
| bjcrofts | 1:8614e190908b | 111 | while (queueHead != QUEUETAIL && isNumeric(queueHead)) |
| bjcrofts | 1:8614e190908b | 112 | { |
| bjcrofts | 1:8614e190908b | 113 | val *= 10; |
| bjcrofts | 1:8614e190908b | 114 | val += (int)(*queueHead - '0'); |
| bjcrofts | 1:8614e190908b | 115 | queueHead = incrementIndex(queueHead); |
| bjcrofts | 1:8614e190908b | 116 | } |
| bjcrofts | 1:8614e190908b | 117 | return val; |
| bjcrofts | 1:8614e190908b | 118 | } |
| bjcrofts | 1:8614e190908b | 119 | |
| bjcrofts | 1:8614e190908b | 120 | //Returns true if the character is numeric |
| bjcrofts | 1:8614e190908b | 121 | bool isNumeric(char* qPos) |
| bjcrofts | 1:8614e190908b | 122 | { |
| bjcrofts | 1:8614e190908b | 123 | return ('0' <= *qPos && *qPos <= '9'); |
| bjcrofts | 1:8614e190908b | 124 | } |
| bjcrofts | 1:8614e190908b | 125 | |
| bjcrofts | 1:8614e190908b | 126 | //Reset the GSM DMA idle bit to 0 |
| bjcrofts | 1:8614e190908b | 127 | void resetGSMIdleBit() |
| bjcrofts | 1:8614e190908b | 128 | { |
| bjcrofts | 1:8614e190908b | 129 | UART_S1_REG(UART3) &= ~UART_S1_IDLE_MASK; |
| bjcrofts | 1:8614e190908b | 130 | } |
| bjcrofts | 1:8614e190908b | 131 | |
| bjcrofts | 1:8614e190908b | 132 | //Get the GSM DMA idle bit (if 1, indicates we already received a response) |
| bjcrofts | 1:8614e190908b | 133 | bool getGSMIdleBit() |
| bjcrofts | 1:8614e190908b | 134 | { |
| bjcrofts | 1:8614e190908b | 135 | return (UART_S1_IDLE_MASK & UART_S1_REG(UART3)) >> UART_S1_IDLE_SHIFT; |
| bjcrofts | 1:8614e190908b | 136 | } |
