#!/usr/bin/env python3 """Alarm example for PyPCF8523 RTC driver. This example demonstrates how to use the alarm functionality: - Setting an alarm - Checking alarm status - Clearing an alarm Hardware setup: - Connect PCF8523 to Raspberry Pi I2C bus 1 - Optionally connect INT pin to a GPIO for interrupt handling """ import time from pypcf8523 import PCF8523 def main(): print("PCF8523 Alarm Example") print("=" * 50) # Initialize the RTC rtc = PCF8523(i2c_bus=1) # Get current time current = rtc.datetime print(f"Current time: {current.tm_hour:02d}:{current.tm_min:02d}:{current.tm_sec:02d}") # Set an alarm for 2 minutes from now alarm_minute = (current.tm_min + 2) % 60 alarm_hour = current.tm_hour if alarm_minute < current.tm_min: # Handle hour rollover alarm_hour = (alarm_hour + 1) % 24 print(f"Setting alarm for: {alarm_hour:02d}:{alarm_minute:02d}") rtc.set_alarm(minute=alarm_minute, hour=alarm_hour) # Enable the alarm interrupt rtc.alarm_interrupt = True print("Alarm interrupt enabled") # Clear any existing alarm status rtc.alarm_status = False print("\nWaiting for alarm... (Press Ctrl+C to stop)") print("-" * 50) try: while True: # Read current time current = rtc.datetime time_str = f"{current.tm_hour:02d}:{current.tm_min:02d}:{current.tm_sec:02d}" # Check if alarm triggered if rtc.alarm_status: print(f"\nšŸ”” ALARM! Triggered at {time_str}") # Clear the alarm rtc.alarm_status = False print("Alarm cleared") # Optionally disable the alarm # rtc.clear_alarm() # print("Alarm disabled") break else: print(f"Current time: {time_str} - Waiting for alarm...", end="\r") time.sleep(1.0) except KeyboardInterrupt: print("\n\nExample stopped by user") finally: # Clean up rtc.clear_alarm() rtc.close() print("Alarm cleared and RTC connection closed") if __name__ == "__main__": main()