Advertisement
Guest User

lightscript

a guest
Feb 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. class MotionTracker:
  3.  
  4. def __init__(self):
  5. self.cycle_length = 20
  6. self.REM_threshold = 5
  7. self.cycle_threshold = 4
  8. self.current_cycle = []
  9. self.current_type = True #True for awake, False for asleep
  10. self.cycles_left = self.cycle_threshold
  11.  
  12. def update(self, num):
  13. if self.cycles_left == 0:
  14. return
  15.  
  16. self.current_cycle.append(num)
  17.  
  18. if len(self.current_cycle) >= self.cycle_length:
  19. sum_motion = 0
  20.  
  21. for i in self.current_cycle:
  22. sum_motion = sum_motion + i
  23.  
  24. self.current_cycle = []
  25.  
  26. if sum_motion <= self.REM_threshold:
  27. if self.current_type:
  28. self.current_type = False
  29.  
  30. else:
  31. if not self.current_type:
  32. self.current_type = True
  33. self.cycles_left = self.cycles_left - 1
  34.  
  35.  
  36. def main():
  37. pI = PressureInput()
  38. mTracker = MotionTracker()
  39.  
  40. while True:
  41. # motion = [0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,
  42. # 1,0,0,1,0,1,1,0,1,0,1,0,1,1,0,1,0,1,1,1,
  43. # 1,1,0,1,1,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,
  44. # 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
  45. # 1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,
  46. # 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  47. # 0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,
  48. # 1,1,0,1,1,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,
  49. # 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  50. # 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  51. # 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  52. # 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,]
  53. pI.readSensor()
  54. motion = pI.movedArr
  55. # for i in range(len(motion)):
  56. mTracker.update(motion[i])
  57. # print(i)
  58.  
  59. if mTracker.cycles_left == 0:
  60. #turn on the light
  61. GPIO.setmode(GPIO.BCM)
  62. GPIO.setwarnings(False)
  63. GPIO.setup(18, GPIO.OUT)
  64. print "light on"
  65. GPIO.output(18, GPIO.HIGH)
  66. time.sleep(1)
  67. print "light off"
  68. GPIO.output(18, GPIO.LOW)
  69.  
  70. if __name__ == "__main__":
  71. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement