nmea_gps: renamed vk2828u7 to nmea_gps

This driver will serve as a generic module for grabbing NMEA data from
various GPS devices via a serial interface.  ublox6 will also be
removed in favor of using this driver going forward.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2016-08-25 13:54:49 -06:00
committed by Noel Eck
parent 46460e20d9
commit a040f51cda
19 changed files with 152 additions and 155 deletions

View File

@ -0,0 +1,9 @@
upm_mixed_module_init (NAME nmea_gps
DESCRIPTION "UPM Generic driver for GPS NMEA sensors"
C_HDR nmea_gps.h
C_SRC nmea_gps.c
CPP_HDR nmea_gps.hpp
CPP_SRC nmea_gps.cxx
FTI_SRC nmea_gps_fti.c
CPP_WRAPS_C
REQUIRES mraa)

View File

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

View File

@ -0,0 +1,9 @@
%module jsupm_nmea_gps
%include "../upm.i"
%include "std_string.i"
%include "nmea_gps.hpp"
%{
#include "nmea_gps.hpp"
%}

151
src/nmea_gps/nmea_gps.c Normal file
View File

@ -0,0 +1,151 @@
/*
* 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 <string.h>
#include <assert.h>
#include "nmea_gps.h"
#include "upm_utilities.h"
nmea_gps_context nmea_gps_init(unsigned int uart, unsigned int baudrate,
int enable_pin)
{
nmea_gps_context dev =
(nmea_gps_context)malloc(sizeof(struct _nmea_gps_context));
if (!dev)
return NULL;
// zero out context
memset((void *)dev, 0, sizeof(struct _nmea_gps_context));
dev->uart = NULL;
dev->gpio_en = NULL;
// initialize the MRAA contexts
// uart, default should be 8N1
if (!(dev->uart = mraa_uart_init(uart)))
{
printf("%s: mraa_uart_init() failed.\n", __FUNCTION__);
nmea_gps_close(dev);
return NULL;
}
if (nmea_gps_set_baudrate(dev, baudrate))
{
printf("%s: nmea_gps_set_baudrate() failed.\n", __FUNCTION__);
nmea_gps_close(dev);
return NULL;
}
mraa_uart_set_flowcontrol(dev->uart, false, false);
// now the gpio_en, if enabled
if (enable_pin >= 0)
{
if (!(dev->gpio_en = mraa_gpio_init(enable_pin)))
{
printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
nmea_gps_close(dev);
return NULL;
}
mraa_gpio_dir(dev->gpio_en, MRAA_GPIO_OUT);
// wake up
nmea_gps_enable(dev, true);
}
return dev;
}
void nmea_gps_close(nmea_gps_context dev)
{
assert(dev != NULL);
// sleepy-time
nmea_gps_enable(dev, false);
if (dev->uart)
mraa_uart_stop(dev->uart);
if (dev->gpio_en)
mraa_gpio_close(dev->gpio_en);
free(dev);
}
upm_result_t nmea_gps_enable(const nmea_gps_context dev, bool enable)
{
assert(dev != NULL);
if (!dev->gpio_en)
return UPM_ERROR_NO_RESOURCES;
if (enable)
mraa_gpio_write(dev->gpio_en, 1);
else
mraa_gpio_write(dev->gpio_en, 0);
return UPM_SUCCESS;
}
int nmea_gps_read(const nmea_gps_context dev, char *buffer, size_t len)
{
assert(dev != NULL);
return mraa_uart_read(dev->uart, buffer, len);
}
int nmea_gps_write(const nmea_gps_context dev, char *buffer, size_t len)
{
assert(dev != NULL);
return mraa_uart_write(dev->uart, buffer, len);
}
bool nmea_gps_data_available(const nmea_gps_context dev, unsigned int millis)
{
assert(dev != NULL);
if (mraa_uart_data_available(dev->uart, millis))
return true;
else
return false;
}
upm_result_t nmea_gps_set_baudrate(const nmea_gps_context dev,
unsigned int baudrate)
{
assert(dev != NULL);
if (mraa_uart_set_baudrate(dev->uart, baudrate))
{
printf("%s: mraa_uart_set_baudrate() failed.\n", __FUNCTION__);
return UPM_ERROR_OPERATION_FAILED;
}
return UPM_SUCCESS;
}

89
src/nmea_gps/nmea_gps.cxx Normal file
View File

@ -0,0 +1,89 @@
/*
* 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 "nmea_gps.hpp"
using namespace upm;
using namespace std;
NMEAGPS::NMEAGPS(unsigned int uart, unsigned int baudrate,
int enable_pin) :
m_nmea_gps(nmea_gps_init(uart, baudrate, enable_pin))
{
if (!m_nmea_gps)
throw std::runtime_error(string(__FUNCTION__)
+ ": nmea_gps_init() failed");
}
NMEAGPS::~NMEAGPS()
{
nmea_gps_close(m_nmea_gps);
}
std::string NMEAGPS::readStr(unsigned int size)
{
char buffer[size];
int rv;
if ((rv = nmea_gps_read(m_nmea_gps, buffer, size)) < 0)
throw std::runtime_error(string(__FUNCTION__)
+ ": nmea_gps_read() failed");
return string(buffer, rv);
}
int NMEAGPS::writeStr(std::string buffer)
{
int rv;
if ((rv = nmea_gps_write(m_nmea_gps, (char*)buffer.data(),
buffer.size())) < 0)
throw std::runtime_error(string(__FUNCTION__)
+ ": nmea_gps_write() failed");
return rv;
}
void NMEAGPS::enable(bool enable)
{
if (nmea_gps_enable(m_nmea_gps, enable))
throw std::runtime_error(string(__FUNCTION__)
+ ": nmea_gps_enable() failed");
}
void NMEAGPS::setBaudrate(unsigned int baudrate)
{
if (nmea_gps_set_baudrate(m_nmea_gps, baudrate))
throw std::runtime_error(string(__FUNCTION__)
+ ": nmea_gps_baudrate() failed");
}
bool NMEAGPS::dataAvailable(unsigned int millis)
{
return nmea_gps_data_available(m_nmea_gps, millis);
}

126
src/nmea_gps/nmea_gps.h Normal file
View File

@ -0,0 +1,126 @@
/*
* 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/uart.h"
#include "mraa/gpio.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief UPM C API for a generic GPS serial device reporting NMEA data
*
* This driver was tested with a number of GPS devices that emit
* NMEA data via a serial interface of some sort (typically a UART).
*
* @snippet nmea_gps.c Interesting
*/
/**
* Device context
*/
typedef struct _nmea_gps_context {
mraa_uart_context uart;
mraa_gpio_context gpio_en;
} *nmea_gps_context;
/**
* NMEA_GPS Initializer
*
* @param uart Specify which uart to use.
* @param baudrate Specify the baudrate to use. The device defaults
* to 9600 baud.
* @param enable_pin Specify the GPIO pin to use for the enable pin,
* -1 to not use an enable pin.
* @return an initialized device context on success, NULL on error.
*/
nmea_gps_context nmea_gps_init(unsigned int uart, unsigned int baudrate,
int enable_pin);
/**
* NMEA_GPS sensor close function
*/
void nmea_gps_close(nmea_gps_context dev);
/**
* Read character data from the device.
*
* @param dev sensor context
* @param buffer The character buffer to read data into.
* @param len The maximum size of the buffer
* @return The number of bytes successfully read, or -1 on error
*/
int nmea_gps_read(const nmea_gps_context dev, char *buffer, size_t len);
/**
* Write character data to the device.
*
* @param dev sensor context
* @param buffer The character buffer containing data to write.
* @param len The number of bytes to write.
* @return The number of bytes successfully written, or -1 on error.
*/
int nmea_gps_write(const nmea_gps_context dev, char *buffer, size_t len);
/**
* Enable or disable the device. When disabled, the device enters a
* low power mode and does not emit NMEA data. It will still
* maintain location data however.
*
* @param dev sensor context
* @param enable true to enable the device, false otherwise.
* @return UPM result
*/
upm_result_t nmea_gps_enable(const nmea_gps_context dev, bool enable);
/**
* Set the baudrate of the device. By default, nmea_gps_init() will
* set the baudrate to 9600.
*
* @param dev sensor context
* @param baudrate The baud rate to set for the device.
* @return UPM result
*/
upm_result_t nmea_gps_set_baudrate(const nmea_gps_context dev,
unsigned int baudrate);
/**
* Determine whether there is data available to be read.
*
* @param dev sensor context
* @param millis The number of milliseconds to wait for data to
* become available.
* @return true if data is available to be read, false otherwise.
*/
bool nmea_gps_data_available(const nmea_gps_context dev,
unsigned int millis);
#ifdef __cplusplus
}
#endif

