|
| 1 | +# USB Button Box Python API # |
| 2 | + |
| 3 | +To get started simply import the ioLabs module and create a USBBox instance: |
| 4 | + |
| 5 | + from ioLabs import USBBox |
| 6 | + |
| 7 | + usbbox=USBBox() |
| 8 | + |
| 9 | +If the physical box is not connected (or cannot be detected for some reason) an |
| 10 | +exception will be raised when you create this USBBox instance. |
| 11 | + |
| 12 | +## USBBox structure ## |
| 13 | + |
| 14 | +See the accompanying `rbox_structure` document about the high-level structure of the box. |
| 15 | +This document specified the more object oriented API for accessing the USBBox. |
| 16 | + |
| 17 | +## Sending Commands ## |
| 18 | + |
| 19 | +Once you have the USBBox instance you can send commands to the USBBox. Commands are |
| 20 | +sent via the `commands` member variable and are named after IDs specified in the |
| 21 | +boxes manual, only in lower case. e.g. |
| 22 | + |
| 23 | + usbbox.commands.resrtc() # send RESRTC (reset realtime clock) command |
| 24 | + usbbox.commands.p2set(0x00) # send P2SET with value 0 (should turn on LEDs) |
| 25 | + usbbox.commands.dirset(1,0,0) # enable loopback mode (button presses turn on LED) |
| 26 | + |
| 27 | +## Receiving reports ## |
| 28 | + |
| 29 | +You can register call-back functions on the `commands` object to receive notification |
| 30 | +about reports received from the box. The actual reports are delivered asynchronously, |
| 31 | +but to avoid thread safety issues they are stored on a queue until `process_received_reports` |
| 32 | +is called. As most events are time-stamped they can be processed as needed. |
| 33 | + |
| 34 | +To register a call-back and have it called whenever a key is pressed one can doing something |
| 35 | +like: |
| 36 | + |
| 37 | + import time |
| 38 | + from ioLabs import USBBox, REPORT |
| 39 | + # REPORT contains the report IDs |
| 40 | + |
| 41 | + usbbox=USBBox() |
| 42 | + def mycallback(report): |
| 43 | + print "got a report: %s" % report |
| 44 | + # register callback just for KEYDN reports |
| 45 | + usbbox.commands.add_callback(REPORT.KEYDN,mycallback) |
| 46 | + |
| 47 | + while True: |
| 48 | + usbbox.process_received_reports() |
| 49 | + time.sleep(0.1) |
| 50 | + |
| 51 | +Calling `process_received_reports` in this way ensures that out call-back will be called on the |
| 52 | +same thread as the rest of the program - avoiding any nasty surprises with asynchronous data |
| 53 | +access. This should also make it easier for integrating with GUI toolkits. |
| 54 | + |
| 55 | +## Recording reports ## |
| 56 | + |
| 57 | +Instead of registering call-backs one can instead opt to have commands sent to a file, using |
| 58 | +the `start_recording` method on the USBBox object. This takes a list of report IDs to record |
| 59 | +and a file to output them to: |
| 60 | + |
| 61 | + import time |
| 62 | + from ioLabs import USBBox, REPORT |
| 63 | + |
| 64 | + usbbox=USBBox() |
| 65 | + outfile=open('output.txt') |
| 66 | + # record all events |
| 67 | + usbbox.start_recording(REPORT.ALL_IDS(),outfile) |
| 68 | + time.sleep(30) |
| 69 | + usbbox.stop_recording() |
| 70 | + # output.txt should now contain last 30 seconds or so events |
| 71 | + |
| 72 | +## Miscellaneous ## |
| 73 | + |
| 74 | +The underlying HID device for the USB Button Box is stored in `device` on the USBBox instance. |
| 75 | +It can be accessed to manually send commands to the device using it's `set_report(report_data)` |
| 76 | +command - simply passing a string of the bytes that should be sent in the report. |
| 77 | + |
0 commit comments