From e0e93281fb5907c5bb3136337ca5b4ee3c8a28c1 Mon Sep 17 00:00:00 2001 From: Abhishek Malik Date: Fri, 9 Sep 2016 19:10:59 -0700 Subject: [PATCH] Alcohol Sensor: Add C Source Signed-off-by: Abhishek Malik --- src/mq303a/CMakeLists.txt | 14 ++++-- src/mq303a/mq303a.c | 69 ++++++++++++++++++++++++++ src/mq303a/mq303a.h | 102 ++++++++++++++++++++++++++++++++++++++ src/mq303a/mq303a_fti.c | 79 +++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+), 5 deletions(-) create mode 100644 src/mq303a/mq303a.c create mode 100644 src/mq303a/mq303a.h create mode 100644 src/mq303a/mq303a_fti.c diff --git a/src/mq303a/CMakeLists.txt b/src/mq303a/CMakeLists.txt index 2607608c..4c042889 100644 --- a/src/mq303a/CMakeLists.txt +++ b/src/mq303a/CMakeLists.txt @@ -1,5 +1,9 @@ -set (libname "mq303a") -set (libdescription "upm mq303a module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME mq303a + DESCRIPTION "upm mq303a sensor module" + C_HDR mq303a.h + C_SRC mq303a.c + CPP_HDR mq303a.hpp + CPP_SRC mq303a.cxx + FTI_SRC mq303a_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/mq303a/mq303a.c b/src/mq303a/mq303a.c new file mode 100644 index 00000000..512db2b9 --- /dev/null +++ b/src/mq303a/mq303a.c @@ -0,0 +1,69 @@ +/* + * Author: Jon Trulson + * Abhishek Malik + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "mq303a.h" + +mq303a_context mq303a_init(int pin, int heater_pin){ + mq303a_context dev = + (mq303a_context)malloc(sizeof(struct _mq303a_context)); + + if (!dev) + return NULL; + + dev->aio_pin = pin; + dev->gpio_pin = heater_pin; + dev->aio = mraa_aio_init(dev->aio_pin); + dev->gpio = mraa_gpio_init(dev->gpio_pin); + + if(mraa_gpio_dir(dev->gpio, MRAA_GPIO_OUT) != MRAA_SUCCESS) + { + free(dev); + return NULL; + } + + return dev; +} + +void mq303a_close(mq303a_context dev){ + free(dev); +} + +upm_result_t mq303a_heater_enable(mq303a_context dev, bool enable){ + if(enable) + mraa_gpio_write(dev->gpio, 0); + else + mraa_gpio_write(dev->gpio, 1); + + return UPM_SUCCESS; +} + +upm_result_t mq303a_get_value(mq303a_context dev, int* val){ + *val = mraa_aio_read(dev->aio); + + if (*val < 0) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} diff --git a/src/mq303a/mq303a.h b/src/mq303a/mq303a.h new file mode 100644 index 00000000..4c1aabaa --- /dev/null +++ b/src/mq303a/mq303a.h @@ -0,0 +1,102 @@ +/* + * Author: Jon Trulson + * Abhishek Malik + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef MQ303A_MQ303A_H_ +#define MQ303A_MQ303A_H_ + +#include "upm.h" +#include "mraa/aio.h" + +/** + * @brief MQ303A Alcohol Sensor library + * @defgroup mq303a libupm-mq303a + * @ingroup analog gpio gaseous tsk + */ +/** + * @library mq303a + * @sensor mq303a + * @comname MQ303A Alcohol Sensor + * @type gaseous + * @man seeed + * @web http://www.seeedstudio.com/document/pdf/MQ303A.pdf + * @con analog gpio + * @kit tsk + * + * @brief API for the MQ303A Alcohol Sensor + * + * UPM module for the MQ303A alcohol sensor. + * This sensor needs to be warmed up before stable results are + * obtained. The higher the value returned from value(), + * the higher the amount of alcohol detected. + * + * @image html mq303a.jpg + * @snippet mq303a.cxx Interesting + */ + +/** + * device context + */ +typedef struct _mq303a_context { + mraa_gpio_context gpio; + mraa_aio_context aio; + uint8_t gpio_pin; + uint8_t aio_pin; +} *mq303a_context; + +/** + * MQ303A Initialization function + * + * @param pin Analog pin to use + * @param heaterPin Digital pin mapped to the analog pin to use + * @return void* pointer to the sensor struct + */ +mq303a_context mq303a_init(int pin, int heater_pin); + +/** + * MQ303A Initialization function + * + * @param void* pointer to the sensor struct + */ +void mq303a_close(mq303a_context dev); + +/** + * This function gets the value of the alcohol content + * + * @param void* pointer to the sensor struct + * @param *val pointer to store the reading + * @return upm_result_t UPM success/error code + */ +upm_result_t mq303a_get_value(mq303a_context dev, int* val); + +/** + * This function enables/disables the heater + * on board the sensor. + * + * @param void* pointer to the sensor struct + * @param bool to turn on/off heater + * @return upm_result_t UPM success/error code + */ +upm_result_t mq303a_heater_enable(mq303a_context dev, bool enable); + +#endif /* MQ303A_MQ303A_H_ */ diff --git a/src/mq303a/mq303a_fti.c b/src/mq303a/mq303a_fti.c new file mode 100644 index 00000000..945e56f4 --- /dev/null +++ b/src/mq303a/mq303a_fti.c @@ -0,0 +1,79 @@ +/* + * Author: Jon Trulson + * Abhishek Malik + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "mq303a.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_mq303a_name[] = "MQ303A"; +const char upm_mq303a_description[] = "MQ303A"; +const upm_protocol_t upm_mq303a_protocol[] = {UPM_GPIO, UPM_ANALOG}; +const upm_sensor_t upm_mq303a_category[] = {UPM_GAS}; + +// forward declarations +const upm_sensor_descriptor_t upm_mq303a_get_descriptor(); +const void* upm_mq303a_get_ft(upm_sensor_t sensor_type); +void* upm_mq303a_init_name(); +void upm_mq303a_close(void* dev); +upm_result_t upm_mq303a_get_value(void* dev, int* val); + +const upm_sensor_descriptor_t upm_mq303a_get_descriptor(){ + upm_sensor_descriptor_t usd; + usd.name = upm_mq303a_name; + usd.description = upm_mq303a_description; + usd.protocol_size = 1; + usd.protocol = upm_mq303a_protocol; + usd.category_size = 1; + usd.category = upm_mq303a_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_mq303a_init_name, + .upm_sensor_close = &upm_mq303a_close, + .upm_sensor_get_descriptor = &upm_mq303a_get_descriptor +}; + +const void* upm_mq303a_get_ft(upm_sensor_t sensor_type){ + if (sensor_type == UPM_SENSOR){ + return &ft; + } + return NULL; +} + +void* upm_mq303a_init_name(){ + return NULL; +} + +void upm_mq303a_close(void* dev){ + mq303a_close((mq303a_context)dev); +} + +upm_result_t upm_mq303a_get_value(void* dev, int* val){ + return mq303a_get_value((mq303a_context)dev, val); +} \ No newline at end of file