#!/usr/bin/python

# based on use_launcher_0, my missile launcher driver

import sys
import os
import time
import usb
import struct

# find all of the USB busses
busses = usb.busses()
print busses

# Find one device
for bus in busses:
    for dev in bus.devices:
        # Bus 002 Device 002: ID 08f7:0002 Vernier EasyTemp
        if dev.idVendor == 0x08f7 and dev.idProduct == 0x0002:
            rdev = dev

dev = rdev
assert dev.idVendor == 0x08f7
assert dev.idProduct == 0x0002
print dev, hex(dev.idVendor), hex(dev.idProduct)

# we know that there's one config, one interface, and one endpoint
conf = dev.configurations[0]
print "confs", dev.configurations
iface = conf.interfaces[0][0]
print "ifaces[0]", conf.interfaces[0]
endpoint = iface.endpoints[0]
print "endpoints", iface.endpoints
# open it
handle = dev.open()

print "iin", iface.interfaceNumber

# usb 2-2: usbfs: interface 0 claimed by ldusb while 'python' sets config #1
# ie. we need to run detacth *once* at least...
if None:
    handle.detachKernelDriver(0) # Should this be iface.interfaceNumber?
# Note that if you don't run this, you'll actually get messages like
# [222572.952000] usb usb1: usbfs: process 20986 (python) did not claim interface 1 before use
# in dmesg...
handle.setConfiguration(conf)
handle.claimInterface(iface)
handle.setAltInterface(iface)
handle.reset()

# print map(hex, map(ord, handle.interruptRead(endpoint.address, 8)))
print "handle", handle
print dir(handle)
#  ['__class__',
#   '__delattr__',
#   '__doc__',
#   '__getattribute__',
#   '__hash__',
#   '__init__',
#   '__new__',
#   '__reduce__',
#   '__reduce_ex__',
#   '__repr__',
#   '__setattr__',
#   '__str__',
#   'bulkRead',
#   'bulkWrite',
#   'claimInterface',
#   'clearHalt',
#   'controlMsg',
#   'detachKernelDriver',
#   'getDescriptor',
#   'getString',
#   'interruptRead',
#   'interruptWrite',
#   'releaseInterface',
#   'reset',
#   'resetEndpoint',
#   'setAltInterface',
#   'setConfiguration']

# print map(hex, map(ord, handle.bulkRead(endpoint.address, 8)))

# from ols_2005_driver_tutorial_example_code.tar.gz(gotemp.c):
CMD_ID_START_MEASUREMENTS=0x18
CMD_ID_INIT=0x1A

# from use_launcher_0, but similar to gotemp.c:
reqBuffer = [0] * 8
reqType = usb.TYPE_CLASS | usb.RECIP_INTERFACE | usb.ENDPOINT_OUT

reqBuffer[0] = CMD_ID_INIT
print handle.controlMsg(reqType, usb.REQ_SET_CONFIGURATION, reqBuffer, value=usb.DT_REPORT, index=0)
print handle.interruptRead(endpoint.address, 8)

reqBuffer[0] = CMD_ID_START_MEASUREMENTS
print handle.controlMsg(reqType, usb.REQ_SET_CONFIGURATION, reqBuffer, value=usb.DT_REPORT, index=0)
print handle.interruptRead(endpoint.address, 8)

for n in range(10):
    time.sleep(0.5)
    pkt = handle.interruptRead(endpoint.address, 8)
    parsed_pkt = struct.unpack("<bbHHH", struct.pack("<8b", *pkt))
    print parsed_pkt
    time.sleep(0.5)
# (-102, 26, 0, 0, 0, 0, 0, 0)
#  0x9a,  0x1a

# 	u8	measurements_in_packet;
# 	u8	rolling_counter;
# 	__le16	measurement0;
# 	__le16	measurement1;
# 	__le16	measurement2;

# 0x9a = -ENETRESET?  or is this the wrong layer


# (-102, 26, 0, 0, 0, 0, 0, 0)
# (90, 24, 0, 0, 0, 0, 0, 0)
# (1, 0, -24, 9, 0, 0, 0, 0)
# Traceback (most recent call last):
#   File "/home/eichin/thok/intranet/python/vernier/gotemp.py", line 96, in <module>
#     print handle.interruptRead(endpoint.address, 8)
# usb.USBError: could not detach kernel driver from interface 0: No data available


#  with sleep 1:
#  (-102, 26, 0, 0, 0, 0, 0, 0)
#  (90, 24, 0, 0, 0, 0, 0, 0)
#  (2, 0, -32, 9, -32, 9, 0, 0)
#  (2, 2, -32, 9, -32, 9, 0, 0)
#  (2, 4, -32, 9, -32, 9, 0, 0)
#  (2, 6, -32, 9, -32, 9, 0, 0)
#  (2, 8, -32, 9, -32, 9, 0, 0)
#  (2, 10, -32, 9, -32, 9, 0, 0)
#  (2, 12, -32, 9, -32, 9, 0, 0)
#  (2, 14, -32, 9, -32, 9, 0, 0)
