From 3d8b732158a7592471f4edd52ae8d1fe21a76719 Mon Sep 17 00:00:00 2001 From: Marc Graham Date: Mon, 12 Oct 2015 17:55:28 -0700 Subject: [PATCH] Cleaned up code. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removed some unneeded calls in update function. Changed call in micsv89.js sample file to use update instead of start. Tested with UPM. 4.0. The micsv89 is sensitive to other devices pulling the I2c bus up, so it is not likely to work on the Arduino breakout. I2c 1 on mini breakout works fine. —Signed-off-by Marc Graham Signed-off-by: Abhishek Malik --- examples/javascript/micsv89.js | 2 +- src/micsv89/micsv89.cxx | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/examples/javascript/micsv89.js b/examples/javascript/micsv89.js index 98b7b0e9..ff69c567 100644 --- a/examples/javascript/micsv89.js +++ b/examples/javascript/micsv89.js @@ -16,7 +16,7 @@ var mics = new upmMICSV89.MICSV89(6); while(1) { - mics.start(); + mics.update(); while(!mics.valid()); console.log("co2: " + mics.co2equ()); console.log("short: " + mics.vocshort()); diff --git a/src/micsv89/micsv89.cxx b/src/micsv89/micsv89.cxx index c86c4259..6fa60368 100644 --- a/src/micsv89/micsv89.cxx +++ b/src/micsv89/micsv89.cxx @@ -31,6 +31,10 @@ MICSV89::MICSV89 (int bus, uint8_t address) { m_valid = false; m_address = address; i2c = new mraa::I2c(bus); + if(i2c->address(m_address) != mraa::SUCCESS){ + throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.address() failed"); + return; + } if(i2c->frequency(mraa::I2C_STD) != mraa::SUCCESS){ throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.frequency(I2C_STD) failed"); return; @@ -41,20 +45,12 @@ MICSV89::MICSV89 (int bus, uint8_t address) { } void MICSV89::update() { - m_valid = false; - if(i2c->address(m_address) != mraa::SUCCESS){ - throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.address() failed"); - return; - } + m_valid = false; if(i2c->write(tx_buf, 3) != mraa::SUCCESS){ throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.write() failed"); return; } sleep(1); //Give the device time to make the measurement. - if(i2c->address(m_address) != mraa::SUCCESS){ - throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.address() failed"); - return; - } if(i2c->read(rx_buf, 6) != 6){ throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.read() failed"); return;