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


//
// Instantiate the one-wire software UART
//
static eCxx::OneWireSoftUART owsu;


//
// Helper functions
//
static void initUARTAndOWSU()
{
    // Initialize UART
    App::uart().begin(9600);

    // Initialize the one-wire software UART
#if 1
    owsu.begin(OWSU_PIN,  50000, eCxx::OneWireSoftUART::DataFormat::_8E2);
#else
    owsu.begin(OWSU_PIN, 200000, eCxx::OneWireSoftUART::DataFormat::_8N1);
#endif
}


static void eCxx_user_task_OWSUMaster_TxRx()
{
    // Process event and delay
    static eCxx::uint32_t prvMillis = 0;
           eCxx::uint32_t curMillis = App::processEvent();
    if(curMillis - prvMillis < 250) return;
    else                            prvMillis = curMillis;

#if 1

    // Send to slave
    static char s = 'z';

    if(s == 'z') s = 'a';
    else         ++s;

    owsu.immWrite(s);

    // Receive from slave
    eCxx::uint8_t r;

    if( owsu.immRead(r, 100) ) { // Timeout 100 mS (using 0 means no timeout!)
        // Send it to the hardware UART
        App::uart().write(r   );
        App::uart().write('\n');
    }

#else

    // Send to slave
    eCxx::uint8_t a[6] = { 'a', 'b', 'c', 'd', 'e', '\n' };

    owsu.immWrite(a, 6);

#endif
}
