dreader-hal/examples/simple_display.py
2025-11-10 18:06:11 +01:00

119 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""
Simple Display Example.
Demonstrates basic usage of the EReaderDisplayHAL:
- Initialize the HAL
- Display an image
- Handle touch events
- Cleanup
This example uses a virtual display (Tkinter) for testing without hardware.
To use real hardware, set virtual_display=False.
"""
import asyncio
import sys
import os
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../src'))
from PIL import Image, ImageDraw, ImageFont
from dreader_hal import EReaderDisplayHAL, GestureType
async def main():
"""Main example function."""
# Create HAL with virtual display for testing
# For real hardware, set virtual_display=False
hal = EReaderDisplayHAL(
width=800,
height=1200,
virtual_display=True, # Use Tkinter window for testing
enable_orientation=False, # Disable orientation (no hardware)
enable_rtc=False, # Disable RTC (no hardware)
enable_power_monitor=False, # Disable power monitor (no hardware)
)
print("Initializing HAL...")
await hal.initialize()
print("HAL initialized!")
# Create a test image
image = Image.new('RGB', (800, 1200), color=(255, 255, 255))
draw = ImageDraw.Draw(image)
# Draw some text
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 48)
except:
font = ImageFont.load_default()
draw.text((50, 100), "DReader HAL Demo", fill=(0, 0, 0), font=font)
draw.text((50, 200), "Touch anywhere to test", fill=(0, 0, 0), font=font)
# Draw gesture instructions
try:
small_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 32)
except:
small_font = font
draw.text((50, 400), "Gestures:", fill=(0, 0, 0), font=small_font)
draw.text((50, 450), "• Tap: Show coordinates", fill=(0, 0, 0), font=small_font)
draw.text((50, 500), "• Swipe Left: Next page", fill=(0, 0, 0), font=small_font)
draw.text((50, 550), "• Swipe Right: Previous page", fill=(0, 0, 0), font=small_font)
draw.text((50, 600), "• Swipe Up: Open menu", fill=(0, 0, 0), font=small_font)
draw.text((50, 650), "• Long Press: Exit", fill=(128, 128, 128), font=small_font)
print("Displaying image...")
await hal.show_image(image)
print("Image displayed!")
# Event loop - handle touch events
print("\nWaiting for touch events...")
print("(Long press to exit)\n")
running = True
while running:
event = await hal.get_touch_event()
if event:
print(f"Touch event: {event.gesture.value} at ({event.x}, {event.y})")
# Handle different gestures
if event.gesture == GestureType.TAP:
print(f" → Tap detected at ({event.x}, {event.y})")
elif event.gesture == GestureType.SWIPE_LEFT:
print(" → Swipe left - next page")
elif event.gesture == GestureType.SWIPE_RIGHT:
print(" → Swipe right - previous page")
elif event.gesture == GestureType.SWIPE_UP:
print(" → Swipe up - open menu")
elif event.gesture == GestureType.SWIPE_DOWN:
print(" → Swipe down - open settings")
elif event.gesture == GestureType.LONG_PRESS:
print(" → Long press - exiting...")
running = False
# Small delay to avoid busy loop
await asyncio.sleep(0.01)
# Cleanup
print("\nCleaning up...")
await hal.cleanup()
print("Done!")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nInterrupted by user")