diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index f4a3dc7e..8bc56b67 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -274,6 +274,7 @@ add_example (l3gd20) add_example (l3gd20-i2c) add_example (bmx055) add_example (ms5611) +add_example (vk2828u7) # These are special cases where you specify example binary, source file and module(s) include_directories (${PROJECT_SOURCE_DIR}/src) diff --git a/examples/c++/vk2828u7.cxx b/examples/c++/vk2828u7.cxx new file mode 100644 index 00000000..ae171539 --- /dev/null +++ b/examples/c++/vk2828u7.cxx @@ -0,0 +1,69 @@ +/* + * Author: Jon Trulson + * 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 +#include +#include + +#include "vk2828u7.hpp" + +using namespace std; + +bool shouldRun = true; + +const size_t bufferLength = 256; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + + // Instantiate a VK2828U7 sensor on uart 0 at 9600 baud with enable + // pin on D3 + upm::VK2828U7 *sensor = new upm::VK2828U7(0, 9600, 3); + + // loop, dumping NMEA data out as fast as it comes in + while (shouldRun && sensor->dataAvailable(5000)) + { + cout << sensor->readStr(bufferLength); + } + + if (shouldRun) + cerr << "Timed out" << endl; + +//! [Interesting] + + cout << "Exiting" << endl; + + delete sensor; + + return 0; +} diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index a6cbca89..fdfc0d8d 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -87,3 +87,4 @@ link_directories (${MRAA_LIBDIR}) # mq? will use module gas # grove* will use module grove add_example (dfrph) +add_example (vk2828u7) diff --git a/examples/c/vk2828u7.c b/examples/c/vk2828u7.c new file mode 100644 index 00000000..6985d5dd --- /dev/null +++ b/examples/c/vk2828u7.c @@ -0,0 +1,80 @@ +/* + * Author: Jon Trulson + * 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 +#include + +#include "vk2828u7.h" + +bool shouldRun = true; + +const size_t bufferLength = 256; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + + // Instantiate a VK2828U7 sensor on uart 0 at 9600 baud with enable + // pin on D3 + vk2828u7_context sensor = vk2828u7_init(0, 9600, 3); + + if (!sensor) + { + printf("vk2828u7_init() failed.\n"); + return 1; + } + + char buffer[bufferLength]; + int rv = 0; + + // loop, dumping NMEA data out as fast as it comes in + while (shouldRun && vk2828u7_data_available(sensor, 5000)) + { + if ((rv = vk2828u7_read(sensor, buffer, bufferLength)) >= 0) + { + int i; + for (i=0; i + * 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_vk2828u7.VK2828U7; + +public class VK2828U7_Example +{ + public static void main(String[] args) throws InterruptedException + { +// ! [Interesting] + System.out.println("Initializing..."); + + // Instantiate a VK2828U7 sensor on uart 0 at 9600 baud with + // enable pin on D3 + VK2828U7 sensor = new VK2828U7(0, 9600, 3); + + // loop, dumping NMEA data out as fast as it comes in + while (sensor.dataAvailable(5000)) + { + System.out.print(sensor.readStr(256)); + } + System.out.println("Timed out"); + + +// ! [Interesting] + } +} diff --git a/examples/javascript/vk2828u7.js b/examples/javascript/vk2828u7.js new file mode 100644 index 00000000..0aaa43cf --- /dev/null +++ b/examples/javascript/vk2828u7.js @@ -0,0 +1,50 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ + +/* + * Author: Jon Trulson + * 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_vk2828u7'); + +// Instantiate a VK2828U7 sensor on uart 0 at 9600 baud with enable +// pin on D3 +var sensor = new sensorObj.VK2828U7(0, 9600, 3); + +// loop, dumping NMEA data out as fast as it comes in +while (sensor.dataAvailable(5000)) +{ + process.stdout.write(sensor.readStr(256)); +} +console.log("Timed out"); + +// exit on ^C +process.on('SIGINT', function() +{ + sensor = null; + sensorObj.cleanUp(); + sensorObj = null; + console.log("Exiting."); + process.exit(0); +}); + diff --git a/examples/python/vk2828u7.py b/examples/python/vk2828u7.py new file mode 100644 index 00000000..eafc4f14 --- /dev/null +++ b/examples/python/vk2828u7.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# Author: Jon Trulson +# 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 time, sys, signal, atexit +import pyupm_vk2828u7 as sensorObj + +# Instantiate a VK2828U7 sensor on uart 0 at 9600 baud with enable +# pin on D3 +sensor = sensorObj.VK2828U7(0, 9600, 3) + +## 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) + +# Every second, sample the VK2828U7 and output the measured lux value + +# loop, dumping NMEA data out as fast as it comes in +while (sensor.dataAvailable(5000)): + sys.stdout.write(sensor.readStr(256)) + +print "Timed out" diff --git a/src/vk2828u7/CMakeLists.txt b/src/vk2828u7/CMakeLists.txt new file mode 100644 index 00000000..16a7e00d --- /dev/null +++ b/src/vk2828u7/CMakeLists.txt @@ -0,0 +1,9 @@ +upm_mixed_module_init (NAME vk2828u7 + DESCRIPTION "upm dfrobot VK2828u7/NMEA serial GPS sensor" + C_HDR vk2828u7.h + C_SRC vk2828u7.c + CPP_HDR vk2828u7.hpp + CPP_SRC vk2828u7.cxx + FTI_SRC vk2828u7_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/vk2828u7/javaupm_vk2828u7.i b/src/vk2828u7/javaupm_vk2828u7.i new file mode 100644 index 00000000..18e291d3 --- /dev/null +++ b/src/vk2828u7/javaupm_vk2828u7.i @@ -0,0 +1,20 @@ +%module javaupm_vk2828u7 +%include "../upm.i" +%include "std_string.i" +%include "typemaps.i" + +%include "vk2828u7.hpp" +%{ + #include "vk2828u7.hpp" +%} + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_vk2828u7"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/vk2828u7/jsupm_vk2828u7.i b/src/vk2828u7/jsupm_vk2828u7.i new file mode 100644 index 00000000..4b78e13e --- /dev/null +++ b/src/vk2828u7/jsupm_vk2828u7.i @@ -0,0 +1,9 @@ +%module jsupm_vk2828u7 +%include "../upm.i" +%include "std_string.i" + +%include "vk2828u7.hpp" +%{ + #include "vk2828u7.hpp" +%} + diff --git a/src/vk2828u7/pyupm_vk2828u7.i b/src/vk2828u7/pyupm_vk2828u7.i new file mode 100644 index 00000000..a3cb1970 --- /dev/null +++ b/src/vk2828u7/pyupm_vk2828u7.i @@ -0,0 +1,13 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_vk2828u7 +%include "../upm.i" +%include "std_string.i" + +%feature("autodoc", "3"); + +%include "vk2828u7.hpp" +%{ + #include "vk2828u7.hpp" +%} + diff --git a/src/vk2828u7/vk2828u7.c b/src/vk2828u7/vk2828u7.c new file mode 100644 index 00000000..c1fdbcfe --- /dev/null +++ b/src/vk2828u7/vk2828u7.c @@ -0,0 +1,151 @@ +/* + * Author: Jon Trulson + * 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 +#include + +#include "vk2828u7.h" + +#include "upm_utilities.h" + +vk2828u7_context vk2828u7_init(unsigned int uart, unsigned int baudrate, + int enable_pin) +{ + vk2828u7_context dev = + (vk2828u7_context)malloc(sizeof(struct _vk2828u7_context)); + + if (!dev) + return NULL; + + // zero out context + memset((void *)dev, 0, sizeof(struct _vk2828u7_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__); + vk2828u7_close(dev); + return NULL; + } + + if (vk2828u7_set_baudrate(dev, baudrate)) + { + printf("%s: vk2828u7_set_baudrate() failed.\n", __FUNCTION__); + vk2828u7_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__); + vk2828u7_close(dev); + return NULL; + } + + mraa_gpio_dir(dev->gpio_en, MRAA_GPIO_OUT); + + // wake up + vk2828u7_enable(dev, true); + } + + return dev; +} + +void vk2828u7_close(vk2828u7_context dev) +{ + assert(dev != NULL); + + // sleepy-time + vk2828u7_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 vk2828u7_enable(const vk2828u7_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 vk2828u7_read(const vk2828u7_context dev, char *buffer, size_t len) +{ + assert(dev != NULL); + + return mraa_uart_read(dev->uart, buffer, len); +} + +int vk2828u7_write(const vk2828u7_context dev, char *buffer, size_t len) +{ + assert(dev != NULL); + + return mraa_uart_write(dev->uart, buffer, len); +} + +bool vk2828u7_data_available(const vk2828u7_context dev, unsigned int millis) +{ + assert(dev != NULL); + + if (mraa_uart_data_available(dev->uart, millis)) + return true; + else + return false; +} + +upm_result_t vk2828u7_set_baudrate(const vk2828u7_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; +} diff --git a/src/vk2828u7/vk2828u7.cxx b/src/vk2828u7/vk2828u7.cxx new file mode 100644 index 00000000..8ce3c1ad --- /dev/null +++ b/src/vk2828u7/vk2828u7.cxx @@ -0,0 +1,89 @@ +/* + * Author: Jon Trulson + * 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 +#include + +#include "vk2828u7.hpp" + +using namespace upm; +using namespace std; + +VK2828U7::VK2828U7(unsigned int uart, unsigned int baudrate, + int enable_pin) : + m_vk2828u7(vk2828u7_init(uart, baudrate, enable_pin)) +{ + if (!m_vk2828u7) + throw std::runtime_error(string(__FUNCTION__) + + ": vk2828u7_init() failed"); +} + +VK2828U7::~VK2828U7() +{ + vk2828u7_close(m_vk2828u7); +} + +std::string VK2828U7::readStr(unsigned int size) +{ + char buffer[size]; + + int rv; + + if ((rv = vk2828u7_read(m_vk2828u7, buffer, size)) < 0) + throw std::runtime_error(string(__FUNCTION__) + + ": vk2828u7_read() failed"); + + return string(buffer, rv); +} + +int VK2828U7::writeStr(std::string buffer) +{ + int rv; + + if ((rv = vk2828u7_write(m_vk2828u7, (char*)buffer.data(), + buffer.size())) < 0) + throw std::runtime_error(string(__FUNCTION__) + + ": vk2828u7_write() failed"); + + return rv; +} + +void VK2828U7::enable(bool enable) +{ + if (vk2828u7_enable(m_vk2828u7, enable)) + throw std::runtime_error(string(__FUNCTION__) + + ": vk2828u7_enable() failed"); +} + +void VK2828U7::setBaudrate(unsigned int baudrate) +{ + if (vk2828u7_set_baudrate(m_vk2828u7, baudrate)) + throw std::runtime_error(string(__FUNCTION__) + + ": vk2828u7_baudrate() failed"); +} + +bool VK2828U7::dataAvailable(unsigned int millis) +{ + return vk2828u7_data_available(m_vk2828u7, millis); +} diff --git a/src/vk2828u7/vk2828u7.h b/src/vk2828u7/vk2828u7.h new file mode 100644 index 00000000..614ecdcf --- /dev/null +++ b/src/vk2828u7/vk2828u7.h @@ -0,0 +1,126 @@ +/* + * Author: Jon Trulson + * 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 +#include "upm.h" +#include "mraa/uart.h" +#include "mraa/gpio.h" + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @brief UPM C API for the DFRobot VK2828U7 GPS Module + * + * The driver was tested with the DFRobot VK2828U7 GPS Module. It + * is accessed via a UART and provides NMEA data. + * + * @snippet vk2828u7.c Interesting + */ + + /** + * Device context + */ + typedef struct _vk2828u7_context { + mraa_uart_context uart; + mraa_gpio_context gpio_en; + } *vk2828u7_context; + + /** + * VK2828U7 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. + */ + vk2828u7_context vk2828u7_init(unsigned int uart, unsigned int baudrate, + int enable_pin); + + /** + * VK2828U7 sensor close function + */ + void vk2828u7_close(vk2828u7_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 vk2828u7_read(const vk2828u7_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 vk2828u7_write(const vk2828u7_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 vk2828u7_enable(const vk2828u7_context dev, bool enable); + + /** + * Set the baudrate of the device. By default, vk2828u7_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 vk2828u7_set_baudrate(const vk2828u7_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 vk2828u7_data_available(const vk2828u7_context dev, + unsigned int millis); + +#ifdef __cplusplus +} +#endif diff --git a/src/vk2828u7/vk2828u7.hpp b/src/vk2828u7/vk2828u7.hpp new file mode 100644 index 00000000..326500b4 --- /dev/null +++ b/src/vk2828u7/vk2828u7.hpp @@ -0,0 +1,131 @@ +/* + * Author: Jon Trulson + * 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 +#include + +#include +#include + +#include "vk2828u7.h" + +namespace upm { + /** + * @brief UPM C++ API for the DFRobot VK2828U7 Analog Accelerometer + * @defgroup vk2828u7 libupm-vk2828u7 + * @ingroup dfrobot uart gpio gps + */ + + /** + * @library vk2828u7 + * @sensor vk2828u7 + * @comname DFRobot VK2828U7 GPS Module + * @type gps + * @man dfrobot + * @con uart gpio + * @web http://www.dfrobot.com/index.php?route=product/product&search=gps&description=true&product_id=1302#.V7tMfN9ytNK + * + * @brief API for the DFRobot VK2828U7 GPS Module + * + * The driver was tested with the DFRobot VK2828U7 GPS module. The + * website claims the device can be run at 5v, however the datasheet + * says the logic inputs and outputs can only handle 3.3v, and there + * does not appear to be a schematic available. So this module was + * tested only at 3.3v. + * + * @snippet vk2828u7.cxx Interesting + */ + + class VK2828U7 { + public: + + /** + * VK2828U7 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. + */ + VK2828U7(unsigned int uart, unsigned int baudrate, + int enable_pin); + + /** + * VK2828U7 object destructor + */ + ~VK2828U7(); + + /** + * 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: + // vk2828u7 device context + vk2828u7_context m_vk2828u7; + + private: + }; +} + + diff --git a/src/vk2828u7/vk2828u7_fti.c b/src/vk2828u7/vk2828u7_fti.c new file mode 100644 index 00000000..0e27e8c7 --- /dev/null +++ b/src/vk2828u7/vk2828u7_fti.c @@ -0,0 +1,96 @@ +/* + * Author: Jon Trulson + * 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 "vk2828u7.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_vk2828u7_name[] = "VK2828U7"; +const char upm_vk2828u7_description[] = "Serial GPS (providing NMEA data)"; +const upm_protocol_t upm_vk2828u7_protocol[] = {UPM_UART, UPM_GPIO}; +const upm_sensor_t upm_vk2828u7_category[] = {UPM_STREAM}; + +// forward declarations +const void* upm_vk2828u7_get_ft(upm_sensor_t sensor_type); +void* upm_vk2828u7_init_name(); +void upm_vk2828u7_close(void *dev); +int upm_vk2828u7_read(void *dev, char *buffer, int len); +int upm_vk2828u7_write(void *dev, char *buffer, int len); +bool upm_vk2828u7_data_available(void *dev, unsigned int timeout); + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_vk2828u7_init_name, + .upm_sensor_close = &upm_vk2828u7_close, +}; + +static const upm_stream_ft sft = +{ + .upm_stream_read = upm_vk2828u7_read, + .upm_stream_write = upm_vk2828u7_write, + .upm_stream_data_available = upm_vk2828u7_data_available +}; + +const void* upm_vk2828u7_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_vk2828u7_init_name() +{ + return NULL; +} + +void upm_vk2828u7_close(void *dev) +{ + vk2828u7_close((vk2828u7_context)dev); +} + +int upm_vk2828u7_read(void *dev, char *buffer, int len) +{ + return vk2828u7_read((vk2828u7_context)dev, buffer, len); +} + +int upm_vk2828u7_write(void *dev, char *buffer, int len) +{ + return vk2828u7_write((vk2828u7_context)dev, buffer, len); +} + +bool upm_vk2828u7_data_available(void *dev, unsigned int timeout) +{ + return vk2828u7_data_available((vk2828u7_context)dev, timeout); +}