CyberAtom C++ Library

2.5.0

Tutorial: Getting System Information

This tutorial guides through construction of CyberAtom API objects and retrieving system information (model and firmware version) from AHRS device connected to PC via USB cable.

The API library uses concept of a logical device object that represents connected device in user's code. This is what cyberatom::Device class is for. Once instantiated, plays central role in device-to-application communication.

In order to construct a logical device object, references to two existing interfaces needs to be provided:

Constructing Connection Object

To use connection via USB cable, cyberatom::UsbConnection object can be created:

Constructing Response Handler

Any implementation of cyberatom::IResponse interface can be used with cyberatom::Device class. If you are interested in receiving only selected types of data from the device, subclassing cyberatom::Response class is probably the easiest thing to do.

E.g. to receive and print system information, you can define your response class like that:

class Response : public cyberatom::Response
{
public:
std::string model;
std::string firmwareVersion;
bool retrieved = false;
// Let's override only sysInfo method
virtual void sysInfo(const char* modelData, const char* firmwareVersionData) override
{
retrieved = true;
model = std::string(modelData);
firmwareVersion = std::string(firmwareVersionData);
}
};

and then in the main function you are free to instantiate it with:

Response response;

Retrieving Data from Device

Once this is done, you can create logical device object:

cyberatom::Device device(connection, response);

With this device object, you are now free to request for various information, e.g system details, like:

device.getSysInfo();

This method call sends request to the device. Now wee also need a way to obtain the information device is sending back. For that we need to use checkForMessages() method, in a periodic loop until the message we expect arrives:

while (!response.retrieved)
{
// busy waiting for response.
std::this_thread::sleep_for(std::chrono::microseconds(1000));
device.checkForMessages();
}

When the system information message arrives from the device, the Response::sysInfo() method will be called back by the device object, which eventually will help to terminate the loop and print the retrieved details:

std::cout << "Model: " << response.model << ", Firmware: " << response.firmwareVersion << std::endl;

Complete Code

With adding some exception handling, the complete code can look like that:

#include <iostream>
#include <string>
#include "cyberatom.hpp"
#include <chrono>
#include <thread>
class Response : public cyberatom::Response
{
public:
std::string model;
std::string firmwareVersion;
bool retrieved = false;
// Let's override only sysInfo method
virtual void sysInfo(const char* modelData, const char* firmwareVersionData) override
{
retrieved = true;
model = std::string(modelData);
firmwareVersion = std::string(firmwareVersionData);
}
};
int main()
{
Response response;
try
{
cyberatom::Device device(connection, response);
// request sending system information from the device
device.getSysInfo();
while (!response.retrieved)
{
// busy waiting for response.
std::this_thread::sleep_for(std::chrono::microseconds(1000));
device.checkForMessages();
}
std::cout << "Model: " << response.model << ", Firmware: " << response.firmwareVersion << std::endl;
}
{
std::cout << "Cannot find CyberAtom USB device." << std::endl;
return -1;
}
{
std::cout << "Cannot open CyberAtom USB device." << std::endl;
return -1;
}
return 0;
}

Summary

This tutorial demonstrates how-to retrieve system information from the CyberAtom AHRS device. The technique is based on using central cyberatom::Device class and implementation of cyberatom::IResponse interface as a handler for incomming data. This is the pattern that works for almost any data retrieval scenario.

Other methods of cyberatom::Device class allows to send data to the device in one-directional way, which is event simpler and does not require implementing own api::IResponse implementation.