mirror of
https://github.com/eclipse/upm.git
synced 2025-07-01 01:11:10 +03:00
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:

committed by
Mihai Tudor Panu

parent
bd6e4ec786
commit
5cefe7f5f3
@ -29,11 +29,13 @@
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ads1015.hpp"
|
||||
#include "ads1x15.hpp"
|
||||
#include "upm_utilities.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace upm;
|
||||
@ -41,39 +43,41 @@ 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 = "./ads1015.data"; // Output filename
|
||||
ofstream f;
|
||||
|
||||
// Initialize and configure the ADS1015
|
||||
ADS1015 *ads1015 = new upm::ADS1015(0, 0x48);
|
||||
ADS1015 ads1015(0, 0x48);
|
||||
|
||||
// Put the ADC into differential mode for pins A0 and A1
|
||||
ads1015->getSample(ADS1X15::DIFF_0_1);
|
||||
ads1015.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
|
||||
ads1015->setGain(ADS1X15::GAIN_TWO);
|
||||
ads1015.setGain(ADS1X15::GAIN_TWO);
|
||||
|
||||
// Set the sample rate to 3300 samples per second (max) and turn on continuous
|
||||
// sampling
|
||||
ads1015->setSPS(ADS1015::SPS_3300);
|
||||
ads1015->setContinuous(true);
|
||||
ads1015.setSPS(ADS1015::SPS_3300);
|
||||
ads1015.setContinuous(true);
|
||||
|
||||
// Enable exceptions from the output stream
|
||||
f.exceptions(ofstream::failbit | ofstream::badbit);
|
||||
// Open the file
|
||||
try{
|
||||
try {
|
||||
f.open(fileName);
|
||||
|
||||
// Output formatting
|
||||
@ -81,19 +85,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++ << " " << ads1015->getLastSample() << endl;
|
||||
usleep(1000);
|
||||
while (running) {
|
||||
f << id++ << " " << ads1015.getLastSample() << endl;
|
||||
upm_delay_us(1000);
|
||||
}
|
||||
|
||||
// Clean-up and exit
|
||||
timer.join();
|
||||
f.close();
|
||||
delete ads1015;
|
||||
} catch (ios_base::failure &e) {
|
||||
} catch (ios_base::failure& e) {
|
||||
cout << "Failed to write to file: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
@ -101,4 +104,3 @@ int main()
|
||||
//! [Interesting]
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user