Python, probably a simple solution with dictionary and functions but dont know how
Question: So...I am doing a volunteer school project (building a quadcopter) and have
been sitting here for a very long time trying to solve the problem...:

Basically, i was able to start the motor at a certain speed per remote control
(Yes, the one youd use for TV :D)

But know, I'm struggling with adding new buttons to compare them with the
input as shown in the following:

    
    
    import serial
    import time
    from RPIO import PWM
    
    ser = serial.Serial("/dev/ttyAMA0")
    ser.baudrate = 2400
    
    motorspeed = 1000
    s = PWM.Servo()
    
    #initializing
    print "\033[1;32mINITIALIZING SEQUENCE STARTED\033[1;m"
    s.set_servo(27, 2000)
    time.sleep(0.5)
    s.set_servo(27, 1000)
    time.sleep(2)
    print "\033[1;32mINITIALIZING SEQUENCE COMPLETE\033[1;m"
    print "\033[1;36mAWAITING PWM SIGNALS\033[1;m"
    
    buttons = {
        "up": [32, 36, 36, 27, 219, 250, 32, 36, 36, 27, 219, 250, 32, 36, 36], 
        "button1": [32, 219, 219, 36, 196, 32, 219, 219, 36, 196, 32, 219, 219, 36, 196]}
    # every button has a signature
    
    input = []
    # signature thats coming from the remote
    
    while input != buttons[:]:
    # I suspect a fault here, I want to compare all key values with the input list
       for i in range(0, 15):
            data = ser.read(1)
            print ord(data)
            input.append(ord(data))
    # Every IR button has a unique "key", thats what I am comparing
        print input
    
        if input == buttons["button1"]:
            print ("Button pressed!")
            s.set_servo(27, motorspeed)
            time.sleep(3)
         elif input == buttons["up"]:
            s.set_servo(27, motorspeed + 100
        else:
            del input[:]
    

What the problem is: I want to compare the list called "input" with the
dictionary "buttons". If one of the lists in dictionary is identical with the
list "input" it should the raise motorspeed


Answer: If I understand what you are looking for help on, you could try something like
this:

    
    
    buttons = {
        "up": [32, 36, 36, 27, 219, 250, 32, 36, 36, 27, 219, 250, 32, 36, 36],    
        "button1": [32, 219, 219, 36, 196, 32, 219, 219, 36, 196, 32, 219, 219, 36, 196],
        "new_button" : [1]
        # etc.
    }
    
    def up_action():
        s.set_servo(27, motorspeed+100)
    
    def button1_action():
        s.set_servo(27, motorspeed)
        time.sleep(3)
    
    def new_button_action():
        print "Hello! I am a new button!"
    
    actions = {
         "up" : up_action,
         "button1" : button1_action,
         "new_button" : new_button_action,
         # etc.
    }
    
    input = ... # get your input the way you have it
                # EDIT: As others have pointed out, you may
                # want to adjust your while loop
    
    for button_name,button_signature in buttons.iteritems():
        if input == button_signature:
            button_pressed = button_name
            break
    
    action = actions[button_pressed]
    action()
    

To add functionality for any new button you would just need to add an entry to
each dictionary:

  1. Add the input signature to `buttons` ({"button_name": [...input sequence...]})
  2. Create a function that performs the desired behavior.
  3. Add that function to `actions` ({"button_name": button_function_name})

