Created
September 19, 2010 11:20
-
-
Save tailhook/586682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pypm | |
import time | |
def listdev(options): | |
for i in xrange(pypm.CountDevices()): | |
inf = pypm.GetDeviceInfo(i) | |
print '{0}.{default} api: {1}, name: {2}, input: {3}, output: {4}'\ | |
.format(i, *inf, default='*' if options.device == i else ' ') | |
def serve(options): | |
inp = pypm.Input(options.device) | |
while True: | |
if inp.Poll(): | |
buf = inp.Read(128) | |
print buf | |
else: | |
time.sleep(0.1) | |
def get_options(): | |
import optparse | |
op = optparse.OptionParser() | |
op.add_option('-l', '--list', | |
help="List devices", | |
dest="action", default=serve, action="store_const", const=listdev) | |
op.add_option('-d', '--device', | |
help="Select device for input", | |
dest="device", default=pypm.GetDefaultInputDeviceID(), type='int') | |
return op | |
def main(): | |
op = get_options() | |
options, args = op.parse_args() | |
if args: | |
op.error("No arguments expected") | |
pypm.Initialize() | |
try: | |
options.action(options) | |
finally: | |
pypm.Terminate() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment