/*
 * 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 eCxx::Logic;


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

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

    //
    // ===== Interrupt test using CCL =====
    //
    auto& logic = App::logic();

    // ----- Create a 1-input inverter with filter and edge detector using LUT2  -----
    // Inp Out
    // 210
    // 000 1
    // 001 0 (disabled)
    // 010 0 (disabled)
    // 011 0 (disabled)
    // 100 0
    // 101 0 (disabled)
    // 110 0 (disabled)
    // 111 0 (disabled)
    logic.lut[2].enabled      = true;
    logic.lut[2].input[0]     = Logic::Input::Disabled;
    logic.lut[2].input[1]     = Logic::Input::Disabled;
    logic.lut[2].input[2]     = Logic::Input::PinPullUp;
    logic.lut[2].output       = Logic::Output::Disabled;
    logic.lut[2].clockSource  = Logic::ClockSource::Internal1K;
    logic.lut[2].filter       = Logic::Filter::Filter;
    logic.lut[2].edgeDetector = Logic::EdgeDetector::Enabled;
    logic.lut[2].truthTable   = 0b00000001;

    // ----- Attach interrupt to LUT2 -----
    logic.attachInterrupt(
        2,
        +[]() {
            App::uart().writeString     ( PGM_STR("LUT2 interrupt at ") );
            App::uart().writeDecStringln( App::micros()                 );
        },
        App::InterruptMode::Rising
    );

    // ----- Start the CCL -----
    logic.begin();
}


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