/*
 * 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"


//
// Pin numbers
//
#ifndef __AVR_ARCH_X__

// ATmega328   : eCxx EINT0
// ATmega32U4  : eCxx EINT1
// ATmega2560  : eCxx EINT4
// AT90USB1286 : eCxx EINT4
static constexpr eCxx::uint8_t RECV  = 2;

static constexpr eCxx::uint8_t DATA  = 8;
static constexpr eCxx::uint8_t SHIFT = 7;
static constexpr eCxx::uint8_t LATCH = 4;

#else

// ATmega4808 : eCxx EINT0
static constexpr eCxx::uint8_t RECV  = 4;

#endif


//
// Include common source code for StreamSampler test application
//
#include "../CommonCode/StreamSampler_Common.h"


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

#ifndef __AVR_ARCH_X__
    // Turn off all the 7-segments that happen to also be on the development board
    App::setPinMode(DATA,  App::PinMode::Output);
    App::setPinMode(SHIFT, App::PinMode::Output);
    App::setPinMode(LATCH, App::PinMode::Output);

    App::shiftOut( DATA, SHIFT, eCxx::ActiveClockEdge::Rising, LATCH, eCxx::ActiveState::High, eCxx::BitOrder::MSBFirst, eCxx::makeArray<eCxx::uint8_t, 0xFF, 0x08>() );
#endif

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

    // Initialize and run the data-stream sampler
    initAndRunStreamSampler();
}


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