def configAnalogInputs(controller):
    global G_DEVICE_INFO
    print "==========================================="
    print "Please move ALL the inputs"
    print "==========================================="
    print "The order you move them will be mapped into Joy/axes."
    print "If you want to finish analog mapping, please type 'q'"
    analog_configs = []
    while True:
        ready = select.select([sys.stdin], [], [], 0.1)[0]
        if ready:
            line = sys.stdin.readline()
            if line.startswith("q"):
                print "We installed %d analog inputs" % (len(analog_configs))
                G_DEVICE_INFO["analogs"] = analog_configs
                return
        while controller.poll():
            data = controller.read(1)
            for elem_set in data:
                try:
                    (command, index, val) = MIDIParse(elem_set)
                    if (command, index) not in analog_configs:
                        print "(%d, %d) installing into %d" % (
                            command, index, len(analog_configs))
                        analog_configs.append((command, index))
                except MIDIException, e:
                    print "(%d, %d, %d) is not supported" % (
                        elem_set[0][0], elem_set[0][1], elem_set[0][2])
def main():
    global joy
    pygame.midi.init()
    rospy.init_node('midi_joy')
    # parse the arg
    argv = rospy.myargv()
    if len(argv) == 0:
        rospy.logfatal("You need to specify config yaml file")
        sys.exit(1)
    config_file = argv[1]
    joy_pub = rospy.Publisher("/joy", Joy, queue_size=10)
    autorepeat_rate = rospy.get_param("~autorepeat_rate", 0)
    if autorepeat_rate == 0:
        r = rospy.Rate(1000)
    else:
        r = rospy.Rate(autorepeat_rate)
    with open(config_file, "r") as f:
        config = yaml.load(f)
        # open the device
        controller = openMIDIInputByName(config["device_name"])

        joy = Joy()
        joy.axes = [0.0] * len(config["analogs"])
        # automatically mapping to buttons from axes if it has NOTE_OFF or NOTE_ON MIDI commands
        button_configs = [
            c for c in config["analogs"]
            if c[0] == MIDICommand.NOTE_ON or c[0] == MIDICommand.NOTE_OFF
        ]
        if config.has_key("output"):
            out_controller = openMIDIOutputByName(config["device_name"])
            s = rospy.Subscriber(
                "~set_feedback", JoyFeedbackArray,
                lambda msg: feedback_array_cb(out_controller, config, msg))
        joy.buttons = [0] * len(button_configs)
        while not rospy.is_shutdown():
            joy.header.stamp = rospy.Time.now()
            p = False
            while controller.poll():
                data = controller.read(1)
                for elem_set in data:
                    p = True
                    (command, ind, val) = MIDIParse(elem_set)
                    try:
                        index = config["analogs"].index((command, ind))
                        joy.axes[index] = val
                        if command == MIDICommand.NOTE_ON or command == MIDICommand.NOTE_OFF:
                            button_index = button_configs.index((command, ind))
                            if val == 0.0:
                                joy.buttons[button_index] = 0
                            else:
                                joy.buttons[button_index] = 1
                    except:
                        rospy.logwarn("unknown MIDI message: (%d, %d, %f)" %
                                      (command, ind, val))
            if (autorepeat_rate != 0) or p:
                joy_pub.publish(joy)
            r.sleep()
Esempio n. 3
0
def main():
    pygame.midi.init()
    controller = pygame.midi.Input(int(sys.argv[1]))
    while True:
        while controller.poll():
            data = controller.read(1)
            for elem_set in data:
                midi_command = elem_set[0][0]
                print(elem_set[0], end=' ')
                print("(0x%X, 0x%X, 0x%X)" %
                      (midi_command, elem_set[0][1], elem_set[0][2]),
                      end=' ')
                print(MIDICommand.toStr(MIDICommand.detect(midi_command)))
                print(MIDIParse(elem_set))
        time.sleep(0.1)