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


//
// Instantiate the WS2812B driver
//
static eCxx::WS2812BDriver ws2812b;


//
// Animation-related variables
//

// Create a 8x4 LED matrix using two WS2812B 4x4 LED matrixes connected together
typedef eCxx::ColorFX2D<eCxx::GRBColor, 8, 4, eCxx::CBP::BlockRowMajorPixelAltColMajor, eCxx::CBP::RawBuffer, 4, 4> CFXRawRGB;

static CFXRawRGB cfx;


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

    //
    // Update the LED animation
    //

    static eCxx::uint8_t x = cfx.XElementCount - 1;
    static eCxx::uint8_t y = cfx.YElementCount - 1;

    // Turn off the last pixel
    static const eCxx::RGBColor& offColor = eCxx::Ext::RGB::black;
    cfx.setXY(x, y, offColor);

    // Update the pixel position
    ++x;
    if(x >= cfx.XElementCount) {
        x = 0;
        ++y;
        if(y >= cfx.YElementCount) y = 0;
    }

    // Turn on the new pixel
    static const eCxx::RGBColor& onColor = eCxx::RGBColor(16, 16, 16);
    cfx.setXY(x, y, onColor);

    // Update the WS2812B
    ws2812b.update();
}
