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


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

    // TWISlave Rx buffer
    static char          rxBuff[16];
    static eCxx::uint8_t rxLen = 0;

    // Initialize TWI in slave mode
    App::twiSlave().begin(

        // Slave address
        twiSlaveAddress,

        // User Rx callback
        [](eCxx::uint8_t len) {
            // Receive from master - save the received data to buffer
            eCxx::ignoreUnused(len);
            rxLen = 0;
            while( App::twiSlave().canRead() ) {
                eCxx::uint8_t g;
                App::twiSlave().read(g);
                rxBuff[rxLen++] = g;
            }
            // Send the number of received bytes to UART
            App::uart().writeDecStringln(len);
        },

        // User Tx callback
        []() {
            // Send to master
            App::twiSlave().writeString( PGM_STR("Hello ") );
            App::twiSlave().write(rxBuff, rxLen);
            App::twiSlave().write('!');
            rxLen = 0;
        }

    );
}
