69 lines
1.5 KiB
C++
69 lines
1.5 KiB
C++
|
#include "xbee_wrapper.h"
|
||
|
|
||
|
|
||
|
XBeeWrapper::XBeeWrapper(){ xbee = XBee(); };
|
||
|
|
||
|
|
||
|
void XBeeWrapper::begin(unsigned long baud){
|
||
|
xbee.setSerial(Serial1);
|
||
|
Serial1.begin(baud);
|
||
|
};
|
||
|
|
||
|
|
||
|
uint8_t XBeeWrapper::receive(Packet& pkt){
|
||
|
xbee.readPacket();
|
||
|
|
||
|
if( !xbee.getResponse().isAvailable() )
|
||
|
return XBWRECV_NONE;
|
||
|
|
||
|
uint8_t apiId = xbee.getResponse().getApiId();
|
||
|
if( apiId != RX_16_RESPONSE && apiId != RX_64_RESPONSE )
|
||
|
return XBWRECV_NONE;
|
||
|
|
||
|
if( apiId == RX_16_RESPONSE ){
|
||
|
|
||
|
Rx16Response res;
|
||
|
xbee.getResponse().getRx16Response(res);
|
||
|
|
||
|
// extract data and propagate error
|
||
|
if( !pkt.read(res.getData(), res.getDataLength()) )
|
||
|
return XBWRECV_ERROR;
|
||
|
|
||
|
} else {
|
||
|
|
||
|
Rx64Response res;
|
||
|
xbee.getResponse().getRx64Response(res);
|
||
|
|
||
|
// extract data and propagate error
|
||
|
if( !pkt.read(res.getData(), res.getDataLength()) )
|
||
|
return XBWRECV_ERROR;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
return XBWRECV_OK;
|
||
|
};
|
||
|
|
||
|
uint8_t XBeeWrapper::broadcast(Packet& pkt){
|
||
|
// build broadcast addr
|
||
|
XBeeAddress64 bcast = XBeeAddress64(0x00000000, 0x0000FFFF);
|
||
|
|
||
|
// build payload from packet
|
||
|
uint8_t payload[PROTO_SIZE];
|
||
|
size_t payload_size = pkt.write(payload);
|
||
|
|
||
|
// send
|
||
|
Tx64Request tx = Tx64Request(bcast, payload, payload_size);
|
||
|
xbee.send(tx);
|
||
|
|
||
|
// // check status
|
||
|
// TxStatusResponse txStatus = TxStatusResponse();
|
||
|
// xbee.readPacket();
|
||
|
// if( xbee.getResponse().isAvailable() ){
|
||
|
// if( xbee.getResponse().getApiId() == TX_STATUS_RESPONSE ){
|
||
|
// xbee.getResponse().getTxStatusResponse(txStatus);
|
||
|
// return txStatus.isSuccess() ? XBWSEND_OK : XBWSEND_ERROR;
|
||
|
// }
|
||
|
// }
|
||
|
return XBWSEND_OK;
|
||
|
};
|