lautalarm_multiprocessing(with_bug).py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import time
  2. import multiprocessing
  3. import Adafruit_BBIO.GPIO as GPIO
  4. import Adafruit_BBIO.PWM as PWM
  5. class Lautalarm:
  6. def __init__(self, laut_pin, rot_pin, duration=60, refresh_intvl=1):
  7. self.triggered = False
  8. self.activited = False
  9. self.duration = duration
  10. self.laut_pin = laut_pin
  11. self.rot_pin = rot_pin
  12. self.refresh_intvl = refresh_intvl
  13. def __del__(self):
  14. print("Lautalarm deleted.")
  15. def run(self):
  16. while True:
  17. if not self.triggered:
  18. print("...")
  19. else:
  20. # start laut, start rot, start timer
  21. if not self.activited:
  22. laut = LAUT(pin=self.laut_pin, lautalarm=self)
  23. rot = ROT(pin=self.rot_pin, lautalarm=self)
  24. laut.start()
  25. rot.start()
  26. ttimer = Termtimer(timeout=self.duration, lautalarm=self)
  27. ttimer.start()
  28. self.activited = True
  29. print("+++")
  30. time.sleep(self.refresh_intvl)
  31. class IR:
  32. def __init__(self, pin, lautalarm):
  33. self.pin = pin
  34. self.lautalarm = lautalarm
  35. GPIO.setup(self.pin, GPIO.IN)
  36. GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_ir)
  37. def __del__(self):
  38. print("IR {} deleted.".format(self.pin))
  39. GPIO.remove_event_detect(self.pin)
  40. def cb_ir(self, pin):
  41. print("cb_ir called")
  42. self.lautalarm.triggered = True
  43. class PIR:
  44. def __init__(self, pin, lautalarm):
  45. self.pin = pin
  46. self.lautalarm = lautalarm
  47. GPIO.setup(self.pin, GPIO.IN)
  48. GPIO.add_event_detect(self.pin, GPIO.RISING, callback=self.cb_pir)
  49. def __del__(self):
  50. print("PIR {} deleted.".format(self.pin))
  51. GPIO.remove_event_detect(self.pin)
  52. def cb_pir(self, pin):
  53. print("cb_pir called")
  54. self.lautalarm.triggered = True
  55. class Termtimer(multiprocessing.Process):
  56. def __init__(self, timeout, lautalarm):
  57. super().__init__()
  58. self.timeout = timeout
  59. self.lautalarm = lautalarm
  60. def __del__(self):
  61. print("Termtimer deleted.")
  62. def run(self):
  63. time.sleep(self.timeout)
  64. self.lautalarm.triggered = False
  65. self.lautalarm.activited = False
  66. class ROT(multiprocessing.Process):
  67. def __init__(self, pin, lautalarm, blink_freq=2):
  68. super().__init__()
  69. self.pin = pin
  70. self.blink_freq = blink_freq
  71. self.blink_intvl = 1 / blink_freq
  72. GPIO.setup(self.pin, GPIO.OUT)
  73. self.lautalarm = lautalarm
  74. def __del__(self):
  75. print("ROT {} deleted.".format(self.pin))
  76. def rot_start(self):
  77. GPIO.output(self.pin, GPIO.HIGH)
  78. def rot_stop(self):
  79. GPIO.output(self.pin, GPIO.LOW)
  80. def run(self):
  81. while True:
  82. if not self.lautalarm.activited:
  83. return
  84. self.rot_start()
  85. time.sleep(self.blink_intvl)
  86. self.rot_stop()
  87. class LAUT(multiprocessing.Process):
  88. def __init__(self, pin, lautalarm, laut_freq=1, pwm_perc=50, pwm_freq=10000):
  89. super().__init__()
  90. self.pin = pin
  91. self.laut_freq = laut_freq
  92. self.laut_intvl = 1 / laut_freq
  93. self.pwm_perc = pwm_perc
  94. self.pwm_freq = pwm_freq
  95. GPIO.setup(self.pin, GPIO.OUT)
  96. self.lautalarm = lautalarm
  97. def __del__(self):
  98. print("LAUT {} deleted.".format(self.pin))
  99. def laut_start(self):
  100. PWM.start(self.pin, self.pwm_perc, self.pwm_freq)
  101. def laut_stop(self):
  102. PWM.stop(self.pin)
  103. def run(self):
  104. while True:
  105. if not self.lautalarm.activited:
  106. return
  107. self.laut_start()
  108. time.sleep(self.laut_intvl)
  109. self.laut_stop()
  110. if __name__ == "__main__":
  111. myalarm = Lautalarm(laut_pin="P8_13", rot_pin="P9_27", duration=10)
  112. ir = IR("P9_12", myalarm)
  113. pir = PIR("P9_41", myalarm)
  114. # mag = MAG("P8_7", myalarm)
  115. myalarm.run()