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