interactive_midi_config.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import pygame
4 import pygame.midi
5 import select
6 import sys
7 import yaml
8 import roslib
9 
10 try:
11  from jsk_teleop_joy.midi_util import MIDIParse, MIDICommand, MIDIException
12 except:
13  roslib.load_manifest("jsk_teleop_joy")
14  from jsk_teleop_joy.midi_util import MIDIParse, MIDICommand, MIDIException
15 
16 G_DEVICE_INFO = {
17  "device_name": "",
18  "analogs": [] #[[command, index], [command, index], ...
19  }
20 
21 class ParseException(Exception):
22  pass
23 
25  global G_DEVICE_INFO
26  devices = pygame.midi.get_count()
27  print("===========================================")
28  print("First, we choose device name:")
29  for d in range(devices):
30  info = pygame.midi.get_device_info(d)
31  if info[2] == 1:
32  print(" [%d] %s (%s)" % (d, info[1], "input"))
33  else:
34  print(" [%d] %s (%s)" % (d, info[1], "output"))
35  val = raw_input("Please select the device by number[%d-%d]:" % (0, d))
36  try:
37  parsed_number = int(val)
38  if parsed_number >= 0 and parsed_number <= d:
39  name = pygame.midi.get_device_info(parsed_number)[1]
40  G_DEVICE_INFO["device_name"] = name
41  print("")
42  print("device_name: %s" % (name))
43  return parsed_number
44  else:
45  raise ParseException("please input number bewtween %d to %d" % (0, d))
46  except ValueError:
47  raise ParseException("please input number")
48 
49 def configAnalogInputs(controller):
50  global G_DEVICE_INFO
51  print("===========================================")
52  print("Please move ALL the inputs")
53  print("===========================================")
54  print("The order you move them will be mapped into Joy/axes.")
55  print("If you want to finish analog mapping, please type 'q'")
56  analog_configs = []
57  while True:
58  ready = select.select([sys.stdin], [], [], 0.1)[0]
59  if ready:
60  line = sys.stdin.readline()
61  if line.startswith("q"):
62  print("We installed %d analog inputs" % (len(analog_configs)))
63  G_DEVICE_INFO["analogs"] = analog_configs
64  return
65  while controller.poll():
66  data = controller.read(1)
67  for elem_set in data:
68  try:
69  (command, index, val) = MIDIParse(elem_set)
70  if (command, index) not in analog_configs:
71  print("(%d, %d) installing into %d" % (command, index, len(analog_configs)))
72  analog_configs.append((command, index))
73  except MIDIException as e:
74  print("(%d, %d, %d) is not supported" % (elem_set[0][0], elem_set[0][1], elem_set[0][2]))
75 
76 def main():
77  pygame.midi.init()
78  while True:
79  try:
80  device_num = parseDeviceName()
81  break
82  except ParseException as e:
83  print(e.message)
84  print("")
85  continue
86  controller = pygame.midi.Input(device_num)
87  configAnalogInputs(controller)
88  f = open('/tmp/midi.yaml', 'w')
89  f.write(yaml.dump(G_DEVICE_INFO))
90  f.close()
91  print("writing the configuration to /tmp/midi.yaml")
92 
93 if __name__ == "__main__":
94  main()
95 
96 
def MIDIParse(message)
Definition: midi_util.py:7


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Fri May 14 2021 02:52:11