mirror of
https://github.com/eclipse/upm.git
synced 2025-07-03 10:21:19 +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
@ -22,59 +22,50 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "ads1015.hpp"
|
||||
#include "iADC.hpp"
|
||||
#include "mraa/gpio.hpp"
|
||||
#include "upm_utilities.h"
|
||||
|
||||
#define EDISON_I2C_BUS 1
|
||||
#define FT4222_I2C_BUS 0
|
||||
#define EDISON_GPIO_SI7005_CS 20
|
||||
|
||||
|
||||
//! [Interesting]
|
||||
// Simple example of using IADC to determine
|
||||
// which sensor is present and return its name.
|
||||
// IADC is then used to get readings from sensor
|
||||
|
||||
|
||||
upm::IADC* getADC()
|
||||
int
|
||||
main()
|
||||
{
|
||||
upm::IADC* adc = NULL;
|
||||
try {
|
||||
adc = new upm::ADS1015(EDISON_I2C_BUS);
|
||||
mraa::Gpio gpio(EDISON_GPIO_SI7005_CS);
|
||||
gpio.dir(mraa::DIR_OUT_HIGH);
|
||||
return adc;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "ADS1015: " << e.what() << std::endl;
|
||||
}
|
||||
return adc;
|
||||
}
|
||||
/* Create an instance of the ADS1015 sensor */
|
||||
upm::ADS1015 sensor(EDISON_I2C_BUS);
|
||||
mraa::Gpio gpio(EDISON_GPIO_SI7005_CS);
|
||||
gpio.dir(mraa::DIR_OUT_HIGH);
|
||||
|
||||
int main ()
|
||||
{
|
||||
upm::IADC* adc = getADC();
|
||||
if (adc == NULL) {
|
||||
std::cout << "ADC not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "ADC " << adc->getModuleName() << " detected. " ;
|
||||
std::cout << adc->getNumInputs() << " inputs available" << std::endl;
|
||||
while (true) {
|
||||
for (unsigned int i=0; i<adc->getNumInputs(); ++i) {
|
||||
std::cout << "Input " << i;
|
||||
try {
|
||||
float voltage = adc->getVoltage(i);
|
||||
std::cout << ": Voltage = " << voltage << "V" << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
delete adc;
|
||||
return 0;
|
||||
/* Show usage from the IADC interface */
|
||||
upm::IADC* adc = static_cast<upm::IADC*>(&sensor);
|
||||
|
||||
if (adc == NULL) {
|
||||
std::cout << "ADC not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "ADC " << adc->getModuleName() << " detected. ";
|
||||
std::cout << adc->getNumInputs() << " inputs available" << std::endl;
|
||||
while (true) {
|
||||
for (unsigned int i = 0; i < adc->getNumInputs(); ++i) {
|
||||
std::cout << "Input " << i;
|
||||
try {
|
||||
float voltage = adc->getVoltage(i);
|
||||
std::cout << ": Voltage = " << voltage << "V" << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
upm_delay(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
|
Reference in New Issue
Block a user