def run(self): self.log("processing thread started") while self.running: # get the nr of vectors that were over the threshold nr_of_vectors = None try: nr_of_vectors = self.MOTIONS_FIFO.get(False) except Queue.Empty: pass if nr_of_vectors: # if enough vectors were over the thershold | and if button is active if nr_of_vectors > self.NR_VECTORS and (GPIO.input(24)==0 and self.DISABLE_RECORDING==0): #print nr_of_vectors # save the time of the last motion self.lastMotion = time.time() # increasse the motion counter self.motionsCounter += 1 #self.log("+1 motions counter = {0}".format(self.motionsCounter)) # if it's not recording, check if there were enough motions if not self.recording and self.motionsCounter > self.MOTIONS: # save the recording started time self.startedRecording = time.time() # get current timestamp for filenames self.timestamp = str(int(self.startedRecording)) # start recording to file, by switching from writing to stream to write to file self.recording = True rat.set_red_led(True) self.camera.split_recording("/tmp/part1_{0}.h264".format(self.timestamp), splitter_port=1) # and save the last 2 seconds from the stream self.write_stream("/tmp/part0_{0}.h264".format(self.timestamp)) self.MOTIONS_FIFO.task_done() now = time.time() # if there was no motion in the last 2 seconds, reset motion counter if (now-self.lastMotion) > self.MIN_LEN: #self.log("reset motions counter = {0}") self.motionsCounter = 0 if self.recording: if (now-self.lastMotion) > self.POST_MOTION or (now-self.startedRecording) > self.MAX_LEN: self.log("stop recording with {0}s since last motion, and length: {1}s".format((now-self.lastMotion), (now-self.startedRecording))) self.recording = False rat.set_red_led(False) self.camera.split_recording(self.stream, splitter_port=1) RECORDED_VIDEOS_FIFO.put( ("part0_{0}.h264".format(self.timestamp), "part1_{0}.h264".format(self.timestamp), "done_{0}.mp4".format(self.timestamp)))
pressed = False rat.post_log("Shutdown Button Started") while True: if not pressed and GPIO.input(17)==0: pressed = time.time() elif pressed and GPIO.input(17)==0: #print "pressed for {0}".format(time.time()-pressed) counter = int((time.time()-pressed)*10) inv_counter = 50-counter if inv_counter>=10: if counter%(inv_counter/10) == 0: RED_LED = not RED_LED rat.set_red_led(RED_LED) elif inv_counter<10: RED_LED = True rat.set_red_led(RED_LED) if counter>60: rat.set_green_led(True) rat.post_log("shutdown system") p = subprocess.Popen("shutdown -P -h now", shell=True) p.communicate() exit(0) elif pressed and GPIO.input(17)==1: RED_LED = False rat.set_red_led(RED_LED) pressed = False RED_LED = False
import sys import io import Queue import threading import traceback import RPi.GPIO as GPIO skip = False if len(sys.argv)>1: if sys.argv[1]=='skip': skip = True if not skip: for _ in xrange(0,5): rat.set_red_led(True) rat.set_green_led(False) time.sleep(1) rat.set_red_led(False) rat.set_green_led(True) time.sleep(1) def log(msg): sys.stdout.write("[camera] "+str(msg)+"\n") sys.stdout.flush() rat.set_red_led(False) rat.set_green_led(False) GPIO.setmode(GPIO.BCM)