Don't like ads? PRO users don't see any ads ;-)
Guest

digitalmihailo

By: a guest on Oct 13th, 2010  |  syntax: Python  |  size: 4.59 KB  |  hits: 484  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. #
  3. # Adapted from http://svn.debian.org/wsvn/libhid/trunk/swig/test_libhid.py
  4. #
  5. #
  6.  
  7. from hid import false
  8. import sys
  9. import time
  10.  
  11. # allow it to run right out of the build dir
  12. from hid import true
  13. import os
  14. libsdir = os.getcwd() + '/.libs'
  15. if os.path.isdir(libsdir) and os.path.isfile(libsdir + '/_hid.so'):
  16.   sys.path.insert(0, libsdir)
  17.  
  18. from hid import *
  19.  
  20.  
  21. packet_len = 64
  22.  
  23. # Packing a request. Please see HexWax documentation for the list of all commands
  24. # Packets are 64 bytes long, most of the commands are 4 bytes long. So up to 18
  25. # can be batched into a packet. For example command with bytes [0x94, 0x0, 0x0, 0x0]
  26. # is getting firmware id
  27. def pack_request(*arguments):
  28.     packet = [0x0] * packet_len
  29.     i = 0
  30.     for arg in arguments:
  31.         packet[i] = arg
  32.         i += 1
  33.     #packet[0:4] = [0x94, 0x0, 0x0, 0x0] #get firmware id
  34.     return ''.join([chr(c) for c in packet])
  35.  
  36. # Logs error to the error output
  37. def log_error(functionName, ret):
  38.     sys.stderr.write(functionName + (" failed with return code %d\n" % ret))
  39.  
  40. # Logs result onto standard output. Result is 64 bytes as decimal numbers
  41. # Response is 64 bytes long
  42. def show_result(bytes):
  43.     sys.stdout.write("Result:")
  44.     sys.stdout.write(''.join(['%d ' % ord(abyte) for abyte in bytes]))
  45.  
  46. # Turns LED on the bord on or off depending on input parameter on. 0 is turning
  47. # the led on 1 is turning it off. The command is 0x9F set port bit (set output
  48. # pin value), port is 0x03 (port C), 0x06 is bit index (so this is 7th bit),
  49. # and the last bit is 0 for clear, 1 for set
  50. def set_led(on, hid):
  51.     if on:
  52.         param = 0x00
  53.     else:
  54.         param = 0x01
  55.  
  56.     raw = pack_request(0x9F, 0x03, 0x06, param) #set port bit - 0 to turn it on 1 to turn it off
  57.  
  58.     ret = hid_interrupt_write(hid, 1, raw, packet_len)
  59.     if ret != HID_RET_SUCCESS:
  60.         log_error("hid_set_output_report", ret)
  61.  
  62.     ret, bytes = hid_interrupt_read(hid, 0x81, packet_len, 100)
  63.     if ret != HID_RET_SUCCESS:
  64.         log_error("hid_get_input_report", ret)
  65.  
  66.     show_result(bytes)
  67.  
  68.  
  69.  
  70. def main():
  71.     #initialising debuging
  72.     hid_set_debug(HID_DEBUG_ALL)
  73.     hid_set_debug_stream(sys.stderr)
  74.     hid_set_usb_debug(0)
  75.  
  76.     #init hid
  77.     ret = hid_init()
  78.     if ret != HID_RET_SUCCESS:
  79.         log_error("hid_init", ret)
  80.  
  81.     #find our device
  82.     hid = hid_new_HIDInterface()
  83.     matcher = HIDInterfaceMatcher()
  84.     matcher.vendor_id = 0x0b40
  85.     matcher.product_id = 0x0132
  86.  
  87.     #removed following lines if you're running this from command line
  88.     #my netbeans didn't want to show standard output so I had to redirect it to
  89.     #a file
  90.     fsock = open('out.log', 'w')
  91.     sys.stdout = fsock
  92.     fsock2 = open('out.err', 'w')
  93.     sys.stderr = fsock2
  94.  
  95.     #open our device
  96.     ret = hid_force_open(hid, 0, matcher, 3)
  97.     if ret != HID_RET_SUCCESS:
  98.         log_error("hid_force_open", ret)
  99.  
  100.     #write details about the device like handle, location, manufacturer etc.
  101.     ret = hid_write_identification(sys.stdout, hid)
  102.     if ret != HID_RET_SUCCESS:
  103.         log_error("hid_write_identification", ret)
  104.  
  105.     #prepare our own command - this is get firmware id, only the first byte is
  106.     #significant
  107.     raw = pack_request(0x94)
  108.  
  109.     #send the packet
  110.     ret = hid_interrupt_write(hid, 1, raw, packet_len)
  111.     if ret != HID_RET_SUCCESS:
  112.         log_error("hid_set_output_report", ret)
  113.  
  114.     #then read the result
  115.     ret, bytes = hid_interrupt_read(hid, 0x81, packet_len, 100)
  116.     if ret != HID_RET_SUCCESS:
  117.         log_error("hid_get_input_report", ret)
  118.  
  119.     show_result(bytes)
  120.  
  121.     #prepare another request
  122.     #set register TRISC bit 6 - port C bit 6 to be output
  123.     #mind though 0x9B command can be used to write any register, so check the
  124.     #microcontroller's datasheet
  125.     raw = pack_request(0x9B, 0x94, 0x06, 0x00)
  126.  
  127.     ret = hid_interrupt_write(hid, 1, raw, packet_len)
  128.     if ret != HID_RET_SUCCESS:
  129.         log_error("hid_set_output_report", ret)
  130.  
  131.     ret, bytes = hid_interrupt_read(hid, 0x81, packet_len, 100)
  132.     if ret != HID_RET_SUCCESS:
  133.         log_error("hid_get_input_report", ret)
  134.  
  135.     show_result(bytes)
  136.  
  137.  
  138.     #once we have our bit set as output we can control it. Here, LED is turned
  139.     #on and then turned off after a second and it's done 10 times
  140.     for i in range(10):
  141.         set_led(true, hid)
  142.         time.sleep(1)
  143.         set_led(false, hid)
  144.         time.sleep(1)
  145.    
  146.     #close and cleanup when we're done
  147.     ret = hid_close(hid)
  148.     if ret != HID_RET_SUCCESS:
  149.         log_error("hid_close", ret)
  150.  
  151.     hid_cleanup()
  152.  
  153. if __name__ == '__main__':
  154.   main()