tsl2561: throw exception(s) on fatal errors

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson 2015-09-08 15:54:21 -06:00 committed by Mihai Tudor Panu
parent 74cbc25f21
commit 5516faca38

View File

@ -27,6 +27,8 @@
*/ */
#include <string>
#include <stdexcept>
#include <unistd.h> #include <unistd.h>
#include "tsl2561.h" #include "tsl2561.h"
@ -45,30 +47,34 @@ TSL2561::TSL2561(int bus, uint8_t devAddr, uint8_t gain, uint8_t integrationTime
mraa::Result error = m_i2ControlCtx.address(m_controlAddr); mraa::Result error = m_i2ControlCtx.address(m_controlAddr);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Messed up i2c bus in TSL2561()\n"); throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_address() failed");
return; return;
} }
// POWER UP. // POWER UP.
error = i2cWriteReg(REGISTER_Control,CONTROL_POWERON); error = m_i2ControlCtx.writeReg(REGISTER_Control, CONTROL_POWERON);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: Unable to power up - Ensure TSL2561 is connected to I2C\n"); throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to power up TSL2561");
return; return;
} }
// Power on Settling time // Power on Settling time
usleep(1000); usleep(1000);
// Gain & Integration time . // Gain & Integration time .
error = i2cWriteReg(REGISTER_Timing, m_gain | m_integrationTime); error = m_i2ControlCtx.writeReg(REGISTER_Timing, m_gain | m_integrationTime);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: Unable to set gain/time - Ensure TSL2561 is connected to I2C\n"); throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set gain/time");
return; return;
} }
// Set interrupt threshold to default. // Set interrupt threshold to default.
error = i2cWriteReg(REGISTER_Interrupt,0x00); error = m_i2ControlCtx.writeReg(REGISTER_Interrupt, 0x00);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: Unable to interrupt threshold - Ensure TSL2561 is connected to I2C\n"); throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set interrupt threshold");
return; return;
} }
} }
@ -76,7 +82,7 @@ TSL2561::TSL2561(int bus, uint8_t devAddr, uint8_t gain, uint8_t integrationTime
TSL2561::~TSL2561() TSL2561::~TSL2561()
{ {
// POWER DOWN // POWER DOWN
i2cWriteReg(REGISTER_Control,CONTROL_POWEROFF); m_i2ControlCtx.writeReg(REGISTER_Control, CONTROL_POWEROFF);
} }
int int
@ -96,22 +102,22 @@ TSL2561::getLux()
error = i2cReadReg(REGISTER_Channal0H, ch0_high); error = i2cReadReg(REGISTER_Channal0H, ch0_high);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: Unable to read channel0H in getRawLux()\n"); fprintf(stderr, "Error: Unable to read channel0H in getRawLux()\n");
return error; return error;
} }
rawLuxCh0 = ch0_high*256+ch0_low; rawLuxCh0 = ch0_high*256+ch0_low;
error= i2cReadReg(REGISTER_Channal1L, ch1_low); error= i2cReadReg(REGISTER_Channal1L, ch1_low);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: Unable to read channel1L in getRawLux()\n"); fprintf(stderr, "Error: Unable to read channel1L in getRawLux()\n");
return error; return error;
} }
error = i2cReadReg(REGISTER_Channal1H, ch1_high); error = i2cReadReg(REGISTER_Channal1H, ch1_high);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: Unable to read channel1H in getRawLux()\n"); fprintf(stderr, "Error: Unable to read channel1H in getRawLux()\n");
return error; return error;
} }
rawLuxCh1 = ch1_high*256+ch1_low; rawLuxCh1 = ch1_high*256+ch1_low;
@ -198,15 +204,15 @@ TSL2561::i2cWriteReg (uint8_t reg, uint8_t value)
// Write register to I2C // Write register to I2C
error = m_i2ControlCtx.writeByte (reg); error = m_i2ControlCtx.writeByte (reg);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: on i2c bus write reg in i2cWriteReg()\n"); fprintf(stderr, "Error: on i2c bus write reg in i2cWriteReg()\n");
return error; return error;
} }
// Write value to I2C // Write value to I2C
error = m_i2ControlCtx.writeByte (value); error = m_i2ControlCtx.writeByte (value);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: on i2c bus write value in i2cWriteReg()\n"); fprintf(stderr, "Error: on i2c bus write value in i2cWriteReg()\n");
return error; return error;
} }
usleep(100000); usleep(100000);
@ -221,7 +227,6 @@ TSL2561::i2cReadReg(uint8_t reg, uint8_t &data)
// Start transmission to device // Start transmission to device
error = m_i2ControlCtx.address(m_controlAddr); error = m_i2ControlCtx.address(m_controlAddr);
if (error != mraa::SUCCESS) { if (error != mraa::SUCCESS) {
fprintf(stderr, "Error: on i2c bus address setup in i2cReadReg()\n"); fprintf(stderr, "Error: on i2c bus address setup in i2cReadReg()\n");
return error; return error;