mb704x: Initial implementation; C; FTI; C++ wraps C

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson 2016-12-02 16:42:33 -07:00
parent e1e9067744
commit 6667646d32
17 changed files with 800 additions and 0 deletions

View File

@ -324,6 +324,7 @@ add_example (sht1x)
add_example (ms5803) add_example (ms5803)
add_example (ims) add_example (ims)
add_example (ecezo) add_example (ecezo)
add_example (mb704x)
# These are special cases where you specify example binary, source file and module(s) # These are special cases where you specify example binary, source file and module(s)
include_directories (${PROJECT_SOURCE_DIR}/src) include_directories (${PROJECT_SOURCE_DIR}/src)

70
examples/c++/mb704x.cxx 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 <upm_utilities.h>
#include <mb704x.hpp>
using namespace std;
using namespace upm;
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a MB704X sensor using default parameters (bus 0,
// address 112)
upm::MB704X *sensor = new upm::MB704X();
while (shouldRun)
{
cout << "Range: "
<< sensor->getRange()
<< " cm"
<< endl;
upm_delay_ms(500);
}
cout << "Exiting..." << endl;
delete sensor;
//! [Interesting]
return 0;
}

View File

@ -140,6 +140,7 @@ add_example (my9221)
add_example (ms5803) add_example (ms5803)
add_example (ims) add_example (ims)
add_example (ecezo) add_example (ecezo)
add_example (mb704x)
# 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)

77
examples/c/mb704x.c Normal file
View 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 <unistd.h>
#include <signal.h>
#include <upm_utilities.h>
#include <mb704x.h>
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a MB704X sensor on i2c bus 0, address 112.
mb704x_context sensor = mb704x_init(0, 112);
if (!sensor)
{
printf("mb704x_init() failed.\n");
return 1;
}
while (shouldRun)
{
// this will take about 1 second to complete
int range = mb704x_get_range(sensor);
if (range < 0)
{
printf("Error getting range.\n");
}
else
{
printf("Range: %d cm\n", range);
}
upm_delay_ms(500);
}
printf("Exiting\n");
mb704x_close(sensor);
//! [Interesting]
return 0;
}

View File

@ -159,6 +159,7 @@ add_example(SHT1X_Example sht1x)
add_example(MS5803_Example ms5803) add_example(MS5803_Example ms5803)
add_example(ECEZO_Example ecezo) add_example(ECEZO_Example ecezo)
add_example(IMS_Example ims) add_example(IMS_Example ims)
add_example(MB704X_Example mb704x)
add_example_with_path(Jhd1313m1_lcdSample lcd i2clcd) add_example_with_path(Jhd1313m1_lcdSample lcd i2clcd)
add_example_with_path(Jhd1313m1Sample lcd i2clcd) add_example_with_path(Jhd1313m1Sample lcd i2clcd)

View File

@ -0,0 +1,48 @@
/*
* 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.
*/
import upm_mb704x.MB704X;
public class MB704X_Example
{
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
// Instantiate a MB704X sensor using default parameters (bus 0,
// address 112)
MB704X sensor = new MB704X();
while (true)
{
System.out.println("Range: "
+ sensor.getRange()
+ " cm");
Thread.sleep(500);
}
// ! [Interesting]
}
}

View File

@ -0,0 +1,47 @@
/*
* 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.
*/
var sensorObj = require('jsupm_mb704x');
// Instantiate a MB704X sensor using default parameters (bus 0,
// address 112)
var sensor = new sensorObj.MB704X();
setInterval(function()
{
console.log("Range: "
+ sensor.getRange()
+ " cm");
}, 500);
// exit on ^C
process.on('SIGINT', function()
{
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
console.log("Exiting.");
process.exit(0);
});

56
examples/python/mb704x.py Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/python
# 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.
from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_mb704x as sensorObj
def main():
# Instantiate a MB704X sensor using default parameters (bus 0,
# address 112)
sensor = sensorObj.MB704X();
## Exit handlers ##
# This function stops python from printing a stacktrace when you
# hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit
def exitHandler():
print("Exiting")
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
while (1):
print("Range: "
+ str(sensor.getRange())
+ " cm")
time.sleep(.5)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,9 @@
upm_mixed_module_init (NAME mb704x
DESCRIPTION "MB7040/7047 I2C MaxSonar WR Ultrasonic Ranger"
C_HDR mb704x.h
C_SRC mb704x.c
CPP_HDR mb704x.hpp
CPP_SRC mb704x.cxx
FTI_SRC mb704x_fti.c
CPP_WRAPS_C
REQUIRES mraa)