128
src/nmea_gps/nmea_gps.hpp Normal file
View File

@ -0,0 +1,128 @@
/*
* 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 "nmea_gps.h"
namespace upm {
/**
* @brief UPM C++ API for a generic GPS serial device reporting NMEA data
* @defgroup nmea_gps libupm-nmea_gps
* @ingroup uart gpio gps
*/
/**
* @library nmea_gps
* @sensor nmea_gps
* @comname Generic Serial GPS NMEA device
* @type gps
* @man dfrobot seed
* @con uart gpio
* @altname VK2828u7 ublox LEA-6H
*
* @brief API for the NMEA GPS Module
*
* This driver was tested with a number of GPS devices that emit
* NMEA data via a serial interface of some sort (typically a UART).
*
* @snippet nmea_gps.cxx Interesting
*/
class NMEAGPS {
public:
/**
* NMEAGPS object constructor
*
* @param uart Specify which uart to use.
* @param baudrate Specify the baudrate to use. The device defaults
* to 9600 baud.
* @param enable_pin Specify the GPIO pin to use for the enable pin,
* -1 to not use an enable pin.
*/
NMEAGPS(unsigned int uart, unsigned int baudrate,
int enable_pin);
/**
* NMEAGPS object destructor
*/
~NMEAGPS();
/**
* Read character data from the device.
*
* @param size The maximum number of characters to read.
* @return string containing the data read.
*/
std::string readStr(size_t size);
/**
* Write character data to the device.
*
* @param buffer The string containing the data to write.
* @return The number of bytes written.
*/
int writeStr(std::string buffer);
/**
* Enable or disable the device. When disabled, the device enters a
* low power mode and does not emit NMEA data. It will still
* maintain location data however.
*
* @param enable true to enable the device, false otherwise.
*/
void enable(bool enable);
/**
* Set the baudrate of the device. By default, the constructor
* will set the baudrate to 9600.
*
* @param baudrate The baud rate to set for the device.
*/
void setBaudrate(unsigned int baudrate);
/**
* Determine whether there is data available to be read.
*
* @param millis The number of milliseconds to wait for data to
* become available.
* @return true if data is available to be read, false otherwise.
*/
bool dataAvailable(unsigned int millis);
protected:
// nmeaGPS device context
nmea_gps_context m_nmea_gps;
private:
};
}

