LLAP Library for Ciseco wireless products.
Dependents: Ciseco_LLAP_Test Ciseco_SRF_Shield
Library for Ciseco wireless modules http://shop.ciseco.co.uk/rf-module-range/
Tested with Nucleo F401RE and http://shop.ciseco.co.uk/srf-shield-wireless-transciever-for-all-arduino-type-boards/
LLAPSerial.cpp@1:8f3ec117823d, 2014-04-15 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Tue Apr 15 22:10:17 2014 +0000
- Revision:
- 1:8f3ec117823d
- Parent:
- 0:c1b97c30cbc5
- Child:
- 2:73b87761ce69
updates comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:c1b97c30cbc5 | 1 | /** LLAP Serial for use with Ciseco SRF/XRF wireless modules |
SomeRandomBloke | 0:c1b97c30cbc5 | 2 | * Wireless modules available at http://shop.ciseco.co.uk/rf-module-range/ |
SomeRandomBloke | 0:c1b97c30cbc5 | 3 | * Library developped with ST Micro Nucleo F401 and Ciseco SRF shield. |
SomeRandomBloke | 0:c1b97c30cbc5 | 4 | * Sheild needs to be modified as Tx/Rx on pins 0 and 1 conflict with the USB debug port. |
SomeRandomBloke | 0:c1b97c30cbc5 | 5 | * They need linking to Rx - PA_12, Tx - PA_11 on the outer row of pins. |
SomeRandomBloke | 0:c1b97c30cbc5 | 6 | * See http://mbed.org/platforms/ST-Nucleo-F401RE/ for pinouts. |
SomeRandomBloke | 0:c1b97c30cbc5 | 7 | * |
SomeRandomBloke | 0:c1b97c30cbc5 | 8 | * This code is based on the Ciseco LLAPSerial library for Arduino at https://github.com/CisecoPlc/LLAPSerial but updated to |
SomeRandomBloke | 0:c1b97c30cbc5 | 9 | * work as a mbed library |
SomeRandomBloke | 0:c1b97c30cbc5 | 10 | * |
SomeRandomBloke | 0:c1b97c30cbc5 | 11 | * Converted and updated by Andrew Lindsay @AndrewDLindsay April 2014 |
SomeRandomBloke | 1:8f3ec117823d | 12 | * |
SomeRandomBloke | 1:8f3ec117823d | 13 | * The MIT License (MIT) |
SomeRandomBloke | 1:8f3ec117823d | 14 | * |
SomeRandomBloke | 1:8f3ec117823d | 15 | * Copyright (c) 2014 Andrew Lindsay |
SomeRandomBloke | 1:8f3ec117823d | 16 | * |
SomeRandomBloke | 1:8f3ec117823d | 17 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
SomeRandomBloke | 1:8f3ec117823d | 18 | * of this software and associated documentation files (the "Software"), to deal |
SomeRandomBloke | 1:8f3ec117823d | 19 | * in the Software without restriction, including without limitation the rights |
SomeRandomBloke | 1:8f3ec117823d | 20 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
SomeRandomBloke | 1:8f3ec117823d | 21 | * copies of the Software, and to permit persons to whom the Software is |
SomeRandomBloke | 1:8f3ec117823d | 22 | * furnished to do so, subject to the following conditions: |
SomeRandomBloke | 1:8f3ec117823d | 23 | * |
SomeRandomBloke | 1:8f3ec117823d | 24 | * The above copyright notice and this permission notice shall be included in |
SomeRandomBloke | 1:8f3ec117823d | 25 | * all copies or substantial portions of the Software. |
SomeRandomBloke | 1:8f3ec117823d | 26 | * |
SomeRandomBloke | 1:8f3ec117823d | 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
SomeRandomBloke | 1:8f3ec117823d | 28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
SomeRandomBloke | 1:8f3ec117823d | 29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
SomeRandomBloke | 1:8f3ec117823d | 30 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
SomeRandomBloke | 1:8f3ec117823d | 31 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
SomeRandomBloke | 1:8f3ec117823d | 32 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
SomeRandomBloke | 1:8f3ec117823d | 33 | * THE SOFTWARE. |
SomeRandomBloke | 1:8f3ec117823d | 34 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 35 | */ |
SomeRandomBloke | 0:c1b97c30cbc5 | 36 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 37 | #include "mbed.h" |
SomeRandomBloke | 0:c1b97c30cbc5 | 38 | #include "LLAPSerial.h" |
SomeRandomBloke | 0:c1b97c30cbc5 | 39 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 40 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 41 | // Constructors to pass in Tx/Rx pins and optional ID, default is -- as defined in LLAPSerial.h |
SomeRandomBloke | 0:c1b97c30cbc5 | 42 | LLAPSerial::LLAPSerial(PinName txPin, PinName rxPin, char *dID) : srf(txPin, rxPin) |
SomeRandomBloke | 0:c1b97c30cbc5 | 43 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 44 | srf.baud(115200); |
SomeRandomBloke | 0:c1b97c30cbc5 | 45 | bMsgReceived = false; |
SomeRandomBloke | 0:c1b97c30cbc5 | 46 | setDeviceId(dID); |
SomeRandomBloke | 0:c1b97c30cbc5 | 47 | cMessage[12]=0; // ensure terminated |
SomeRandomBloke | 0:c1b97c30cbc5 | 48 | inPtr = cMessage; |
SomeRandomBloke | 0:c1b97c30cbc5 | 49 | // Attach the receive interrupt handler |
SomeRandomBloke | 0:c1b97c30cbc5 | 50 | srf.attach( this,&LLAPSerial::SerialEvent ); |
SomeRandomBloke | 0:c1b97c30cbc5 | 51 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 52 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 53 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 54 | void LLAPSerial::processMessage() |
SomeRandomBloke | 0:c1b97c30cbc5 | 55 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 56 | //if (LLAP.cMessage[0] != 'a') return; //not needed as already checked |
SomeRandomBloke | 0:c1b97c30cbc5 | 57 | // if (cMessage[1] != deviceId[0]) return; |
SomeRandomBloke | 0:c1b97c30cbc5 | 58 | // if (cMessage[2] != deviceId[1]) return; |
SomeRandomBloke | 0:c1b97c30cbc5 | 59 | // now we have LLAP.cMessage[3] to LLAP.cMessage[11] as the actual message |
SomeRandomBloke | 0:c1b97c30cbc5 | 60 | if (0 == strncmp(&cMessage[3],"HELLO----",9)) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 61 | srf.printf("%s",cMessage); // echo the message |
SomeRandomBloke | 0:c1b97c30cbc5 | 62 | return; |
SomeRandomBloke | 0:c1b97c30cbc5 | 63 | } else if (0 == strncmp(&cMessage[3],"CHDEVID",7)) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 64 | if (strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[10]) != 0 && strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[11]) != 0) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 65 | deviceId[0] = cMessage[10]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 66 | deviceId[1] = cMessage[11]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 67 | srf.printf( "%s", cMessage); // echo the message |
SomeRandomBloke | 0:c1b97c30cbc5 | 68 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 69 | } else { |
SomeRandomBloke | 0:c1b97c30cbc5 | 70 | strncpy(sMessage, &cMessage[0], 12); // let the main program deal with it |
SomeRandomBloke | 0:c1b97c30cbc5 | 71 | bMsgReceived = true; |
SomeRandomBloke | 0:c1b97c30cbc5 | 72 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 73 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 74 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 75 | void LLAPSerial::SerialEvent( void ) |
SomeRandomBloke | 0:c1b97c30cbc5 | 76 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 77 | if (bMsgReceived) return; // get out if previous message not yet processed |
SomeRandomBloke | 0:c1b97c30cbc5 | 78 | if (srf.readable() ) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 79 | // get the new char: |
SomeRandomBloke | 0:c1b97c30cbc5 | 80 | char inChar = (char)srf.getc(); |
SomeRandomBloke | 0:c1b97c30cbc5 | 81 | if (inChar == 'a') { |
SomeRandomBloke | 0:c1b97c30cbc5 | 82 | // Start of a new message |
SomeRandomBloke | 0:c1b97c30cbc5 | 83 | inPtr = cMessage; |
SomeRandomBloke | 0:c1b97c30cbc5 | 84 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 85 | *inPtr++ = inChar; |
SomeRandomBloke | 0:c1b97c30cbc5 | 86 | if( inPtr >= &cMessage[12]) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 87 | // Message received, terminate, process and reset pointer |
SomeRandomBloke | 0:c1b97c30cbc5 | 88 | *inPtr = '\0'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 89 | processMessage(); |
SomeRandomBloke | 0:c1b97c30cbc5 | 90 | inPtr = cMessage; |
SomeRandomBloke | 0:c1b97c30cbc5 | 91 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 92 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 93 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 94 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 95 | /* |
SomeRandomBloke | 0:c1b97c30cbc5 | 96 | void LLAPSerial::SerialEvent( void ) |
SomeRandomBloke | 0:c1b97c30cbc5 | 97 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 98 | if (bMsgReceived) return; // get out if previous message not yet processed |
SomeRandomBloke | 0:c1b97c30cbc5 | 99 | if (srf.readable() ) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 100 | // get the new byte: |
SomeRandomBloke | 0:c1b97c30cbc5 | 101 | char inChar = (char)srf.getc(); |
SomeRandomBloke | 0:c1b97c30cbc5 | 102 | if (inChar == 'a') { |
SomeRandomBloke | 0:c1b97c30cbc5 | 103 | cMessage[0] = inChar; |
SomeRandomBloke | 0:c1b97c30cbc5 | 104 | for (int i = 1; i<12; i++) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 105 | inChar = (char)srf.getc(); |
SomeRandomBloke | 0:c1b97c30cbc5 | 106 | if( inChar == 'a' ) |
SomeRandomBloke | 0:c1b97c30cbc5 | 107 | return; // out of sync so abort and pick it up next time round |
SomeRandomBloke | 0:c1b97c30cbc5 | 108 | cMessage[i] = inChar; |
SomeRandomBloke | 0:c1b97c30cbc5 | 109 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 110 | cMessage[12]=0; |
SomeRandomBloke | 0:c1b97c30cbc5 | 111 | processMessage(); |
SomeRandomBloke | 0:c1b97c30cbc5 | 112 | } else |
SomeRandomBloke | 0:c1b97c30cbc5 | 113 | srf.getc(); // throw away the character |
SomeRandomBloke | 0:c1b97c30cbc5 | 114 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 115 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 116 | */ |
SomeRandomBloke | 0:c1b97c30cbc5 | 117 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 118 | void LLAPSerial::sendMessage(char *sToSend) |
SomeRandomBloke | 0:c1b97c30cbc5 | 119 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 120 | cMessage[0] = 'a'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 121 | cMessage[1] = deviceId[0]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 122 | cMessage[2] = deviceId[1]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 123 | for (int i = 0; i<9; i++) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 124 | if (i < strlen(sToSend) ) |
SomeRandomBloke | 0:c1b97c30cbc5 | 125 | cMessage[i+3] = sToSend[i]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 126 | else |
SomeRandomBloke | 0:c1b97c30cbc5 | 127 | cMessage[i+3] = '-'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 128 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 129 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 130 | srf.printf("%s",cMessage); |
SomeRandomBloke | 0:c1b97c30cbc5 | 131 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 132 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 133 | void LLAPSerial::sendMessage(char* sToSend, char* valueToSend) |
SomeRandomBloke | 0:c1b97c30cbc5 | 134 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 135 | cMessage[0] = 'a'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 136 | cMessage[1] = deviceId[0]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 137 | cMessage[2] = deviceId[1]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 138 | for (int i = 0; i<9; i++) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 139 | if (i < strlen(sToSend)) |
SomeRandomBloke | 0:c1b97c30cbc5 | 140 | cMessage[i+3] = sToSend[i]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 141 | else if (i < strlen(sToSend) + strlen(valueToSend)) |
SomeRandomBloke | 0:c1b97c30cbc5 | 142 | cMessage[i+3] = valueToSend[i - strlen(sToSend)]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 143 | else |
SomeRandomBloke | 0:c1b97c30cbc5 | 144 | cMessage[i+3] = '-'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 145 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 146 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 147 | srf.printf("%s", cMessage); |
SomeRandomBloke | 0:c1b97c30cbc5 | 148 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 149 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 150 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 151 | void LLAPSerial::sendInt(char *sToSend, int value) |
SomeRandomBloke | 0:c1b97c30cbc5 | 152 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 153 | char cValue[7]; // long enough for -32767 and the trailing zero |
SomeRandomBloke | 0:c1b97c30cbc5 | 154 | sprintf( cValue,"%d", value ); |
SomeRandomBloke | 0:c1b97c30cbc5 | 155 | int cValuePtr = 0; |
SomeRandomBloke | 0:c1b97c30cbc5 | 156 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 157 | cMessage[0] = 'a'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 158 | cMessage[1] = deviceId[0]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 159 | cMessage[2] = deviceId[1]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 160 | for (int i = 0; i<9; i++) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 161 | if (i < strlen(sToSend)) |
SomeRandomBloke | 0:c1b97c30cbc5 | 162 | cMessage[i+3] = sToSend[i]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 163 | else if (cValuePtr < 7 && cValue[cValuePtr] !=0) |
SomeRandomBloke | 0:c1b97c30cbc5 | 164 | cMessage[i+3] = cValue[cValuePtr++]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 165 | else |
SomeRandomBloke | 0:c1b97c30cbc5 | 166 | cMessage[i+3] = '-'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 167 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 168 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 169 | srf.printf("%s",cMessage); |
SomeRandomBloke | 0:c1b97c30cbc5 | 170 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 171 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 172 | void LLAPSerial::sendIntWithDP(char *sToSend, int value, int decimalPlaces) |
SomeRandomBloke | 0:c1b97c30cbc5 | 173 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 174 | char cValue[8]; // long enough for -3276.7 and the trailing zero |
SomeRandomBloke | 0:c1b97c30cbc5 | 175 | int cValuePtr=0; |
SomeRandomBloke | 0:c1b97c30cbc5 | 176 | //itoa(value, cValue,10); |
SomeRandomBloke | 0:c1b97c30cbc5 | 177 | sprintf( cValue,"%d", value ); |
SomeRandomBloke | 0:c1b97c30cbc5 | 178 | char* cp = &cValue[strlen(cValue)]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 179 | *(cp+1) = 0; // new terminator |
SomeRandomBloke | 0:c1b97c30cbc5 | 180 | while (decimalPlaces-- && --cp ) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 181 | *(cp+1) = *cp; |
SomeRandomBloke | 0:c1b97c30cbc5 | 182 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 183 | *cp = '.'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 184 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 185 | cMessage[0] = 'a'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 186 | cMessage[1] = deviceId[0]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 187 | cMessage[2] = deviceId[1]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 188 | for (int i = 0; i<9; i++) { |
SomeRandomBloke | 0:c1b97c30cbc5 | 189 | if (i < strlen(sToSend)) |
SomeRandomBloke | 0:c1b97c30cbc5 | 190 | cMessage[i+3] = sToSend[i]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 191 | else if (cValuePtr < 8 && cValue[cValuePtr] !=0) |
SomeRandomBloke | 0:c1b97c30cbc5 | 192 | cMessage[i+3] = cValue[cValuePtr++]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 193 | else |
SomeRandomBloke | 0:c1b97c30cbc5 | 194 | cMessage[i+3] = '-'; |
SomeRandomBloke | 0:c1b97c30cbc5 | 195 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 196 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 197 | srf.printf("%s", cMessage); |
SomeRandomBloke | 0:c1b97c30cbc5 | 198 | } |
SomeRandomBloke | 0:c1b97c30cbc5 | 199 | |
SomeRandomBloke | 0:c1b97c30cbc5 | 200 | void LLAPSerial::setDeviceId(char* cId) |
SomeRandomBloke | 0:c1b97c30cbc5 | 201 | { |
SomeRandomBloke | 0:c1b97c30cbc5 | 202 | deviceId[0] = cId[0]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 203 | deviceId[1] = cId[1]; |
SomeRandomBloke | 0:c1b97c30cbc5 | 204 | } |