mirror of
https://github.com/eclipse/upm.git
synced 2025-03-24 01:10:22 +03:00
yg1006: add C driver and example, C++ wraps C driver
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
c9ed65f22d
commit
22c7310428
@ -120,6 +120,7 @@ add_example (ds18b20)
|
|||||||
add_example (dfrec)
|
add_example (dfrec)
|
||||||
add_example (sht1x)
|
add_example (sht1x)
|
||||||
add_example (water)
|
add_example (water)
|
||||||
|
add_example (yg1006)
|
||||||
|
|
||||||
# Custom examples
|
# Custom examples
|
||||||
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
||||||
|
70
examples/c/yg1006.c
Normal file
70
examples/c/yg1006.c
Normal 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 "yg1006.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 YG1006 sensor on digital pin D2
|
||||||
|
yg1006_context sensor = yg1006_init(2);
|
||||||
|
|
||||||
|
if (!sensor)
|
||||||
|
{
|
||||||
|
printf("yg1006_init() failed.\n");
|
||||||
|
return(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (shouldRun)
|
||||||
|
{
|
||||||
|
if (yg1006_flame_detected(sensor))
|
||||||
|
printf("Flame detected.\n");
|
||||||
|
else
|
||||||
|
printf("No flame detected.\n");
|
||||||
|
|
||||||
|
upm_delay(1);
|
||||||
|
}
|
||||||
|
//! [Interesting]
|
||||||
|
|
||||||
|
printf("Exiting...\n");
|
||||||
|
|
||||||
|
yg1006_close(sensor);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
set (libname "yg1006")
|
upm_mixed_module_init (NAME yg1006
|
||||||
set (libdescription "upm yg1006 flame sensor module")
|
DESCRIPTION "UPM flame sensor"
|
||||||
set (module_src ${libname}.cxx)
|
C_HDR yg1006.h
|
||||||
set (module_hpp ${libname}.hpp)
|
C_SRC yg1006.c
|
||||||
upm_module_init()
|
CPP_HDR yg1006.hpp
|
||||||
|
CPP_SRC yg1006.cxx
|
||||||
|
FTI_SRC yg1006_fti.c
|
||||||
|
CPP_WRAPS_C
|
||||||
|
REQUIRES mraa)
|
||||||
|
77
src/yg1006/yg1006.c
Normal file
77
src/yg1006/yg1006.c
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* 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 <assert.h>
|
||||||
|
#include "yg1006.h"
|
||||||
|
|
||||||
|
yg1006_context yg1006_init(unsigned int pin)
|
||||||
|
{
|
||||||
|
yg1006_context dev =
|
||||||
|
(yg1006_context)malloc(sizeof(struct _yg1006_context));
|
||||||
|
|
||||||
|
if (!dev)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
dev->gpio = NULL;
|
||||||
|
|
||||||
|
// make sure MRAA is initialized
|
||||||
|
int mraa_rv;
|
||||||
|
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
|
||||||
|
{
|
||||||
|
printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
|
||||||
|
yg1006_close(dev);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize the MRAA context
|
||||||
|
|
||||||
|
if (!(dev->gpio = mraa_gpio_init(pin)))
|
||||||
|
{
|
||||||
|
printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
|
||||||
|
yg1006_close(dev);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN);
|
||||||
|
|
||||||
|
return dev;
|
||||||
|
}
|
||||||
|
|
||||||
|
void yg1006_close(yg1006_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
if (dev->gpio)
|
||||||
|
mraa_gpio_close(dev->gpio);
|
||||||
|
|
||||||
|
free(dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool yg1006_flame_detected(const yg1006_context dev)
|
||||||
|
{
|
||||||
|
assert(dev != NULL);
|
||||||
|
|
||||||
|
// gpio is low when a flame is detected
|
||||||
|
return (mraa_gpio_read(dev->gpio) ? false : true);
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Jon Trulson <jtrulson@ics.com>
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -31,24 +31,20 @@
|
|||||||
using namespace upm;
|
using namespace upm;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
YG1006::YG1006(int pin)
|
YG1006::YG1006(unsigned int pin) :
|
||||||
|
m_yg1006(yg1006_init(pin))
|
||||||
{
|
{
|
||||||
if ( !(m_gpio = mraa_gpio_init(pin)) )
|
if (!m_yg1006)
|
||||||
{
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
": water_init() failed");
|
||||||
": mraa_gpio_init() failed, invalid pin?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
YG1006::~YG1006()
|
YG1006::~YG1006()
|
||||||
{
|
{
|
||||||
mraa_gpio_close(m_gpio);
|
yg1006_close(m_yg1006);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool YG1006::flameDetected()
|
bool YG1006::flameDetected()
|
||||||
{
|
{
|
||||||
return (!mraa_gpio_read(m_gpio) ? true : false);
|
return yg1006_flame_detected(m_yg1006);
|
||||||
}
|
}
|
||||||
|
74
src/yg1006/yg1006.h
Normal file
74
src/yg1006/yg1006.h
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <upm.h>
|
||||||
|
#include <mraa/gpio.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief YG1006 Flame Sensor
|
||||||
|
*
|
||||||
|
* UPM module for the YG1006 flame sensor. It detects a flame or any
|
||||||
|
* other light source in the 760-1,100 nm wavelength range.
|
||||||
|
*
|
||||||
|
* @snippet yg1006.cxx Interesting
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Device context
|
||||||
|
*/
|
||||||
|
typedef struct _yg1006_context {
|
||||||
|
mraa_gpio_context gpio;
|
||||||
|
} *yg1006_context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* YG1006 initializer
|
||||||
|
*
|
||||||
|
* @param pin Digital pin to use
|
||||||
|
* @return an initialized device context on success, NULL on error.
|
||||||
|
*/
|
||||||
|
yg1006_context yg1006_init(unsigned int pin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* YG1006 close function
|
||||||
|
*/
|
||||||
|
void yg1006_close(yg1006_context dev);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether a flame has been detected
|
||||||
|
*
|
||||||
|
* @return true if a flame or another comparable light source has
|
||||||
|
* been detected
|
||||||
|
*/
|
||||||
|
bool yg1006_flame_detected(const yg1006_context dev);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Jon Trulson <jtrulson@ics.com>
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -24,7 +24,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mraa/gpio.h>
|
#include "yg1006.h"
|
||||||
|
|
||||||
namespace upm {
|
namespace upm {
|
||||||
/**
|
/**
|
||||||
@ -32,6 +32,7 @@ namespace upm {
|
|||||||
* @defgroup yg1006 libupm-yg1006
|
* @defgroup yg1006 libupm-yg1006
|
||||||
* @ingroup seeed gpio light hak
|
* @ingroup seeed gpio light hak
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @library yg1006
|
* @library yg1006
|
||||||
* @sensor yg1006
|
* @sensor yg1006
|
||||||
@ -53,25 +54,29 @@ namespace upm {
|
|||||||
*/
|
*/
|
||||||
class YG1006 {
|
class YG1006 {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YG1006 constructor
|
* YG1006 constructor
|
||||||
*
|
*
|
||||||
* @param pin Digital pin to use
|
* @param pin Digital pin to use
|
||||||
*/
|
*/
|
||||||
YG1006(int pin);
|
YG1006(unsigned int pin);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YG1006 destructor
|
* YG1006 destructor
|
||||||
*/
|
*/
|
||||||
~YG1006();
|
~YG1006();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines whether a flame has been detected
|
* Determines whether a flame has been detected
|
||||||
*
|
*
|
||||||
* @return True if a flame or another comparable light source has been detected
|
* @return true if a flame or another comparable light source has
|
||||||
|
* been detected
|
||||||
*/
|
*/
|
||||||
bool flameDetected();
|
bool flameDetected();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mraa_gpio_context m_gpio;
|
yg1006_context m_yg1006;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
82
src/yg1006/yg1006_fti.c
Normal file
82
src/yg1006/yg1006_fti.c
Normal 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 "yg1006.h"
|
||||||
|
#include <upm_fti.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file implements the Function Table Interface (FTI) for this sensor
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char upm_yg1006_name[] = "YG1006";
|
||||||
|
const char upm_yg1006_description[] = "Flame sensor";
|
||||||
|
const upm_protocol_t upm_yg1006_protocol[] = {UPM_GPIO};
|
||||||
|
const upm_sensor_t upm_yg1006_category[] = {UPM_BINARY};
|
||||||
|
|
||||||
|
// forward declarations
|
||||||
|
const void* upm_yg1006_get_ft(upm_sensor_t sensor_type);
|
||||||
|
void* upm_yg1006_init_name();
|
||||||
|
void upm_yg1006_close(void *dev);
|
||||||
|
upm_result_t upm_yg1006_flame_detected(void *dev, bool *value);
|
||||||
|
|
||||||
|
static const upm_sensor_ft ft =
|
||||||
|
{
|
||||||
|
.upm_sensor_init_name = upm_yg1006_init_name,
|
||||||
|
.upm_sensor_close = upm_yg1006_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const upm_binary_ft bft =
|
||||||
|
{
|
||||||
|
.upm_binary_get_value = upm_yg1006_flame_detected,
|
||||||
|
};
|
||||||
|
|
||||||
|
const void* upm_yg1006_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_yg1006_init_name()
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void upm_yg1006_close(void *dev)
|
||||||
|
{
|
||||||
|
yg1006_close((yg1006_context)dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
upm_result_t upm_yg1006_flame_detected(void *dev, bool *value)
|
||||||
|
{
|
||||||
|
*value = yg1006_flame_detected((yg1006_context)dev);
|
||||||
|
|
||||||
|
return UPM_SUCCESS;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user