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