def __init__(self): self.pir = MyPiPIR(MyPiPIR.DEFAULT) self.led = MyPiLed(MyPiLed.RED) self.buzzer = MyPiBuzzer(MyPiBuzzer.DEFAULT) self.locks = [] self.tries = 0 self.max_tries = 3 self.locks.append(Lock('Vault')) self.logger = MyLogger("SecuritySystem") self.check_interval = self.__class__.CHECK_INTERVAL self.enabled = False
from led import MyPiLed led1 = MyPiLed(MyPiLed.RED) led2 = MyPiLed(MyPiLed.YELLOW) print("Non blocking") threads = [] threads.append(led1.blink(20,0.5)) threads.append(led2.blink(30,0.2)) for t in threads: t.join() print("Blocking") led1.blink(5,0.5,True) led2.blink(5,0.5,True)
#!/usr/bin/python # Get LED control class import RPi.GPIO as GPIO from led import MyPiLed try: yellow = MyPiLed(MyPiLed.YELLOW) red = MyPiLed(MyPiLed.RED) # Blink LEDs yellow.blink(4, 0.5) red.blink(5, 0.8) except KeyboardInterrupt: print("Quit!") finally: GPIO.cleanup()
#!/usr/bin/python # Get LED control class import RPi.GPIO as GPIO from led import MyPiLed try: yellow = MyPiLed(MyPiLed.YELLOW) red = MyPiLed(MyPiLed.RED) # Blink LEDs yellow.blink(4,0.5) red.blink(5,0.8) except KeyboardInterrupt: print("Quit!") finally: GPIO.cleanup()
class SecuritySystem: CHECK_INTERVAL = 0.01 THREADS = [] CODE = 1234 def __init__(self): self.pir = MyPiPIR(MyPiPIR.DEFAULT) self.led = MyPiLed(MyPiLed.RED) self.buzzer = MyPiBuzzer(MyPiBuzzer.DEFAULT) self.locks = [] self.tries = 0 self.max_tries = 3 self.locks.append(Lock('Vault')) self.logger = MyLogger("SecuritySystem") self.check_interval = self.__class__.CHECK_INTERVAL self.enabled = False def __check_code(self): while self.tries <= self.max_tries: code = input("Enter security system code (Tries: " + str(self.max_tries - self.tries) + "): ") if str(code) == str(self.__class__.CODE): return True else: self.tries += 1 self.logger.warn("Code entered incorrectly " + str(self.max_tries) + " times") self.buzzer.on() return False # Public implementation (non-blocking) def enable(self): if self.__check_code(): self.lockdown() if len(self.__class__.THREADS) == 0: self.enabled = True t = threading.Thread(target=self.__enable) t.start() self.__class__.THREADS.append(t) return t else: self.logger.warn("Security already active") return self.__class__THREADS[0] # Private implementation (blocking) def __enable(self): while self.enabled == True: state = self.pir.state() if state == MyPiPIR.ACTIVATED: self.logger.warn("Motion detected!") self.buzzer.on() self.led.blink(5, 0.2) time.sleep(1) elif state == MyPiPIR.DEACTIVATED: self.logger.info("Waiting for motion...") self.led.off() self.buzzer.off() elif state == MyPiPIR.ACTIVE: self.logger.warn("Motion still being detected!") self.led.blink(5, 0.2) self.buzzer.on() time.sleep(1) elif state == MyPiPIR.INACTIVE: self.led.off() self.buzzer.off() time.sleep(self.check_interval) # Disable the security system, wait for threads to finish def disable(self): if self.__check_code(): self.enabled = False self.end_lockdown() for t in self.__class__.THREADS: t.join() def lockdown(self): for lock in self.locks: lock.lock() def end_lockdown(self): for lock in self.locks: lock.unlock()
from led import MyPiLed from my_queue import MyQueue import os try: yellow = MyPiLed(4) red = MyPiLed(17) q = MyQueue() count = 0 led_choice = 0 os.system("clear") print("Which LED do you want to flash?") print("1: Yellow?") print("2: Red?") led_choice = input("Choose your option: ") os.system("clear") if led_choice == '1': print("You picked the Yellow LED") count = input("How many times do you want to blink?: ") yellow.blink(int(count), 0.5) if led_choice == '2': print("You picked the Red LED") count = input("How many times do you want to blink?: ") red.blink(int(count), 0.5) except:
from led import MyPiLed led1 = MyPiLed(MyPiLed.RED) led2 = MyPiLed(MyPiLed.YELLOW) print("Non blocking") threads = [] threads.append(led1.blink(20, 0.5)) threads.append(led2.blink(30, 0.2)) for t in threads: t.join() print("Blocking") led1.blink(5, 0.5, True) led2.blink(5, 0.5, True)
class SecuritySystem: CHECK_INTERVAL = 0.01 THREADS = [] CODE = 1234 def __init__(self): self.pir = MyPiPIR(MyPiPIR.DEFAULT) self.led = MyPiLed(MyPiLed.RED) self.buzzer = MyPiBuzzer(MyPiBuzzer.DEFAULT) self.locks = [] self.tries = 0 self.max_tries = 3 self.locks.append(Lock('Vault')) self.logger = MyLogger("SecuritySystem") self.check_interval = self.__class__.CHECK_INTERVAL self.enabled = False def __check_code(self): while self.tries <= self.max_tries: code = input("Enter security system code (Tries: " + str(self.max_tries - self.tries) + "): ") if str(code) == str(self.__class__.CODE): return True else: self.tries+=1 self.logger.warn("Code entered incorrectly " + str(self.max_tries) + " times") self.buzzer.on() return False # Public implementation (non-blocking) def enable(self): if self.__check_code(): self.lockdown() if len(self.__class__.THREADS) == 0: self.enabled = True t = threading.Thread(target=self.__enable) t.start() self.__class__.THREADS.append(t) return t else: self.logger.warn("Security already active") return self.__class__THREADS[0] # Private implementation (blocking) def __enable(self): while self.enabled == True: state = self.pir.state() if state == MyPiPIR.ACTIVATED: self.logger.warn("Motion detected!") self.buzzer.on() self.led.blink(5,0.2) time.sleep(1) elif state == MyPiPIR.DEACTIVATED: self.logger.info("Waiting for motion...") self.led.off() self.buzzer.off() elif state == MyPiPIR.ACTIVE: self.logger.warn("Motion still being detected!") self.led.blink(5,0.2) self.buzzer.on() time.sleep(1) elif state == MyPiPIR.INACTIVE: self.led.off() self.buzzer.off() time.sleep(self.check_interval) # Disable the security system, wait for threads to finish def disable(self): if self.__check_code(): self.enabled = False self.end_lockdown() for t in self.__class__.THREADS: t.join() def lockdown(self): for lock in self.locks: lock.lock() def end_lockdown(self): for lock in self.locks: lock.unlock()
from my_queue import MyQueue from led import MyPiLed try: # Running code led1 = MyPiLed(MyPiLed.YELLOW) led2 = MyPiLed(MyPiLed.RED) q = MyQueue() q.put([led1, 50, 0.1]) q.put([led2, 10, 0.5]) q.join() q.clear() except KeyboardInterrupt: q.clear() led1.off() led2.off() MyPiLed.reset()
import time import RPi.GPIO as GPIO from pir import MyPiPIR from led import MyPiLed from buzzer import MyPiBuzzer from my_logger import MyLogger logger = MyLogger("PIR") logger.debug("PIR Module Test (Ctrl-C to exit)") pir1 = MyPiPIR(MyPiPIR.DEFAULT) led1 = MyPiLed(MyPiLed.RED) buzzer1 = MyPiBuzzer(MyPiBuzzer.DEFAULT) check_interval = 0.01 try: logger.info("Ready!") while True: state = pir1.state() if state == MyPiPIR.ACTIVATED: logger.warn("Motion detected!") buzzer1.on() led1.blink(5, 0.2) time.sleep(1) elif state == MyPiPIR.DEACTIVATED: logger.info("Waiting for motion...") led1.off() buzzer1.off()
import time import RPi.GPIO as GPIO from pir import MyPiPIR from led import MyPiLed from buzzer import MyPiBuzzer from my_logger import MyLogger logger = MyLogger("PIR") logger.debug("PIR Module Test (Ctrl-C to exit)") pir1 = MyPiPIR(MyPiPIR.DEFAULT) led1 = MyPiLed(MyPiLed.RED) buzzer1 = MyPiBuzzer(MyPiBuzzer.DEFAULT) check_interval = 0.01 try: logger.info("Ready!") while True: state = pir1.state() if state == MyPiPIR.ACTIVATED: logger.warn("Motion detected!") buzzer1.on() led1.blink(5,0.2) time.sleep(1) elif state == MyPiPIR.DEACTIVATED: logger.info("Waiting for motion...") led1.off() buzzer1.off()