Package pype :: Module optix
[frames] | no frames]

Source Code for Module pype.optix

 1  #!/usr/bin/env pypenv 
 2  # -*- Mode: Python; tab-width: 4; py-indent-offset: 4; -*- 
 3   
 4  # NOTE: THIS NEEDS THE >= 1.0 pyusb library (not the old c-based version) 
 5   
 6  VENDOR_ID  = 0x0765 
 7  PRODUCT_ID = 0xD094 
 8  BUFSIZE    = 8*16 
 9  TIMEOUT    = 20000 
10   
11  import sys, string 
12   
13 -class OptixMissingDevice(Exception): pass
14
15 -class Optix():
16 - def __init__(self):
17 import usb.core 18 import usb.util 19 self.dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID) 20 if self.dev is None: 21 raise OptixMissing 22 23 self.dev.set_configuration() 24 cfg = self.dev.get_active_configuration() 25 intf = cfg[(0,0)] 26 self.ep_r = usb.util.find_descriptor(intf, 27 custom_match = \ 28 lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN) 29 self.ep_w = usb.util.find_descriptor(intf, 30 custom_match = \ 31 lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT) 32 self.dev.set_interface_altsetting(intf.bInterfaceNumber, intf.bAlternateSetting) 33 34 # will generate error if not attached and set's extra digit precision 35 # at same time 36 self.reset() 37 self.reset() 38 self.read('010ACF') # extra digit resolutio 39 self.read('0019CF') # no black point subtraction 40 self.read('ECF') # factory cal 41 self.read('0117CF') # enable drift compensation
42
43 - def read(self, cmd):
44 self.ep_r.clear_halt() 45 self.ep_w.clear_halt() 46 self.dev.write(self.ep_w.bEndpointAddress, cmd+'\r', TIMEOUT) 47 return self.dev.read(self.ep_r.bEndpointAddress, BUFSIZE, TIMEOUT)
48
49 - def reset(self):
50 self.readstr('0PR')
51
52 - def id(self):
53 s = self.readstr('SV') 54 return s.split('\r')[0]
55
56 - def readstr(self, cmd):
57 # is is a string of form ..results...\r<NN>, where NN is result code.. 58 s = string.join(map(chr, self.read(cmd)), '') 59 return s
60
61 - def XYZ(self):
62 s = self.readstr('0201RM') 63 s = string.strip(string.split(s, '\r')[0]) 64 return map(float,string.split(s)[1::2])
65
66 - def Yxy(self):
67 s = self.readstr('0301RM') 68 s = string.strip(string.split(s, '\r')[0]) 69 return map(float,string.split(s)[1::2])
70
71 - def set_mode(self, lcd=1):
72 if lcd: 73 self.read('0216CF') 74 else: 75 self.read('0116CF')
76
77 - def selfcalibrate(self):
78 self.read('CO')
79
80 - def clear(self):
81 self.read('CE') 82 self.read('CE') 83 self.read('CE')
84 85 if __name__ == '__main__': 86 o = Optix() 87 o.clear() 88 print o.id() 89 print 'Yxy', o.Yxy() 90 print 'XYZ', o.XYZ() 91