diff --git a/api/mraa/aio.hpp b/api/mraa/aio.hpp index 63a2cf6..27c6a64 100644 --- a/api/mraa/aio.hpp +++ b/api/mraa/aio.hpp @@ -24,6 +24,7 @@ #pragma once +#include #include "aio.h" namespace mraa { @@ -45,6 +46,9 @@ class Aio { */ Aio(unsigned int pin) { m_aio = mraa_aio_init(pin); + if (m_aio == NULL) { + throw std::invalid_argument("Invalid AIO pin specified - do you have an ADC?"); + } } /** * Aio destructor diff --git a/api/mraa/gpio.hpp b/api/mraa/gpio.hpp index 82a3a47..3ce3c49 100644 --- a/api/mraa/gpio.hpp +++ b/api/mraa/gpio.hpp @@ -25,6 +25,7 @@ #pragma once #include "gpio.h" +#include namespace mraa { @@ -80,12 +81,20 @@ class Gpio { * you so this may not always work as expected. */ Gpio(int pin, bool owner=true, bool raw=false) { - if (raw) + if (raw) { m_gpio = mraa_gpio_init_raw(pin); - else + } + else { m_gpio = mraa_gpio_init(pin); - if (!owner) + } + + if (m_gpio == NULL) { + throw std::invalid_argument("Invalid GPIO pin specified"); + } + + if (!owner) { mraa_gpio_owner(m_gpio, 0); + } } /** * Gpio object destructor, this will only unexport the gpio if we where diff --git a/api/mraa/i2c.hpp b/api/mraa/i2c.hpp index dcd3897..2adfd89 100644 --- a/api/mraa/i2c.hpp +++ b/api/mraa/i2c.hpp @@ -25,6 +25,7 @@ #pragma once #include "i2c.h" +#include namespace mraa { @@ -48,10 +49,15 @@ class I2c { * @param raw Whether to disable pinmapper for your board */ I2c(int bus, bool raw=false) { - if (raw) + if (raw) { m_i2c = mraa_i2c_init_raw(bus); - else + } + else { m_i2c = mraa_i2c_init(bus); + } + if (m_i2c == NULL) { + throw std::invalid_argument("Invalid i2c bus"); + } } /** * Closes the I2c Bus used. This does not guarrantee the bus will not diff --git a/api/mraa/pwm.hpp b/api/mraa/pwm.hpp index 7259c0f..a627e41 100644 --- a/api/mraa/pwm.hpp +++ b/api/mraa/pwm.hpp @@ -25,6 +25,7 @@ #pragma once #include "pwm.h" +#include namespace mraa { @@ -47,12 +48,20 @@ class Pwm { * if the pinmapper exported it */ Pwm(int pin, int chipid=-1, bool owner = true) { - if (chipid == -1) + if (chipid == -1) { m_pwm = mraa_pwm_init(pin); - else + } + else { m_pwm = mraa_pwm_init_raw(pin, chipid); - if (!owner) + } + + if (m_pwm == NULL) { + throw std::invalid_argument("Error initialising PWM on pin"); + } + + if (!owner) { mraa_pwm_owner(m_pwm, 0); + } } /** * Pwm destructor diff --git a/api/mraa/spi.hpp b/api/mraa/spi.hpp index 64dc00c..6153051 100644 --- a/api/mraa/spi.hpp +++ b/api/mraa/spi.hpp @@ -25,6 +25,7 @@ #pragma once #include "spi.h" +#include namespace mraa { @@ -44,6 +45,10 @@ class Spi { */ Spi(int bus) { m_spi = mraa_spi_init(bus); + + if (m_spi == NULL) { + throw std::invalid_argument("Error initialising SPI bus"); + } } /** * Closes spi bus diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index f70d7e6..7bf807a 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -25,6 +25,7 @@ #pragma once #include "uart.h" +#include namespace mraa { @@ -43,6 +44,10 @@ class Uart { */ Uart(int uart) { m_uart = mraa_uart_init(uart); + + if (m_uart == NULL) { + throw std::invalid_argument("Error initialising UART"); + } } /** * Uart destructor diff --git a/examples/python/aio.py b/examples/python/aio.py index cc9e604..9be21f3 100644 --- a/examples/python/aio.py +++ b/examples/python/aio.py @@ -25,5 +25,9 @@ import mraa print (mraa.getVersion()) -x = mraa.Aio(0) -print (x.read()) + +try: + x = mraa.Aio(0) + print (x.read()) +except: + print ("Are you sure you have an ADC?") diff --git a/src/mraa.i b/src/mraa.i index 5928d23..6ffacca 100644 --- a/src/mraa.i +++ b/src/mraa.i @@ -1,5 +1,6 @@ %include stdint.i %include std_string.i +%include exception.i #ifdef DOXYGEN %include common_hpp_doc.i @@ -26,6 +27,16 @@ mraa_init(); %} +%exception { + try { + $action + } catch(const std::invalid_argument& e) { + SWIG_exception(SWIG_ValueError, e.what()); + } catch(...) { + SWIG_exception(SWIG_RuntimeError, "Unknown exception"); + } +} + %typemap(in) uint8_t = char; %typemap(in) unsigned char* = char*; %apply (char *STRING, size_t LENGTH) { (char *data, size_t length) };