mirror of
https://github.com/eclipse/upm.git
synced 2025-07-02 01:41:12 +03:00
Collision: Added C Src and Example
This module was initially known as GroveCollision. It has been now changed to Collision. C source and examples added. Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
9
src/collision/CMakeLists.txt
Normal file
9
src/collision/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
||||
upm_mixed_module_init (NAME collision
|
||||
DESCRIPTION "upm collisionsensor module"
|
||||
C_HDR collision.h
|
||||
C_SRC collision.c
|
||||
CPP_HDR collision.hpp
|
||||
CPP_SRC collision.cxx
|
||||
FTI_SRC collision_fti.c
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa)
|
60
src/collision/collision.c
Normal file
60
src/collision/collision.c
Normal file
@ -0,0 +1,60 @@
|
||||
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Abhishek Malik <abhishek.malik@intel.com>
|
||||
* Copyright (c) 2015 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 "collision.h"
|
||||
|
||||
collision_context collision_init(int pin){
|
||||
collision_context dev =
|
||||
(collision_context)malloc(sizeof(struct _collision_context));
|
||||
|
||||
if (!dev)
|
||||
return NULL;
|
||||
|
||||
dev->gpio_pin = pin;
|
||||
dev->gpio = mraa_gpio_init(dev->gpio_pin);
|
||||
|
||||
if(mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN) != MRAA_SUCCESS)
|
||||
{
|
||||
free(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
void collision_close(collision_context dev){
|
||||
free(dev);
|
||||
}
|
||||
|
||||
upm_result_t collision_is_colliding(collision_context dev, bool* collision_val){
|
||||
int value = mraa_gpio_read(dev->gpio);
|
||||
|
||||
if(!value)
|
||||
*collision_val = true;
|
||||
else
|
||||
*collision_val = false;
|
||||
|
||||
return UPM_SUCCESS;
|
||||
}
|
53
src/collision/collision.cxx
Normal file
53
src/collision/collision.cxx
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Copyright (c) 2015 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 <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "collision.hpp"
|
||||
|
||||
using namespace upm;
|
||||
|
||||
Collision::Collision(int 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);
|
||||
}
|
||||
|
||||
Collision::~Collision()
|
||||
{
|
||||
mraa_gpio_close(m_gpio);
|
||||
}
|
||||
|
||||
bool Collision::isColliding()
|
||||
{
|
||||
// Collisions cause 0; no collision is 1
|
||||
return (!(bool)mraa_gpio_read(m_gpio));
|
||||
}
|
91
src/collision/collision.h
Normal file
91
src/collision/collision.h
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Abhishek Malik <abhishek.malik@intel.com>
|
||||
* Copyright (c) 2015 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 COLLISION_H_
|
||||
#define COLLISION_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "upm.h"
|
||||
#include "mraa/gpio.h"
|
||||
|
||||
/**
|
||||
* @brief MVS0608 - GPIO Collision Sensor library
|
||||
* @ingroup gpio accelerometer
|
||||
*/
|
||||
|
||||
/**
|
||||
* @library collision
|
||||
* @sensor Collision
|
||||
* @comname Collision Sensor
|
||||
* @altname Grove Collision Sensor
|
||||
* @type accelerometer
|
||||
* @con gpio
|
||||
*
|
||||
* @brief API for the Collision Sensor
|
||||
*
|
||||
* MVS0608 can detect whether any
|
||||
* collision movement or vibration happens.
|
||||
* It outputs a low pulse signal when vibration is detected.
|
||||
*
|
||||
* @image html grovecollision.jpg
|
||||
* @snippet collision.c Interesting
|
||||
*/
|
||||
|
||||
/**
|
||||
* device context
|
||||
*/
|
||||
typedef struct _collision_context {
|
||||
mraa_gpio_context gpio;
|
||||
uint8_t gpio_pin;
|
||||
} *collision_context;
|
||||
|
||||
/**
|
||||
* Collision Initialization function
|
||||
*
|
||||
* @param pin number
|
||||
* @return void* pointer to the sensor struct
|
||||
*/
|
||||
collision_context collision_init(int pin);
|
||||
|
||||
/**
|
||||
* Collision Initialization function
|
||||
*
|
||||
* @param void* pointer to the sensor struct
|
||||
*/
|
||||
void collision_close(collision_context dev);
|
||||
|
||||
/**
|
||||
* This function tells you whether the sensor has
|
||||
* collided with something or not.
|
||||
*
|
||||
* @param void* pointer to the sensor struct
|
||||
* @param bool* pointer to hold the collision value
|
||||
* @return upm_result_t UPM success/error code
|
||||
*/
|
||||
upm_result_t collision_is_colliding(collision_context dev, bool* collision_val);
|
||||
|
||||
#endif /* COLLISION_H_ */
|
73
src/collision/collision.hpp
Normal file
73
src/collision/collision.hpp
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Copyright (c) 2015 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 <mraa/gpio.h>
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
* @brief Collision Sensor library
|
||||
* @defgroup collision libupm-collision
|
||||
* @ingroup seeed gpio accelerometer
|
||||
*/
|
||||
|
||||
/**
|
||||
* @library collision
|
||||
* @sensor collision
|
||||
* @comname Collision Sensor
|
||||
* @altname Grove Collision Sensor
|
||||
* @type accelerometer
|
||||
* @man seeed
|
||||
* @con gpio
|
||||
*
|
||||
* @brief API for the Collision Sensor
|
||||
*
|
||||
* The Collision Sensor can detect whether any
|
||||
* collision movement or vibration happens.
|
||||
* It outputs a low pulse signal when vibration is detected.
|
||||
*
|
||||
* @image html grovecollision.jpg
|
||||
* @snippet collision.cxx Interesting
|
||||
*/
|
||||
class Collision {
|
||||
public:
|
||||
/**
|
||||
* Collision sensor constructor
|
||||
*
|
||||
* @param pin Digital pin to use
|
||||
*/
|
||||
Collision(int pin);
|
||||
/**
|
||||
* Collision destructor
|
||||
*/
|
||||
~Collision();
|
||||
/**
|
||||
* @return bool Defines whether something is colliding with sensor
|
||||
*/
|
||||
bool isColliding();
|
||||
|
||||
private:
|
||||
mraa_gpio_context m_gpio;
|
||||
};
|
||||
}
|
78
src/collision/collision_fti.c
Normal file
78
src/collision/collision_fti.c
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Author: Zion Orent <sorent@ics.com>
|
||||
* Abhishek Malik <abhishek.malik@intel.com>
|
||||
* Copyright (c) 2015 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 "collision.h"
|
||||
#include "upm_fti.h"
|
||||
|
||||
/**
|
||||
* This file implements the Function Table Interface (FTI) for this sensor
|
||||
*/
|
||||
|
||||
const char upm_collision_name[] = "Collision";
|
||||
const char upm_collision_description[] = "Collision sensor";
|
||||
const upm_protocol_t upm_collision_protocol[] = {UPM_GPIO};
|
||||
const upm_sensor_t upm_collision_category[] = {UPM_ELECTRICITY};
|
||||
|
||||
// forward declarations
|
||||
const upm_sensor_descriptor_t upm_collision_get_descriptor();
|
||||
const void* upm_collision_get_ft(upm_sensor_t sensor_type);
|
||||
void* upm_collision_init_name();
|
||||
void upm_collision_close(void* dev);
|
||||
|
||||
const upm_sensor_descriptor_t upm_collision_get_descriptor(){
|
||||
upm_sensor_descriptor_t usd;
|
||||
usd.name = upm_collision_name;
|
||||
usd.description = upm_collision_description;
|
||||
usd.protocol_size = 1;
|
||||
usd.protocol = upm_collision_protocol;
|
||||
usd.category_size = 1;
|
||||
usd.category = upm_collision_category;
|
||||
return usd;
|
||||
}
|
||||
|
||||
static const upm_sensor_ft ft =
|
||||
{
|
||||
.upm_sensor_init_name = &upm_collision_init_name,
|
||||
.upm_sensor_close = &upm_collision_close,
|
||||
.upm_sensor_get_descriptor = &upm_collision_get_descriptor
|
||||
};
|
||||
|
||||
const void* upm_collision_get_ft(upm_sensor_t sensor_type){
|
||||
if(sensor_type == UPM_SENSOR){
|
||||
return &ft;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* upm_collision_init_name(){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void upm_collision_close(void* dev){
|
||||
collision_close((collision_context)dev);
|
||||
}
|
||||
|
||||
upm_result_t upm_collision_is_colliding(void* dev, bool* collision_val){
|
||||
return collision_is_colliding((collision_context)dev, collision_val);
|
||||
}
|
19
src/collision/javaupm_collision.i
Normal file
19
src/collision/javaupm_collision.i
Normal file
@ -0,0 +1,19 @@
|
||||
%module javaupm_collision
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "collision.hpp"
|
||||
%}
|
||||
|
||||
%include "collision.hpp"
|
||||
|
||||
%pragma(java) jniclasscode=%{
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("javaupm_collision");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Native code library failed to load. \n" + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
%}
|
8
src/collision/jsupm_collision.i
Normal file
8
src/collision/jsupm_collision.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module jsupm_collision
|
||||
%include "../upm.i"
|
||||
|
||||
%{
|
||||
#include "collision.hpp"
|
||||
%}
|
||||
|
||||
%include "collision.hpp"
|
11
src/collision/pyupm_collision.i
Normal file
11
src/collision/pyupm_collision.i
Normal file
@ -0,0 +1,11 @@
|
||||
// Include doxygen-generated documentation
|
||||
%include "pyupm_doxy2swig.i"
|
||||
%module pyupm_collision
|
||||
%include "../upm.i"
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "collision.hpp"
|
||||
%{
|
||||
#include "collision.hpp"
|
||||
%}
|
Reference in New Issue
Block a user