Guest User

Untitled

a guest
Jun 19th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import board
  2. import digitalio
  3. import time
  4. import datetime
  5. import os
  6. os.environ["BLINKA_FT232H"] = "1"
  7.  
  8. class MagnetLock():
  9.  
  10. def __init__(self,led_pin,mag_pin) -> None:
  11. self.led = digitalio.DigitalInOut(led_pin)
  12. self.mag_lock = digitalio.DigitalInOut(mag_pin)
  13. self.led.direction = digitalio.Direction.OUTPUT
  14. self.mag_lock.direction = digitalio.Direction.OUTPUT
  15.  
  16. def unlock(self):
  17. self.led.value=False
  18. self.mag_lock.value=False
  19.  
  20. def lock(self):
  21. self.led.value=True
  22. self.mag_lock.value=True
  23.  
  24. def __repr__(self):
  25. return self.mag_lock.value
  26.  
  27. class KeyHolder():
  28. def __init__(self, locks) -> None:
  29. self.locks = locks
  30.  
  31. def timer_lock(self,mins):
  32. try:
  33. for mag_lock in self.locks:
  34. mag_lock.lock()
  35. print(f"timer started for {mins} minutes")
  36. secs = mins*60
  37. start_time = time.time()
  38. end_time = start_time+secs
  39. while time.time() < end_time:
  40. tl = datetime.timedelta(seconds=round(end_time-time.time(),2))
  41. print(f"seconds remaining: {tl}", end="\r", flush=True)
  42. print("done, unlocking ")
  43. for mag_lock in self.locks:
  44. mag_lock.unlock()
  45. except Exception as e:
  46. print(f"exception: {e}")
  47. finally:
  48. for mag_lock in self.locks:
  49. mag_lock.unlock()
  50.  
  51. if __name__ == "__main__":
  52. a = MagnetLock(board.C4, board.D4)
  53. b = MagnetLock(board.C5, board.D5)
  54. kh = KeyHolder([a,b])
  55.  
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment