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/
Diff: LLAPSerial.cpp
- Revision:
- 0:c1b97c30cbc5
- Child:
- 1:8f3ec117823d
diff -r 000000000000 -r c1b97c30cbc5 LLAPSerial.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LLAPSerial.cpp Tue Apr 15 22:02:18 2014 +0000 @@ -0,0 +1,181 @@ +/** LLAP Serial for use with Ciseco SRF/XRF wireless modules + * Wireless modules available at http://shop.ciseco.co.uk/rf-module-range/ + * Library developped with ST Micro Nucleo F401 and Ciseco SRF shield. + * Sheild needs to be modified as Tx/Rx on pins 0 and 1 conflict with the USB debug port. + * They need linking to Rx - PA_12, Tx - PA_11 on the outer row of pins. + * See http://mbed.org/platforms/ST-Nucleo-F401RE/ for pinouts. + * + * This code is based on the Ciseco LLAPSerial library for Arduino at https://github.com/CisecoPlc/LLAPSerial but updated to + * work as a mbed library + * + * Converted and updated by Andrew Lindsay @AndrewDLindsay April 2014 + */ + +#include "mbed.h" +#include "LLAPSerial.h" + + +// Constructors to pass in Tx/Rx pins and optional ID, default is -- as defined in LLAPSerial.h +LLAPSerial::LLAPSerial(PinName txPin, PinName rxPin, char *dID) : srf(txPin, rxPin) +{ + srf.baud(115200); + bMsgReceived = false; + setDeviceId(dID); + cMessage[12]=0; // ensure terminated + inPtr = cMessage; + // Attach the receive interrupt handler + srf.attach( this,&LLAPSerial::SerialEvent ); +} + + +void LLAPSerial::processMessage() +{ + //if (LLAP.cMessage[0] != 'a') return; //not needed as already checked +// if (cMessage[1] != deviceId[0]) return; +// if (cMessage[2] != deviceId[1]) return; + // now we have LLAP.cMessage[3] to LLAP.cMessage[11] as the actual message + if (0 == strncmp(&cMessage[3],"HELLO----",9)) { + srf.printf("%s",cMessage); // echo the message + return; + } else if (0 == strncmp(&cMessage[3],"CHDEVID",7)) { + if (strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[10]) != 0 && strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[11]) != 0) { + deviceId[0] = cMessage[10]; + deviceId[1] = cMessage[11]; + srf.printf( "%s", cMessage); // echo the message + } + } else { + strncpy(sMessage, &cMessage[0], 12); // let the main program deal with it + bMsgReceived = true; + } +} + +void LLAPSerial::SerialEvent( void ) +{ + if (bMsgReceived) return; // get out if previous message not yet processed + if (srf.readable() ) { + // get the new char: + char inChar = (char)srf.getc(); + if (inChar == 'a') { + // Start of a new message + inPtr = cMessage; + } + *inPtr++ = inChar; + if( inPtr >= &cMessage[12]) { + // Message received, terminate, process and reset pointer + *inPtr = '\0'; + processMessage(); + inPtr = cMessage; + } + } +} + +/* +void LLAPSerial::SerialEvent( void ) +{ + if (bMsgReceived) return; // get out if previous message not yet processed + if (srf.readable() ) { + // get the new byte: + char inChar = (char)srf.getc(); + if (inChar == 'a') { + cMessage[0] = inChar; + for (int i = 1; i<12; i++) { + inChar = (char)srf.getc(); + if( inChar == 'a' ) + return; // out of sync so abort and pick it up next time round + cMessage[i] = inChar; + } + cMessage[12]=0; + processMessage(); + } else + srf.getc(); // throw away the character + } +} +*/ + +void LLAPSerial::sendMessage(char *sToSend) +{ + cMessage[0] = 'a'; + cMessage[1] = deviceId[0]; + cMessage[2] = deviceId[1]; + for (int i = 0; i<9; i++) { + if (i < strlen(sToSend) ) + cMessage[i+3] = sToSend[i]; + else + cMessage[i+3] = '-'; + } + + srf.printf("%s",cMessage); +} + +void LLAPSerial::sendMessage(char* sToSend, char* valueToSend) +{ + cMessage[0] = 'a'; + cMessage[1] = deviceId[0]; + cMessage[2] = deviceId[1]; + for (int i = 0; i<9; i++) { + if (i < strlen(sToSend)) + cMessage[i+3] = sToSend[i]; + else if (i < strlen(sToSend) + strlen(valueToSend)) + cMessage[i+3] = valueToSend[i - strlen(sToSend)]; + else + cMessage[i+3] = '-'; + } + + srf.printf("%s", cMessage); +} + + +void LLAPSerial::sendInt(char *sToSend, int value) +{ + char cValue[7]; // long enough for -32767 and the trailing zero + sprintf( cValue,"%d", value ); + int cValuePtr = 0; + + cMessage[0] = 'a'; + cMessage[1] = deviceId[0]; + cMessage[2] = deviceId[1]; + for (int i = 0; i<9; i++) { + if (i < strlen(sToSend)) + cMessage[i+3] = sToSend[i]; + else if (cValuePtr < 7 && cValue[cValuePtr] !=0) + cMessage[i+3] = cValue[cValuePtr++]; + else + cMessage[i+3] = '-'; + } + + srf.printf("%s",cMessage); +} + +void LLAPSerial::sendIntWithDP(char *sToSend, int value, int decimalPlaces) +{ + char cValue[8]; // long enough for -3276.7 and the trailing zero + int cValuePtr=0; + //itoa(value, cValue,10); + sprintf( cValue,"%d", value ); + char* cp = &cValue[strlen(cValue)]; + *(cp+1) = 0; // new terminator + while (decimalPlaces-- && --cp ) { + *(cp+1) = *cp; + } + *cp = '.'; + + cMessage[0] = 'a'; + cMessage[1] = deviceId[0]; + cMessage[2] = deviceId[1]; + for (int i = 0; i<9; i++) { + if (i < strlen(sToSend)) + cMessage[i+3] = sToSend[i]; + else if (cValuePtr < 8 && cValue[cValuePtr] !=0) + cMessage[i+3] = cValue[cValuePtr++]; + else + cMessage[i+3] = '-'; + } + + srf.printf("%s", cMessage); +} + +void LLAPSerial::setDeviceId(char* cId) +{ + deviceId[0] = cId[0]; + deviceId[1] = cId[1]; +}