11 from optparse
import OptionParser
13 roslib.load_manifest(
'jsk_teleop_joy')
21 string = 0-9 or [number-number] and this returns list of the numbers 27 if string.startswith(
"[")
and string.endswith(
"]"):
29 r = re.compile(
"\[([0-9]+)-([0-9]+)\]")
32 first_number = m.group(1)
33 last_number = m.group(2)
34 return range(int(first_number), int(last_number) + 1)
36 raise MIDIWriteError(
"failed to parse string: %s" % (string))
38 raise MIDIWriteError(
"failed to parse string: %s" % (string))
43 all_midi_commands =
"" 44 for c
in MIDICommand.allCommands():
45 midi_command_str =
"%s (%d)" % (MIDICommand.toStr(c), c)
46 if len(all_midi_commands) > 0:
47 all_midi_commands =
"%s, %s (%d)" % (all_midi_commands, MIDICommand.toStr(c),
50 all_midi_commands = midi_command_str
51 parser = OptionParser()
52 parser.add_option(
"--device_index",
"-d", help=
"device index", type=
"int")
53 parser.add_option(
"--midi_command",
"-m",
54 help=
"midi command: candidates are: " + all_midi_commands)
55 parser.add_option(
"--channel",
"-c", help=
"channel. You can specify the number in 0-9 or [from-to] format", default=
"0")
56 parser.add_option(
"--param1",
"-p", help=
"param1. You can specify the number in 0-9 or [from-to] format")
57 parser.add_option(
"--param2",
"-P", help=
"param2. You can specify the number in 0-9 or [from-to] format")
58 parser.add_option(
"--interval",
"-i", help=
"interval between each command", default=0.1, type=
"float")
59 parser.add_option(
"--write",
"-w", help=
"insert this output configuration into the configuration yaml")
60 parser.add_option(
"--use-param1", help=
"this option is only available with -w option. It specifies to use parameter1 for the intensity", action=
"store_true")
61 parser.add_option(
"--verbose",
"-v", help=
"verbose", action=
"store_true")
62 (options, args) = parser.parse_args()
65 if options.device_index ==
None:
67 if options.midi_command ==
None:
70 if options.param1 ==
None:
72 if options.param2 ==
None:
80 midi_command = int(options.midi_command)
83 for candidate
in MIDICommand.allCommands():
84 if MIDICommand.toStr(candidate).lower() == options.midi_command.lower():
85 midi_command = candidate
87 if midi_command ==
None:
88 raise MIDIWriteError(
"Unknown midi command: %s" % (options.midi_command))
92 controller = pygame.midi.Output(options.device_index)
95 midi_command_w_channel = midi_command | channel
99 print(
"Writing [%d, %d, %d] ([0x%X, 0x%X, 0x%X])" % (midi_command_w_channel, param1, param2,
100 midi_command_w_channel, param1, param2))
101 controller.write_short(midi_command_w_channel, param1, param2)
102 rospy.sleep(options.interval)
107 with open(options.write,
"r+")
as f:
108 config = yaml.load(f)
109 if "output" in config:
110 output = config[
"output"]
114 if options.use_param1:
115 configuration = (midi_command, channel,
True)
116 if configuration
not in output:
117 output.append(configuration)
120 configuration = (midi_command, channel,
False, param1)
121 if configuration
not in output:
122 output.append(configuration)
124 config[
"output"] = output
125 f.write(yaml.dump(config))
127 if __name__ ==
"__main__":
def parseRangedNumber(string)