1 /*
2 * Author: Marc Graham <marc@m2ag.net>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "micsv89.h"
26
27 using namespace upm;
28
MICSV89(int bus,uint8_t address)29 MICSV89::MICSV89 (int bus, uint8_t address) {
30 m_name = "micsv89";
31 m_valid = false;
32 m_address = address;
33 i2c = new mraa::I2c(bus);
34 if(i2c->frequency(mraa::I2C_STD) != mraa::SUCCESS){
35 throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.frequency(I2C_STD) failed");
36 return;
37 }
38 tx_buf[0] = 0x09;
39 tx_buf[1] = 0x00;
40 tx_buf[2] = 0x00;
41 }
42
update()43 void MICSV89::update() {
44 m_valid = false;
45 if(i2c->address(m_address) != mraa::SUCCESS){
46 throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.address() failed");
47 return;
48 }
49 if(i2c->write(tx_buf, 3) != mraa::SUCCESS){
50 throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.write() failed");
51 return;
52 }
53 sleep(1); //Give the device time to make the measurement.
54 if(i2c->address(m_address) != mraa::SUCCESS){
55 throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.address() failed");
56 return;
57 }
58 if(i2c->read(rx_buf, 6) != 6){
59 throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.read() failed");
60 return;
61 }
62 m_valid = true;
63 }
64
co2equ()65 float MICSV89::co2equ() {
66 return ((rx_buf[0] - 13) * (1600/229) + 400);
67 }
68
vocshort()69 int MICSV89::vocshort() {
70 return rx_buf[1];
71 }
72
tvoc()73 float MICSV89::tvoc() {
74 return rx_buf[2] * (1000/229);
75 }
76
resistor()77 float MICSV89::resistor() {
78 return 10 * (rx_buf[3] + (256 * rx_buf[4]) + (65536 * rx_buf[5]));
79 }
80
~MICSV89()81 MICSV89::~MICSV89() {
82 delete i2c;
83 }
84