No Description

hmtrp.cxx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Author: Jon Trulson <jtrulson@ics.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. #include <unistd.h>
  25. #include <string.h>
  26. #include <iostream>
  27. #include <signal.h>
  28. #include "hmtrp.h"
  29. using namespace std;
  30. bool shouldRun = true;
  31. void sig_handler(int signo)
  32. {
  33. if (signo == SIGINT)
  34. shouldRun = false;
  35. }
  36. void printUsage()
  37. {
  38. cout << "Usage:" << endl;
  39. cout << "Pass a commandline argument (any argument) to this program"
  40. << endl;
  41. cout << "to query the radio configuration and output it. NOTE: the"
  42. << endl;
  43. cout << "radio must be in CONFIG mode for this to work."
  44. << endl;
  45. cout << endl;
  46. cout << "Running this program without arguments will simply transmit"
  47. << endl;
  48. cout << "'Hello World!' every second, and output any data received from"
  49. << endl;
  50. cout << "another radio."
  51. << endl;
  52. cout << endl;
  53. }
  54. const size_t bufferLength = 256;
  55. int main (int argc, char **argv)
  56. {
  57. signal(SIGINT, sig_handler);
  58. //! [Interesting]
  59. // Instantiate a HMTRP radio device on uart 0
  60. upm::HMTRP* radio = new upm::HMTRP(0);
  61. // make sure port is initialized properly. 9600 baud is the default.
  62. if (!radio->setupTty(B9600))
  63. {
  64. cerr << "Failed to setup tty port parameters" << endl;
  65. return 1;
  66. }
  67. printUsage();
  68. // By default, this radio simply transmits data sent via writeData()
  69. // and reads any available data via readData().
  70. // It can be placed into a configuration mode by grounding the
  71. // CONFIG pin on the module. When this is done, the various
  72. // configuration query and config methods can be used. In this
  73. // example, by default, we just read any data available fom the
  74. // device, and periodically transmit "Hello World".
  75. // If any argument was specified on the command line, do a simple
  76. // configuration query and output the results. The radio must be in
  77. // CONFIG mode for this to work.
  78. if (argc > 1)
  79. {
  80. // config mode
  81. uint32_t freq;
  82. uint32_t dataRate;
  83. uint16_t rxBandwidth;
  84. uint8_t modulation;
  85. uint8_t txPower;
  86. uint32_t uartBaud;
  87. if (radio->getConfig(&freq, &dataRate, &rxBandwidth, &modulation,
  88. &txPower, &uartBaud))
  89. {
  90. cout << "Radio configuration:" << endl;
  91. cout << "freq: " << freq << " dataRate: " << dataRate
  92. << " rxBandwidth: " << rxBandwidth << "Khz" << endl;
  93. cout << "modulation: " << int(modulation) << "Khz txPower: "
  94. << int(txPower) << " uartBaud: " << uartBaud << endl;
  95. }
  96. else
  97. {
  98. cerr << "getConfig() failed. Make sure the radio is in "
  99. << "CONFIG mode." << endl;
  100. }
  101. }
  102. else
  103. {
  104. // normal read/write mode
  105. char radioBuffer[bufferLength];
  106. int counter = 0;
  107. cout << "Running in normal read/write mode." << endl;
  108. while (shouldRun)
  109. {
  110. // we don't want the read to block in this example, so always
  111. // check to see if data is available first.
  112. if (radio->dataAvailable())
  113. {
  114. memset(radioBuffer, 0, bufferLength);
  115. int rv = radio->readData(radioBuffer, bufferLength - 1);
  116. if (rv > 0)
  117. cout << "Received: " << radioBuffer << endl;
  118. if (rv < 0) // some sort of read error occured
  119. {
  120. cerr << "Port read error." << endl;
  121. break;
  122. }
  123. continue;
  124. }
  125. usleep(100000); // 100ms
  126. counter++;
  127. // every second, transmit "Hello World"
  128. if (counter > 10)
  129. {
  130. static const char *hello = "Hello World!";
  131. cout << "Transmitting hello world..." << endl;
  132. radio->writeData((char *)hello, strlen(hello) + 1);
  133. counter = 0;
  134. }
  135. }
  136. }
  137. //! [Interesting]
  138. cout << "Exiting..." << endl;
  139. delete radio;
  140. return 0;
  141. }