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,100 +22,89 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <string>
#include "sx1276.hpp"
#include "upm_utilities.h"
using namespace std;
int shouldRun = true;
void sig_handler(int signo)
void
sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
int
main(int argc, char** argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
signal(SIGINT, sig_handler);
//! [Interesting]
cout << "Specify an argument to go into receive mode. Default is transmit"
<< endl;
cout << "Specify an argument to go into receive mode. Default is transmit" << endl;
bool rx = false;
if (argc > 1)
rx = true;
bool rx = false;
if (argc > 1)
rx = true;
// Instantiate an SX1276 using default parameters
upm::SX1276 *sensor = new upm::SX1276();
// Instantiate an SX1276 using default parameters
upm::SX1276 sensor;
// 915Mhz
sensor->setChannel(915000000);
// 915Mhz
sensor.setChannel(915000000);
// FSK configuration (rx and tx must be configured the same):
// Tx output power = 14 dBm
// FSK freq deviation = 25000 Hz
// FSK bandwidth = 50000 bps
// FSK AFC bandwidth = 83333 Hz
// FSK datarate = 50000 bps
// FSK preamble len = 5
// FSK fixed length payload = false
// FSK CRC check = true
// FSK (rx) continuous Rx mode = false
// FSK configuration (rx and tx must be configured the same):
// Tx output power = 14 dBm
// FSK freq deviation = 25000 Hz
// FSK bandwidth = 50000 bps
// FSK AFC bandwidth = 83333 Hz
// FSK datarate = 50000 bps
// FSK preamble len = 5
// FSK fixed length payload = false
// FSK CRC check = true
// FSK (rx) continuous Rx mode = false
sensor->setTxConfig(sensor->MODEM_FSK, 14, 25000, 0,
50000, 0, 5, false, true, false, 0, false);
sensor->setRxConfig(sensor->MODEM_FSK, 50000, 50000,
0, 83333, 5, 0, false, 0, true,
false, 0, false, true);
sensor.setTxConfig(sensor.MODEM_FSK, 14, 25000, 0, 50000, 0, 5, false, true, false, 0, false);
int count = 0;
int buflen = 64;
char buffer[buflen];
sensor.setRxConfig(
sensor.MODEM_FSK, 50000, 50000, 0, 83333, 5, 0, false, 0, true, false, 0, false, true);
while (shouldRun)
{
if (!rx)
{
snprintf(buffer, buflen, "Ping %d", count++);
cout << "Sending..." << std::string(buffer) << endl;
sensor->sendStr(string(buffer), 3000);
sensor->setSleep();
sleep(1);
}
else
{
// receiving
cout << "Attempting to receive..." << endl;
int rv;
if ((rv = sensor->setRx(3000)))
{
cout << "setRx returned " << rv << endl;
}
else
{
cout << "Received Buffer: " << sensor->getRxBufferStr() << endl;
int count = 0;
int buflen = 64;
char buffer[buflen];
while (shouldRun) {
if (!rx) {
snprintf(buffer, buflen, "Ping %d", count++);
cout << "Sending..." << std::string(buffer) << endl;
sensor.sendStr(string(buffer), 3000);
sensor.setSleep();
upm_delay(1);
} else {
// receiving
cout << "Attempting to receive..." << endl;
int rv;
if ((rv = sensor.setRx(3000))) {
cout << "setRx returned " << rv << endl;
} else {
cout << "Received Buffer: " << sensor.getRxBufferStr() << endl;
}
// go back to sleep when done
sensor->setSleep();
usleep(250000);
// go back to upm_delay when done
sensor.setSleep();
upm_delay_us(250000);
}
}
//! [Interesting]
//! [Interesting]
cout << "Exiting..." << endl;
delete sensor;
return 0;
cout << "Exiting..." << endl;
return 0;
}