Source

media_control_buttons / media_control.py

Full commit
Luke Plant 302a7e4 















































#!/usr/bin/env python

import time
import os
import RPi.GPIO as GPIO

import os
import sys

def mpc_command(cmd):
    def f():
        os.system("mpc %s" % cmd)
    return f

ACTIONS = [
    (27, mpc_command('toggle')),
    (22, mpc_command('prev')),
    (17, mpc_command('next')),
]


def setup():
    GPIO.setmode(GPIO.BCM)
    for p, f in ACTIONS:
        GPIO.setup(p, GPIO.IN)


def mainloop():
    pressed = set()
    while True:
        for p, f in ACTIONS:
            if GPIO.input(p) == False: # pressed
                if p not in pressed:
                    print p
                    pressed.add(p)
                    f()
            else:
                if p in pressed:
                    pressed.remove(p)

        time.sleep(0.05)

def main():
    setup()
    mainloop()


if __name__ == '__main__':
    main()