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


jsk_teleop_joy
Author(s): Ryohei Ueda
autogenerated on Sun May 28 2023 03:03:37