No Description

hmtrp.js 5.0KB

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