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 
25 //NOT TESTED!!!
26 public class HMTRPSample {
27 
28 	static private final int bufferLength = 255;
29 
30 	static {
31 		try {
32 			System.loadLibrary("javaupm_hmtrp");
33 		} catch (UnsatisfiedLinkError e) {
34 			System.err.println("error in loading native library");
35 			System.exit(-1);
36 		}
37 	}
38 
printUsage()39 	private static void printUsage() {
40 		System.out.println("Usage:");
41 		System.out.println("Pass a commandline argument (any argument) to this program");
42 		System.out.println("to query the radio configuration and output it.  NOTE: the");
43 		System.out.println("radio must be in CONFIG mode for this to work.");
44 		System.out.println("Running this program without arguments will simply transmit");
45 		System.out.println("'Hello World!' every second, and output any data received from");
46 		System.out.println("another radio.");
47 	}
48 
main(String[] args)49 	public static void main(String[] args) throws InterruptedException {
50 		// ! [Interesting]
51 		// Instantiate a HMTRP radio device on uart 0
52 		upm_hmtrp.HMTRP radio = new upm_hmtrp.HMTRP(0);
53 
54 		// make sure port is initialized properly. 9600 baud is the default.
55 		if (!radio.setupTty()) {
56 			System.err.println("Failed to setup tty port parameters");
57 			System.exit(-1);
58 		}
59 		printUsage();
60 
61 		// By default, this radio simply transmits data sent via writeData()
62 		// and reads any available data via readData().
63 
64 		// It can be placed into a configuration mode by grounding the
65 		// CONFIG pin on the module. When this is done, the various
66 		// configuration query and config methods can be used. In this
67 		// example, by default, we just read any data available fom the
68 		// device, and periodically transmit "Hello World".
69 
70 		// If any argument was specified on the command line, do a simple
71 		// configuration query and output the results. The radio must be in·
72 		// CONFIG mode for this to work.
73 
74 		if (args.length > 0) {
75 			// config mode
76 			long[] freq = {0};
77 			long[] dataRate = {0};
78 			int[] rxBandwidth = {0};
79 			short[] modulation = {0};
80 			short[] txPower = {0};
81 			long[] uartBaud = {0};
82 
83 			if (radio.getConfig(freq, dataRate, rxBandwidth, modulation, txPower, uartBaud)) {
84 				System.out.println("Radio configuration:");
85 				System.out.println("freq: " + freq[0] + " dataRate: " + dataRate[0]
86 						+ " rxBandwidth: " + rxBandwidth[0] + "Khz");
87 				System.out.println("modulation: " + modulation[0] + "Khz txPower: " + txPower[0]
88 						+ " uartBaud: " + uartBaud[0]);
89 			} else {
90 				System.err.println("getConfig() failed.  Make sure the radio is in CONFIG mode.");
91 			}
92 		} else {
93 			// normal read/write mode
94 			byte[] radioBuffer = new byte[bufferLength];
95 			byte[] hello = "Hello World".getBytes();
96 			int counter = 0;
97 
98 			System.out.println("Running in normal read/write mode.");
99 
100 			while (true) {
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 (radio.dataAvailable()) {
104 					int rv = radio.readData(radioBuffer);
105 
106 					if (rv > 0) {
107 						System.out.print("Received: ");
108 						for (int i = 0; i < radioBuffer.length; i++)
109 							System.out.print((char) radioBuffer[i]);
110 						System.out.println();
111 					} else {
112 						System.err.println("Port read error.");
113 						break;
114 					}
115 					continue;
116 				}
117 
118 				Thread.sleep(100);
119 
120 				counter++;
121 				// every second, transmit "Hello World!"
122 				if (counter > 10) {
123 					System.out.println("Transmitting hello world...");
124 					radio.writeData(hello);
125 					counter = 0;
126 				}
127 			}
128 		}
129 		// ! [Interesting]
130 	}
131 }
132