From a3a73ea1e85423351b1a21c14b60e4669e3a3330 Mon Sep 17 00:00:00 2001 From: Jaculabilis Date: Wed, 10 Jan 2018 15:19:31 -0600 Subject: [PATCH] Added initial PWM code --- validate.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/validate.py b/validate.py index 64b953c..a076664 100644 --- a/validate.py +++ b/validate.py @@ -2,6 +2,10 @@ import sys from datetime import datetime +import time +import RPi.GPIO as IO + +MOTOR_PIN = 19 def timestamped(s): """ @@ -9,6 +13,25 @@ def timestamped(s): """ return "[{:%Y-%m-%d %H:%M:%S}] {}".format(datetime.now(), s) +def setup_pwm(): + """ + Performs initialization for pulse width modulation. + Returns the PWM object for the motor pin. + """ + IO.setwarnings(False) + IO.setmode(IO.BCM) + IO.setup(MOTOR_PIN, IO.OUT) + p = IO.PWM(MOTOR_PIN, 100) # Second argument is frequency, I think + return p + +def unlock_door(p): + """ + Uses PWM to turn the motor connected to the door handle. + """ + p.start(50) + time.sleep(1.0) + p.stop() + def read_loop(): """ Simple REPL skeleton for validating scanned IDs. @@ -16,6 +39,8 @@ def read_loop(): # Open the log file in append mode log_file = open("./access.log", "a") log_file.write(timestamped("=== Door security initiated ===\n")) + # Initialize the PWM pin + pwm_pin = setup_pwm() # Begin the loop while True: @@ -34,6 +59,7 @@ def read_loop(): log_file.write(s) # TODO: Play open tone # TODO: Run unlock procedure + unlock_door(pwm_pin) if __name__ == "__main__": read_loop()