1#!/usr/bin/python
2# Author: Zion Orent <zorent@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
24import time, sys, signal, atexit
25import pyupm_hmtrp as upmHmtrp
26
27# Instantiate a HMTRP radio device on uart 0
28my_HMTRP_Radio = upmHmtrp.HMTRP(0)
29
30
31## Exit handlers ##
32# This stops python from printing a stacktrace when you hit control-C
33def SIGINTHandler(signum, frame):
34	raise SystemExit
35
36# This function lets you run code on exit,
37# including functions from my_HMTRP_Radio
38def exitHandler():
39	print "Exiting"
40	sys.exit(0)
41
42# Register exit handlers
43atexit.register(exitHandler)
44signal.signal(signal.SIGINT, SIGINTHandler)
45
46
47myCounter = 0
48
49# normal read/write mode
50bufferLength = 256
51radioBuffer = upmHmtrp.charArray(bufferLength)
52
53# make sure port is initialized properly. 9600 baud is the default.
54if (not my_HMTRP_Radio.setupTty(upmHmtrp.cvar.int_B9600)):
55	print "Failed to setup tty port parameters"
56	sys.exit(0)
57
58
59usageStr = ("Usage:\n"
60"Pass a commandline argument (any argument) to this program\n"
61"to query the radio configuration and output it.  NOTE: the\n"
62"radio must be in CONFIG mode for this to work.\n\n"
63"Running this program without arguments will simply transmit\n"
64"'Hello World!' every second, and output any data received from\n"
65"another radio.\n\n")
66print usageStr
67
68'''
69By default, this radio simply transmits data sent via writeData()
70and reads any available data via readData().
71
72It can be placed into a configuration mode by grounding the
73CONFIG pin on the module.  When this is done, the various
74configuration query and config methods can be used.  In this
75example, by default, we just read any data available fom the
76device, and periodically transmit "Hello World".
77
78If any argument was specified on the command line, do a simple
79configuration query and output the results.  The radio must be in
80CONFIG mode for this to work.
81
82
83Note that the first command-line argument should be "hmtry.py"
84The data we want would be the second... if it exists
85'''
86if (len(sys.argv) > 1):
87	# config mode
88	freq = upmHmtrp.uint32Array(0)
89	dataRate = upmHmtrp.uint32Array(0)
90	rxBandwidth = upmHmtrp.uint16Array(0)
91	modulation = upmHmtrp.uint8Array(0)
92	txPower = upmHmtrp.uint8Array(0)
93	uartBaud = upmHmtrp.uint32Array(0)
94
95	if (my_HMTRP_Radio.getConfig(freq, dataRate, rxBandwidth,
96	modulation, txPower, uartBaud)):
97		print "Radio configuration:"
98		outputStr = ("freq: {0} dataRate: {1} "
99		"rxBandwidth: {2}Khz").format(freq.__getitem__(0),
100		dataRate.__getitem__(0),
101		rxBandwidth.__getitem__(0))
102		print outputStr
103
104		outputStr = "modulation: %d Khz txPower: %d uartBaud: %d" % (
105		modulation.__getitem__(0), txPower.__getitem__(0),
106		uartBaud.__getitem__(0))
107		print outputStr
108	else:
109		errString = ("getConfig() failed.  Make sure the radio "
110		"is in CONFIG mode.")
111		print errString
112else:
113	print "Running in normal read/write mode."
114	while (1):
115		# we don't want the read to block in this example, so always
116		# check to see if data is available first.
117		if (my_HMTRP_Radio.dataAvailable()):
118			rv = my_HMTRP_Radio.readData(radioBuffer, bufferLength)
119
120			if (rv > 0):
121				resultStr = "";
122				for x in range(rv):
123					resultStr += radioBuffer.__getitem__(x)
124				print "Received:", resultStr
125
126			if (rv < 0): # some sort of read error occured
127				print "Port read error."
128				sys.exit(0)
129		myCounter += 1
130		# every second, transmit "Hello World"
131		if (myCounter > 10):
132			msg = "Hello World!"
133
134			print "Transmitting %s..." % msg
135
136			# Adding 1 for NULL terminator.
137			# Note that SWIG automatically adds a NULL terminator,
138			# so no need to NULL-terminate ourselves.
139			# Just increment the message length to include
140			# the NULL that's already there
141			my_HMTRP_Radio.writeData(msg, (len(msg) + 1))
142			myCounter = 0
143		time.sleep(.1)
144