View File

@ -0,0 +1,21 @@
%module javaupm_mb704x
%include "../upm.i"
%include "std_string.i"
%include "stdint.i"
%include "typemaps.i"
%include "mb704x.hpp"
%{
#include "mb704x.hpp"
%}
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_mb704x");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}

View File

@ -0,0 +1,8 @@
%module jsupm_mb704x
%include "../upm.i"
%include "std_string.i"
%include "mb704x.hpp"
%{
#include "mb704x.hpp"
%}

115
src/mb704x/mb704x.c Normal file
View File

@ -0,0 +1,115 @@
/*
* 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 <stdlib.h>
#include <string.h>
#include <assert.h>
#include <upm_utilities.h>
#include "mb704x.h"
// The delay to wait (in ms) for a ranging command to complete before
// requesting the range data.
#define MB704X_RANGE_DELAY (100)
// range command
#define MB704X_CMD_RANGE (81)
mb704x_context mb704x_init(unsigned int bus, int addr)
{
// 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);
return NULL;
}
mb704x_context dev =
(mb704x_context)malloc(sizeof(struct _mb704x_context));
if (!dev)
return NULL;
// zero out context
memset((void *)dev, 0, sizeof(struct _mb704x_context));
// initialize the MRAA contexts
if (!(dev->i2c = mraa_i2c_init(bus)))
{
printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__);
mb704x_close(dev);
return NULL;
}
if (mraa_i2c_address(dev->i2c, (uint8_t)addr))
{
printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__);
mb704x_close(dev);
return NULL;
}
// max power up delay
upm_delay_ms(135);
return dev;
}
void mb704x_close(mb704x_context dev)
{
assert(dev != NULL);
if (dev->i2c)
mraa_i2c_stop(dev->i2c);
free(dev);
}
int mb704x_get_range(const mb704x_context dev)
{
assert(dev != NULL);
// first, send the range command.
uint8_t cmd = MB704X_CMD_RANGE;
if (mraa_i2c_write(dev->i2c, &cmd, 1))
{
printf("%s: mraa_i2c_write() failed.\n", __FUNCTION__);
return -1;
}
// delay for measurement
upm_delay_ms(MB704X_RANGE_DELAY);
// At this point, we should be able to do our read of the data
uint8_t buffer[2];
if (mraa_i2c_read(dev->i2c, buffer, 2) != 2)
{
printf("%s: mraa_i2c_read() failed.\n", __FUNCTION__);
return -1;
}
return ((buffer[0] << 8) | buffer[1]);
}

49
src/mb704x/mb704x.cxx Normal file
View File

@ -0,0 +1,49 @@
/*
* 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 <iostream>
#include <stdexcept>
#include "mb704x.hpp"
using namespace upm;
using namespace std;
MB704X::MB704X(unsigned int bus, unsigned int addr) :
m_mb704x(mb704x_init(bus, addr))
{
if (!m_mb704x)
throw std::runtime_error(string(__FUNCTION__)
+ ": mb704x_*_init() failed");
}
MB704X::~MB704X()
{
mb704x_close(m_mb704x);
}
int MB704X::getRange()
{
return mb704x_get_range(m_mb704x);
}

78
src/mb704x/mb704x.h Normal file
View File

@ -0,0 +1,78 @@
/*
* 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 <stdint.h>
#include <upm.h>
#include <mraa/i2c.h>
#include <mraa/gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file mb704x.h
* @library mb704x
* @brief C API for the MB704x MaxSonar-WR Ultrasonic Ranger
*
* @include mb704x.c
*/
/**
* Device context
*/
typedef struct _mb704x_context {
mraa_i2c_context i2c;
} *mb704x_context;
/**
* MB704X Initializer
*
* @param bus Specify which the I2C bus to use.
* @param addr Specify the I2C address to use. The default is 112.
* @return an initialized device context on success, NULL on error.
*/
mb704x_context mb704x_init(unsigned int bus, int addr);
/**
* MB704X sensor close function
*/
void mb704x_close(mb704x_context dev);
/**
* Query the device for a range reading. The range will be
* reported in centimeters (cm).
*
* @param dev Device context
* @return Measured range, -1 on error. The range is reported in
* centimeters (cm).
*/
int mb704x_get_range(const mb704x_context dev);
#ifdef __cplusplus
}
#endif

