mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
linefinder: C implementation; FTI; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
b5a33f6ee3
commit
f101db2af1
@ -128,6 +128,7 @@ add_example (lm35)
|
||||
add_example (rotaryencoder)
|
||||
add_example (rpr220)
|
||||
add_example (md)
|
||||
add_example (linefinder)
|
||||
|
||||
# Custom examples
|
||||
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
||||
|
72
examples/c/linefinder.c
Normal file
72
examples/c/linefinder.c
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <upm_utilities.h>
|
||||
#include "linefinder.h"
|
||||
|
||||
int shouldRun = true;
|
||||
|
||||
void sig_handler(int signo)
|
||||
{
|
||||
if (signo == SIGINT)
|
||||
shouldRun = false;
|
||||
}
|
||||
|
||||
|
||||
int main ()
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
//! [Interesting]
|
||||
// Instantiate a Line Finder sensor on digital pin D2
|
||||
linefinder_context sensor = linefinder_init(2);
|
||||
|
||||
if (!sensor)
|
||||
{
|
||||
printf("linefinder_init() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// check every second for the presence of white or black detection
|
||||
while (shouldRun)
|
||||
{
|
||||
if (linefinder_white_detected(sensor))
|
||||
printf("White detected.\n");
|
||||
else
|
||||
printf("Black detected.\n");
|
||||
|
||||
upm_delay(1);
|
||||
}
|
||||
|
||||
printf("Exiting...\n");
|
||||
|
||||
linefinder_close(sensor);
|
||||
|
||||
//! [Interesting]
|
||||
return 0;
|
||||
}
|
@ -1,5 +1,9 @@
|
||||
upm_mixed_module_init (NAME linefinder
|
||||
DESCRIPTION "Infrared (IR) based line finder"
|
||||
C_HDR linefinder.h
|
||||
C_SRC linefinder.c
|
||||
CPP_HDR linefinder.hpp
|
||||
CPP_SRC linefinder.cxx
|
||||
FTI_SRC linefinder_fti.c
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa)
|
||||
|
84
src/linefinder/linefinder.c
Normal file
84
src/linefinder/linefinder.c
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 "linefinder.h"
|
||||
|
||||
linefinder_context linefinder_init(int pin)
|
||||
{
|
||||
linefinder_context dev =
|
||||
(linefinder_context)malloc(sizeof(struct _linefinder_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);
|
||||
linefinder_close(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// MRAA contexts...
|
||||
if ( !(dev->gpio = mraa_gpio_init(pin)) )
|
||||
{
|
||||
printf("%s: mraa_gpio_init() failed\n",
|
||||
__FUNCTION__);
|
||||
linefinder_close(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN);
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
void linefinder_close(linefinder_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (dev->gpio)
|
||||
mraa_gpio_close(dev->gpio);
|
||||
|
||||
free(dev);
|
||||
}
|
||||
|
||||
bool linefinder_white_detected(const linefinder_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
return (!mraa_gpio_read(dev->gpio) ? true : false);
|
||||
}
|
||||
|
||||
bool linefinder_black_detected(const linefinder_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
return (mraa_gpio_read(dev->gpio) ? true : false);
|
||||
}
|
@ -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
|
||||
@ -31,29 +31,25 @@
|
||||
using namespace upm;
|
||||
using namespace std;
|
||||
|
||||
LineFinder::LineFinder(int pin)
|
||||
LineFinder::LineFinder(int pin) :
|
||||
m_linefinder(linefinder_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_linefinder)
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": linefinder_init() failed");
|
||||
}
|
||||
|
||||
LineFinder::~LineFinder()
|
||||
{
|
||||
mraa_gpio_close(m_gpio);
|
||||
linefinder_close(m_linefinder);
|
||||
}
|
||||
|
||||
bool LineFinder::whiteDetected()
|
||||
{
|
||||
return (!mraa_gpio_read(m_gpio) ? true : false);
|
||||
return linefinder_white_detected(m_linefinder);
|
||||
}
|
||||
|
||||
bool LineFinder::blackDetected()
|
||||
{
|
||||
return (mraa_gpio_read(m_gpio) ? true : false);
|
||||
return linefinder_black_detected(m_linefinder);
|
||||
}
|
||||
|
77
src/linefinder/linefinder.h
Normal file
77
src/linefinder/linefinder.h
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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <upm.h>
|
||||
|
||||
#include <mraa/gpio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Device context
|
||||
*/
|
||||
typedef struct _linefinder_context {
|
||||
mraa_gpio_context gpio;
|
||||
} *linefinder_context;
|
||||
|
||||
/**
|
||||
* Line Finder inititialization
|
||||
*
|
||||
* @param pin Digital pin to use
|
||||
* @return device context
|
||||
*/
|
||||
linefinder_context linefinder_init(int pin);
|
||||
|
||||
/**
|
||||
* LineFinder close
|
||||
*
|
||||
* @param Device context
|
||||
*/
|
||||
void linefinder_close(linefinder_context dev);
|
||||
|
||||
/**
|
||||
* Determines whether white has been detected
|
||||
*
|
||||
* @param Device context
|
||||
* @return True if white is detected
|
||||
*/
|
||||
bool linefinder_white_detected(const linefinder_context dev);
|
||||
|
||||
/**
|
||||
* Determines whether black has been detected
|
||||
*
|
||||
* @param Device context
|
||||
* @return True if black is detected
|
||||
*/
|
||||
bool linefinder_black_detected(const linefinder_context dev);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -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
|
||||
@ -23,63 +23,65 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <mraa/gpio.h>
|
||||
#include <linefinder.h>
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
* @brief Line Finder Sensor library
|
||||
* @defgroup linefinder libupm-linefinder
|
||||
* @ingroup seeed dfrobot gpio color robok
|
||||
*/
|
||||
/**
|
||||
* @brief Line Finder Sensor library
|
||||
* @defgroup linefinder libupm-linefinder
|
||||
* @ingroup seeed dfrobot gpio color robok
|
||||
*/
|
||||
|
||||
/**
|
||||
* @library linefinder
|
||||
* @sensor linefinder
|
||||
* @comname Line Finder
|
||||
* @altname Grove Line Finder, DFRobot Line Tracking sensor
|
||||
* @type color
|
||||
* @man seeed, dfrobot
|
||||
* @con gpio
|
||||
* @kit robok
|
||||
*
|
||||
* @brief API for the Line Finder Sensor
|
||||
*
|
||||
* UPM module for the Line Finder sensor. It outputs a
|
||||
* digital signal indicating whether it is detecting black on a
|
||||
* white background, or white on a black background.
|
||||
*
|
||||
* @image html linefinder.jpg
|
||||
* @snippet linefinder.cxx Interesting
|
||||
*/
|
||||
class LineFinder {
|
||||
public:
|
||||
/**
|
||||
* Line Finder digital sensor constructor
|
||||
* @library linefinder
|
||||
* @sensor linefinder
|
||||
* @comname Line Finder
|
||||
* @altname Grove Line Finder, DFRobot Line Tracking sensor
|
||||
* @type color
|
||||
* @man seeed, dfrobot
|
||||
* @con gpio
|
||||
* @kit robok
|
||||
*
|
||||
* @param pin Digital pin to use
|
||||
*/
|
||||
LineFinder(int pin);
|
||||
/**
|
||||
* LineFinder destructor
|
||||
*/
|
||||
~LineFinder();
|
||||
/**
|
||||
* Determines whether white has been detected
|
||||
* @brief API for the Line Finder Sensor
|
||||
*
|
||||
* @return True if white is detected
|
||||
*/
|
||||
bool whiteDetected();
|
||||
/**
|
||||
* Determines whether black has been detected
|
||||
* UPM module for the Line Finder sensor. It outputs a
|
||||
* digital signal indicating whether it is detecting black on a
|
||||
* white background, or white on a black background.
|
||||
*
|
||||
* @return True if black is detected
|
||||
* @image html linefinder.jpg
|
||||
* @snippet linefinder.cxx Interesting
|
||||
*/
|
||||
bool blackDetected();
|
||||
class LineFinder {
|
||||
public:
|
||||
/**
|
||||
* Line Finder digital sensor constructor
|
||||
*
|
||||
* @param pin Digital pin to use
|
||||
*/
|
||||
LineFinder(int pin);
|
||||
|
||||
private:
|
||||
mraa_gpio_context m_gpio;
|
||||
};
|
||||
/**
|
||||
* LineFinder destructor
|
||||
*/
|
||||
~LineFinder();
|
||||
|
||||
/**
|
||||
* Determines whether white has been detected
|
||||
*
|
||||
* @return True if white is detected
|
||||
*/
|
||||
bool whiteDetected();
|
||||
|
||||
/**
|
||||
* Determines whether black has been detected
|
||||
*
|
||||
* @return True if black is detected
|
||||
*/
|
||||
bool blackDetected();
|
||||
|
||||
private:
|
||||
linefinder_context m_linefinder;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
94
src/linefinder/linefinder_fti.c
Normal file
94
src/linefinder/linefinder_fti.c
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 "linefinder.h"
|
||||
#include <upm_fti.h>
|
||||
|
||||
/**
|
||||
* This file implements the Function Table Interface (FTI) for this sensor
|
||||
*/
|
||||
|
||||
const char upm_linefinder_name[] = "LINEFINDER";
|
||||
const char upm_linefinder_description[] = "Line Finder";
|
||||
const upm_protocol_t upm_linefinder_protocol[] = {UPM_GPIO};
|
||||
const upm_sensor_t upm_linefinder_category[] = {UPM_BINARY};
|
||||
|
||||
// forward declarations
|
||||
const void* upm_linefinder_get_ft(upm_sensor_t sensor_type);
|
||||
void* upm_linefinder_init_name();
|
||||
void upm_linefinder_close(void *dev);
|
||||
upm_result_t upm_linefinder_black_detected(void *dev, bool *value);
|
||||
|
||||
const upm_sensor_descriptor_t upm_linefinder_get_descriptor()
|
||||
{
|
||||
upm_sensor_descriptor_t usd;
|
||||
usd.name = upm_linefinder_name;
|
||||
usd.description = upm_linefinder_description;
|
||||
usd.protocol_size = 1;
|
||||
usd.protocol = upm_linefinder_protocol;
|
||||
usd.category_size = 1;
|
||||
usd.category = upm_linefinder_category;
|
||||
return usd;
|
||||
}
|
||||
|
||||
static const upm_sensor_ft ft =
|
||||
{
|
||||
.upm_sensor_init_name = upm_linefinder_init_name,
|
||||
.upm_sensor_close = upm_linefinder_close,
|
||||
};
|
||||
|
||||
static const upm_binary_ft bft =
|
||||
{
|
||||
.upm_binary_get_value = upm_linefinder_black_detected,
|
||||
};
|
||||
|
||||
const void* upm_linefinder_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_linefinder_init_name()
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void upm_linefinder_close(void *dev)
|
||||
{
|
||||
linefinder_close((linefinder_context)dev);
|
||||
}
|
||||
|
||||
upm_result_t upm_linefinder_black_detected(void *dev, bool *value)
|
||||
{
|
||||
*value = linefinder_black_detected((linefinder_context)dev);
|
||||
|
||||
return UPM_SUCCESS;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user