/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#include <eCxx/eCxx.h>

#include "../AVR_Common.h"


//
// Check the MCU
//
#ifndef __AVR_ARCH_X__
#error "this test application is meant for AVRX platform only"
#endif


//
// For convenience
//
using RTCtr = eCxx::RealTimeCounter;


//
// The init function
//
void eCxx_user_init()
{
    // Initialize application
    initBasicAVRApp();

    // Initialize UART
    auto& u = App::uart();
    u.begin(9600);

    // Initialize real-time counter
    u.writeStringln( PGM_STR("\nInitializing RTC for wake-up event ...") );
    App::yield();

    RTCtr::setClock(RTCtr::ClockSource::Internal1K, RTCtr::ClockPrescaler::_1);

    RTCtr::setPeriodicInterruptCycle(RTCtr::PICycle::_16384);
    RTCtr::attachPeriodicInterrupt( +[]() {
        // Restore some configurations that have been changed by the power-down process
        App::restoreOnWakeUp();
        // Inform that the MCU has been waken-up
        App::uart().writeStringln( PGM_STR("WAKE-UP!\n") );
    } );

    RTCtr::start();

    // Power-down
    u.writeStringln( PGM_STR("Entering power-down mode (will wake-up in ~16 seconds) ...\n") );
    u.flush();
    App::yield();

    App::powerDown();
}


//
// The task function
//
void eCxx_user_task()
{
    // Process event
    eCxx::Application::processEvent();
}
