bmpx8x: rewrite in C; FTI; C++ wraps C

This driver has been rewritten from scratch.

See docs/apichanges.md for a list of API compatibility changes
compared to the original driver.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2017-04-04 17:48:41 -06:00
parent 8d43c431f2
commit c57a0d2c30
17 changed files with 1346 additions and 424 deletions

View File

@ -1,6 +1,11 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* This driver was rewritten based on the original driver written by:
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -24,57 +29,50 @@
#include <unistd.h>
#include <iostream>
#include "bmpx8x.hpp"
#include <signal.h>
int doWork = 0;
upm::BMPX8X *sensor = NULL;
#include "bmpx8x.hpp"
void
sig_handler(int signo)
using namespace std;
bool shouldRun = true;
void sig_handler(int signo)
{
printf("got signal\n");
if (signo == SIGINT) {
printf("exiting application\n");
doWork = 1;
}
if (signo == SIGINT)
shouldRun = false;
}
int
main(int argc, char **argv)
int main(int argc, char **argv)
{
//! [Interesting]
uint32_t presure = 0;
float temperature = 0;
float altitude = 0;
uint32_t sealevel = 0;
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a BMPX8X sensor on I2C
sensor = new upm::BMPX8X(0, ADDR);
// Instantiate a BMPX8X sensor on I2C using defaults.
upm::BMPX8X sensor;
// Print the pressure, altitude, sea level, and
// temperature values every 0.1 seconds
while (!doWork) {
presure = sensor->getPressure ();
temperature = sensor->getTemperature ();
altitude = sensor->getAltitude ();
sealevel = sensor->getSealevelPressure ();
// temperature values every 0.5 seconds
while (shouldRun)
{
sensor.update();
std::cout << "pressure value = " <<
presure <<
", altitude value = " <<
altitude <<
", sealevel value = " <<
sealevel <<
", temperature = " <<
temperature << std::endl;
usleep (100000);
cout << "Pressure: "
<< sensor.getPressure()
<< " Pa, Temperature: "
<< sensor.getTemperature()
<< " C, Altitude: "
<< sensor.getAltitude()
<< " m, Sea level: "
<< sensor.getSealevelPressure()
<< " Pa"
<< endl;
usleep(500000);
}
//! [Interesting]
std::cout << "exiting application" << std::endl;
delete sensor;
cout << "Exiting..." << endl;
//! [Interesting]
return 0;
}

View File

@ -157,6 +157,7 @@ add_example (bmg160)
add_example (bma250e)
add_example (bmm150)
add_example (rsc)
add_example (bmpx8x)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

88
examples/c/bmpx8x.c Normal file
View File

@ -0,0 +1,88 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include "upm_utilities.h"
#include "bmpx8x.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main(int argc, char **argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a BMPX8X instance using default i2c bus and address
bmpx8x_context sensor = bmpx8x_init(BMPX8X_DEFAULT_I2C_BUS,
BMPX8X_DEFAULT_I2C_ADDR);
if (!sensor)
{
printf("bmpx8x_init() failed.\n");
return 1;
}
// Print the pressure, altitude, sea level, and
// temperature values every 0.5 seconds
while (shouldRun)
{
if (bmpx8x_update(sensor))
{
printf("bmpx8x_update() failed\n");
bmpx8x_close(sensor);
return 1;
}
// assume sea level pressure is 101325 Pa.
float altitude = bmpx8x_get_altitude(sensor, 101325);
int sealevel = bmpx8x_get_sealevel_pressure(sensor, altitude);
printf("Pressure: %d Pa, Temperature: %f C, "
"Altitude %f m, Sea level %d Pa\n",
bmpx8x_get_pressure(sensor),
bmpx8x_get_temperature(sensor),
altitude,
sealevel);
upm_delay_ms(500);
}
printf("Exiting...\n");
bmpx8x_close(sensor);
//! [Interesting]
return 0;
}

View File

