class InactivitySensor(PeriodicSensor): def __init__(self, queue, period, wait_before_sleep=WAIT_BEFORE_SLEEP, time_to_sleep=TIME_TO_SLEEP): self._wait_before_sleep = wait_before_sleep self._time_to_sleep = time_to_sleep self._queue = queue self._py = Pytrack() message = "Wake up reason: " + str(self._py.get_wake_reason()) # display the reset reason code and the sleep remaining in seconds # possible values of wakeup reason are: # WAKE_REASON_ACCELEROMETER = 1 # WAKE_REASON_PUSH_BUTTON = 2 # WAKE_REASON_TIMER = 4 # WAKE_REASON_INT_PIN = 8 self.printout(message) time.sleep(0.5) # enable wakeup source from INT pin self._py.setup_int_pin_wake_up(False) self.value = 0 # enable activity and also inactivity interrupts, using the default callback handler self._py.setup_int_wake_up(True, True) PeriodicSensor.__init__(self, queue, period) def read(self): self.value = self._queue.get_timeout() if self.value > self._wait_before_sleep: self._sleep() def _sleep(self): self.printout('Sleeping') EventSensor._event_handler(self) #self._queue._client.publish(self._queue._topic, 'Sleeping') self._py.setup_sleep(self._time_to_sleep) self._py.go_to_sleep() pass
# enabling garbage collector gc.enable() # display the reset reason code and the sleep remaining in seconds # possible values of wakeup reason are: # WAKE_REASON_ACCELEROMETER = 1 # WAKE_REASON_PUSH_BUTTON = 2 # WAKE_REASON_TIMER = 4 # WAKE_REASON_INT_PIN = 8 print("Wakeup reason: " + str(py.get_wake_reason()) + "; Aproximate sleep remaining: " + str(py.get_sleep_remaining()) + " sec") time.sleep(0.5) # enable wakeup source from INT pin py.setup_int_pin_wake_up(False) # enable activity and also inactivity interrupts, using the default callback handler py.setup_int_wake_up(True, True) acc = LIS2HH12() # enable the activity/inactivity interrupts # set the accelereation threshold to 2000mG (2G) and the min duration to 200ms acc.enable_activity_interrupt(2000, 200) # check if we were awaken due to activity if acc.activity(): pycom.rgbled(0xFF0000) else: pycom.rgbled(0x00FF00) # timer wake-up Green time.sleep(2)