hmtrp: Initial implementation

This was tested on the Grove Serial RF Pro.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: John Van Drasek <john.r.van.drasek@intel.com>
This commit is contained in:
John Van Drasek
2015-02-25 09:09:40 -08:00
parent fad4accfcc
commit fab4c26238
9 changed files with 1088 additions and 0 deletions

View File

@ -83,6 +83,7 @@ add_executable (rpr220-intr-example rpr220-intr.cxx)
add_executable (mma7660-example mma7660.cxx)
add_executable (cjq4435-example cjq4435.cxx)
add_executable (adxl335-example adxl335.cxx)
add_executable (hmtrp-example hmtrp.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@ -151,6 +152,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/rpr220)
include_directories (${PROJECT_SOURCE_DIR}/src/mma7660)
include_directories (${PROJECT_SOURCE_DIR}/src/cjq4435)
include_directories (${PROJECT_SOURCE_DIR}/src/adxl335)
include_directories (${PROJECT_SOURCE_DIR}/src/hmtrp)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@ -237,3 +239,4 @@ target_link_libraries (rpr220-intr-example rpr220 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (mma7660-example mma7660 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (cjq4435-example cjq4435 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (adxl335-example adxl335 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (hmtrp-example hmtrp ${CMAKE_THREAD_LIBS_INIT})

164
examples/hmtrp.cxx Normal file
View File

@ -0,0 +1,164 @@
/*
* Author: Jon Trulson <jtrulson@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 <unistd.h>
#include <iostream>
#include <signal.h>
#include "hmtrp.h"
using namespace std;
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
void printUsage()
{
cout << "Usage:" << endl;
cout << "Pass a commandline argument (any argument) to this program"
<< endl;
cout << "to query the radio configuration and output it. NOTE: the"
<< endl;
cout << "radio must be in CONFIG mode for this to work."
<< endl;
cout << endl;
cout << "Running this program without arguments will simply transmit"
<< endl;
cout << "'Hello World!' every second, and output any data received from"
<< endl;
cout << "another radio."
<< endl;
cout << endl;
}
const size_t bufferLength = 256;
int main (int argc, char **argv)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a HMTRP radio device on uart 0
upm::HMTRP* radio = new upm::HMTRP(0);
// make sure port is initialized properly. 9600 baud is the default.
if (!radio->setupTty(B9600))
{
cerr << "Failed to setup tty port parameters" << endl;
return 1;
}
printUsage();
// By default, this radio simply transmits data sent via writeData()
// and reads any available data via readData().
// It can be placed into a configuration mode by grounding the
// CONFIG pin on the module. When this is done, the various
// configuration query and config methods can be used. In this
// example, by default, we just read any data available fom the
// device, and periodically transmit "Hello World".
// If any argument was specified on the command line, do a simple
// configuration query and output the results. The radio must be in
// CONFIG mode for this to work.
if (argc > 1)
{
// config mode
uint32_t freq;
uint32_t dataRate;
uint16_t rxBandwidth;
uint8_t modulation;
uint8_t txPower;
uint32_t uartBaud;
if (radio->getConfig(&freq, &dataRate, &rxBandwidth, &modulation,
&txPower, &uartBaud))
{
cout << "Radio configuration:" << endl;
cout << "freq: " << freq << " dataRate: " << dataRate
<< " rxBandwidth: " << rxBandwidth << "Khz" << endl;
cout << "modulation: " << int(modulation) << "Khz txPower: "
<< int(txPower) << " uartBaud: " << uartBaud << endl;
}
else
{
cerr << "getConfig() failed. Make sure the radio is in "
<< "CONFIG mode." << endl;
}
}
else
{
// normal read/write mode
char radioBuffer[bufferLength];
int counter = 0;
cout << "Running in normal read/write mode." << endl;
while (shouldRun)
{
// we don't want the read to block in this example, so always
// check to see if data is available first.
if (radio->dataAvailable())
{
int rv = radio->readData(radioBuffer, bufferLength);
if (rv > 0)
cout << "Received: " << radioBuffer << endl;
if (rv < 0) // some sort of read error occured
{
cerr << "Port read error." << endl;
break;
}
continue;
}
usleep(100000); // 100ms
counter++;
// every second, transmit "Hello World"
if (counter > 10)
{
static const char *hello = "Hello World!";
cout << "Transmitting hello world..." << endl;
radio->writeData((char *)hello, strlen(hello) + 1);
counter = 0;
}
}
}
//! [Interesting]
cout << "Exiting..." << endl;
delete radio;
return 0;
}

View File

@ -0,0 +1,161 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*
* Author: Zion Orent <zorent@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.
*/
function printUsage()
{
var outputStr = "Usage:\n" +
"Pass a commandline argument (any argument) to this program\n" +
"to query the radio configuration and output it. NOTE: the\n" +
"radio must be in CONFIG mode for this to work.\n\n" +
"Running this program without arguments will simply transmit\n" +
"'Hello World!' every second, and output any data received from\n" +
"another radio.\n\n";
console.log(outputStr);
}
var HMTRP_Radio = require('jsupm_hmtrp');
// Instantiate a HMTRP radio device on uart 0
var my_HMTRP_Radio = new HMTRP_Radio.HMTRP(0);
var g_myInterval;
var g_counter = 0;
// normal read/write mode
var bufferLength = 256;
var radioBuffer = new HMTRP_Radio.charArray(bufferLength);
// make sure port is initialized properly. 9600 baud is the default.
if (!my_HMTRP_Radio.setupTty(HMTRP_Radio.int_B9600))
{
console.log("Failed to setup tty port parameters");
process.exit(0);
}
printUsage();
// By default, this radio simply transmits data sent via writeData()
// and reads any available data via readData().
// It can be placed into a configuration mode by grounding the
// CONFIG pin on the module. When this is done, the various
// configuration query and config methods can be used. In this
// example, by default, we just read any data available fom the
// device, and periodically transmit "Hello World".
// If any argument was specified on the command line, do a simple
// configuration query and output the results. The radio must be in
// CONFIG mode for this to work.
// Note that the first command-line argument
// should be "node" and the second is "hmtrp.js".
// The data we want would be the third... if it exists
if (process.argv.length > 2)
{
// config mode
var freq = new HMTRP_Radio.uint32Array(0);
var dataRate = new HMTRP_Radio.uint32Array(0);
var rxBandwidth = new HMTRP_Radio.uint16Array(0);
var modulation = new HMTRP_Radio.uint8Array(0);
var txPower = new HMTRP_Radio.uint8Array(0);
var uartBaud = new HMTRP_Radio.uint32Array(0);
if (my_HMTRP_Radio.getConfig(
freq, dataRate, rxBandwidth, modulation, txPower, uartBaud))
{
console.log("Radio configuration:");
var outputStr = "freq: " + freq.getitem(0) +
" dataRate: " + dataRate.getitem(0) +
" rxBandwidth: " + rxBandwidth.getitem(0) + "Khz";
console.log(outputStr);
outputStr = "modulation: " + parseInt(modulation.getitem(0));
outputStr += " Khz txPower: " + parseInt(txPower.getitem(0));
outputStr += " uartBaud: " + uartBaud.getitem(0);
console.log(outputStr);
}
else
{
var errString = "getConfig() failed. Make sure the radio " +
"is in CONFIG mode.";
console.log(errString);
}
}
else
{
console.log("Running in normal read/write mode.");
g_myInterval = setInterval(runRadio, 100); // 100ms
}
function runRadio()
{
// we don't want the read to block in this example, so always
// check to see if data is available first.
if (my_HMTRP_Radio.dataAvailable())
{
var rv = my_HMTRP_Radio.readData(radioBuffer, bufferLength);
if (rv > 0)
{
var resultStr = "";
for (var x = 0; x < rv; x++)
resultStr += radioBuffer.getitem(x);
console.log("Received: " + resultStr);
}
if (rv < 0) // some sort of read error occured
{
console.log("Port read error.");
return;
}
}
g_counter++;
// every second, transmit "Hello World"
if (g_counter > 10)
{
var msg = "Hello World!";
console.log("Transmitting " + msg + "...");
// Adding 1 for NULL terminator.
// Note that SWIG automatically adds a NULL terminator,
// so no need to NULL-terminate ourselves.
// Just increment the message length to include
// the NULL that's already there
my_HMTRP_Radio.writeData(msg, (msg.length + 1));
g_counter = 0;
}
}
// When exiting: clear interval and print message
process.on('SIGINT', function()
{
clearInterval(g_myInterval);
console.log("Exiting...");
process.exit(0);
});