biss0001: 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-23 14:17:42 -06:00
parent 22c7310428
commit b8061943aa
8 changed files with 380 additions and 60 deletions

View File

@ -121,6 +121,7 @@ add_example (dfrec)
add_example (sht1x)
add_example (water)
add_example (yg1006)
add_example (biss0001)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

70
examples/c/biss0001.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 "biss0001.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 BISS0001 sensor on digital pin D2
biss0001_context sensor = biss0001_init(2);
if (!sensor)
{
printf("biss0001_init() failed.\n");
return(1);
}
while (shouldRun)
{
if (biss0001_motion_detected(sensor))
printf("Motion detected.\n");
else
printf("No motion detected.\n");
upm_delay(1);
}
//! [Interesting]
printf("Exiting...\n");
biss0001_close(sensor);
return 0;
}

View File

@ -1,5 +1,9 @@
set (libname "biss0001")
set (libdescription "upm biss0001 motion module")
set (module_src ${libname}.cxx)
set (module_hpp ${libname}.hpp)
upm_module_init()
upm_mixed_module_init (NAME biss0001
DESCRIPTION "UPM biss0001 PIR motion sensor"
C_HDR biss0001.h
C_SRC biss0001.c
CPP_HDR biss0001.hpp
CPP_SRC biss0001.cxx
FTI_SRC biss0001_fti.c
CPP_WRAPS_C
REQUIRES mraa)

77
src/biss0001/biss0001.c Normal file
View File

@ -0,0 +1,77 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Zion Orent <zorent@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 "biss0001.h"
biss0001_context biss0001_init(unsigned int pin)
{
biss0001_context dev =
(biss0001_context)malloc(sizeof(struct _biss0001_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);
biss0001_close(dev);
return NULL;
}
// initialize the MRAA context
if (!(dev->gpio = mraa_gpio_init(pin)))
{
printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
biss0001_close(dev);
return NULL;
}
mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN);
return dev;
}
void biss0001_close(biss0001_context dev)
{
assert(dev != NULL);
if (dev->gpio)
mraa_gpio_close(dev->gpio);
free(dev);
}
bool biss0001_motion_detected(const biss0001_context dev)
{
assert(dev != NULL);
return (mraa_gpio_read(dev->gpio) ? true : false);
}

View File

@ -1,6 +1,7 @@
/*
* Author: Zion Orent <sorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
* 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
@ -30,23 +31,20 @@
using namespace upm;
BISS0001::BISS0001(int pin)
BISS0001::BISS0001(unsigned int pin) :
m_biss0001(biss0001_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_biss0001)
throw std::runtime_error(std::string(__FUNCTION__) +
": biss0001_init() failed");
}
BISS0001::~BISS0001()
{
mraa_gpio_close(m_gpio);
biss0001_close(m_biss0001);
}
bool BISS0001::value()
{
return (mraa_gpio_read(m_gpio) ? true : false);
return biss0001_motion_detected(m_biss0001);
}

76
src/biss0001/biss0001.h Normal file
View File

@ -0,0 +1,76 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Zion Orent <zorent@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 BISS0001 PIR Motion Sensor
*
* This module tests for movement in it's detecting range.
*
* @snippet biss0001.c Interesting
*/
/**
* Device context
*/
typedef struct _biss0001_context {
mraa_gpio_context gpio;
} *biss0001_context;
/**
* BISS0001 initilaizer
*
* @param pin Digital pin to use.
* @return an initialized device context on success, NULL on error.
*/
biss0001_context biss0001_init(unsigned int pin);
/**
* BISS0001 close function
*
* @param dev The device context.
*/
void biss0001_close(biss0001_context dev);
/**
* Gets the motion value from the sensor
*
* @param dev The device context.
* @return true if motion was detected, false otherwise
*/
bool biss0001_motion_detected(const biss0001_context dev);
#ifdef __cplusplus
}
#endif

View File

@ -1,6 +1,7 @@
/*
* Author: Zion Orent <sorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
* Author: Zion Orent <zorent@ics.com>
* 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
@ -23,55 +24,66 @@
*/
#pragma once
#include <string>
#include <mraa/aio.h>
#include <biss0001.h>
namespace upm {
/**
* @brief BISS0001 Motion Sensor library
* @defgroup biss0001 libupm-biss0001
* @ingroup seeed gpio light tsk
*/
/**
* @brief BISS0001 Motion Sensor library
* @defgroup biss0001 libupm-biss0001
* @ingroup seeed gpio light tsk
*/
/**
* @library biss0001
* @sensor biss0001
* @comname BISS0001 Motion Sensor
* @altname Grove PIR Motion Sensor
* @type light
* @man seeed
* @web http://www.seeedstudio.com/depot/Grove-PIR-Motion-Sensor-p-802.html
* @con gpio
* @kit tsk
*
* @brief API for the BISS0001 Motion Sensor
*
* UPM module for the BISS0001 Motion Sensor
*
* @image html biss0001.jpg
* @snippet biss0001.cxx Interesting
*/
/**
* @library biss0001
* @sensor biss0001
* @comname BISS0001 Motion Sensor
* @altname Grove PIR Motion Sensor
* @type light
* @man seeed
* @web http://www.seeedstudio.com/depot/Grove-PIR-Motion-Sensor-p-802.html
* @con gpio
* @kit tsk
*
* @brief API for the BISS0001 Motion Sensor
*
* UPM module for the BISS0001 Motion Sensor
*
* @image html biss0001.jpg
* @snippet biss0001.cxx Interesting
*/
class BISS0001 {
public:
/**
* BISS0001 motion sensor constructor
*
* @param pin Digital pin to use
*/
BISS0001(int pin);
/**
* BISS0001 destructor
*/
~BISS0001();
/**
* Gets the motion value from the sensor
*
* @return Motion reading
*/
bool value();
/**
* BISS0001 motion sensor constructor
*
* @param pin Digital pin to use
*/
BISS0001(unsigned int pin);
/**
* BISS0001 destructor
*/
~BISS0001();
/**
* Gets the motion value from the sensor.
*
* @return true if motion was detected, false otherwise.
*/
bool value();
/**
* Gets the motion value from the sensor. This is a more
* informative method name, but we want to keep compatibility
* with the original for now.
*
* @return true if motion was detected, false otherwise.
*/
bool motionDetected() { return value(); };
private:
mraa_gpio_context m_gpio;
biss0001_context m_biss0001;
};
}

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 "biss0001.h"
#include <upm_fti.h>
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_biss0001_name[] = "BISS0001";
const char upm_biss0001_description[] = "PIR Motion Sensor";
const upm_protocol_t upm_biss0001_protocol[] = {UPM_GPIO};
const upm_sensor_t upm_biss0001_category[] = {UPM_BINARY};
// forward declarations
const void* upm_biss0001_get_ft(upm_sensor_t sensor_type);
void* upm_biss0001_init_name();
void upm_biss0001_close(void *dev);
upm_result_t upm_biss0001_motion_detected(void *dev, bool *value);
static const upm_sensor_ft ft =
{
.upm_sensor_init_name = upm_biss0001_init_name,
.upm_sensor_close = upm_biss0001_close,
};
static const upm_binary_ft bft =
{
.upm_binary_get_value = upm_biss0001_motion_detected,
};
const void* upm_biss0001_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_biss0001_init_name()
{
return NULL;
}
void upm_biss0001_close(void *dev)
{
biss0001_close((biss0001_context)dev);
}
upm_result_t upm_biss0001_motion_detected(void *dev, bool *value)
{
*value = biss0001_motion_detected((biss0001_context)dev);
return UPM_SUCCESS;
}