From b6e63743700f6dfe3482b369871289ce65d2fb9b Mon Sep 17 00:00:00 2001 From: Houman brinjcargorabi Date: Mon, 23 May 2016 13:59:37 +0100 Subject: [PATCH] c++: Added the ability to initialise a class using a context struct Signed-off-by: Houman Brinjcargorabi Signed-off-by: Brendan Le Foll --- api/mraa/aio.hpp | 15 ++++++++++++++- api/mraa/gpio.hpp | 13 +++++++++++++ api/mraa/i2c.hpp | 12 ++++++++++++ api/mraa/pwm.hpp | 14 ++++++++++++++ api/mraa/spi.hpp | 14 ++++++++++++++ api/mraa/uart.hpp | 14 ++++++++++++++ 6 files changed, 81 insertions(+), 1 deletion(-) diff --git a/api/mraa/aio.hpp b/api/mraa/aio.hpp index acb1522..38ea8bc 100644 --- a/api/mraa/aio.hpp +++ b/api/mraa/aio.hpp @@ -47,13 +47,26 @@ class Aio * * @param pin channel number to read ADC inputs */ - Aio(unsigned int pin) + Aio(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 Constructor, takes a pointer to the AIO context and initialises + * the AIO class + * + * @param void * to an AIO context + */ + Aio(void* aio_context) + { + m_aio = (mraa_aio_context) aio_context; + if (m_aio == NULL) { + throw std::invalid_argument("Invalid AIO context"); + } + } /** * Aio destructor */ diff --git a/api/mraa/gpio.hpp b/api/mraa/gpio.hpp index 4c63e20..e2d7ad5 100644 --- a/api/mraa/gpio.hpp +++ b/api/mraa/gpio.hpp @@ -107,6 +107,19 @@ class Gpio mraa_gpio_owner(m_gpio, 0); } } + /** + * Gpio Constructor, takes a pointer to the GPIO context and initialises + * the GPIO class + * + * @param void * to GPIO context + */ + Gpio(void* gpio_context) + { + m_gpio = (mraa_gpio_context) gpio_context; + if (m_gpio == NULL) { + throw std::invalid_argument("Invalid GPIO context"); + } + } /** * Gpio object destructor, this will only unexport the gpio if we where * the owner diff --git a/api/mraa/i2c.hpp b/api/mraa/i2c.hpp index af02c95..52e2153 100644 --- a/api/mraa/i2c.hpp +++ b/api/mraa/i2c.hpp @@ -62,6 +62,18 @@ class I2c throw std::invalid_argument("Invalid i2c bus"); } } + /** + * I2C constructor, takes a pointer to a I2C context and initialises the I2C class + * + * @param void * to an I2C context + */ + I2c(void* i2c_context) + { + m_i2c = (mraa_i2c_context) i2c_context; + if (m_i2c == NULL) { + throw std::invalid_argument("Invalid I2C context"); + } + } /** * 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 71b0455..c75255a 100644 --- a/api/mraa/pwm.hpp +++ b/api/mraa/pwm.hpp @@ -66,6 +66,20 @@ class Pwm mraa_pwm_owner(m_pwm, 0); } } + + /** + * Pwm constructor, takes a pointer to the PWM context and + * initialises the class + * + * @param void * to a PWM context + */ + Pwm(void* pwm_context) + { + m_pwm = (mraa_pwm_context) pwm_context; + if (m_pwm == NULL) { + throw std::invalid_argument("Invalid PWM context"); + } + } /** * Pwm destructor */ diff --git a/api/mraa/spi.hpp b/api/mraa/spi.hpp index f75dbfb..94a3f64 100644 --- a/api/mraa/spi.hpp +++ b/api/mraa/spi.hpp @@ -79,6 +79,20 @@ class Spi } } + /** + * Spi Constructor, takes a pointer to a SPI context and initialises + * the SPI class + * + * @param void * to SPI context + */ + Spi(void* spi_context) + { + m_spi = (mraa_spi_context) spi_context; + if (m_spi == NULL) { + throw std::invalid_argument("Invalid SPI context"); + } + } + /** * Closes spi bus */ diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index 6f2843f..0771fdc 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -75,6 +75,20 @@ class Uart } } + /** + * Uart Constructor, takes a pointer to the UART context and initialises + * the UART class + * + * @param void * to a UART context + */ + Uart(void* uart_context) + { + m_uart = (mraa_uart_context) uart_context; + + if (m_uart == NULL) { + throw std::invalid_argument("Invalid UART context"); + } + } /** * Uart destructor */