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

@ -28,11 +28,13 @@
*/
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h>
#include "ads1115.hpp"
#include "ads1x15.hpp"
#include "upm_utilities.h"
using namespace std;
using namespace upm;
@ -40,16 +42,18 @@ using namespace upm;
bool running = true; // Controls main read/write loop
// Thread function
void stop()
void
stop()
{
sleep(10);
upm_delay(10);
running = false;
}
int main()
int
main()
{
//! [Interesting]
long id = 0; // Sample number
long id = 0; // Sample number
string fileName = "./ads1115.data"; // Output filename
ofstream f;
@ -57,26 +61,26 @@ int main()
// There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
ADS1115 *ads1115 = new upm::ADS1115(0, 0x48);
ADS1115 ads1115(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
ads1115->getSample(ADS1X15::DIFF_0_1);
ads1115.getSample(ADS1X15::DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
ads1115->setGain(ADS1X15::GAIN_TWO);
ads1115.setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 860 samples per second (max) and turn on continuous
// sampling
ads1115->setSPS(ADS1115::SPS_860);
ads1115->setContinuous(true);
ads1115.setSPS(ADS1115::SPS_860);
ads1115.setContinuous(true);
// Enable exceptions from the output stream
f.exceptions(ofstream::failbit | ofstream::badbit);
// Open the file
try{
try {
f.open(fileName);
// Output formatting
@ -84,19 +88,18 @@ int main()
f.precision(7);
// Start the thread that will stop logging after 10 seconds
thread timer (stop);
thread timer(stop);
// Read sensor data and write it to the output file every ms
while(running){
f << id++ << " " << ads1115->getLastSample() << endl;
usleep(1000);
while (running) {
f << id++ << " " << ads1115.getLastSample() << endl;
upm_delay_us(1000);
}
// Clean-up and exit
timer.join();
f.close();
delete ads1115;
} catch (ios_base::failure &e) {
} catch (ios_base::failure& e) {
cout << "Failed to write to file: " << e.what() << endl;
return 1;
}
@ -104,4 +107,3 @@ int main()
//! [Interesting]
return 0;
}