It’s that time of year, when we all go to a lot of Holiday parties. This evening I’ll be attending one – and I’ve decided to bring along Indigo, one of the three new battery-powered Lights. I’ve written a small Python script that has connected a series of different apps to her tactile switch. (Yes, the Light has a tactile switch in the front of its central opening.)
It’s not very complicated, and references the locations of specific programs in the file system – something that is not advisable and will be discouraged in release software, but was quick, dirty, and effective. Here’s the code:
#!/usr/bin/python # # Make the Light act like... a fun party device! # # Poll the tactile switch, when pressed, run a command. # When pressed again, turn it off. Go on to next command. # import subprocess, time, os switch_gpio = """/sys/class/gpio/gpio0/value""" ons = ["""/srv/http/cgi-bin/candle &""", """/srv/http/cgi-bin/flame &""", """/srv/http/cgi-bin/sweep &""", """/srv/http/cgi-bin/supersweep &""", """/srv/http/cgi-bin/placcel &""" ] offs = ["""sudo killall candle; batsignal""", """sudo killall flame; batsignal""", """sudo killall sweep; batsignal""", """sudo killall supersweep; batsignal""", """sudo killall placcel; batsignal""" ] cmd_location = 0 MAX_CMD = 4 def read_switch(): try: val = subprocess.check_output(["cat", switch_gpio]) except subprocess.CalledProcessError: print "CalledProcessError!" return False if val[:1] == "1": return False else: return True def change_state(new_state): global cmd_location, MAX_CMD if (new_state): os.system(ons[cmd_location]) else: os.system(offs[cmd_location]) cmd_location = cmd_location + 1 if (cmd_location > MAX_CMD): cmd_location = 0 if __name__ == '__main__': print cmd_location curr_state = False flip_flop = False while 1: switch_state = read_switch() #print switch_state if (curr_state == False) and (switch_state == True): if (flip_flop): flip_flop = False else: flip_flop = True change_state(flip_flop) curr_state = switch_state time.sleep(.1)