document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import sys
  5. import time
  6.  
  7. import usb.core
  8. import usb.util
  9.  
  10.  
  11.  
  12. packet_len = 64
  13.  
  14. # Packing a request. Please see HexWax documentation for the list of all commands
  15. # Packets are 64 bytes long, most of the commands are 4 bytes long. So up to 18
  16. # can be batched into a packet. For example command with bytes [0x94, 0x0, 0x0, 0x0]
  17. # is getting firmware id
  18. def pack_request(*arguments):
  19.     packet = [0x0] * packet_len
  20.     i = 0
  21.     for arg in arguments:
  22.         packet[i] = arg
  23.         i += 1
  24.     #packet[0:4] = [0x94, 0x0, 0x0, 0x0] #get firmware id
  25.     return \'\'.join([chr(c) for c in packet])
  26.  
  27. # Logs error to the error output
  28. def log_error(functionName, ret):
  29.     sys.stderr.write(functionName + (" failed with return code %d\\n" % ret))
  30.  
  31. # Logs result onto standard output. Result is 64 bytes as decimal numbers
  32. # Response is 64 bytes long
  33. def show_result(bytes):
  34.     sys.stdout.write("Result:")
  35.     sys.stdout.write(\'\'.join([\'%d \' % abyte for abyte in bytes]))
  36.  
  37. # Turns LED on the bord on or off depending on input parameter on. 0 is turning
  38. # the led on 1 is turning it off. The command is 0x9F set port bit (set output
  39. # pin value), port is 0x03 (port C), 0x06 is bit index (so this is 7th bit),
  40. # and the last bit is 0 for clear, 1 for set
  41. def set_led(on, dev):
  42.     if on:
  43.         param = 0x00
  44.     else:
  45.         param = 0x01
  46.  
  47.     raw = pack_request(0x9F, 0x03, 0x06, param) #set port bit - 0 to turn it on 1 to turn it off
  48.  
  49.     dev.write(1, raw, 0, 100)
  50.  
  51.     bytes = dev.read(0x81, packet_len, 0, 100)
  52.     show_result(bytes)
  53.  
  54.  
  55.  
  56. def main():
  57.     #initialising debuging - don\'t have a clue if this will work
  58.     os.environ[\'PYUSB_LOG_FILENAME\'] = \'debug\'
  59.  
  60.     dev = usb.core.find(idVendor=0x0b40, idProduct=0x0132)
  61.  
  62.     # was it found?
  63.     if dev is None:
  64.         raise ValueError(\'Device not found\')
  65.  
  66.     try:
  67.         dev.detach_kernel_driver(0)
  68.     except: # this usually mean that kernel driver has already been dettached
  69.         pass
  70.  
  71.     # set the active configuration, the device has 2 configurations you can see them
  72.     # by using: lsusb -vvv -d 0x0b40:
  73.     # this device has configurations 1 and 2 I\'m using the first one, not sure at the
  74.     # moment if 2nd would work the same
  75.     dev.set_configuration(1)
  76.  
  77.  
  78.     #removed following lines if you\'re running this from command line
  79.     #my netbeans didn\'t want to show standard output so I had to redirect it to
  80.     #a file
  81.     fsock = open(\'out.log\', \'w\')
  82.     sys.stdout = fsock
  83.     fsock2 = open(\'out.err\', \'w\')
  84.     sys.stderr = fsock2
  85.  
  86.  
  87.     #prepare our own command - this is get firmware id, only the first byte is
  88.     #significant
  89.     raw = pack_request(0x94)
  90.  
  91.     #send the packet
  92.     dev.write(1, raw, 0, 100)
  93.  
  94.     #then read the result
  95.     bytes = dev.read(0x81, packet_len, 0, 100)
  96.     show_result(bytes)
  97.  
  98.     #prepare another request
  99.     #set register TRISC bit 6 - port C bit 6 to be output
  100.     #mind though 0x9B command can be used to write any register, so check the
  101.     #microcontroller\'s datasheet
  102.     raw = pack_request(0x9B, 0x94, 0x06, 0x00)
  103.  
  104.     dev.write(1, raw, 0, 100)
  105.  
  106.     bytes = dev.read(0x81, packet_len, 0, 100)
  107.     show_result(bytes)
  108.  
  109.  
  110.     #once we have our bit set as output we can control it. Here, LED is turned
  111.     #on and then turned off after a second and it\'s done 10 times
  112.     for i in range(10):
  113.         set_led(True, dev)
  114.         time.sleep(1)
  115.         set_led(False, dev)
  116.         time.sleep(1)
  117.  
  118.     #done
  119.  
  120.  
  121. if __name__ == \'__main__\':
  122.   main()
');