aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTat-Chee Wan (USM)2011-05-19 09:39:46 +0800
committerTat-Chee Wan (USM)2011-05-19 09:39:46 +0800
commite6e1c7160cb117d7a27999c29b73e32ba86517fe (patch)
tree355fb27845a52535dd3fd43c1f0f93054eccc769
parent4e4321aac2ac5c1ddc7c26736480ff2358827288 (diff)
rewrite test cases to make them modular
-rw-r--r--pyfantom.py140
1 files changed, 99 insertions, 41 deletions
diff --git a/pyfantom.py b/pyfantom.py
index 02a36e8..90fa389 100644
--- a/pyfantom.py
+++ b/pyfantom.py
@@ -471,47 +471,105 @@ class NXT:
print "pyfantom: No NXT in __del__."
if __name__ == '__main__':
- get_info = False
+ import struct
+ import time
+ MAXBUFLEN = 64
+
+ def get_nxt_from_iterator(searchBT):
+ devicelist = []
+ for i in NXTIterator(searchBT):
+ devicelist.append(i.get_nxt())
+ return devicelist
+
+ def get_resource_string_from_iterator(searchBT):
+ resourcelist = []
+ for i in NXTIterator(searchBT):
+ resourcelist.append(i.get_name())
+ return resourcelist
+
+ def get_nxt_from_resource_string(rs):
+ return NXT(rs)
+
+ def get_nxt_info(i):
+ print "resource string:", i.get_resource_string()
+ print " firmware version:", i.get_firmware_version()
+ print " get device info:", i.get_device_info()
+
+ def play_tone_nxt(i):
+ # Play Tone using Send Direct Command
+ print "send direct command (tone)..."
+ cmd = struct.pack('5B', 0x03, 0x00, 0x08, 0x10, 0x00)
+ r = i.send_direct_command(cmd)
+ time.sleep(1)
+ # Play Tone using Send Raw Command
+ print "send raw command (tone)..."
+ cmd = struct.pack('6B', 0x80, 0x03, 0x00, 0x18, 0x10, 0x00)
+ r = i.write(cmd)
+
+ def write_read_nxt(i, readbuflen=MAXBUFLEN):
+ # Test Variable Sized Reads of reply message
+ # Write VERSION SYS_CMD.
+ # Query:
+ # SYS_CMD: 0x01
+ # VERSION: 0x88
+ cmd = struct.pack('2B', 0x01, 0x88)
+ r = i.write(cmd)
+ print "wrote", r
+ # Response (7 data bytes):
+ # REPLY_CMD: 0x02
+ # VERSION: 0x88
+ # SUCCESS: 0x00
+ # PROTOCOL_VERSION minor
+ # PROTOCOL_VERSION major
+ # FIRMWARE_VERSION minor
+ # FIRMWARE_VERSION major
+ rep = i.read(readbuflen)
+ print "read", struct.unpack('%dB' % len(rep), rep)
+
+ def read_emptybuf_nxt(i, readbuflen=MAXBUFLEN):
+ rep = i.read(readbuflen)
+ print "read", struct.unpack('%dB' % len(rep), rep)
+
+
+ # check_bt == True: Bluetooth Interface
+ # check_bt == False: USB Interface
+ check_bt = False
+ get_info = True
+ play_tone = True
write_read = True
- for i in NXTIterator(False):
+ empty_readbuf = True
+
+
+ print "Retrieving list of NXT objects"
+ nxtlist = get_nxt_from_iterator(check_bt)
+ for i in nxtlist:
+ print "nxtlist: ", i
if get_info:
- print "name:", i.get_name()
- print "resource string:", i.get_resource_string()
- print "get_nxt:"
- nxt = i.get_nxt()
- print " firmware version:", nxt.get_firmware_version()
- print " get device info:", nxt.get_device_info()
- rs = nxt.get_resource_string()
- print " resource string:", rs
- del nxt
- print "NXT():"
- nxt = NXT(rs)
- print " resource string:", nxt.get_resource_string()
- del nxt
+ get_nxt_info(i)
+ if play_tone:
+ play_tone_nxt(i)
if write_read:
- nxt = i.get_nxt()
- import struct
- # Write VERSION SYS_CMD.
- # Query:
- # SYS_CMD: 0x01
- # VERSION: 0x88
- cmd = struct.pack('2B', 0x01, 0x88)
- r = nxt.write(cmd)
- print "wrote", r
- # Response:
- # REPLY_CMD: 0x02
- # VERSION: 0x88
- # SUCCESS: 0x00
- # PROTOCOL_VERSION minor
- # PROTOCOL_VERSION major
- # FIRMWARE_VERSION minor
- # FIRMWARE_VERSION major
- rep = nxt.read(7)
- print "read", struct.unpack('%dB' % len(rep), rep)
- # Same thing, without response.
- cmd = struct.pack('2B', 0x81, 0x88)
- r = nxt.write(cmd)
- print "wrote", r
- rep = nxt.read(7)
- print "read", struct.unpack('%dB' % len(rep), rep)
- del nxt
+ write_read_nxt(i)
+ if empty_readbuf:
+ read_emptybuf_nxt(i)
+ i.close()
+ del i
+
+ print "Creating NXT objects from list of Resource Strings"
+ nxtresourcelist = get_resource_string_from_iterator(check_bt)
+ for rs in nxtresourcelist:
+ print "nxtresourcelist: ", rs
+ i = NXT(rs, check_bt)
+ if get_info:
+ get_nxt_info(i)
+ if play_tone:
+ play_tone_nxt(i)
+ if write_read:
+ write_read_nxt(i)
+ if empty_readbuf:
+ read_emptybuf_nxt(i)
+ i.close()
+ del i
+
+ exit (0)
+