Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions buildhat/wedo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,28 @@ class MotionSensor(Device):
:raises DeviceError: Occurs if there is no motion sensor attached to port
"""

default_mode = 0

def __init__(self, port):
"""
Initialise motion sensor

:param port: Port of device
"""
super().__init__(port)
self.mode(0)
self.mode(self.default_mode)

def set_default_data_mode(self, mode):
"""
Set the mode most often queried from this device.

This significantly improves performance when repeatedly accessing data

:param mode: 0 for distance (default), 1 for movement count
"""
if mode == 1 or mode == 0:
self.default_mode = mode
self.mode(mode)

def get_distance(self):
"""
Expand All @@ -52,4 +66,26 @@ def get_distance(self):
:return: Distance from motion sensor
:rtype: int
"""
return self.get()[0]
return self._get_data_from_mode(0)

def get_movement_count(self):
"""
Return the movement counter

This is the count of how many times the sensor has detected an object
that moved within 4 blocks of the sensor since the sensor has been
plugged in or the BuildHAT reset

:return: Count of objects detected
:rtype: int
"""
return self._get_data_from_mode(1)

def _get_data_from_mode(self, mode):
if self.default_mode == mode:
return self.get()[0]
else:
self.mode(mode)
retval = self.get()[0]
self.mode(self.default_mode)
return retval