interactive_midi_config.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import pygame
00004 import pygame.midi
00005 import select
00006 import sys
00007 import yaml
00008 import roslib
00009 
00010 try:
00011   from jsk_teleop_joy.midi_util import MIDIParse, MIDICommand, MIDIException
00012 except:
00013   roslib.load_manifest("jsk_teleop_joy")
00014   from jsk_teleop_joy.midi_util import MIDIParse, MIDICommand, MIDIException
00015 
00016 G_DEVICE_INFO = {
00017   "device_name": "",
00018   "analogs": [] #[[command, index], [command, index], ...
00019   }
00020 
00021 class ParseException(Exception):
00022   pass
00023 
00024 def parseDeviceName():
00025   global G_DEVICE_INFO
00026   devices = pygame.midi.get_count()
00027   print "==========================================="
00028   print "First, we choose device name:"
00029   for d in range(devices):
00030     info = pygame.midi.get_device_info(d)
00031     if info[2] == 1:
00032       print "  [%d] %s (%s)" % (d, info[1], "input")
00033     else:
00034       print "  [%d] %s (%s)" % (d, info[1], "output")
00035   val = raw_input("Please select the device by number[%d-%d]:" % (0, d))
00036   try:
00037     parsed_number = int(val)
00038     if parsed_number >= 0 and parsed_number <= d:
00039       name = pygame.midi.get_device_info(parsed_number)[1]
00040       G_DEVICE_INFO["device_name"] = name
00041       print ""
00042       print "device_name: %s"  % (name)
00043       return parsed_number
00044     else:
00045       raise ParseException("please input number bewtween %d to %d" % (0, d))
00046   except ValueError:
00047     raise ParseException("please input number")
00048 
00049 def configAnalogInputs(controller):
00050   global G_DEVICE_INFO
00051   print "==========================================="
00052   print "Please move ALL the inputs"
00053   print "==========================================="
00054   print "The order you move them will be mapped into Joy/axes."
00055   print "If you want to finish analog mapping, please type 'q'"
00056   analog_configs = []
00057   while True:
00058     ready = select.select([sys.stdin], [], [], 0.1)[0]
00059     if ready:
00060       line = sys.stdin.readline()
00061       if line.startswith("q"):
00062         print "We installed %d analog inputs" % (len(analog_configs))
00063         G_DEVICE_INFO["analogs"] = analog_configs
00064         return
00065     while controller.poll():
00066       data = controller.read(1)
00067       for elem_set in data:
00068         try:
00069           (command, index, val) = MIDIParse(elem_set)
00070           if (command, index) not in analog_configs:
00071             print "(%d, %d) installing into %d" % (command, index, len(analog_configs))
00072             analog_configs.append((command, index))
00073         except MIDIException, e:
00074           print "(%d, %d, %d) is not supported" % (elem_set[0][0], elem_set[0][1], elem_set[0][2])
00075 
00076 def main():
00077   pygame.midi.init()
00078   while True:
00079     try:
00080       device_num = parseDeviceName()
00081       break
00082     except ParseException, e:
00083       print e.message
00084       print ""
00085       continue
00086   controller = pygame.midi.Input(device_num)
00087   configAnalogInputs(controller)
00088   f = open('/tmp/midi.yaml', 'w')
00089   f.write(yaml.dump(G_DEVICE_INFO))
00090   f.close()
00091   print "writing the configuration to /tmp/midi.yaml"
00092   
00093 if __name__ == "__main__":
00094   main()
00095 
00096   


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Fri Apr 19 2019 03:45:43