/*
 * 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 "NodeMCU_Common.h"


//
// Pin numbers
//
#if 0
static constexpr eCxx::uint8_t OWI = eCxx::NodeMCU::D2; // GPIO4 (SDA)
#else
static constexpr eCxx::uint8_t OWI = eCxx::NodeMCU::D1; // GPIO5 (SCL)
#endif


//
// Include common source code for OWIMaster test application
//
#include "../CommonCode/OWIMaster_Common.h"


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

    // There is no need to enable the 74LVC541 buffers on the eCxx NodeMCU development board because
    // the OWI pin is not buffered but routed through a bidirectional logic level translator and the
    // UART is only used for communication with the host PC

    // 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();
}
