2008-12-29
Back Story
On the Linux Outlaws Podcast, there is a feature on the show called the "crap alert"; which is an alarm sound that plays whenever Fabian, one of the show hosts, states that something is crap. There were a few discussions on the show's forums regarding playing the alarm on one's own computer.

"Hey, I have a switch and I want to use it to play a command on my computer!"

But how do I do it?
A bit of research found www.swen.uwaterloo.ca/~drayside/altinput/, which has information regarding making a switch to connect to the serial port of a computer. After a bit of soldering, my serial port switch was complete. All it needed was some sort of driver. Having previously worked with Python to access data from a serial port, I decided to use Python to poll the state of the switch and run a command when the switch is depressed.

Here is the "driver"
#!/usr/bin/env python
import serial
import gobject
import os
import sys
import subprocess

class serial_checker (gobject.GObject):
def __init__(self):
self.config = {"port":0,"carrier_detect_pressed":"echo carrier\ detect\ pressed\n"}
#read the config file
cfile = os.path.expanduser("~/.serial_switch" )
file_lines = open(cfile)
for line in file_lines:
#trim the white spaces
line = line.strip()
#if the line has length and the first char isn't a hash
if len(line) and line[0]!="#":
#this is a parsible line
(key,value) = line.split(":",1)
self.config[key.strip()] = value.strip()

self.cd_switch_down = False
self.cd_switch_action_running = False
gobject.GObject.__init__(self)
self.switch_count = 0
#define our serial
self.ser = serial.Serial(self.config["port"])
self.mainloop = gobject.MainLoop()
gobject.timeout_add(150, self.check_switches )

def cd_switch_action(self):
#what is the user requested command?
command = self.config["carrier_detect_pressed"]
#print command
#run the command, this should be in a thread or asynchronous
subprocess.Popen(command,shell=True)

def run(self):
#run the main loop
self.mainloop.run()

#check the switches
def check_switches(self):
cdval = self.ser.getCD()
if (cdval):
if(self.cd_switch_down==False):
self.cd_switch_down = True
#try to run the cd switch action
self.cd_switch_action()
else:
self.cd_switch_down = False

return True

#when the class is closed, clean up
def __del__(self):
self.ser.close()
self.mainloop.quit()


if (__name__=="__main__"):
ser_check = serial_checker()
ser_check.run()


When the python script is first run, it reads a configuration file from my home directory named ".serial_switch", which contains the command that I want to run when the switch is depressed. The config looks like this:
# this is a config file for serial_switch
# all configurations will be in key:value format

# serial port (default /dev/ttyS0 )
#port:/dev/ttyS0

#what happens when the carrier_detect switch is pressed
carrier_detect_pressed: gmrun


Although I played around with various commands that I thought would be fun to run when the switch was depressed, I found that gmrun was more useful than playing an audio file.

TODO
Add more switches to the serial connector.
Physically, the serial connector should be able to hand 3 or 4 switches so I should add the switches and update the software accordingly.

What it looks like
Here is the video I uploaded to youtube.
Comments
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
4 minus 4
Answer:
required
MCM_468x60_static
subscribe
 
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008