Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import board
- import digitalio
- import time
- import datetime
- import os
- os.environ["BLINKA_FT232H"] = "1"
- class MagnetLock():
- def __init__(self,led_pin,mag_pin) -> None:
- self.led = digitalio.DigitalInOut(led_pin)
- self.mag_lock = digitalio.DigitalInOut(mag_pin)
- self.led.direction = digitalio.Direction.OUTPUT
- self.mag_lock.direction = digitalio.Direction.OUTPUT
- def unlock(self):
- self.led.value=False
- self.mag_lock.value=False
- def lock(self):
- self.led.value=True
- self.mag_lock.value=True
- def __repr__(self):
- return self.mag_lock.value
- class KeyHolder():
- def __init__(self, locks) -> None:
- self.locks = locks
- def timer_lock(self,mins):
- try:
- for mag_lock in self.locks:
- mag_lock.lock()
- print(f"timer started for {mins} minutes")
- secs = mins*60
- start_time = time.time()
- end_time = start_time+secs
- while time.time() < end_time:
- tl = datetime.timedelta(seconds=round(end_time-time.time(),2))
- print(f"seconds remaining: {tl}", end="\r", flush=True)
- print("done, unlocking ")
- for mag_lock in self.locks:
- mag_lock.unlock()
- except Exception as e:
- print(f"exception: {e}")
- finally:
- for mag_lock in self.locks:
- mag_lock.unlock()
- if __name__ == "__main__":
- a = MagnetLock(board.C4, board.D4)
- b = MagnetLock(board.C5, board.D5)
- kh = KeyHolder([a,b])
Advertisement
Add Comment
Please, Sign In to add comment