/*
 * 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
//
static eCxx::ColorFX1D<eCxx::GRBColor, 16, eCxx::CBP::RawBuffer> cfxLEDStrip;

static auto anim1D = eCxx::makeColorAnimator1D<1>(cfxLEDStrip);


//
// Helper functions
//
static void eCxx_user_init_initializeAnimation()
{
    // Initialize the animation
    using ChaserProc = eCxx::Animation1DProc_Chaser< decltype(anim1D) >;

    const eCxx::uint8_t fullValue = eCxx::cie1931lut38(4);
    const eCxx::uint8_t halfValue = eCxx::cie1931lut38(2);

    static ChaserProc chaser(
        ChaserProc::Mode::LightBouncing,
        eCxx::RGBColor(        0,         0,         0),
        eCxx::RGBColor(halfValue, halfValue, halfValue),
        eCxx::RGBColor(fullValue, fullValue, fullValue)
    );

    anim1D.initSegment(0, chaser);

    // Reset and update the animation for the first time
    anim1D.reset();
    anim1D.update();
}


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

    // Update the LED animation
    anim1D.update();

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