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

static constexpr eCxx::uint8_t OWI   = eCxx::AVR::A4;

#ifndef __AVR_ARCH_X__
static constexpr eCxx::uint8_t DATA  = 8;
static constexpr eCxx::uint8_t SHIFT = 7;
static constexpr eCxx::uint8_t LATCH = 4;
#endif


//
// Include common source code for OWIMaster test application
//
#include "../CommonCode/OWIMaster_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 OWIMaster with Dallas temperature sensor
    initOWIMasterWithDallasTemperatureSensor();
}


//
// The task function
//
void eCxx_user_task()
{
    // Process event and delay
    static eCxx::uint32_t prvMillis = 0;
           eCxx::uint32_t curMillis = App::processEvent();
    if(curMillis - prvMillis < 3000) return;
    else                             prvMillis = curMillis;

    // Read Dallas temperature sensor and send the result to UART
    readDallasTemperatureSensor();
}
