Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- class MotionTracker:
- def __init__(self):
- self.cycle_length = 20
- self.REM_threshold = 5
- self.cycle_threshold = 4
- self.current_cycle = []
- self.current_type = True #True for awake, False for asleep
- self.cycles_left = self.cycle_threshold
- def update(self, num):
- if self.cycles_left == 0:
- return
- self.current_cycle.append(num)
- if len(self.current_cycle) >= self.cycle_length:
- sum_motion = 0
- for i in self.current_cycle:
- sum_motion = sum_motion + i
- self.current_cycle = []
- if sum_motion <= self.REM_threshold:
- if self.current_type:
- self.current_type = False
- else:
- if not self.current_type:
- self.current_type = True
- self.cycles_left = self.cycles_left - 1
- def main():
- pI = PressureInput()
- mTracker = MotionTracker()
- while True:
- # motion = [0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,
- # 1,0,0,1,0,1,1,0,1,0,1,0,1,1,0,1,0,1,1,1,
- # 1,1,0,1,1,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,
- # 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
- # 1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,
- # 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
- # 0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,
- # 1,1,0,1,1,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,
- # 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- # 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- # 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- # 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,]
- pI.readSensor()
- motion = pI.movedArr
- # for i in range(len(motion)):
- mTracker.update(motion[i])
- # print(i)
- if mTracker.cycles_left == 0:
- #turn on the light
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(18, GPIO.OUT)
- print "light on"
- GPIO.output(18, GPIO.HIGH)
- time.sleep(1)
- print "light off"
- GPIO.output(18, GPIO.LOW)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement