Motion Sensor¶
- class buildhat.MotionSensor(port)¶
Motion sensor
- Parameters:
port – Port of device
- Raises:
DeviceError – Occurs if there is no motion sensor attached to port
- callback(func)¶
Set callback function
- Parameters:
func – Callback function
- property connected¶
Whether device is connected or not
- Returns:
Connection status
- static desc_for_id(typeid)¶
Translate integer type id to something more descriptive than the device name
- Parameters:
typeid – Type of device
- Returns:
Description of device
- property description¶
Device on port info
- Returns:
Device description
- deselect()¶
Unselect data from mode
- get()¶
Extract information from device
- Returns:
Data from device
- Raises:
DeviceError – Occurs if device not in valid mode
- get_distance()¶
Return the distance from motion sensor
- Returns:
Distance from motion sensor
- Return type:
int
- get_movement_count()¶
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
- Returns:
Count of objects detected
- Return type:
int
- property interval¶
Interval between data points in milliseconds
- Getter:
Gets interval
- Setter:
Sets interval
- Returns:
Device interval
- Return type:
int
- isconnected()¶
Whether it is connected or not
- Raises:
DeviceError – Occurs if device no longer the same
- mode(modev)¶
Set combimode or simple mode
- Parameters:
modev – List of tuples for a combimode, or integer for simple mode
- property name¶
Determine name of device on port
- Returns:
Device name
- static name_for_id(typeid)¶
Translate integer type id to device name (python class)
- Parameters:
typeid – Type of device
- Returns:
Name of device
- off()¶
Turn off sensor
- on()¶
Turn on sensor
- reverse()¶
Reverse polarity
- select()¶
Request data from mode
- Raises:
DeviceError – Occurs if device not in valid mode
- set_default_data_mode(mode)¶
Set the mode most often queried from this device.
This significantly improves performance when repeatedly accessing data
- Parameters:
mode – 0 for distance (default), 1 for movement count
- property typeid¶
Type ID of device
- Returns:
Type ID
- property typeidcur¶
Type ID currently present
- Returns:
Type ID
Example¶
"""Example using motion sensor"""
from time import sleep
from buildhat import MotionSensor
motion = MotionSensor('A')
for _ in range(50):
print(motion.get_distance())
sleep(0.1)