@ -1,6 +1,7 @@
/*
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
* Copyright (c) 2015 Intel Corporation.
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -22,27 +23,33 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//NOT TESTED!!!
public class BMPX8XSample {
public static void main(String[] args) throws InterruptedException {
// ! [Interesting]
// Instantiate a BMPX8X sensor on I2C
upm_bmpx8x.BMPX8X sensor = new upm_bmpx8x.BMPX8X(0);
public static void main(String[] args) throws InterruptedException {
// ! [Interesting]
// Print the pressure, altitude, sea level, and
// temperature values every second
while (true) {
System.out.println("Pressure: " + sensor.getPressure());
System.out.println("Altitude: " + sensor.getAltitude());
System.out.println("Sealevel pressure: "
+ sensor.getSealevelPressure());
System.out.println("Temperature: " + sensor.getTemperature());
System.out.println();
// Instantiate a BMPX8X sensor on I2C using defaults.
upm_bmpx8x.BMPX8X sensor = new upm_bmpx8x.BMPX8X();
Thread.sleep(1000);
}
// ! [Interesting]
}
// Print the pressure, altitude, sea level, and
// temperature values every .5 seconds
while (true)
{
sensor.update();
}
System.out.println("Pressure: "
+ sensor.getPressure()
+ " Pa, Temperature: "
+ sensor.getTemperature()
+ " C, Altitude: "
+ sensor.getAltitude()
+ " m, Sea level: "
+ sensor.getSealevelPressure()
+ " Pa");
Thread.sleep(500);
}
// ! [Interesting]
}
}

View File

@ -1,6 +1,7 @@
/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -22,31 +23,34 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//Load Barometer module
var bmpx8x = require('jsupm_bmpx8x');
// load this on i2c
var myBarometerObj = new bmpx8x.BMPX8X(0, bmpx8x.ADDR);
var pressure, temperature, altitude, sealevel;
var sensorObj = require('jsupm_bmpx8x');
// Instantiate a BMPX8X sensor on I2C using defaults.
var sensor = new sensorObj.BMPX8X();
// Print the pressure, altitude, sea level, and
// temperature values every 0.1 seconds
// temperature values every 0.5 seconds
setInterval(function()
{
var pressure = myBarometerObj.getPressure();
var temperature = myBarometerObj.getTemperature();
var altitude = myBarometerObj.getAltitude();
var sealevel = myBarometerObj.getSealevelPressure();
sensor.update();
var BMPX8Xresults = "pressure value = " + pressure;
BMPX8Xresults += ", altitude value = " + altitude;
BMPX8Xresults += ", sealevel value = " + sealevel;
BMPX8Xresults += ", temperature = " + temperature;
console.log(BMPX8Xresults);
}, 100);
console.log("Pressure: "
+ sensor.getPressure()
+ " Pa, Temperature: "
+ sensor.getTemperature()
+ " C, Altitude: "
+ sensor.getAltitude()
+ " m, Sea level: "
+ sensor.getSealevelPressure()
+ " Pa");
}, 500);
// Print message when exiting
// exit on ^C
process.on('SIGINT', function()
{
console.log("Exiting...");
process.exit(0);
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
console.log("Exiting.");
process.exit(0);
});

View File

@ -1,6 +1,7 @@
#!/usr/bin/python
# Author: Zion Orent <zorent@ics.com>
# Copyright (c) 2015 Intel Corporation.
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2014-2017 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@ -26,15 +27,18 @@ import time, sys, signal, atexit
from upm import pyupm_bmpx8x as upmBmpx8x
def main():
# Load Barometer module on i2c
myBarometer = upmBmpx8x.BMPX8X(0, upmBmpx8x.ADDR);
# Load Barometer module on i2c using default values
sensor = upmBmpx8x.BMPX8X(0);
## Exit handlers ##
# This function stops python from printing a stacktrace when you hit control-C
# This function stops python from printing a stacktrace when you hit
# control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit, including functions from myBarometer
# This function lets you run code on exit, including functions
# from sensor
def exitHandler():
print("Exiting")
sys.exit(0)
@ -46,17 +50,19 @@ def main():
# Print the pressure, altitude, sea level, and
# temperature values every 0.1 seconds
while(1):
outputStr = ("pressure value = {0}"
", altitude value = {1}"
", sealevel value = {2}"
", temperature = {3}".format(
myBarometer.getPressure(),
myBarometer.getTemperature(),
myBarometer.getAltitude(),
myBarometer.getSealevelPressure()))
sensor.update()
outputStr = ("Pressure: {0}"
" Pa, Temperature: {1}"
" C, Altitude: {2}"
" m, Sea Level: {3} Pa".format(
sensor.getPressure(),
sensor.getTemperature(),
sensor.getAltitude(),
sensor.getSealevelPressure()))
print(outputStr)
time.sleep(.1)
time.sleep(.5)
if __name__ == '__main__':
main()