Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
XBee_Robot.cpp@2:5040ec01dba1, 2016-01-04 (annotated)
- Committer:
- sleighton
- Date:
- Mon Jan 04 19:56:44 2016 +0000
- Revision:
- 2:5040ec01dba1
- Child:
- 3:cf539cfd3d59
Working with serial interrupt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sleighton | 2:5040ec01dba1 | 1 | #include "XBee_Robot.h" |
sleighton | 2:5040ec01dba1 | 2 | #include <vector> |
sleighton | 2:5040ec01dba1 | 3 | |
sleighton | 2:5040ec01dba1 | 4 | XBee_Robot::XBee_Robot(PinName _txIn, PinName _rxIn): dataLink(_txIn,_rxIn) |
sleighton | 2:5040ec01dba1 | 5 | { |
sleighton | 2:5040ec01dba1 | 6 | _tx = _txIn; //redundant |
sleighton | 2:5040ec01dba1 | 7 | _rx = _rxIn; |
sleighton | 2:5040ec01dba1 | 8 | } |
sleighton | 2:5040ec01dba1 | 9 | |
sleighton | 2:5040ec01dba1 | 10 | void XBee_Robot::setRxInterrupt() |
sleighton | 2:5040ec01dba1 | 11 | { |
sleighton | 2:5040ec01dba1 | 12 | dataLink.attach(this,&XBee_Robot::Rx_interrupt, Serial::RxIrq); |
sleighton | 2:5040ec01dba1 | 13 | } |
sleighton | 2:5040ec01dba1 | 14 | |
sleighton | 2:5040ec01dba1 | 15 | void XBee_Robot::Rx_interrupt() |
sleighton | 2:5040ec01dba1 | 16 | { |
sleighton | 2:5040ec01dba1 | 17 | while(dataLink.readable()){ |
sleighton | 2:5040ec01dba1 | 18 | printf("%c",dataLink.getc()); |
sleighton | 2:5040ec01dba1 | 19 | } |
sleighton | 2:5040ec01dba1 | 20 | } |
sleighton | 2:5040ec01dba1 | 21 | |
sleighton | 2:5040ec01dba1 | 22 | void XBee_Robot::transmitRequest(uint8_t *BitAddress64, uint8_t *BitAddress16, uint8_t broadcastRadius, uint8_t options, uint8_t *data,size_t dataLength) |
sleighton | 2:5040ec01dba1 | 23 | { |
sleighton | 2:5040ec01dba1 | 24 | //calculate checksum |
sleighton | 2:5040ec01dba1 | 25 | uint16_t length = 0x0E + dataLength; //calculate length of packet (14 + data length) |
sleighton | 2:5040ec01dba1 | 26 | uint8_t lengthu = length >>8; //upper 8 bits |
sleighton | 2:5040ec01dba1 | 27 | uint8_t lengthl = length & 0xFF; //lower 8 bits |
sleighton | 2:5040ec01dba1 | 28 | |
sleighton | 2:5040ec01dba1 | 29 | |
sleighton | 2:5040ec01dba1 | 30 | std::vector<uint8_t> transmitRequestPacket; //create new vector packet |
sleighton | 2:5040ec01dba1 | 31 | //populate packet |
sleighton | 2:5040ec01dba1 | 32 | transmitRequestPacket.push_back(0x7E); //start delimeter |
sleighton | 2:5040ec01dba1 | 33 | transmitRequestPacket.push_back(lengthu); //upper byte of length |
sleighton | 2:5040ec01dba1 | 34 | transmitRequestPacket.push_back(lengthl); //lower byte of length |
sleighton | 2:5040ec01dba1 | 35 | transmitRequestPacket.push_back(0x10); //API ID (transmit request) |
sleighton | 2:5040ec01dba1 | 36 | transmitRequestPacket.push_back(0x01); //channel ID |
sleighton | 2:5040ec01dba1 | 37 | transmitRequestPacket.insert(transmitRequestPacket.end(), BitAddress64, BitAddress64+8); //64 bit destination address |
sleighton | 2:5040ec01dba1 | 38 | transmitRequestPacket.insert(transmitRequestPacket.end(), BitAddress16, BitAddress16+2); //16 bit network address |
sleighton | 2:5040ec01dba1 | 39 | transmitRequestPacket.push_back(broadcastRadius); //broadcast radius (0 = max hops) |
sleighton | 2:5040ec01dba1 | 40 | transmitRequestPacket.push_back(options); //additional options for packet |
sleighton | 2:5040ec01dba1 | 41 | transmitRequestPacket.insert(transmitRequestPacket.end(), data, data+dataLength); //data |
sleighton | 2:5040ec01dba1 | 42 | uint8_t checksum = calculateChecksum(transmitRequestPacket); |
sleighton | 2:5040ec01dba1 | 43 | transmitRequestPacket.push_back(checksum); //calculate and add checksum |
sleighton | 2:5040ec01dba1 | 44 | |
sleighton | 2:5040ec01dba1 | 45 | for (int i = 0; i < transmitRequestPacket.size(); i++){ |
sleighton | 2:5040ec01dba1 | 46 | dataLink.printf("%c",transmitRequestPacket[i]); //send packet |
sleighton | 2:5040ec01dba1 | 47 | printf("%c",transmitRequestPacket[i]); |
sleighton | 2:5040ec01dba1 | 48 | } |
sleighton | 2:5040ec01dba1 | 49 | } |
sleighton | 2:5040ec01dba1 | 50 | |
sleighton | 2:5040ec01dba1 | 51 | uint8_t XBee_Robot::calculateChecksum(std::vector<uint8_t> & packet) |
sleighton | 2:5040ec01dba1 | 52 | { |
sleighton | 2:5040ec01dba1 | 53 | uint8_t checksum = 0xFF; //start with FF as last byte of sum is subtracted from FF |
sleighton | 2:5040ec01dba1 | 54 | for (int i = 3; i < packet.size(); i++) |
sleighton | 2:5040ec01dba1 | 55 | checksum -= packet[i]; |
sleighton | 2:5040ec01dba1 | 56 | return checksum; |
sleighton | 2:5040ec01dba1 | 57 | |
sleighton | 2:5040ec01dba1 | 58 | } |