Skip to content

Commit

Permalink
Add back missing module and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Ware committed May 15, 2015
1 parent 0b34027 commit b4cb9ac
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 0 deletions.
80 changes: 80 additions & 0 deletions examples/c++/bmpx8x.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Author: Yevgeniy Kiveisha <[email protected]>
* Copyright (c) 2014 Intel Corporation.
*
* 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 <iostream>
#include "bmpx8x.h"
#include <signal.h>

int doWork = 0;
upm::BMPX8X *sensor = NULL;

void
sig_handler(int signo)
{
printf("got signal\n");
if (signo == SIGINT) {
printf("exiting application\n");
doWork = 1;
}
}

int
main(int argc, char **argv)
{
//! [Interesting]
uint32_t presure = 0;
float temperature = 0;
float altitude = 0;
uint32_t sealevel = 0;

// Instantiate a BMPX8X sensor on I2C
sensor = new upm::BMPX8X(0, ADDR);

// 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 ();

std::cout << "pressure value = " <<
presure <<
", altitude value = " <<
altitude <<
", sealevel value = " <<
sealevel <<
", temperature = " <<
temperature << std::endl;
usleep (100000);
}
//! [Interesting]

std::cout << "exiting application" << std::endl;

delete sensor;

return 0;
}
55 changes: 55 additions & 0 deletions examples/javascript/bmpx8x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*global */
/*
* Author: Zion Orent <[email protected]>
* Copyright (c) 2014 Intel Corporation.
*
* 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.
*/

//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;

// Print the pressure, altitude, sea level, and
// temperature values every 0.1 seconds
setInterval(function()
{
var pressure = myBarometerObj.getPressure();
var temperature = myBarometerObj.getTemperature();
var altitude = myBarometerObj.getAltitude();
var sealevel = myBarometerObj.getSealevelPressure();

var BMPX8Xresults = "pressure value = " + pressure;
BMPX8Xresults += ", altitude value = " + altitude;
BMPX8Xresults += ", sealevel value = " + sealevel;
BMPX8Xresults += ", temperature = " + temperature;
console.log(BMPX8Xresults);
}, 100);

// Print message when exiting
process.on('SIGINT', function()
{
console.log("Exiting...");
process.exit(0);
});
5 changes: 5 additions & 0 deletions src/bmpx8x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set (libname "bmpx8x")
set (libdescription "upm BMPX8X")
set (module_src ${libname}.cxx)
set (module_h ${libname}.h)
upm_module_init()
222 changes: 222 additions & 0 deletions src/bmpx8x/bmpx8x.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* Author: Yevgeniy Kiveisha <[email protected]>
* Copyright (c) 2014 Intel Corporation.
*
* 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 <iostream>
#include <unistd.h>
#include <stdlib.h>

#include "bmpx8x.h"

using namespace upm;

BMPX8X::BMPX8X (int bus, int devAddr, uint8_t mode) {
m_name = "BMPX8X";

m_controlAddr = devAddr;
m_bus = bus;

m_i2ControlCtx = mraa_i2c_init(m_bus);

mraa_result_t ret = mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
if (ret != MRAA_SUCCESS) {
fprintf(stderr, "Messed up i2c bus\n");
}

if (i2cReadReg_8 (0xD0) != 0x55) {
std::cout << "Error :: Cannot continue" << std::endl;
return;
}

if (mode > BMP085_ULTRAHIGHRES) {
mode = BMP085_ULTRAHIGHRES;
}
oversampling = mode;

/* read calibration data */
ac1 = i2cReadReg_16 (BMP085_CAL_AC1);
ac2 = i2cReadReg_16 (BMP085_CAL_AC2);
ac3 = i2cReadReg_16 (BMP085_CAL_AC3);
ac4 = i2cReadReg_16 (BMP085_CAL_AC4);
ac5 = i2cReadReg_16 (BMP085_CAL_AC5);
ac6 = i2cReadReg_16 (BMP085_CAL_AC6);

b1 = i2cReadReg_16 (BMP085_CAL_B1);
b2 = i2cReadReg_16 (BMP085_CAL_B2);

mb = i2cReadReg_16 (BMP085_CAL_MB);
mc = i2cReadReg_16 (BMP085_CAL_MC);
md = i2cReadReg_16 (BMP085_CAL_MD);
}

