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


#include <eCxx/eCxx_LEDDriver.h>

#include "AVR_Common.h"


//
// Pin numbers
//
#ifndef __AVR_ARCH_X__

// ATmega328P : these eCxx pin numbers are the same as the pin numbers on the
//              silkscreen of Arduino and compatible boards
static constexpr eCxx::uint8_t BCM_R =  9;
static constexpr eCxx::uint8_t BCM_G = 10;
static constexpr eCxx::uint8_t BCM_B = 11;

#else

// ATmega4808
static constexpr eCxx::uint8_t BCM_R = 14; // A0 on the silkscreen of Thinary ATmega4808 board
static constexpr eCxx::uint8_t BCM_G = 15; // A1 on the silkscreen of Thinary ATmega4808 board
static constexpr eCxx::uint8_t BCM_B = 16; // A2 on the silkscreen of Thinary ATmega4808 board

#endif


//
// Include common source code for BCMOutput test application
//
#include "../CommonCode/BCMOutput_Common.h"


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

    // Initialize BCM pins
    eCxx_user_init_initializeBCMPin();

    // Initialize periodic interrupt for BCM
#ifdef __AVR_ARCH_X__

    using TCB = eCxx::TimerCounterB;

    auto& tcb = *App::tcb1();

    tcb.setClock(TCB::ClockSource::Peripheral1);
    tcb.setMode(TCB::Mode::PeriodicInterrupt);
    tcb.setPeriod(F_CPU / 25000U);
    tcb.setPMOutput(TCB::PWMOutput::Disabled);
    tcb.attachInterrupt( +[]() {
        bcmRGB.update();
    } );
    tcb.start();

#else

    using TC1 = eCxx::TimerCounter1;

    // Initialize Timer1
    TC1::start(TC1::ClockPrescaler::_1);

    TC1::setPeriod(F_CPU / 25000U);
    TC1::attachPeriodicInterrupt( +[]() {
        bcmRGB.update();
    } );

#endif
}


//
// The task function
//
void eCxx_user_task()
{
    // Process event and update the BCM animation
    eCxx_user_task_updateBCMAnimation();
}
