making a function using SoftwareSerial & XBee instances
i trying build function in cpp file uses softwareserial , xbee library functions within it. have created following code without compilation errors , seems work fine; wondering if there another/better way improve code?
header file:
cpp file:
sketch file:
thanks in advance
header file:
code: [select]
#ifndef test_h
#define test_h
#include <arduino.h>
#include <softwareserial.h>
#include <xbee.h>
void processdata(xbee *myxbee, zbrxresponse *myrx, softwareserial *mynss);
#endif
cpp file:
code: [select]
#include "test.h"
void processdata(xbee *myxbee, zbrxresponse *myrx, softwareserial *mynss){
myxbee->readpacket();
if (myxbee->getresponse().isavailable()) {
// got something
if (myxbee->getresponse().getapiid() == zb_rx_response) {
// got zb rx packet
// fill our zb rx class
myxbee->getresponse().getzbrxresponse(*myrx);
if (myrx->getoption() == zb_packet_acknowledged) {
// sender got ack
mynss->println("packet acknowledged:");
} else {
mynss->println("packet not acknowledged");
}
(int cnt = 0; cnt < myrx->getdatalength(); cnt++) {
mynss->write(myrx->getdata()[cnt]);
}
mynss->println("\n");
}
} else if (myxbee->getresponse().iserror()) {
mynss->print("oh no!!! error code:");
mynss->println(myxbee->getresponse().geterrorcode());
}
}
sketch file:
code: [select]
#include "test.h"
#include <softwareserial.h>
#include <xbee.h>
xbee xbee = xbee();
zbrxresponse rx = zbrxresponse();
// define newsoftserial tx/rx pins
// connect arduino pin 8 tx of usb-serial device
uint8_t ssrx = 8;
// connect arduino pin 9 rx of usb-serial device
uint8_t sstx = 9;
softwareserial nss(ssrx, sstx);
void setup() {
serial.begin(57600);
xbee.setserial(serial);
nss.begin(57600);
nss.println("starting up!");
}
void loop() {
processdata(&xbee, &rx, &nss);
}
thanks in advance
you might want pass reference versus pointer no big deal
Arduino Forum > Using Arduino > Programming Questions > making a function using SoftwareSerial & XBee instances
arduino
Comments
Post a Comment