water: add C driver and example. C++ wraps C driver.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson 2016-09-22 16:42:16 -06:00
parent 29b11063ee
commit dff782a477
11 changed files with 364 additions and 18 deletions

View File

@ -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)

View File

@ -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;
}

70
examples/c/water.c Normal file
View File

@ -0,0 +1,70 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* 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 <unistd.h>
#include <signal.h>
#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;
}

43
include/fti/upm_binary.h Normal file
View File

@ -0,0 +1,43 @@
/*
* Authors: Jon Trulson <jtrulson@ics.com>
* 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_ */

View File

@ -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 <fti/upm_orp.h>
#include <fti/upm_ec.h>
#include <fti/upm_humidity.h>
#include <fti/upm_binary.h>
#ifdef __cplusplus
}

View File

@ -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)

78
src/water/water.c Normal file
View File

@ -0,0 +1,78 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* 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 <assert.h>
#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);
}

View File

@ -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);
}

70
src/water/water.h Normal file
View File

@ -0,0 +1,70 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* 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

View File

@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* 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 <string>
#include <mraa/gpio.h>
#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;
};
}

82
src/water/water_fti.c Normal file
View File

@ -0,0 +1,82 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* 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;
}