From 8d38bd0bcc7063e150c6f7c1d8ca4d0e6c090d1a Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 10 Sep 2015 11:44:03 -0600 Subject: [PATCH] hcsr04: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/hcsr04/hcsr04.cxx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/hcsr04/hcsr04.cxx b/src/hcsr04/hcsr04.cxx index 4340b817..18e5c628 100644 --- a/src/hcsr04/hcsr04.cxx +++ b/src/hcsr04/hcsr04.cxx @@ -24,6 +24,8 @@ */ #include +#include +#include #include #include #include @@ -47,8 +49,9 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) { m_triggerPinCtx = mraa_gpio_init (triggerPin); if (m_triggerPinCtx == NULL) { - fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", triggerPin); - exit (1); + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_pwm_init() failed, invalid pin?"); + return; } mraa_gpio_dir(m_triggerPinCtx, MRAA_GPIO_OUT); @@ -56,8 +59,9 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) { m_echoPinCtx = mraa_gpio_init(echoPin); if (m_echoPinCtx == NULL) { - fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", echoPin); - exit (1); + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init() failed, invalid pin?"); + return; } mraa_gpio_dir(m_echoPinCtx, MRAA_GPIO_IN);