mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
th02:: added new humidity&temperature sensor
Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
This commit is contained in:
parent
50fd1d7478
commit
ee2d398bfa
@ -34,6 +34,7 @@ add_executable (mq3-example mq3-example.cxx)
|
||||
add_executable (mq5-example mq5-example.cxx)
|
||||
add_executable (mq9-example mq9-example.cxx)
|
||||
add_executable (tcs3414cs-example tcs3414cs-example.cxx)
|
||||
add_executable (th02-example th02-example.cxx)
|
||||
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
|
||||
@ -62,6 +63,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/gas)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/tcs3414cs)
|
||||
include_directories (${PROJECT_SOURCE_DIR}/src/th02)
|
||||
|
||||
target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
|
||||
@ -99,3 +101,4 @@ target_link_libraries (mq3-example gas ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (mq5-example gas ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT})
|
||||
target_link_libraries (th02-example th02 ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
64
examples/th02-example.cxx
Normal file
64
examples/th02-example.cxx
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* 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 "th02.h"
|
||||
#include <signal.h>
|
||||
|
||||
int doWork = 0;
|
||||
upm::TH02 *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]
|
||||
float temperature = 0.0;
|
||||
float humidity = 0.0;
|
||||
sensor = new upm::TH02 ();
|
||||
|
||||
while (!doWork) {
|
||||
temperature = sensor->getTemperature ();
|
||||
// humidity = sensor->getHumidity ();
|
||||
std::cout << "Temperature = " << temperature << ", Humidity = " << humidity << std::endl;
|
||||
usleep (500000);
|
||||
}
|
||||
//! [Interesting]
|
||||
|
||||
std::cout << "exiting application" << std::endl;
|
||||
|
||||
delete sensor;
|
||||
|
||||
return 0;
|
||||
}
|
5
src/th02/CMakeLists.txt
Normal file
5
src/th02/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
set (libname "th02")
|
||||
set (libdescription "Temperature and Humidity Sensor Pro")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_h ${libname}.h)
|
||||
upm_module_init()
|
8
src/th02/jsupm_th02.i
Normal file
8
src/th02/jsupm_th02.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_th02
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "th02.h"
|
||||
%}
|
||||
|
||||
%include "th02.h"
|
9
src/th02/pyupm_th02.i
Normal file
9
src/th02/pyupm_th02.i
Normal file
@ -0,0 +1,9 @@
|
||||
%module pyupm_th02
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "th02.h"
|
||||
%{
|
||||
#include "th02.h"
|
||||
%}
|
160
src/th02/th02.cxx
Normal file
160
src/th02/th02.cxx
Normal file
@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Credits to Seeed Studeo.
|
||||
*
|
||||
* 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 "th02.h"
|
||||
|
||||
using namespace upm;
|
||||
|
||||
struct TH02Exception : public std::exception {
|
||||
std::string message;
|
||||
TH02Exception (std::string msg) : message (msg) { }
|
||||
~TH02Exception () throw () { }
|
||||
const char* what() const throw () { return message.c_str(); }
|
||||
};
|
||||
|
||||
TH02::TH02 () {
|
||||
m_name = "TH02";
|
||||
m_i2Ctx = mraa_i2c_init(0);
|
||||
|
||||
mraa_result_t ret = mraa_i2c_address(m_i2Ctx, ADDR);
|
||||
if (ret != MRAA_SUCCESS) {
|
||||
throw TH02Exception ("Couldn't initilize I2C.");
|
||||
}
|
||||
}
|
||||
|
||||
TH02::~TH02 () {
|
||||
mraa_i2c_stop(m_i2Ctx);
|
||||
}
|
||||
|
||||
float
|
||||
TH02::getTemperature () {
|
||||
uint8_t buffer[3];
|
||||
uint16_t temperature = 0;
|
||||
|
||||
/* Start a new temperature conversion */
|
||||
i2cWriteReg (REG_CONFIG, CMD_MEASURE_TEMP);
|
||||
|
||||
/* Wait until conversion is done */
|
||||
while (getStatus() == false);
|
||||
|
||||
if (i2cReadReg_N (REG_DATA_H, 3, buffer) > 2) {
|
||||
temperature = (buffer[1] << 8) | buffer[2];
|
||||
temperature >>= 2;
|
||||
|
||||
return (temperature / 32.0) - 50.0;
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float
|
||||
TH02::getHumidity () {
|
||||
uint8_t buffer[3];
|
||||
uint16_t humidity = 0;
|
||||
|
||||
/* Start a new humility conversion */
|
||||
i2cWriteReg (REG_CONFIG, CMD_MEASURE_HUMI);
|
||||
|
||||
/* Wait until conversion is done */
|
||||
while (getStatus() == false);
|
||||
|
||||
if (i2cReadReg_N (REG_DATA_H, 3, buffer) > 2) {
|
||||
humidity = (buffer[1] << 8) | buffer[2];
|
||||
humidity >>= 4;
|
||||
|
||||
return (humidity / 16.0) - 24.0;
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
bool
|
||||
TH02::getStatus () {
|
||||
uint8_t buffer[1];
|
||||
|
||||
if (i2cReadReg_N (REG_STATUS, 1, buffer) > 0) {
|
||||
if (buffer[0] & STATUS_RDY_MASK) {
|
||||
return true; // ready
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* **************
|
||||
* private area
|
||||
* **************
|
||||
*/
|
||||
uint16_t
|
||||
TH02::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
|
||||
int readByte = 0;
|
||||
|
||||
if (m_i2Ctx == NULL) {
|
||||
throw TH02Exception ("Couldn't find initilized I2C.");
|
||||
}
|
||||
|
||||
mraa_i2c_address(m_i2Ctx, ADDR);
|
||||
mraa_i2c_write_byte(m_i2Ctx, reg);
|
||||
|
||||
mraa_i2c_address(m_i2Ctx, ADDR);
|
||||
readByte = mraa_i2c_read(m_i2Ctx, buffer, len);
|
||||
return readByte;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
TH02::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) {
|
||||
mraa_result_t error = MRAA_SUCCESS;
|
||||
|
||||
if (m_i2Ctx == NULL) {
|
||||
throw TH02Exception ("Couldn't find initilized I2C.");
|
||||
}
|
||||
|
||||
error = mraa_i2c_address (m_i2Ctx, ADDR);
|
||||
error = mraa_i2c_write_byte (m_i2Ctx, reg);
|
||||
error = mraa_i2c_write (m_i2Ctx, buffer, len);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
TH02::i2cWriteReg (uint8_t reg, uint8_t data) {
|
||||
mraa_result_t error = MRAA_SUCCESS;
|
||||
|
||||
if (m_i2Ctx == NULL) {
|
||||
throw TH02Exception ("Couldn't find initilized I2C.");
|
||||
}
|
||||
|
||||
error = mraa_i2c_address (m_i2Ctx, ADDR);
|
||||
error = mraa_i2c_write_byte (m_i2Ctx, reg);
|
||||
error = mraa_i2c_write_byte (m_i2Ctx, data);
|
||||
|
||||
return error;
|
||||
}
|
103
src/th02/th02.h
Normal file
103
src/th02/th02.h
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Credits to Seeed Studeo.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <mraa/i2c.h>
|
||||
|
||||
#define ADDR 0x40 // device address
|
||||
|
||||
#define REG_STATUS 0x00
|
||||
#define REG_DATA_H 0x01
|
||||
#define REG_DATA_L 0x02
|
||||
#define REG_CONFIG 0x03
|
||||
#define REG_ID 0x11
|
||||
|
||||
#define STATUS_RDY_MASK 0x01
|
||||
|
||||
#define CMD_MEASURE_HUMI 0x01
|
||||
#define CMD_MEASURE_TEMP 0x11
|
||||
|
||||
#define TH02_WR_REG_MODE 0xC0
|
||||
#define TH02_RD_REG_MODE 0x80
|
||||
|
||||
#define HIGH 1
|
||||
#define LOW 0
|
||||
|
||||
namespace upm {
|
||||
|
||||
/**
|
||||
* @brief C++ API for TH02 chip (Temperature and Humidity Sensor Pro)
|
||||
*
|
||||
* This file defines the TH02 C++ interface for libth02
|
||||
*
|
||||
* @snippet th02-example.cxx Interesting
|
||||
*
|
||||
*/
|
||||
class TH02 {
|
||||
public:
|
||||
/**
|
||||
* Instanciates a TH02 object
|
||||
*/
|
||||
TH02 ();
|
||||
|
||||
/**
|
||||
* TH02 object destructor, basicaly it close i2c connection.
|
||||
*/
|
||||
~TH02 ();
|
||||
|
||||
/**
|
||||
* Get the temperature value from sensor.
|
||||
*/
|
||||
float getTemperature ();
|
||||
|
||||
/**
|
||||
* Get the humidity value from sensor.
|
||||
*/
|
||||
float getHumidity ();
|
||||
|
||||
/**
|
||||
* Get the sensor's status.
|
||||
*/
|
||||
bool getStatus ();
|
||||
|
||||
/**
|
||||
* Return name of the component
|
||||
*/
|
||||
std::string name()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
private:
|
||||
std::string m_name;
|
||||
mraa_i2c_context m_i2Ctx;
|
||||
|
||||
uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
|
||||
mraa_result_t i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
|
||||
mraa_result_t i2cWriteReg (uint8_t reg, uint8_t data);
|
||||
};
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user