#!/usr/bin/env python # -*- coding: utf-8 -*- # # The IR-Code is from the app "shutter" by Thomas Waelti # import os, time, subprocess, datetime ambient_sensor = '/sys/class/i2c-adapter/i2c-2/2-0029/lux' #Treshold for the ambient light min_threshold = 7 #Max treshold to avoid a release via the flash (not neccessary with "break") max_threshold = 100 #Camera to use REMOTE = "CANON-RC1/RC5" CODE = "S" #REMOTE = "NikonDSLR" #CODE = "shutter" #REMOTE = "Olympus_RM-2" #CODE = "CAPTURE" #REMOTE = "Pentax_RC-F" #CODE = "CAPTURE" def start_lircd(): #Check if Lircd runs lircdPID = subprocess.Popen(["pgrep", "lircd"], stdout=subprocess.PIPE).communicate()[0] if lircdPID == "": lircdPID = 0 stopLircdOnEnd = True if lircdPID > 0: message = "LIRCD already started: PID %d\n" % int(lircdPID) print message stopLircdOnEnd = False else: #Start if it doesn't message = "LIRCD started for " + os.getenv("LOGNAME") print message arg = [] if os.getenv("LOGNAME") == "root": #For remote debugging through SSH as root arg = ["/etc/init.d/lirc", "start"] else: arg = ["sudo", "/etc/init.d/lirc", "start"] lircdstart = subprocess.Popen(arg) lircdstart.wait() return stopLircdOnEnd def stop_lircd(stopLircdOnEnd): if stopLircdOnEnd == True: #We launched it, so we need to stop it again if os.getenv("LOGNAME") == "root": #For remote debugging through SSH as root lircdstop = subprocess.Popen(["/etc/init.d/lirc", "stop"]) else: lircdstop = subprocess.Popen(["sudo", "/etc/init.d/lirc", "stop"]) lircdstop.wait() print "LIRCD stopped." def on_toggle(remote, code): irsend = subprocess.Popen(('/usr/bin/irsend', 'send_once', remote, code), stdin = subprocess.PIPE) irsend.stdin.close() irsend.wait() if irsend.returncode != 0: message = "IRsend failed: %s" % irsend.returncode else: message = "IRsend success: %s" % irsend.returncode print message stopLircdOnEnd = start_lircd() # TODO: BAD BAD _VERY_ BAD!!! f = open(ambient_sensor, 'r') print "Waiting for event..." while True: value = int(f.read()) if value >= min_threshold and value <= max_threshold: on_toggle(REMOTE, CODE) print "treshold (%s) exceeded: %s" %(min_threshold, value) break f.seek(0) stop_lircd(stopLircdOnEnd)