1/*jslint node:true, vars:true, bitwise:true, unparam:true */ 2/*jshint unused:true */ 3 4/* 5* Author: Zion Orent <zorent@ics.com> 6* Copyright (c) 2015 Intel Corporation. 7* 8* Permission is hereby granted, free of charge, to any person obtaining 9* a copy of this software and associated documentation files (the 10* "Software"), to deal in the Software without restriction, including 11* without limitation the rights to use, copy, modify, merge, publish, 12* distribute, sublicense, and/or sell copies of the Software, and to 13* permit persons to whom the Software is furnished to do so, subject to 14* the following conditions: 15* 16* The above copyright notice and this permission notice shall be 17* included in all copies or substantial portions of the Software. 18* 19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26*/ 27 28function printUsage() 29{ 30 var outputStr = "Usage:\n" + 31 "Pass a commandline argument (any argument) to this program\n" + 32 "to query the radio configuration and output it. NOTE: the\n" + 33 "radio must be in CONFIG mode for this to work.\n\n" + 34 "Running this program without arguments will simply transmit\n" + 35 "'Hello World!' every second, and output any data received from\n" + 36 "another radio.\n\n"; 37 console.log(outputStr); 38} 39 40var HMTRP_Radio = require('jsupm_hmtrp'); 41 42// Instantiate a HMTRP radio device on uart 0 43var my_HMTRP_Radio = new HMTRP_Radio.HMTRP(0); 44 45var g_myInterval; 46var g_counter = 0; 47 48// normal read/write mode 49var bufferLength = 256; 50var radioBuffer = new HMTRP_Radio.charArray(bufferLength); 51 52// make sure port is initialized properly. 9600 baud is the default. 53if (!my_HMTRP_Radio.setupTty(HMTRP_Radio.int_B9600)) 54{ 55 console.log("Failed to setup tty port parameters"); 56 process.exit(0); 57} 58 59printUsage(); 60 61// By default, this radio simply transmits data sent via writeData() 62// and reads any available data via readData(). 63 64// It can be placed into a configuration mode by grounding the 65// CONFIG pin on the module. When this is done, the various 66// configuration query and config methods can be used. In this 67// example, by default, we just read any data available fom the 68// device, and periodically transmit "Hello World". 69 70// If any argument was specified on the command line, do a simple 71// configuration query and output the results. The radio must be in 72// CONFIG mode for this to work. 73 74 75// Note that the first command-line argument 76// should be "node" and the second is "hmtrp.js". 77// The data we want would be the third... if it exists 78if (process.argv.length > 2) 79{ 80 // config mode 81 var freq = new HMTRP_Radio.uint32Array(0); 82 var dataRate = new HMTRP_Radio.uint32Array(0); 83 var rxBandwidth = new HMTRP_Radio.uint16Array(0); 84 var modulation = new HMTRP_Radio.uint8Array(0); 85 var txPower = new HMTRP_Radio.uint8Array(0); 86 var uartBaud = new HMTRP_Radio.uint32Array(0); 87 88 if (my_HMTRP_Radio.getConfig( 89 freq, dataRate, rxBandwidth, modulation, txPower, uartBaud)) 90 { 91 console.log("Radio configuration:"); 92 var outputStr = "freq: " + freq.getitem(0) + 93 " dataRate: " + dataRate.getitem(0) + 94 " rxBandwidth: " + rxBandwidth.getitem(0) + "Khz"; 95 console.log(outputStr); 96 97 outputStr = "modulation: " + parseInt(modulation.getitem(0)); 98 outputStr += " Khz txPower: " + parseInt(txPower.getitem(0)); 99 outputStr += " uartBaud: " + uartBaud.getitem(0); 100 console.log(outputStr); 101 } 102 else 103 { 104 var errString = "getConfig() failed. Make sure the radio " + 105 "is in CONFIG mode."; 106 console.log(errString); 107 } 108} 109else 110{ 111 console.log("Running in normal read/write mode."); 112 g_myInterval = setInterval(runRadio, 100); // 100ms 113} 114 115function runRadio() 116{ 117 // we don't want the read to block in this example, so always 118 // check to see if data is available first. 119 if (my_HMTRP_Radio.dataAvailable()) 120 { 121 var rv = my_HMTRP_Radio.readData(radioBuffer, bufferLength); 122 123 if (rv > 0) 124 { 125 var resultStr = ""; 126 for (var x = 0; x < rv; x++) 127 resultStr += radioBuffer.getitem(x); 128 console.log("Received: " + resultStr); 129 } 130 131 if (rv < 0) // some sort of read error occured 132 { 133 console.log("Port read error."); 134 return; 135 } 136 } 137 g_counter++; 138 // every second, transmit "Hello World" 139 if (g_counter > 10) 140 { 141 var msg = "Hello World!"; 142 143 console.log("Transmitting " + msg + "..."); 144 145 // Adding 1 for NULL terminator. 146 // Note that SWIG automatically adds a NULL terminator, 147 // so no need to NULL-terminate ourselves. 148 // Just increment the message length to include 149 // the NULL that's already there 150 my_HMTRP_Radio.writeData(msg, (msg.length + 1)); 151 g_counter = 0; 152 } 153} 154 155// When exiting: clear interval and print message 156process.on('SIGINT', function() 157{ 158 clearInterval(g_myInterval); 159 console.log("Exiting..."); 160 process.exit(0); 161}); 162