45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Example of using PyBMA400 to detect orientation changes.
|
|
This shows how to use the watcher module to detect flips between
|
|
landscape and portrait orientations.
|
|
"""
|
|
import asyncio
|
|
from pybma400 import BMA400, detect_orientation_flip, is_landscape
|
|
|
|
async def main():
|
|
try:
|
|
# Initialize the sensor
|
|
sensor = BMA400() # Default I2C bus=1, address=0x14
|
|
print("BMA400 sensor initialized successfully!")
|
|
|
|
# Start with the current orientation state
|
|
current_state = is_landscape(sensor.acceleration)
|
|
print(f"Initial orientation: {'Landscape' if current_state else 'Portrait'}")
|
|
|
|
# Monitor for orientation changes
|
|
while True:
|
|
print("Monitoring for orientation changes...")
|
|
|
|
# Call the detect_orientation_flip function with our sensor
|
|
# We'll use is_landscape as our evaluation function
|
|
new_state = await detect_orientation_flip(
|
|
current_state=current_state,
|
|
eval_func=is_landscape,
|
|
flip_delay=0.5, # Shorter delay for demonstration purposes
|
|
sensor=sensor
|
|
)
|
|
|
|
# Update our state
|
|
if new_state != current_state:
|
|
current_state = new_state
|
|
print(f"Orientation changed to: {'Landscape' if current_state else 'Portrait'}")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\nMonitoring stopped by user")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|