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

@ -1,44 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include "hlg150h.hpp"
#define HLG150H_GPIO_RELAY 21
#define HLG150H_GPIO_PWM 22
void printState(upm::ILightController &lightController)
{
if (lightController.isPowered())
{
std::cout << "Light is powered, brightness = " << lightController.getBrightness() << std::endl;
}
else
{
std::cout << "Light is not powered." << std::endl;
}
}
int main( int argc, char **argv )
{
//! [Interesting]
upm::HLG150H lightController(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM);
std::cout << "Existing state: "; printState(lightController);
if (argc == 2)
{
std::string arg = argv[1];
int brightness = ::atoi(argv[1]);
if (brightness > 0)
{
lightController.setPowerOn();
lightController.setBrightness(brightness);
}
else
lightController.setPowerOff();
}
std::cout << "Now: ";printState(lightController);
//! [Interesting]
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <string>
#include "hlg150h.hpp"
#include "iLightController.hpp"
#define HLG150H_GPIO_RELAY 21
#define HLG150H_GPIO_PWM 22
void
printState(upm::ILightController& lightController)
{
if (lightController.isPowered()) {
std::cout << "Light is powered, brightness = " << lightController.getBrightness()
<< std::endl;
} else {
std::cout << "Light is not powered." << std::endl;
}
}
int
main(int argc, char** argv)
{
//! [Interesting]
upm::HLG150H lightController(HLG150H_GPIO_RELAY, HLG150H_GPIO_PWM);
std::cout << "Existing state: ";
printState(lightController);
if (argc == 2) {
std::string arg = argv[1];
int brightness = ::atoi(argv[1]);
if (brightness > 0) {
lightController.setPowerOn();
lightController.setBrightness(brightness);
} else
lightController.setPowerOff();
}
std::cout << "Now: ";
printState(lightController);
//! [Interesting]
return 0;
}