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/

Committer:
SomeRandomBloke
Date:
Tue Apr 15 22:02:18 2014 +0000
Revision:
0:c1b97c30cbc5
Child:
1:8f3ec117823d
Initial commit, needs documenting properly.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:c1b97c30cbc5 12 */
SomeRandomBloke 0:c1b97c30cbc5 13
SomeRandomBloke 0:c1b97c30cbc5 14 #include "mbed.h"
SomeRandomBloke 0:c1b97c30cbc5 15 #include "LLAPSerial.h"
SomeRandomBloke 0:c1b97c30cbc5 16
SomeRandomBloke 0:c1b97c30cbc5 17
SomeRandomBloke 0:c1b97c30cbc5 18 // Constructors to pass in Tx/Rx pins and optional ID, default is -- as defined in LLAPSerial.h
SomeRandomBloke 0:c1b97c30cbc5 19 LLAPSerial::LLAPSerial(PinName txPin, PinName rxPin, char *dID) : srf(txPin, rxPin)
SomeRandomBloke 0:c1b97c30cbc5 20 {
SomeRandomBloke 0:c1b97c30cbc5 21 srf.baud(115200);
SomeRandomBloke 0:c1b97c30cbc5 22 bMsgReceived = false;
SomeRandomBloke 0:c1b97c30cbc5 23 setDeviceId(dID);
SomeRandomBloke 0:c1b97c30cbc5 24 cMessage[12]=0; // ensure terminated
SomeRandomBloke 0:c1b97c30cbc5 25 inPtr = cMessage;
SomeRandomBloke 0:c1b97c30cbc5 26 // Attach the receive interrupt handler
SomeRandomBloke 0:c1b97c30cbc5 27 srf.attach( this,&LLAPSerial::SerialEvent );
SomeRandomBloke 0:c1b97c30cbc5 28 }
SomeRandomBloke 0:c1b97c30cbc5 29
SomeRandomBloke 0:c1b97c30cbc5 30
SomeRandomBloke 0:c1b97c30cbc5 31 void LLAPSerial::processMessage()
SomeRandomBloke 0:c1b97c30cbc5 32 {
SomeRandomBloke 0:c1b97c30cbc5 33 //if (LLAP.cMessage[0] != 'a') return; //not needed as already checked
SomeRandomBloke 0:c1b97c30cbc5 34 // if (cMessage[1] != deviceId[0]) return;
SomeRandomBloke 0:c1b97c30cbc5 35 // if (cMessage[2] != deviceId[1]) return;
SomeRandomBloke 0:c1b97c30cbc5 36 // now we have LLAP.cMessage[3] to LLAP.cMessage[11] as the actual message
SomeRandomBloke 0:c1b97c30cbc5 37 if (0 == strncmp(&cMessage[3],"HELLO----",9)) {
SomeRandomBloke 0:c1b97c30cbc5 38 srf.printf("%s",cMessage); // echo the message
SomeRandomBloke 0:c1b97c30cbc5 39 return;
SomeRandomBloke 0:c1b97c30cbc5 40 } else if (0 == strncmp(&cMessage[3],"CHDEVID",7)) {
SomeRandomBloke 0:c1b97c30cbc5 41 if (strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[10]) != 0 && strchr("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ", cMessage[11]) != 0) {
SomeRandomBloke 0:c1b97c30cbc5 42 deviceId[0] = cMessage[10];
SomeRandomBloke 0:c1b97c30cbc5 43 deviceId[1] = cMessage[11];
SomeRandomBloke 0:c1b97c30cbc5 44 srf.printf( "%s", cMessage); // echo the message
SomeRandomBloke 0:c1b97c30cbc5 45 }
SomeRandomBloke 0:c1b97c30cbc5 46 } else {
SomeRandomBloke 0:c1b97c30cbc5 47 strncpy(sMessage, &cMessage[0], 12); // let the main program deal with it
SomeRandomBloke 0:c1b97c30cbc5 48 bMsgReceived = true;
SomeRandomBloke 0:c1b97c30cbc5 49 }
SomeRandomBloke 0:c1b97c30cbc5 50 }
SomeRandomBloke 0:c1b97c30cbc5 51
SomeRandomBloke 0:c1b97c30cbc5 52 void LLAPSerial::SerialEvent( void )
SomeRandomBloke 0:c1b97c30cbc5 53 {
SomeRandomBloke 0:c1b97c30cbc5 54 if (bMsgReceived) return; // get out if previous message not yet processed
SomeRandomBloke 0:c1b97c30cbc5 55 if (srf.readable() ) {
SomeRandomBloke 0:c1b97c30cbc5 56 // get the new char:
SomeRandomBloke 0:c1b97c30cbc5 57 char inChar = (char)srf.getc();
SomeRandomBloke 0:c1b97c30cbc5 58 if (inChar == 'a') {
SomeRandomBloke 0:c1b97c30cbc5 59 // Start of a new message
SomeRandomBloke 0:c1b97c30cbc5 60 inPtr = cMessage;
SomeRandomBloke 0:c1b97c30cbc5 61 }
SomeRandomBloke 0:c1b97c30cbc5 62 *inPtr++ = inChar;
SomeRandomBloke 0:c1b97c30cbc5 63 if( inPtr >= &cMessage[12]) {
SomeRandomBloke 0:c1b97c30cbc5 64 // Message received, terminate, process and reset pointer
SomeRandomBloke 0:c1b97c30cbc5 65 *inPtr = '\0';
SomeRandomBloke 0:c1b97c30cbc5 66 processMessage();
SomeRandomBloke 0:c1b97c30cbc5 67 inPtr = cMessage;
SomeRandomBloke 0:c1b97c30cbc5 68 }
SomeRandomBloke 0:c1b97c30cbc5 69 }
SomeRandomBloke 0:c1b97c30cbc5 70 }
SomeRandomBloke 0:c1b97c30cbc5 71
SomeRandomBloke 0:c1b97c30cbc5 72 /*
SomeRandomBloke 0:c1b97c30cbc5 73 void LLAPSerial::SerialEvent( void )
SomeRandomBloke 0:c1b97c30cbc5 74 {
SomeRandomBloke 0:c1b97c30cbc5 75 if (bMsgReceived) return; // get out if previous message not yet processed
SomeRandomBloke 0:c1b97c30cbc5 76 if (srf.readable() ) {
SomeRandomBloke 0:c1b97c30cbc5 77 // get the new byte:
SomeRandomBloke 0:c1b97c30cbc5 78 char inChar = (char)srf.getc();
SomeRandomBloke 0:c1b97c30cbc5 79 if (inChar == 'a') {
SomeRandomBloke 0:c1b97c30cbc5 80 cMessage[0] = inChar;
SomeRandomBloke 0:c1b97c30cbc5 81 for (int i = 1; i<12; i++) {
SomeRandomBloke 0:c1b97c30cbc5 82 inChar = (char)srf.getc();
SomeRandomBloke 0:c1b97c30cbc5 83 if( inChar == 'a' )
SomeRandomBloke 0:c1b97c30cbc5 84 return; // out of sync so abort and pick it up next time round
SomeRandomBloke 0:c1b97c30cbc5 85 cMessage[i] = inChar;
SomeRandomBloke 0:c1b97c30cbc5 86 }
SomeRandomBloke 0:c1b97c30cbc5 87 cMessage[12]=0;
SomeRandomBloke 0:c1b97c30cbc5 88 processMessage();
SomeRandomBloke 0:c1b97c30cbc5 89 } else
SomeRandomBloke 0:c1b97c30cbc5 90 srf.getc(); // throw away the character
SomeRandomBloke 0:c1b97c30cbc5 91 }
SomeRandomBloke 0:c1b97c30cbc5 92 }
SomeRandomBloke 0:c1b97c30cbc5 93 */
SomeRandomBloke 0:c1b97c30cbc5 94
SomeRandomBloke 0:c1b97c30cbc5 95 void LLAPSerial::sendMessage(char *sToSend)
SomeRandomBloke 0:c1b97c30cbc5 96 {
SomeRandomBloke 0:c1b97c30cbc5 97 cMessage[0] = 'a';
SomeRandomBloke 0:c1b97c30cbc5 98 cMessage[1] = deviceId[0];
SomeRandomBloke 0:c1b97c30cbc5 99 cMessage[2] = deviceId[1];
SomeRandomBloke 0:c1b97c30cbc5 100 for (int i = 0; i<9; i++) {
SomeRandomBloke 0:c1b97c30cbc5 101 if (i < strlen(sToSend) )
SomeRandomBloke 0:c1b97c30cbc5 102 cMessage[i+3] = sToSend[i];
SomeRandomBloke 0:c1b97c30cbc5 103 else
SomeRandomBloke 0:c1b97c30cbc5 104 cMessage[i+3] = '-';
SomeRandomBloke 0:c1b97c30cbc5 105 }
SomeRandomBloke 0:c1b97c30cbc5 106
SomeRandomBloke 0:c1b97c30cbc5 107 srf.printf("%s",cMessage);
SomeRandomBloke 0:c1b97c30cbc5 108 }
SomeRandomBloke 0:c1b97c30cbc5 109
SomeRandomBloke 0:c1b97c30cbc5 110 void LLAPSerial::sendMessage(char* sToSend, char* valueToSend)
SomeRandomBloke 0:c1b97c30cbc5 111 {
SomeRandomBloke 0:c1b97c30cbc5 112 cMessage[0] = 'a';
SomeRandomBloke 0:c1b97c30cbc5 113 cMessage[1] = deviceId[0];
SomeRandomBloke 0:c1b97c30cbc5 114 cMessage[2] = deviceId[1];
SomeRandomBloke 0:c1b97c30cbc5 115 for (int i = 0; i<9; i++) {
SomeRandomBloke 0:c1b97c30cbc5 116 if (i < strlen(sToSend))
SomeRandomBloke 0:c1b97c30cbc5 117 cMessage[i+3] = sToSend[i];
SomeRandomBloke 0:c1b97c30cbc5 118 else if (i < strlen(sToSend) + strlen(valueToSend))
SomeRandomBloke 0:c1b97c30cbc5 119 cMessage[i+3] = valueToSend[i - strlen(sToSend)];
SomeRandomBloke 0:c1b97c30cbc5 120 else
SomeRandomBloke 0:c1b97c30cbc5 121 cMessage[i+3] = '-';
SomeRandomBloke 0:c1b97c30cbc5 122 }
SomeRandomBloke 0:c1b97c30cbc5 123
SomeRandomBloke 0:c1b97c30cbc5 124 srf.printf("%s", cMessage);
SomeRandomBloke 0:c1b97c30cbc5 125 }
SomeRandomBloke 0:c1b97c30cbc5 126
SomeRandomBloke 0:c1b97c30cbc5 127
SomeRandomBloke 0:c1b97c30cbc5 128 void LLAPSerial::sendInt(char *sToSend, int value)
SomeRandomBloke 0:c1b97c30cbc5 129 {
SomeRandomBloke 0:c1b97c30cbc5 130 char cValue[7]; // long enough for -32767 and the trailing zero
SomeRandomBloke 0:c1b97c30cbc5 131 sprintf( cValue,"%d", value );
SomeRandomBloke 0:c1b97c30cbc5 132 int cValuePtr = 0;
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 if (cValuePtr < 7 && cValue[cValuePtr] !=0)
SomeRandomBloke 0:c1b97c30cbc5 141 cMessage[i+3] = cValue[cValuePtr++];
SomeRandomBloke 0:c1b97c30cbc5 142 else
SomeRandomBloke 0:c1b97c30cbc5 143 cMessage[i+3] = '-';
SomeRandomBloke 0:c1b97c30cbc5 144 }
SomeRandomBloke 0:c1b97c30cbc5 145
SomeRandomBloke 0:c1b97c30cbc5 146 srf.printf("%s",cMessage);
SomeRandomBloke 0:c1b97c30cbc5 147 }
SomeRandomBloke 0:c1b97c30cbc5 148
SomeRandomBloke 0:c1b97c30cbc5 149 void LLAPSerial::sendIntWithDP(char *sToSend, int value, int decimalPlaces)
SomeRandomBloke 0:c1b97c30cbc5 150 {
SomeRandomBloke 0:c1b97c30cbc5 151 char cValue[8]; // long enough for -3276.7 and the trailing zero
SomeRandomBloke 0:c1b97c30cbc5 152 int cValuePtr=0;
SomeRandomBloke 0:c1b97c30cbc5 153 //itoa(value, cValue,10);
SomeRandomBloke 0:c1b97c30cbc5 154 sprintf( cValue,"%d", value );
SomeRandomBloke 0:c1b97c30cbc5 155 char* cp = &cValue[strlen(cValue)];
SomeRandomBloke 0:c1b97c30cbc5 156 *(cp+1) = 0; // new terminator
SomeRandomBloke 0:c1b97c30cbc5 157 while (decimalPlaces-- && --cp ) {
SomeRandomBloke 0:c1b97c30cbc5 158 *(cp+1) = *cp;
SomeRandomBloke 0:c1b97c30cbc5 159 }
SomeRandomBloke 0:c1b97c30cbc5 160 *cp = '.';
SomeRandomBloke 0:c1b97c30cbc5 161
SomeRandomBloke 0:c1b97c30cbc5 162 cMessage[0] = 'a';
SomeRandomBloke 0:c1b97c30cbc5 163 cMessage[1] = deviceId[0];
SomeRandomBloke 0:c1b97c30cbc5 164 cMessage[2] = deviceId[1];
SomeRandomBloke 0:c1b97c30cbc5 165 for (int i = 0; i<9; i++) {
SomeRandomBloke 0:c1b97c30cbc5 166 if (i < strlen(sToSend))
SomeRandomBloke 0:c1b97c30cbc5 167 cMessage[i+3] = sToSend[i];
SomeRandomBloke 0:c1b97c30cbc5 168 else if (cValuePtr < 8 && cValue[cValuePtr] !=0)
SomeRandomBloke 0:c1b97c30cbc5 169 cMessage[i+3] = cValue[cValuePtr++];
SomeRandomBloke 0:c1b97c30cbc5 170 else
SomeRandomBloke 0:c1b97c30cbc5 171 cMessage[i+3] = '-';
SomeRandomBloke 0:c1b97c30cbc5 172 }
SomeRandomBloke 0:c1b97c30cbc5 173
SomeRandomBloke 0:c1b97c30cbc5 174 srf.printf("%s", cMessage);
SomeRandomBloke 0:c1b97c30cbc5 175 }
SomeRandomBloke 0:c1b97c30cbc5 176
SomeRandomBloke 0:c1b97c30cbc5 177 void LLAPSerial::setDeviceId(char* cId)
SomeRandomBloke 0:c1b97c30cbc5 178 {
SomeRandomBloke 0:c1b97c30cbc5 179 deviceId[0] = cId[0];
SomeRandomBloke 0:c1b97c30cbc5 180 deviceId[1] = cId[1];
SomeRandomBloke 0:c1b97c30cbc5 181 }