119 lines
3.5 KiB
Python
Executable File
119 lines
3.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Simple Accelerometer Demo - Using Unified Event API
|
|
|
|
This is a simplified version of the accelerometer demo that uses
|
|
the HAL's get_event() convenience method to poll both touch and
|
|
accelerometer in a single call.
|
|
|
|
Usage:
|
|
python examples/demo_accelerometer_simple.py <epub_file>
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path for imports
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from dreader.hal_hardware import HardwareDisplayHAL
|
|
from dreader.application import EbookReader
|
|
from dreader.gesture import GestureType
|
|
|
|
|
|
async def main():
|
|
"""Simple demo using unified event API"""
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python demo_accelerometer_simple.py <epub_file>")
|
|
sys.exit(1)
|
|
|
|
epub_path = sys.argv[1]
|
|
|
|
# Create HAL with accelerometer enabled
|
|
print("Initializing hardware...")
|
|
hal = HardwareDisplayHAL(
|
|
width=1872,
|
|
height=1404,
|
|
enable_orientation=True
|
|
)
|
|
|
|
await hal.initialize()
|
|
|
|
# Load accelerometer calibration (optional)
|
|
if hal.load_accelerometer_calibration("accelerometer_config.json"):
|
|
print("✓ Accelerometer calibrated - tilt gestures enabled")
|
|
else:
|
|
print("✗ No accelerometer calibration - touch only")
|
|
|
|
# Create reader and load book
|
|
print(f"\nLoading: {epub_path}")
|
|
reader = EbookReader(page_size=(hal.width, hal.height), margin=60)
|
|
|
|
if not reader.load_epub(epub_path):
|
|
print(f"ERROR: Failed to load {epub_path}")
|
|
await hal.cleanup()
|
|
return
|
|
|
|
print(f"Loaded: {reader.book_title}")
|
|
|
|
# Display first page
|
|
img = reader.get_current_page()
|
|
await hal.show_image(img)
|
|
|
|
print("\nControls:")
|
|
print(" Swipe LEFT or Tilt FORWARD → Next page")
|
|
print(" Swipe RIGHT or Tilt BACKWARD → Previous page")
|
|
print(" Long press → Exit\n")
|
|
|
|
# Main event loop - simple unified API!
|
|
running = True
|
|
while running:
|
|
# Get event from any source (touch or accelerometer)
|
|
event = await hal.get_event()
|
|
|
|
if event:
|
|
print(f"Gesture: {event.gesture.value}")
|
|
|
|
# Page navigation
|
|
if event.gesture in [GestureType.SWIPE_LEFT, GestureType.TILT_FORWARD]:
|
|
img = reader.next_page()
|
|
if img:
|
|
progress = reader.get_reading_progress()
|
|
print(f" → Page {progress['current']}/{progress['total']} ({progress['percent']:.1f}%)")
|
|
await hal.show_image(img)
|
|
else:
|
|
print(" → End of book")
|
|
|
|
elif event.gesture in [GestureType.SWIPE_RIGHT, GestureType.TILT_BACKWARD]:
|
|
img = reader.previous_page()
|
|
if img:
|
|
progress = reader.get_reading_progress()
|
|
print(f" ← Page {progress['current']}/{progress['total']} ({progress['percent']:.1f}%)")
|
|
await hal.show_image(img)
|
|
else:
|
|
print(" ← Start of book")
|
|
|
|
# Exit
|
|
elif event.gesture == GestureType.LONG_PRESS:
|
|
print("\nExiting...")
|
|
running = False
|
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
await hal.cleanup()
|
|
print("Done!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
asyncio.run(main())
|
|
except KeyboardInterrupt:
|
|
print("\nInterrupted")
|
|
sys.exit(0)
|
|
except Exception as e:
|
|
print(f"\nError: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|