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