BMPX8X::~BMPX8X() {
mraa_i2c_stop(m_i2ControlCtx);
}

int32_t
BMPX8X::getPressure () {
int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
uint32_t B4, B7;

UT = getTemperatureRaw();
UP = getPressureRaw();
B5 = computeB5(UT);

// do pressure calcs
B6 = B5 - 4000;
X1 = ((int32_t)b2 * ( (B6 * B6)>>12 )) >> 11;
X2 = ((int32_t)ac2 * B6) >> 11;
X3 = X1 + X2;
B3 = ((((int32_t)ac1*4 + X3) << oversampling) + 2) / 4;

X1 = ((int32_t)ac3 * B6) >> 13;
X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
B7 = ((uint32_t)UP - B3) * (uint32_t)( 50000UL >> oversampling );

if (B7 < 0x80000000) {
p = (B7 * 2) / B4;
} else {
p = (B7 / B4) * 2;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * p) >> 16;

p = p + ((X1 + X2 + (int32_t)3791)>>4);

return p;
}

int32_t
BMPX8X::getPressureRaw () {
uint32_t raw;

i2cWriteReg (BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));

if (oversampling == BMP085_ULTRALOWPOWER) {
usleep(5000);
} else if (oversampling == BMP085_STANDARD) {
usleep(8000);
} else if (oversampling == BMP085_HIGHRES) {
usleep(14000);
} else {
usleep(26000);
}

raw = i2cReadReg_16 (BMP085_PRESSUREDATA);

raw <<= 8;
raw |= i2cReadReg_8 (BMP085_PRESSUREDATA + 2);
raw >>= (8 - oversampling);

return raw;
}

int16_t
BMPX8X::getTemperatureRaw () {
i2cWriteReg (BMP085_CONTROL, BMP085_READTEMPCMD);
usleep(5000);
return i2cReadReg_16 (BMP085_TEMPDATA);
}

float
BMPX8X::getTemperature () {
int32_t UT, B5; // following ds convention
float temp;

UT = getTemperatureRaw ();

B5 = computeB5 (UT);
temp = (B5 + 8) >> 4;
temp /= 10;

return temp;
}

int32_t
BMPX8X::getSealevelPressure(float altitudeMeters) {
float pressure = getPressure ();
return (int32_t)(pressure / pow(1.0-altitudeMeters/44330, 5.255));
}

float
BMPX8X::getAltitude (float sealevelPressure) {
float altitude;

float pressure = getPressure ();

altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));

return altitude;
}

int32_t
BMPX8X::computeB5(int32_t UT) {
int32_t X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) >> 15;
int32_t X2 = ((int32_t)mc << 11) / (X1+(int32_t)md);

return X1 + X2;
}

mraa_result_t
BMPX8X::i2cWriteReg (uint8_t reg, uint8_t value) {
mraa_result_t error = MRAA_SUCCESS;

uint8_t data[2] = { reg, value };
error = mraa_i2c_address (m_i2ControlCtx, m_controlAddr);
error = mraa_i2c_write (m_i2ControlCtx, data, 2);

return error;
}

uint16_t
BMPX8X::i2cReadReg_16 (int reg) {
uint16_t data;

mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
mraa_i2c_write_byte(m_i2ControlCtx, reg);

mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
mraa_i2c_read(m_i2ControlCtx, (uint8_t *)&data, 0x2);

uint8_t high = (data & 0xFF00) >> 8;
data = (data << 8) & 0xFF00;
data |= high;

return data;
}

uint8_t
BMPX8X::i2cReadReg_8 (int reg) {
uint8_t data;

mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
mraa_i2c_write_byte(m_i2ControlCtx, reg);

mraa_i2c_address(m_i2ControlCtx, m_controlAddr);
mraa_i2c_read(m_i2ControlCtx, &data, 0x1);

return data;
}
Loading

0 comments on commit b4cb9ac

Please sign in to comment.