Home | History | Annotate | Download | only in python
      1 #!/usr/bin/python
      2 # Author: Zion Orent <zorent (at] 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 import time, sys, signal, atexit
     25 import pyupm_hm11 as upmHm11
     26 
     27 # Instantiate a HM11 BLE Module on UART 0
     28 my_ble_obj = upmHm11.HM11(0)
     29 
     30 
     31 ## Exit handlers ##
     32 # This stops python from printing a stacktrace when you hit control-C
     33 def SIGINTHandler(signum, frame):
     34 	raise SystemExit
     35 
     36 # This function lets you run code on exit,
     37 # including functions from my_ble_obj
     38 def exitHandler():
     39 	print "Exiting"
     40 	sys.exit(0)
     41 
     42 # Register exit handlers
     43 atexit.register(exitHandler)
     44 signal.signal(signal.SIGINT, SIGINTHandler)
     45 
     46 
     47 bufferLength = 256
     48 
     49 # make sure port is initialized properly. 9600 baud is the default.
     50 if (not my_ble_obj.setupTty(upmHm11.cvar.int_B9600)):
     51 	print "Failed to setup tty port parameters"
     52 	sys.exit(0)
     53 
     54 
     55 usageStr = ("Usage:\n"
     56 "Pass a commandline argument (any argument) to this program\n"
     57 "to query the radio configuration and output it.  NOTE: the\n"
     58 "radio must be in CONFIG mode for this to work.\n\n"
     59 "Running this program without arguments will simply transmit\n"
     60 "'Hello World!' every second, and output any data received from\n"
     61 "another radio.\n\n")
     62 print usageStr
     63 
     64 # simple helper function to send a command and wait for a response
     65 def sendCommand(bleObj, cmd):
     66 	bleBuffer = upmHm11.charArray(bufferLength)
     67 	bleObj.writeData(cmd, len(cmd))
     68 
     69 	# wait up to 1 second
     70 	if (bleObj.dataAvailable(1000)):
     71 		bleObj.readData(bleBuffer, bufferLength)
     72 		bleData = ""
     73 		# read only the number of characters
     74 		# specified by myGPSSensor.readData
     75 		for x in range(0, bufferLength):
     76 			if (bleBuffer.__getitem__(x) == '\0'):
     77 				break
     78 			else:
     79 				bleData += bleBuffer.__getitem__(x)
     80 		print bleData
     81 	else:
     82 		print "Timed out waiting for response"
     83 
     84 
     85 if (len(sys.argv) > 1):
     86 	print "Sending command line argument (" + sys.argv[1] + ")..."
     87 	sendCommand(my_ble_obj, sys.argv[1])
     88 else:
     89 	# query the module address
     90 	addr = "AT+ADDR?";
     91 	print "Querying module address (" + addr + ")..."
     92 
     93 	sendCommand(my_ble_obj, addr)
     94 	time.sleep(1)
     95 	# query the module address
     96 	pin = "AT+PASS?";
     97 	print "Querying module PIN (" + pin + ")..."
     98 	sendCommand(my_ble_obj, pin)
     99 
    100 	# Other potentially useful commands are:
    101 	#
    102 	# AT+VERS? - query module version
    103 	# AT+ROLE0 - set as slave
    104 	# AT+ROLE1 - set as master
    105 	# AT+CLEAR - clear all previous settings
    106 	# AT+RESET - restart the device
    107 	#
    108 	# A comprehensive list is available from the datasheet at:
    109 	# http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf
    110 
    111