From b4e4e9ae8919a1f3bb421b4232b5c958b8c3eecd Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Tue, 8 Sep 2015 13:08:05 -0600 Subject: [PATCH] otp538u: throw exception(s) on fatal errors. In addition, throw out_of_range() exceptions in object/ambientTemperature() methods when they occur instead of just returning 0, which is a valid temperature return value. Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/otp538u/otp538u.cxx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/otp538u/otp538u.cxx b/src/otp538u/otp538u.cxx index 5b78d182..6231ae38 100644 --- a/src/otp538u/otp538u.cxx +++ b/src/otp538u/otp538u.cxx @@ -23,6 +23,8 @@ */ #include +#include +#include #include "otp538u.h" @@ -55,7 +57,8 @@ OTP538U::OTP538U(int pinA, int pinO, float aref) if ( !(m_aioA = mraa_aio_init(pinA)) ) { - cerr << __FUNCTION__ << ": mraa_aio_init() failed" << endl; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init(pinA) failed, invalid pin?"); return; } @@ -64,7 +67,8 @@ OTP538U::OTP538U(int pinA, int pinO, float aref) if ( !(m_aioO = mraa_aio_init(pinO)) ) { - cerr << __FUNCTION__ << ": mraa_aio_init() failed" << endl; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init(pinO) failed, invalid pin?"); return; } @@ -109,8 +113,9 @@ float OTP538U::ambientTemperature() } if (j >= otp538u_rt_table_max) - { - cerr << __FUNCTION__ << ": ambient temperature out of range." << endl; + { + throw std::out_of_range(std::string(__FUNCTION__) + + ": ambient temperature out of range."); return 0; } @@ -123,7 +128,8 @@ float OTP538U::ambientTemperature() // too cold if (slot < 0) { - cerr << __FUNCTION__ << ": ambient temperature out of range." << endl; + throw std::out_of_range(std::string(__FUNCTION__) + + ": ambient temperature out of range."); return 0; } @@ -174,7 +180,8 @@ float OTP538U::objectTemperature() if (slot >= (otp538u_vt_table_max - 1)) { - cerr << __FUNCTION__ << ": object temperature out of range." << endl; + throw std::out_of_range(std::string(__FUNCTION__) + + ": object temperature out of range."); return 0; }