From dff782a4772c3763df9e29ad3e130c56a15d5afe Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 22 Sep 2016 16:42:16 -0600 Subject: [PATCH] water: add C driver and example. C++ wraps C driver. Signed-off-by: Jon Trulson --- examples/c/CMakeLists.txt | 1 + examples/c/dfrec.c | 4 +- examples/c/water.c | 70 +++++++++++++++++++++++++++++++++ include/fti/upm_binary.h | 43 ++++++++++++++++++++ include/upm_fti.h | 4 +- src/water/CMakeLists.txt | 4 ++ src/water/water.c | 78 +++++++++++++++++++++++++++++++++++++ src/water/water.cxx | 18 ++++----- src/water/water.h | 70 +++++++++++++++++++++++++++++++++ src/water/water.hpp | 8 ++-- src/water/water_fti.c | 82 +++++++++++++++++++++++++++++++++++++++ 11 files changed, 364 insertions(+), 18 deletions(-) create mode 100644 examples/c/water.c create mode 100644 include/fti/upm_binary.h create mode 100644 src/water/water.c create mode 100644 src/water/water.h create mode 100644 src/water/water_fti.c diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 41e84bf7..e7b7bd73 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -119,6 +119,7 @@ add_example (led) add_example (ds18b20) add_example (dfrec) add_example (sht1x) +add_example (water) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/dfrec.c b/examples/c/dfrec.c index c2e8b855..31af6c83 100644 --- a/examples/c/dfrec.c +++ b/examples/c/dfrec.c @@ -39,9 +39,9 @@ void sig_handler(int signo) int main() { + if (mraa_init() != MRAA_SUCCESS) { - if (mraa_init() != MRAA_SUCCESS) - perror("Failed to initialize mraa\n"); + printf("Failed to initialize mraa\n"); return -1; } diff --git a/examples/c/water.c b/examples/c/water.c new file mode 100644 index 00000000..1ebc4bfc --- /dev/null +++ b/examples/c/water.c @@ -0,0 +1,70 @@ +/* + * Author: Jon Trulson + * 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 +#include + +#include "water.h" +#include "upm_utilities.h" + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main () +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a Water sensor on digital pin D2 + water_context sensor = water_init(2); + + if (!sensor) + { + printf("dfrec_init() failed.\n"); + return(1); + } + + while (shouldRun) + { + if (water_is_wet(sensor)) + printf("Sensor is wet\n"); + else + printf("Sensor is dry\n"); + + upm_delay(1); + } +//! [Interesting] + + printf("Exiting...\n"); + + water_close(sensor); + + return 0; +} diff --git a/include/fti/upm_binary.h b/include/fti/upm_binary.h new file mode 100644 index 00000000..097c965b --- /dev/null +++ b/include/fti/upm_binary.h @@ -0,0 +1,43 @@ +/* + * Authors: Jon Trulson + * 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 UPM_BINARY_H_ +#define UPM_BINARY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +// Binary function table. This table is for simple sensors that just +// return a single TRUE/FALSE, ON/OFF type values. It can also be +// used to set a simple binary value. +typedef struct _upm_binary_ft { + upm_result_t (*upm_binary_get_value) (void* dev, bool* value); + upm_result_t (*upm_binary_set_value) (void* dev, bool value); +} upm_binary_ft; + +#ifdef __cplusplus +} +#endif + +#endif /* UPM_BINARY_H_ */ diff --git a/include/upm_fti.h b/include/upm_fti.h index e2153acd..bd227c6a 100644 --- a/include/upm_fti.h +++ b/include/upm_fti.h @@ -73,7 +73,8 @@ typedef enum { UPM_VOLTAGE, UPM_WIRELESS, UPM_STREAM, - UPM_ORP + UPM_ORP, + UPM_BINARY } upm_sensor_t; /* Supported IO protocols via MRAA */ @@ -120,6 +121,7 @@ typedef struct _upm_sensor_ft* (*func_get_upm_sensor_ft)(upm_sensor_t sensor_typ #include #include #include +#include #ifdef __cplusplus } diff --git a/src/water/CMakeLists.txt b/src/water/CMakeLists.txt index 5873f29e..6f304034 100644 --- a/src/water/CMakeLists.txt +++ b/src/water/CMakeLists.txt @@ -1,5 +1,9 @@ upm_mixed_module_init (NAME water DESCRIPTION "UPM water sensor module" + C_HDR water.h + C_SRC water.c CPP_HDR water.hpp CPP_SRC water.cxx + FTI_SRC water_fti.c + CPP_WRAPS_C REQUIRES mraa) diff --git a/src/water/water.c b/src/water/water.c new file mode 100644 index 00000000..00e0d142 --- /dev/null +++ b/src/water/water.c @@ -0,0 +1,78 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2014-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 +#include "water.h" + +water_context water_init(unsigned int pin) +{ + water_context dev = + (water_context)malloc(sizeof(struct _water_context)); + + if (!dev) + return NULL; + + dev->gpio = NULL; + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + printf("mraa_init() failed (%d).\n", mraa_rv); + mraa_result_print(mraa_rv); + water_close(dev); + return NULL; + } + + // initialize the MRAA context + + if (!(dev->gpio = mraa_gpio_init(pin))) + { + printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); + water_close(dev); + return NULL; + } + + mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN); + + return dev; +} + +void water_close(const water_context dev) +{ + assert(dev != NULL); + + if (dev->gpio) + mraa_gpio_close(dev->gpio); + + free(dev); +} + +bool water_is_wet(const water_context dev) +{ + assert(dev != NULL); + + // gpio is low when wet + return (mraa_gpio_read(dev->gpio) ? false : true); +} diff --git a/src/water/water.cxx b/src/water/water.cxx index f62f7072..cf7d8e96 100644 --- a/src/water/water.cxx +++ b/src/water/water.cxx @@ -31,24 +31,20 @@ using namespace upm; using namespace std; -Water::Water(int pin) +Water::Water(unsigned int pin) : + m_water(water_init(pin)) { - if ( !(m_gpio = mraa_gpio_init(pin)) ) - { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_gpio_init() failed, invalid pin?"); - return; - } - - mraa_gpio_dir(m_gpio, MRAA_GPIO_IN); + if (!m_water ) + throw std::runtime_error(std::string(__FUNCTION__) + + ": water_init() failed"); } Water::~Water() { - mraa_gpio_close(m_gpio); + water_close(m_water); } bool Water::isWet() { - return (!mraa_gpio_read(m_gpio) ? true : false); + return water_is_wet(m_water); } diff --git a/src/water/water.h b/src/water/water.h new file mode 100644 index 00000000..2c546343 --- /dev/null +++ b/src/water/water.h @@ -0,0 +1,70 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2014-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. + */ +#pragma once + +#include "upm.h" +#include "mraa/gpio.h" + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @brief Water Sensor + * + * This module simply tests for the presence of water. + * + * @snippet water.c Interesting + */ + + /** + * Device context + */ + typedef struct _water_context { + mraa_gpio_context gpio; + } *water_context; + + /** + * Water initializer + * + * @param pin Digital pin to use + * @return an initialized device context on success, NULL on error. + */ + water_context water_init(unsigned int pin); + + /** + * Water close function + */ + void water_close(water_context dev); + + /** + * Gets the water (wet/not wet) value from the sensor. + * + * @return true if the sensor is wet, false otherwise + */ + bool water_is_wet(const water_context dev); + +#ifdef __cplusplus +} +#endif diff --git a/src/water/water.hpp b/src/water/water.hpp index 6d83f64b..6b24472c 100644 --- a/src/water/water.hpp +++ b/src/water/water.hpp @@ -1,6 +1,6 @@ /* * Author: Jon Trulson - * Copyright (c) 2014 Intel Corporation. + * Copyright (c) 2014-2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -24,7 +24,7 @@ #pragma once #include -#include +#include "water.h" namespace upm { /** @@ -56,7 +56,7 @@ namespace upm { * * @param pin Digital pin to use */ - Water(int pin); + Water(unsigned int pin); /** * Water destructor */ @@ -69,6 +69,6 @@ namespace upm { bool isWet(); private: - mraa_gpio_context m_gpio; + water_context m_water; }; } diff --git a/src/water/water_fti.c b/src/water/water_fti.c new file mode 100644 index 00000000..8eb26f99 --- /dev/null +++ b/src/water/water_fti.c @@ -0,0 +1,82 @@ +/* + * Author: Jon Trulson + * 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 "water.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_water_name[] = "water"; +const char upm_water_description[] = "Water presence sensor"; +const upm_protocol_t upm_water_protocol[] = {UPM_GPIO}; +const upm_sensor_t upm_water_category[] = {UPM_BINARY}; + +// forward declarations +const void* upm_water_get_ft(upm_sensor_t sensor_type); +void* upm_water_init_name(); +void upm_water_close(void *dev); +upm_result_t upm_water_is_wet(void *dev, bool *value); + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = upm_water_init_name, + .upm_sensor_close = upm_water_close, +}; + +static const upm_binary_ft bft = +{ + .upm_binary_get_value = upm_water_is_wet, +}; + +const void* upm_water_get_ft(upm_sensor_t sensor_type) +{ + switch(sensor_type) + { + case UPM_SENSOR: + return &ft; + case UPM_BINARY: + return &bft; + default: + return NULL; + } +} + +void *upm_water_init_name() +{ + return NULL; +} + +void upm_water_close(void *dev) +{ + water_close((water_context)dev); +} + +upm_result_t upm_water_is_wet(void *dev, bool *value) +{ + *value = water_is_wet((water_context)dev); + + return UPM_SUCCESS; +}