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,102 +22,89 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <signal.h>
#include "l3gd20.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;
}
float rad2deg(float x)
float
rad2deg(float x)
{
return x * (180.0 / M_PI);
return x * (180.0 / M_PI);
}
int main(int argc, char **argv)
int
main(int argc, char** argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate an L3GD20 using default parameters
upm::L3GD20 *sensor = new upm::L3GD20(L3GD20_DEFAULT_I2C_BUS,
L3GD20_DEFAULT_I2C_ADDR);
// Instantiate an L3GD20 using default parameters
upm::L3GD20 sensor(L3GD20_DEFAULT_I2C_BUS, L3GD20_DEFAULT_I2C_ADDR);
// set some parameters (these are already the defaults, but are
// provided here as an example)
// set some parameters (these are already the defaults, but are
// provided here as an example)
// 250 deg/s sensitivity
sensor->setRange(sensor->FS_250);
// 250 deg/s sensitivity
sensor.setRange(sensor.FS_250);
// Set ODR to 95Hz, 25Hz cut-off
sensor->setODR(sensor->ODR_CUTOFF_95_25);
// Set ODR to 95Hz, 25Hz cut-off
sensor.setODR(sensor.ODR_CUTOFF_95_25);
// If you already have calibration data, you can specify it here
// sensor->loadCalibratedData(-0.0296269637, -0.0080939643, -0.0077121737);
// If you already have calibration data, you can specify it here
// sensor.loadCalibratedData(-0.0296269637, -0.0080939643, -0.0077121737);
// now output data every 100 milliseconds
while (shouldRun)
{
float x, y, z;
// now output data every 100 milliseconds
while (shouldRun) {
float x, y, z;
sensor->update();
sensor.update();
cout << "Calibrated: " << sensor->getCalibratedStatus() << endl;
cout << "Calibrated: " << sensor.getCalibratedStatus() << endl;
// output is in radians/s
sensor->getGyroscope(&x, &y, &z);
cout << fixed << setprecision(1)
<< "Gyroscope x: " << x
<< " y: " << y
<< " z: " << z
<< " radians"
<< endl;
// output is in radians/s
sensor.getGyroscope(&x, &y, &z);
cout << fixed << setprecision(1) << "Gyroscope x: " << x << " y: " << y << " z: " << z
<< " radians" << endl;
// same data converted to degrees/s
cout << "Gyroscope x: " << rad2deg(x)
<< " y: " << rad2deg(y)
<< " z: " << rad2deg(z)
<< " degrees"
<< endl;
// same data converted to degrees/s
cout << "Gyroscope x: " << rad2deg(x) << " y: " << rad2deg(y) << " z: " << rad2deg(z)
<< " degrees" << endl;
// we show both C and F for temperature
cout << "Compensation Temperature: " << sensor->getTemperature(false)
<< " C / " << sensor->getTemperature(true) << " F"
<< endl;
// we show both C and F for temperature
cout << "Compensation Temperature: " << sensor.getTemperature(false) << " C / "
<< sensor.getTemperature(true) << " F" << endl;
cout << endl;
cout << endl;
usleep(100000);
upm_delay_us(100000);
}
// dump the calibration values if we managed to calibrate
if (sensor->getCalibratedStatus())
{
float calX, calY, calZ;
sensor->getCalibratedData(&calX, &calY, &calZ);
// dump the calibration values if we managed to calibrate
if (sensor.getCalibratedStatus()) {
float calX, calY, calZ;
sensor.getCalibratedData(&calX, &calY, &calZ);
cout << setprecision(10)
<< "Calibration values x: " << calX
<< " y: " << calY
<< " z: " << calZ
<< endl;
cout << setprecision(10) << "Calibration values x: " << calX << " y: " << calY
<< " z: " << calZ << endl;
}
cout << "Exiting..." << endl;
cout << "Exiting..." << endl;
delete sensor;
//! [Interesting]
return 0;
//! [Interesting]
return 0;
}