Added initial PWM code

This commit is contained in:
Jaculabilis 2018-01-10 15:19:31 -06:00
parent 0b2b39bb88
commit a3a73ea1e8
1 changed files with 26 additions and 0 deletions

View File

@ -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()