examples: Remove heap allocation from C++ examples

Cleanup of UPM C++ examples.  Switched from heap allocation to
stack allocation when possible.  This simplifies the samples since it
removes the need for explicit memory management.  A script was used to
identify and replace pointer use.  To simplify the replace script, I
re-formatted the C++ examples using the UPM .clang-format file.
Unfortuantely this changes the look of the UPM C++ examples to a large
degree.  However, examples will now have a standard look/feel and
uniform formatting.

    * Ran clang-format w/provided UPM .clang-format file
    * Removed new's/delete's whenever possible (left those in interface
      examples)
    * Added IIO sensor library implementation of callback void* arg
    * Converted all sleeps to upm defined delays (added header when
      necessary)
    * Scrubbed CXX example includes

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck
2017-08-30 15:00:29 -07:00
committed by Mihai Tudor Panu
parent bd6e4ec786
commit 5cefe7f5f3
290 changed files with 7976 additions and 8520 deletions

View File

@ -22,145 +22,132 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <signal.h>
#include <string.h>
#include <string>
#include "hmtrp.hpp"
#include "upm_utilities.h"
using namespace std;
bool shouldRun = true;
void sig_handler(int signo)
void
sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
if (signo == SIGINT)
shouldRun = false;
}
void printUsage()
void
printUsage()
{
cout << "Usage:" << endl;
cout << "Pass a commandline argument (any argument) to this program"
<< endl;
cout << "to query the radio configuration and output it. NOTE: the"
<< endl;
cout << "radio must be in CONFIG mode for this to work."
<< endl;
cout << endl;
cout << "Running this program without arguments will simply transmit"
<< endl;
cout << "'Hello World!' every second, and output any data received from"
<< endl;
cout << "another radio."
<< endl;
cout << endl;
cout << "Usage:" << endl;
cout << "Pass a commandline argument (any argument) to this program" << endl;
cout << "to query the radio configuration and output it. NOTE: the" << endl;
cout << "radio must be in CONFIG mode for this to work." << endl;
cout << endl;
cout << "Running this program without arguments will simply transmit" << endl;
cout << "'Hello World!' every second, and output any data received from" << endl;
cout << "another radio." << endl;
cout << endl;
}
const size_t bufferLength = 256;
int main (int argc, char **argv)
int
main(int argc, char** argv)
{
signal(SIGINT, sig_handler);
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a HMTRP radio device on uart 0
//! [Interesting]
// Instantiate a HMTRP radio device on uart 0
upm::HMTRP* radio = new upm::HMTRP(0);
upm::HMTRP radio(0);
// make sure port is initialized properly. 9600 baud is the default.
if (!radio->setupTty(B9600))
{
cerr << "Failed to setup tty port parameters" << endl;
return 1;
// make sure port is initialized properly. 9600 baud is the default.
if (!radio.setupTty(B9600)) {
cerr << "Failed to setup tty port parameters" << endl;
return 1;
}
printUsage();
printUsage();
// By default, this radio simply transmits data sent via writeData()
// and reads any available data via readData().
// By default, this radio simply transmits data sent via writeData()
// and reads any available data via readData().
// It can be placed into a configuration mode by grounding the
// CONFIG pin on the module. When this is done, the various
// configuration query and config methods can be used. In this
// example, by default, we just read any data available fom the
// device, and periodically transmit "Hello World".
// It can be placed into a configuration mode by grounding the
// CONFIG pin on the module. When this is done, the various
// configuration query and config methods can be used. In this
// example, by default, we just read any data available fom the
// device, and periodically transmit "Hello World".
// If any argument was specified on the command line, do a simple
// configuration query and output the results. The radio must be in
// CONFIG mode for this to work.
// If any argument was specified on the command line, do a simple
// configuration query and output the results. The radio must be in
// CONFIG mode for this to work.
if (argc > 1)
{
// config mode
uint32_t freq;
uint32_t dataRate;
uint16_t rxBandwidth;
uint8_t modulation;
uint8_t txPower;
uint32_t uartBaud;
if (radio->getConfig(&freq, &dataRate, &rxBandwidth, &modulation,
&txPower, &uartBaud))
{
cout << "Radio configuration:" << endl;
cout << "freq: " << freq << " dataRate: " << dataRate
<< " rxBandwidth: " << rxBandwidth << "Khz" << endl;
cout << "modulation: " << int(modulation) << "Khz txPower: "
<< int(txPower) << " uartBaud: " << uartBaud << endl;
if (argc > 1) {
// config mode
uint32_t freq;
uint32_t dataRate;
uint16_t rxBandwidth;
uint8_t modulation;
uint8_t txPower;
uint32_t uartBaud;
if (radio.getConfig(&freq, &dataRate, &rxBandwidth, &modulation, &txPower, &uartBaud)) {
cout << "Radio configuration:" << endl;
cout << "freq: " << freq << " dataRate: " << dataRate << " rxBandwidth: " << rxBandwidth
<< "Khz" << endl;
cout << "modulation: " << int(modulation) << "Khz txPower: " << int(txPower)
<< " uartBaud: " << uartBaud << endl;
} else {
cerr << "getConfig() failed. Make sure the radio is in "
<< "CONFIG mode." << endl;
}
else
{
cerr << "getConfig() failed. Make sure the radio is in "
<< "CONFIG mode." << endl;
}
}
else
{
// normal read/write mode
char radioBuffer[bufferLength];
int counter = 0;
cout << "Running in normal read/write mode." << endl;
} else {
// normal read/write mode
char radioBuffer[bufferLength];
int counter = 0;
cout << "Running in normal read/write mode." << endl;
while (shouldRun)
{
// we don't want the read to block in this example, so always
// check to see if data is available first.
if (radio->dataAvailable())
{
memset(radioBuffer, 0, bufferLength);
int rv = radio->readData(radioBuffer, bufferLength - 1);
if (rv > 0)
cout << "Received: " << radioBuffer << endl;
if (rv < 0) // some sort of read error occurred
while (shouldRun) {
// we don't want the read to block in this example, so always
// check to see if data is available first.
if (radio.dataAvailable()) {
memset(radioBuffer, 0, bufferLength);
int rv = radio.readData(radioBuffer, bufferLength - 1);
if (rv > 0)
cout << "Received: " << radioBuffer << endl;
if (rv < 0) // some sort of read error occurred
{
cerr << "Port read error." << endl;
break;
cerr << "Port read error." << endl;
break;
}
continue;
continue;
}
usleep(100000); // 100ms
counter++;
// every second, transmit "Hello World"
if (counter > 10)
{
static const char *hello = "Hello World!";
cout << "Transmitting hello world..." << endl;
radio->writeData((char *)hello, strlen(hello) + 1);
counter = 0;
upm_delay_us(100000); // 100ms
counter++;
// every second, transmit "Hello World"
if (counter > 10) {
static const char* hello = "Hello World!";
cout << "Transmitting hello world..." << endl;
radio.writeData((char*) hello, strlen(hello) + 1);
counter = 0;
}
}
}
//! [Interesting]
//! [Interesting]
cout << "Exiting..." << endl;
cout << "Exiting..." << endl;
delete radio;
return 0;
return 0;
}