94
src/mb704x/mb704x.hpp Normal file
View 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.
*/
#pragma once
#include <string>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include "mb704x.h"
namespace upm {
/**
* @brief API for the MB704x MaxSonar-WR Ultrasonic Ranger
* @defgroup mb704x libupm-mb704x
* @ingroup i2c sound
*/
/**
* @library mb704x
* @sensor mb704x
* @comname MB704x MaxSonar-WR Ultrasonic Ranger
* @altname MB7040 MB7047 MB7247
* @type sound
* @man maxbotix
* @con i2c
* @web http://www.maxbotix.com/Ultrasonic_Sensors/I2C_Distance_Sensors.htm
*
* @brief API for the MB704x MaxSonar-WR Ultrasonic Ranger
*
* This driver was tested with the MB704x MaxSonar-WR Ultrasonic
* Ranger using the long distance, weather resistant horn.
* Depending on your sensor type and horn, tanges from 20cm to
* 750cm are possible.
*
* @snippet mb704x.cxx Interesting
*/
class MB704X {
public:
/**
* Initialize a device context.
*
* @param bus Specify which the I2C bus to use. The default is 0.
* @param addr Specify the I2C address to use. The default is 112.
*/
MB704X(unsigned int bus=0, unsigned int addr=112);
/**
* MB704X object destructor
*/
~MB704X();
/**
* Query the device for a range reading. The range will be
* reported in centimeters (cm).
*
* @param dev Device context
* @return Measured range, -1 on error. The range is reported in
* centimeters (cm).
*/
int getRange();
protected:
// mb704x device context
mb704x_context m_mb704x;
private:
};
}

113
src/mb704x/mb704x_fti.c Normal file
View File

@ -0,0 +1,113 @@
/*
* 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 "mb704x.h"
#include "upm_fti.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_mb704x_name[] = "MB704X";
const char upm_mb704x_description[] = "MB704x MaxSonar-WR Ultrasonic Ranger";
const upm_protocol_t upm_mb704x_protocol[] = {UPM_I2C};
const upm_sensor_t upm_mb704x_category[] = {UPM_DISTANCE};
// forward declarations
const void* upm_mb704x_get_ft(upm_sensor_t sensor_type);
void* upm_mb704x_init_name();
void upm_mb704x_close(void *dev);
upm_result_t upm_mb704x_get_distance(void *dev, float *value,
upm_distance_u unit);
const upm_sensor_descriptor_t upm_es08a_get_descriptor()
{
upm_sensor_descriptor_t usd;
usd.name = upm_mb704x_name;
usd.description = upm_mb704x_description;
usd.protocol_size = 1;
usd.protocol = upm_mb704x_protocol;
usd.category_size = 1;
usd.category = upm_mb704x_category;
return usd;
}
static const upm_sensor_ft ft =
{
.upm_sensor_init_name = &upm_mb704x_init_name,
.upm_sensor_close = &upm_mb704x_close,
};
static const upm_distance_ft dft =
{
.upm_distance_get_value = upm_mb704x_get_distance
};
const void* upm_mb704x_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft;
case UPM_DISTANCE:
return &dft;
default:
return NULL;
}
}
void *upm_mb704x_init_name()
{
return NULL;
}
void upm_mb704x_close(void *dev)
{
mb704x_close((mb704x_context)dev);
}
upm_result_t upm_mb704x_get_distance(void *dev, float *value,
upm_distance_u unit)
{
int rv;
if ((rv = mb704x_get_range((mb704x_context)dev)) < 0)
return UPM_ERROR_OPERATION_FAILED;
switch(unit)
{
case CENTIMETER:
*value = (float)rv;
case INCH:
*value = ((float)rv * 0.393701);
default:
return UPM_ERROR_INVALID_PARAMETER;
}
return UPM_SUCCESS;
}

12
src/mb704x/pyupm_mb704x.i Normal file
View File

@ -0,0 +1,12 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_mb704x
%include "../upm.i"
%include "std_string.i"
%feature("autodoc", "3");
%include "mb704x.hpp"
%{
#include "mb704x.hpp"
%}