No Description

hmtrp.cxx 4.9KB

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