View File

@ -0,0 +1,96 @@
/*
* 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 "nmea_gps.h"
#include "upm_fti.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_nmea_gps_name[] = "NMEA_GPS";
const char upm_nmea_gps_description[] = "Serial GPS (providing NMEA data)";
const upm_protocol_t upm_nmea_gps_protocol[] = {UPM_UART, UPM_GPIO};
const upm_sensor_t upm_nmea_gps_category[] = {UPM_STREAM};
// forward declarations
const void* upm_nmea_gps_get_ft(upm_sensor_t sensor_type);
void* upm_nmea_gps_init_name();
void upm_nmea_gps_close(void *dev);
int upm_nmea_gps_read(void *dev, char *buffer, int len);
int upm_nmea_gps_write(void *dev, char *buffer, int len);
bool upm_nmea_gps_data_available(void *dev, unsigned int timeout);
static const upm_sensor_ft ft =
{
.upm_sensor_init_name = &upm_nmea_gps_init_name,
.upm_sensor_close = &upm_nmea_gps_close,
};
static const upm_stream_ft sft =
{
.upm_stream_read = upm_nmea_gps_read,
.upm_stream_write = upm_nmea_gps_write,
.upm_stream_data_available = upm_nmea_gps_data_available
};
const void* upm_nmea_gps_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft;
case UPM_STREAM:
return &sft;
default:
return NULL;
}
}
void* upm_nmea_gps_init_name()
{
return NULL;
}
void upm_nmea_gps_close(void *dev)
{
nmea_gps_close((nmea_gps_context)dev);
}
int upm_nmea_gps_read(void *dev, char *buffer, int len)
{
return nmea_gps_read((nmea_gps_context)dev, buffer, len);
}
int upm_nmea_gps_write(void *dev, char *buffer, int len)
{
return nmea_gps_write((nmea_gps_context)dev, buffer, len);
}
bool upm_nmea_gps_data_available(void *dev, unsigned int timeout)
{
return nmea_gps_data_available((nmea_gps_context)dev, timeout);
}

View File

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