2016-05-04 12:47:39 -06:00
|
|
|
/*
|
|
|
|
* Author: Jon Trulson <jtrulson@ics.com>
|
2017-03-10 09:57:09 -07:00
|
|
|
* Copyright (c) 2016-2017 Intel Corporation.
|
|
|
|
*
|
|
|
|
* The MIT License
|
2016-05-04 12:47:39 -06:00
|
|
|
*
|
|
|
|
* 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 <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "bmp280.hpp"
|
|
|
|
|
|
|
|
using namespace upm;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
// conversion from Celsius to Fahrenheit.
|
2016-05-04 12:47:39 -06:00
|
|
|
|
|
|
|
static float c2f(float c)
|
|
|
|
{
|
|
|
|
return (c * (9.0 / 5.0) + 32.0);
|
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
BMP280::BMP280(int bus, int addr, int cs) :
|
|
|
|
m_bmp280(bmp280_init(bus, addr, cs))
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
if (!m_bmp280)
|
|
|
|
throw std::runtime_error(string(__FUNCTION__)
|
|
|
|
+ ": bmp280_init() failed");
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
BMP280::~BMP280()
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_close(m_bmp280);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void BMP280::update()
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
if (bmp280_update(m_bmp280))
|
|
|
|
throw std::runtime_error(string(__FUNCTION__)
|
|
|
|
+ ": bmp280_update() failed");
|
|
|
|
}
|
2016-05-04 12:47:39 -06:00
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setSeaLevelPreassure(float seaLevelhPA)
|
|
|
|
{
|
|
|
|
bmp280_set_sea_level_pressure(m_bmp280, seaLevelhPA);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
float BMP280::getAltitude()
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
return bmp280_get_altitude(m_bmp280);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t BMP280::readReg(uint8_t reg)
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
return bmp280_read_reg(m_bmp280, reg);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int BMP280::readRegs(uint8_t reg, uint8_t *buffer, int len)
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
return bmp280_read_regs(m_bmp280, reg, buffer, len);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void BMP280::writeReg(uint8_t reg, uint8_t val)
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
if (bmp280_write_reg(m_bmp280, reg, val))
|
|
|
|
throw std::runtime_error(string(__FUNCTION__)
|
|
|
|
+ ": bmp280_write_reg() failed");
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t BMP280::getChipID()
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
return bmp280_get_chip_id(m_bmp280);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void BMP280::reset()
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_reset(m_bmp280);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float BMP280::getTemperature(bool fahrenheit)
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
float temperature = bmp280_get_temperature(m_bmp280);
|
|
|
|
|
|
|
|
if (fahrenheit)
|
|
|
|
return c2f(temperature);
|
|
|
|
else
|
|
|
|
return temperature;
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
float BMP280::getPressure()
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
return bmp280_get_pressure(m_bmp280);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setFilter(BMP280_FILTER_T filter)
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_set_filter(m_bmp280, filter);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setTimerStandby(BMP280_T_SB_T tsb)
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_set_timer_standby(m_bmp280, tsb);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setMeasureMode(BMP280_MODES_T mode)
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_set_measure_mode(m_bmp280, mode);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setOversampleRatePressure(BMP280_OSRS_P_T rate)
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_set_oversample_rate_pressure(m_bmp280, rate);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setOversampleRateTemperature(BMP280_OSRS_T_T rate)
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_set_oversample_rate_temperature(m_bmp280, rate);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t BMP280::getStatus()
|
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
return bmp280_get_status(m_bmp280);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|
2017-03-10 09:57:09 -07:00
|
|
|
void BMP280::setUsageMode(BMP280_USAGE_MODE_T mode)
|
2016-05-04 12:47:39 -06:00
|
|
|
{
|
2017-03-10 09:57:09 -07:00
|
|
|
bmp280_set_usage_mode(m_bmp280, mode);
|
2016-05-04 12:47:39 -06:00
|
|
|
}
|
|
|
|
|