No Description

HMTRPSample.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
  3. * Copyright (c) 2015 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. //NOT TESTED!!!
  25. public class HMTRPSample {
  26. static private final int bufferLength = 255;
  27. private static void printUsage() {
  28. System.out.println("Usage:");
  29. System.out.println("Pass a commandline argument (any argument) to this program");
  30. System.out.println("to query the radio configuration and output it. NOTE: the");
  31. System.out.println("radio must be in CONFIG mode for this to work.");
  32. System.out.println("Running this program without arguments will simply transmit");
  33. System.out.println("'Hello World!' every second, and output any data received from");
  34. System.out.println("another radio.");
  35. }
  36. public static void main(String[] args) throws InterruptedException {
  37. // ! [Interesting]
  38. // Instantiate a HMTRP radio device on uart 0
  39. upm_hmtrp.HMTRP radio = new upm_hmtrp.HMTRP(0);
  40. // make sure port is initialized properly. 9600 baud is the default.
  41. if (!radio.setupTty()) {
  42. System.err.println("Failed to setup tty port parameters");
  43. System.exit(-1);
  44. }
  45. printUsage();
  46. // By default, this radio simply transmits data sent via writeData()
  47. // and reads any available data via readData().
  48. // It can be placed into a configuration mode by grounding the
  49. // CONFIG pin on the module. When this is done, the various
  50. // configuration query and config methods can be used. In this
  51. // example, by default, we just read any data available fom the
  52. // device, and periodically transmit "Hello World".
  53. // If any argument was specified on the command line, do a simple
  54. // configuration query and output the results. The radio must be in·
  55. // CONFIG mode for this to work.
  56. if (args.length > 0) {
  57. // config mode
  58. long[] freq = {0};
  59. long[] dataRate = {0};
  60. int[] rxBandwidth = {0};
  61. short[] modulation = {0};
  62. short[] txPower = {0};
  63. long[] uartBaud = {0};
  64. if (radio.getConfig(freq, dataRate, rxBandwidth, modulation, txPower, uartBaud)) {
  65. System.out.println("Radio configuration:");
  66. System.out.println("freq: " + freq[0] + " dataRate: " + dataRate[0]
  67. + " rxBandwidth: " + rxBandwidth[0] + "Khz");
  68. System.out.println("modulation: " + modulation[0] + "Khz txPower: " + txPower[0]
  69. + " uartBaud: " + uartBaud[0]);
  70. } else {
  71. System.err.println("getConfig() failed. Make sure the radio is in CONFIG mode.");
  72. }
  73. } else {
  74. // normal read/write mode
  75. byte[] radioBuffer = new byte[bufferLength];
  76. byte[] hello = "Hello World".getBytes();
  77. int counter = 0;
  78. System.out.println("Running in normal read/write mode.");
  79. while (true) {
  80. // we don't want the read to block in this example, so always
  81. // check to see if data is available first.
  82. if (radio.dataAvailable()) {
  83. int rv = radio.readData(radioBuffer);
  84. if (rv > 0) {
  85. System.out.print("Received: ");
  86. for (int i = 0; i < radioBuffer.length; i++)
  87. System.out.print((char) radioBuffer[i]);
  88. System.out.println();
  89. } else {
  90. System.err.println("Port read error.");
  91. break;
  92. }
  93. continue;
  94. }
  95. Thread.sleep(100);
  96. counter++;
  97. // every second, transmit "Hello World!"
  98. if (counter > 10) {
  99. System.out.println("Transmitting hello world...");
  100. radio.writeData(hello);
  101. counter = 0;
  102. }
  103. }
  104. }
  105. // ! [Interesting]
  106. }
  107. }