86 lines
2.3 KiB
Python
86 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Battery Monitor Example.
|
|
|
|
Demonstrates power monitoring with the INA219:
|
|
- Read voltage, current, and power
|
|
- Estimate battery percentage
|
|
- Calculate time remaining
|
|
- Detect charging status
|
|
|
|
This example requires actual INA219 hardware connected via I2C.
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
import os
|
|
|
|
# Add src to path
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../src'))
|
|
|
|
from dreader_hal import EReaderDisplayHAL
|
|
|
|
|
|
async def main():
|
|
"""Main example function."""
|
|
|
|
# Create HAL with power monitoring enabled
|
|
hal = EReaderDisplayHAL(
|
|
width=800,
|
|
height=1200,
|
|
virtual_display=True, # Virtual display for testing
|
|
enable_orientation=False,
|
|
enable_rtc=False,
|
|
enable_power_monitor=True, # Enable power monitoring
|
|
shunt_ohms=0.1, # Shunt resistor value
|
|
battery_capacity_mah=3000, # Battery capacity
|
|
)
|
|
|
|
print("Initializing HAL with power monitoring...")
|
|
try:
|
|
await hal.initialize()
|
|
print("HAL initialized!")
|
|
except RuntimeError as e:
|
|
print(f"Error: {e}")
|
|
print("\nNote: This example requires INA219 hardware on I2C bus.")
|
|
print("If testing without hardware, power monitor will be disabled.")
|
|
return
|
|
|
|
# Monitor battery stats
|
|
print("\nMonitoring battery stats (Ctrl+C to exit)...\n")
|
|
|
|
try:
|
|
while True:
|
|
# Get power statistics
|
|
stats = await hal.get_power_stats()
|
|
|
|
# Display stats
|
|
print(f"\r"
|
|
f"Voltage: {stats.voltage:.2f}V | "
|
|
f"Current: {stats.current:.1f}mA | "
|
|
f"Power: {stats.power:.1f}mW | "
|
|
f"Battery: {stats.battery_percent:.0f}% | "
|
|
f"Charging: {'Yes' if stats.is_charging else 'No'} | "
|
|
f"Time remaining: {stats.time_remaining or 'N/A'} min",
|
|
end="", flush=True)
|
|
|
|
# Check for low battery
|
|
if await hal.is_low_battery(threshold=20.0):
|
|
print("\n⚠️ LOW BATTERY WARNING!")
|
|
|
|
# Wait 1 second
|
|
await asyncio.sleep(1.0)
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n\nStopped by user")
|
|
|
|
finally:
|
|
# Cleanup
|
|
print("Cleaning up...")
|
|
await hal.cleanup()
|
|
print("Done!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|