First commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Basic example of using the PyBMA400 library to read
|
||||
acceleration and temperature data from the BMA400 sensor.
|
||||
"""
|
||||
import time
|
||||
from pybma400 import BMA400
|
||||
|
||||
def main():
|
||||
# Initialize the sensor
|
||||
try:
|
||||
sensor = BMA400() # Default I2C bus=1, address=0x14
|
||||
print("BMA400 sensor initialized successfully!")
|
||||
|
||||
# Configure the sensor
|
||||
sensor.power_mode = BMA400.NORMAL_MODE
|
||||
sensor.output_data_rate = BMA400.ACCEL_100HZ
|
||||
sensor.acc_range = BMA400.ACC_RANGE_4 # ±4g range
|
||||
|
||||
print(f"Power mode: {sensor.power_mode}")
|
||||
print(f"Output data rate: {sensor.output_data_rate}")
|
||||
print(f"Acceleration range: {sensor.acc_range}")
|
||||
print(f"Filter bandwidth: {sensor.filter_bandwidth}")
|
||||
print(f"Oversampling rate: {sensor.oversampling_rate}")
|
||||
|
||||
# Read and display sensor data
|
||||
for i in range(10):
|
||||
# Read acceleration
|
||||
x, y, z = sensor.acceleration
|
||||
print(f"Acceleration: X={x:.2f}, Y={y:.2f}, Z={z:.2f} m/s²")
|
||||
|
||||
# Read temperature
|
||||
temp = sensor.temperature
|
||||
print(f"Temperature: {temp:.1f}°C")
|
||||
|
||||
time.sleep(0.5)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/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())
|
||||
Reference in New Issue
Block a user