Emergency Black Sabbath button [Remade]
It was a little over 8 years ago that I started connecting buttons to serial ports in order to do some physical interactive computery stuff. For the last bunch of years, 2 button switches have been connected to the serial port of a computer hanging on my wall.
Pressing the small button would toggle play/pause on my media playing computer, and pressing the large button will result in the media player playing a Black Sabbath song.
It wasn't until the computer with the buttons went kaput that I realized just how much I use the play/pause toggle. After a few days of going bonkers from the lack of toggle and Black Sabbath, it was time to remedy the situation!
There is always a wave a meloncholy that washes over me when I disassemble something that I put together a long time ago. The serial connector was removed and leads were soldered to the button wires so that I could use the buttons with GPIO pins on a SBC
Connected to GPIO
The first Orb machine was chosen as the new button control device. Once the buttons where connected to GPIO, it was time to hack some code to do what needed doing: watch for a button press, and then run a shell command.
Enter the Python
#!/usr/bin/env python import subprocess import time import os class Switch: #first switch lift switch_down = False previous_value = "" current_value = "" def __init__(self, num): self.num = num #what is the path to the gpio? gpio_path = "/sys/class/gpio/gpio"+num self.value_path = "/sys/class/gpio/gpio"+num+"/value" #is the pin in use? if os.path.exists(gpio_path): #unexport the pin by writing the pin number to /sys/class/gpio/export f = open("/sys/class/gpio/unexport",'w') f.write(num) f.close() #export the pin by writing the pin number to /sys/class/gpio/export f = open("/sys/class/gpio/export",'w') f.write(num) f.close() #set the direction f = open("/sys/class/gpio/gpio"+num+"/direction", "w") f.write("in") f.close() #get the initial value of the pin self.previous_value = self.get_pin_value() def get_state(self): self.current_value = self.get_pin_value() #has the switch changed? if not self.current_value == self.previous_value: #reset the values self.previous_value = self.current_value #is the switch down? if self.switch_down: #not any more self.switch_down = False return "RELEASED" else: self.switch_down = True return "PRESSED" else: if self.switch_down: return "DOWN" else: return "UP" #get the pin value def get_pin_value(self): f = open(self.value_path) val = f.read().strip() f.close() return val looping = True #what number gpio? sw0 = Switch("13") sw1 = Switch("1") cmd = None while looping: time.sleep(0.1) if sw0.get_state() == "PRESSED": print "sw0 pressed" cmd = '/root/Projects/black_sabbath.sh' print cmd subprocess.Popen(cmd, shell=True) if sw1.get_state() == "PRESSED": print "sw1 pressed" cmd = "/root/Projects/redbar_toggle.py" print cmd subprocess.Popen(cmd, shell=True)
In case of emergency, Play Black Sabbath
The small play/pause toggle switch was placed next to the TV, and the Emergency Black Sabbath button was relocated to the floor so that it will be easily accessible to my dog."Kaylee, go play Black Sabbath" :)