LLAP Library for Ciseco wireless products.
Dependents: Ciseco_LLAP_Test Ciseco_SRF_Shield
LLAPSerial.cpp
00001 /** 00002 * LLAP Serial for use with Ciseco SRF/XRF wireless modules 00003 * 00004 * @author Andrew Lindsay 00005 * 00006 * @section DESCRIPTION 00007 * 00008 * Wireless modules available at http://shop.ciseco.co.uk/rf-module-range/ 00009 * Library developped with ST Micro Nucleo F401 and Ciseco SRF shield. 00010 * Sheild needs to be modified as Tx/Rx on pins 0 and 1 conflict with the USB debug port. 00011 * They need joining to Rx - PA_12, Tx - PA_11 on the outer row of pins. 00012 * See http://mbed.org/platforms/ST-Nucleo-F401RE/ for pinouts. 00013 * 00014 * This code is based on the Ciseco LLAPSerial library for Arduino at https://github.com/CisecoPlc/LLAPSerial but updated to 00015 * work as a mbed library 00016 * 00017 * Converted and updated by Andrew Lindsay @AndrewDLindsay April 2014 00018 * 00019 * For the SRF sheild to work correctly, some mods are needed to the Nucleo board. SB13 and SB14 need to be removed, 00020 * SB62 and SB63 need to be bridged. This then removes the UART from the USB output. An alternative UART is then needed 00021 * for debug purposes. 00022 * 00023 * @section LICENSE 00024 * 00025 * The MIT License (MIT) 00026 * 00027 * Copyright (c) 2014 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk) 00028 * 00029 * Permission is hereby granted, free of charge, to any person obtaining a copy 00030 * of this software and associated documentation files (the "Software"), to deal 00031 * in the Software without restriction, including without limitation the rights 00032 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00033 * copies of the Software, and to permit persons to whom the Software is 00034 * furnished to do so, subject to the following conditions: 00035 * 00036 * The above copyright notice and this permission notice shall be included in 00037 * all copies or substantial portions of the Software. 00038 * 00039 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00040 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00041 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00042 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00043 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00044 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00045 * THE SOFTWARE. 00046 00047 */ 00048 00049 #include "mbed.h" 00050 #include "LLAPSerial.h" 00051 00052 // Constructors to pass in Tx/Rx, enable pins and optional ID, default is -- as defined in LLAPSerial.h 00053 LLAPSerial::LLAPSerial(PinName txPin, PinName rxPin, PinName enableSRF, bool checkDevIDin, char *dID) : srf(txPin, rxPin), srfEnable( enableSRF ) 00054 { 00055 srf.baud(115200); 00056 00057 bMsgReceived = false; 00058 setDeviceId(dID); 00059 cMessage[12]=0; // ensure terminated 00060 inPtr = cMessage; 00061 checkDevID = checkDevIDin; 00062 00063 // Enable the SRF 00064 srfEnable = 1; 00065 00066 // Attach the receive interrupt handler 00067 srf.attach( this,&LLAPSerial::SerialEvent ); 00068 } 00069 00070 00071 void LLAPSerial::processMessage() 00072 { 00073 if( checkDevID ) { 00074 if (cMessage[1] != deviceId[0]) return; 00075 if (cMessage[2] != deviceId[1]) return; 00076 } 00077 // now we have LLAP.cMessage[3] to LLAP.cMessage[11] as the actual message 00078 if (0 == strncmp(&cMessage[3],"HELLO----",9)) { 00079 srf.printf("%s",cMessage); // echo the message 00080 return; 00081 } else if (0 == strncmp(&cMessage[3],"CHDEVID",7)) { 00082 if (strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[10]) != 0 && strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[11]) != 0) { 00083 deviceId[0] = cMessage[10]; 00084 deviceId[1] = cMessage[11]; 00085 srf.printf( "%s", cMessage); // echo the message 00086 } 00087 } else { 00088 strncpy(sMessage, &cMessage[0], 12); // let the main program deal with it 00089 bMsgReceived = true; 00090 } 00091 } 00092 00093 void LLAPSerial::SerialEvent( void ) 00094 { 00095 if (bMsgReceived) return; // get out if previous message not yet processed 00096 if (srf.readable() ) { 00097 // get the new char: 00098 char inChar = (char)srf.getc(); 00099 if (inChar == 'a') { 00100 // Start of a new message 00101 inPtr = cMessage; 00102 } 00103 *inPtr++ = inChar; 00104 if( inPtr >= &cMessage[12]) { 00105 // Message received, terminate, process and reset pointer 00106 *inPtr = '\0'; 00107 processMessage(); 00108 inPtr = cMessage; 00109 } 00110 } 00111 } 00112 00113 /* 00114 void LLAPSerial::SerialEvent( void ) 00115 { 00116 if (bMsgReceived) return; // get out if previous message not yet processed 00117 if (srf.readable() ) { 00118 // get the new byte: 00119 char inChar = (char)srf.getc(); 00120 if (inChar == 'a') { 00121 cMessage[0] = inChar; 00122 for (int i = 1; i<12; i++) { 00123 inChar = (char)srf.getc(); 00124 if( inChar == 'a' ) 00125 return; // out of sync so abort and pick it up next time round 00126 cMessage[i] = inChar; 00127 } 00128 cMessage[12]=0; 00129 processMessage(); 00130 } else 00131 srf.getc(); // throw away the character 00132 } 00133 } 00134 */ 00135 00136 void LLAPSerial::sendMessage(char *sToSend) 00137 { 00138 cMessage[0] = 'a'; 00139 cMessage[1] = deviceId[0]; 00140 cMessage[2] = deviceId[1]; 00141 for (int i = 0; i<9; i++) { 00142 if (i < strlen(sToSend) ) 00143 cMessage[i+3] = sToSend[i]; 00144 else 00145 cMessage[i+3] = '-'; 00146 } 00147 00148 srf.printf("%s",cMessage); 00149 } 00150 00151 void LLAPSerial::sendMessage(char* sToSend, char* valueToSend) 00152 { 00153 cMessage[0] = 'a'; 00154 cMessage[1] = deviceId[0]; 00155 cMessage[2] = deviceId[1]; 00156 for (int i = 0; i<9; i++) { 00157 if (i < strlen(sToSend)) 00158 cMessage[i+3] = sToSend[i]; 00159 else if (i < strlen(sToSend) + strlen(valueToSend)) 00160 cMessage[i+3] = valueToSend[i - strlen(sToSend)]; 00161 else 00162 cMessage[i+3] = '-'; 00163 } 00164 00165 srf.printf("%s", cMessage); 00166 } 00167 00168 00169 void LLAPSerial::sendInt(char *sToSend, int value) 00170 { 00171 char cValue[7]; // long enough for -32767 and the trailing zero 00172 sprintf( cValue,"%d", value ); 00173 int cValuePtr = 0; 00174 00175 cMessage[0] = 'a'; 00176 cMessage[1] = deviceId[0]; 00177 cMessage[2] = deviceId[1]; 00178 for (int i = 0; i<9; i++) { 00179 if (i < strlen(sToSend)) 00180 cMessage[i+3] = sToSend[i]; 00181 else if (cValuePtr < 7 && cValue[cValuePtr] !=0) 00182 cMessage[i+3] = cValue[cValuePtr++]; 00183 else 00184 cMessage[i+3] = '-'; 00185 } 00186 00187 srf.printf("%s",cMessage); 00188 } 00189 00190 void LLAPSerial::sendIntWithDP(char *sToSend, int value, int decimalPlaces) 00191 { 00192 char cValue[8]; // long enough for -3276.7 and the trailing zero 00193 int cValuePtr=0; 00194 //itoa(value, cValue,10); 00195 sprintf( cValue,"%d", value ); 00196 char* cp = &cValue[strlen(cValue)]; 00197 *(cp+1) = 0; // new terminator 00198 while (decimalPlaces-- && --cp ) { 00199 *(cp+1) = *cp; 00200 } 00201 *cp = '.'; 00202 00203 cMessage[0] = 'a'; 00204 cMessage[1] = deviceId[0]; 00205 cMessage[2] = deviceId[1]; 00206 for (int i = 0; i<9; i++) { 00207 if (i < strlen(sToSend)) 00208 cMessage[i+3] = sToSend[i]; 00209 else if (cValuePtr < 8 && cValue[cValuePtr] !=0) 00210 cMessage[i+3] = cValue[cValuePtr++]; 00211 else 00212 cMessage[i+3] = '-'; 00213 } 00214 00215 srf.printf("%s", cMessage); 00216 } 00217 00218 void LLAPSerial::setDeviceId(char* cId) 00219 { 00220 deviceId[0] = cId[0]; 00221 deviceId[1] = cId[1]; 00222 }
Generated on Sat Jul 16 2022 11:26:12 by 1.7.2