attie / libxbee3

A C/C++ library to aid the use of Digi XBee radios in API mode
GNU Lesser General Public License v3.0
68 stars 40 forks source link

Update main.cpp #6

Closed RengaPD closed 9 years ago

RengaPD commented 9 years ago

hi attie, even if it's quite simple i spent much time first to understand the proper way to do that, so i propose you to add this little comment if i wrote it correctly.

attie commented 9 years ago

It would be better to make use of the C++ interface...

libxbee::Con has the operator << setup for you:

EXPORT unsigned char operator<< (std::vector<unsigned char> data);

You'd do something like this:

std::string command;
std::vector<unsigned char> data;

// AT command D4 - targeting Digital Pin 4
command = "D4";
std::copy(command.begin(), command.end(), std::back_inserter(data));

// Parameter 0x05, means 'output high'
data.push_back(0x05);

mycon << data

I admit that it's a little unwieldy, but you would wrap this into a function, and call that instead... leaving you with a nice clean interface like this:

mycon << build_io_command(4, OUTPUT_HIGH);
attie commented 9 years ago

PS: I've had a think about this some more, and my previous example can be significantly simplified...

Something like this would be much better, and I'll add a comment to the sample:

mycon << "D4" + std::string(1, char(0x05))
attie commented 9 years ago

see ff3b1ba4115952841a6bf3c114336c71255f0a24