/*
 * 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
//
#ifdef __AVR_ARCH_X__
#error "this test application is meant for AVR platform only"
#endif


//
// For convenience
//
using TC1 = eCxx::TimerCounter1;


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

    // Initialize UART
    App::uart().begin(9600);

    // Initialize Timer1
    TC1::start(TC1::ClockPrescaler::_1024); // ~15625 Hz (at 16 MHz F_CPU)

    TC1::setPeriod(15625U * 2U); // ~0.5 Hz (at 16 MHz F_CPU)
    TC1::attachPeriodicInterrupt( +[]() {
        App::uart().writeStringln( PGM_STR("Timer1 periodic interrupt!\n") );
    } );

    TC1::setCompareMatchValue( TC1::getPeriod() - 15625U / 4U ); // Trigger ~0.25 seconds before the periodic interrupt
    TC1::attachCompareMatchInterrupt( +[]() {
        App::uart().writeStringln( PGM_STR("Timer1 compare-match interrupt!") );
    } );
}


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