FootswitchPI

In my free time I like playing the piano. The FootswitchPI is a small tool I have built to interface the Raspberry PI with a footswitch, allowing me to read sheet music on a PDF and conveniently move to the next/previous page with my foot.


Hardware

On the hardware side I used a standard 2-channels footswitch for guitar amplifiers and connected it to the GPIO ports on the Raspberry PI. The 6.35mm (1/4 inch) stereo jack connector has 3 pins (Tip, Ring and Sleeve) which I have connected to three GPIO pins as follows:

Software

On the software side I selected the following Python modules:

I've then configured the PDF reader to render two pages side by side and modified the keyboard shortcuts to a single key press (e.g. 'n' and 'p') to move to the next and previous page, respectively. The following code snippet shows how to map an hardware footswitch press to a virtual keyboard press.

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import signal
import Xlib

ch_to_char = {23: 'n', 24: 'p'}

def sigint_handler(signum, frame):
    GPIO.cleanup()
    exit()

def btn_callback(ch):
    if ch in ch_to_char:
        keyboard.press(ch_to_char[ch])
        keyboard.release(ch_to_char[ch])
    print('Btn %d callback: %d' % (ch, GPIO.input(ch)))

if __name__ == "__main__":
    try:
        from pynput.keyboard import Key, Controller
    except Xlib.error.DisplayNameError:
        print('pynput requires a running X server!')
        exit()

    signal.signal(signal.SIGINT, sigint_handler)
    keyboard = Controller()

    GPIO.setmode(GPIO.BCM)
    for ch in ch_to_char:
        GPIO.setup(ch, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.add_event_detect(ch, GPIO.BOTH,
                              callback=btn_callback,
                              bouncetime=500)

    input("Press ENTER to quit\n")
    GPIO.cleanup()