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,10 +22,13 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include "tp401.hpp"
#include "upm_utilities.h"
using namespace std;
@ -34,37 +37,41 @@ using namespace std;
std::string
airQuality(uint16_t value)
{
if(value < 50) return "Fresh Air";
if(value < 200) return "Normal Indoor Air";
if(value < 400) return "Low Pollution";
if(value < 600) return "High Pollution - Action Recommended";
if (value < 50)
return "Fresh Air";
if (value < 200)
return "Normal Indoor Air";
if (value < 400)
return "Low Pollution";
if (value < 600)
return "High Pollution - Action Recommended";
return "Very High Pollution - Take Action Immediately";
}
int main ()
int
main()
{
upm::TP401* airSensor = new upm::TP401(0); // Instantiate new grove air quality sensor on analog pin A0
upm::TP401 airSensor(0);
cout << airSensor->name() << endl;
cout << airSensor.name() << endl;
fprintf(stdout, "Heating sensor for 3 minutes...\n");
// wait 3 minutes for sensor to warm up
for(int i = 0; i < 3; i++) {
if(i) {
for (int i = 0; i < 3; i++) {
if (i) {
fprintf(stdout, "Please wait, %d minute(s) passed..\n", i);
}
sleep(60);
upm_delay(60);
}
fprintf(stdout, "Sensor ready!\n");
while(true) {
uint16_t value = airSensor->getSample(); // Read raw value
float ppm = airSensor->getPPM(); // Read CO ppm (can vary slightly from previous read)
while (true) {
uint16_t value = airSensor.getSample(); // Read raw value
float ppm = airSensor.getPPM(); // Read CO ppm (can vary slightly from previous read)
fprintf(stdout, "raw: %4d ppm: %5.2f %s\n", value, ppm, airQuality(value).c_str());
usleep(2500000); // Sleep for 2.5s
upm_delay_us(2500000); // Sleep for 2.5s
}
delete airSensor;
return 0;
}
//! [Interesting]