#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Leonardo La Rocca
"""

import melopero_RV_3028 as mp
import datetime
import gpiozero as gpio
from signal import pause


def main():
    # First initialize and create the rtc device
    rtc = mp.RV_3028()
    # Set the device to use the 24hour format (default) instead of the 12 hour format
    rtc.set_12h_format(False)

    # Then set the date and time.
    # retrieve the datetime from the library datetime
    current_datetime = datetime.datetime.now()

    # set the date and time for the device
    rtc.set_time(current_datetime.hour, current_datetime.minute, current_datetime.second)
    rtc.set_date(current_datetime.weekday(), current_datetime.day, current_datetime.month, current_datetime.year % 2000)

    # First disable other sources of interrupts
    rtc.enable_timer(enable=False, repeat=False, generate_interrupt=False)
    rtc.enable_periodic_time_update_interrupt(generate_interrupt=False)
    rtc.clear_interrupt_flags()

    # set the alarm to trigger 2 minutes from now
    minute_alarm = current_datetime.minute + 2 % 60
    rtc.set_minute_alarm(minute_alarm)
    rtc.enable_alarm(enable=True, generate_interrupt=True)
    print("Alarm set to trigger two minutes from now...")
    print("The alarm will trigger every hour at minute {}".format(minute_alarm))

    # interrupt routine
    def on_interrupt():
        print("Alarm: beep beep")
        print(rtc.get_time())
        print("Press CTRL + C to terminate program...")
        rtc.clear_interrupt_flags()

    # set the pin to listen to interrupts
    int_listener_pin = "GPIO4"
    interrupt = gpio.Button(int_listener_pin, pull_up=None, active_state=False)
    interrupt.when_pressed = on_interrupt

    pause()





if __name__ == "__main__":
    main()