/*
 * 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 initOWSU()
{
    // 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_OWSUSlave_TxRx()
{
    // Process event
    App::processEvent();

#if 1

    // Receive from master
    eCxx::uint8_t c;

    if( owsu.immRead(c, 100) ) { // Timeout 100 mS (using 0 means no timeout!)
        // Convert the case
             if(c >= 'a' && c <= 'z') c = c - 'a' + 'A';
        else if(c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
        // Delay for a while
        App::delayMS(10);
        // Send it back to the master
        owsu.immWrite(c);
    }

#else

    // Receive from master
    eCxx::uint8_t a[5];

    if( owsu.immRead(a, 5, 100) ) { // Timeout 100 mS (using 0 means no timeout!)
        // Delay for a while
        App::delayMS(10);
        // Send it back to the master
        owsu.immWrite(a, 5);
        owsu.immWrite('\n');
    }

#endif
}
