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


#include <eCxx/eCxx_ColorSpace.h>

#include "AVR_Common.h"


//
// Check the RAM size
//
#if RAM_SIZE < 2048
#warning "this test application is meant for AVR MCU with at least 2 kB of RAM"
#endif


//
// Sprite sheet
//
static const eCxx::uint8_t PROGMEM_FAR pgmSpriteData[eCxx::WColor::RawByteSize * 3 * 2 * 5 * 5] = {
    // Sprite (0, 0)
    1,1,1,1,1,
    1,0,0,0,1,
    1,0,0,0,1,
    1,0,0,0,1,
    1,1,1,1,1,
    // Sprite (1, 0)
    0,1,1,1,0,
    1,0,0,0,1,
    1,0,0,0,1,
    1,0,0,0,1,
    0,1,1,1,0,
    // Sprite (2, 0)
    0,0,1,0,0,
    0,1,0,1,0,
    1,0,0,0,1,
    0,1,0,1,0,
    0,0,1,0,0,
    // Sprite (0, 1)
    0,0,0,1,1,
    0,1,1,0,1,
    1,0,0,0,1,
    0,1,1,0,1,
    0,0,0,1,1,
    // Sprite (1, 1)
    1,1,0,0,0,
    1,0,1,1,0,
    1,0,0,0,1,
    1,0,1,1,0,
    1,1,0,0,0,
    // Sprite (2, 1)
    0,0,1,0,0,
    0,0,1,0,0,
    1,1,1,1,1,
    0,0,1,0,0,
    0,0,1,0,0
};

static eCxx::SpriteSheet2D<eCxx::WColor, 3, 2, 5, 5, eCxx::PGMCharPointerFar> ss2d( eCxx::PGM_PTR_FAR(pgmSpriteData) );


//
// Painter
//
static eCxx::ColorFX2DPainter<eCxx::WColor, 32, 32, eCxx::CBP::DefaultLayout, eCxx::CBP::RawBuffer> cfx;


//
// Helper functions
//
template <typename ColorFX2DPainter_T>
static void dumpBufferRaw1Dot(const ColorFX2DPainter_T& painter)
{
    typename ColorFX2DPainter_T::ConstIterator it = painter.colorBufferData();

    App::uart().printfString("\n");

    for(typename ColorFX2DPainter_T::BufferSizeType y = 0; y < ColorFX2DPainter_T::YElementCount; ++y) {
        for(typename ColorFX2DPainter_T::BufferSizeType x = 0; x < ColorFX2DPainter_T::XElementCount; ++x) {
            App::uart().printfString("%s ", (*it++) ? "⬤" : "·");
            App::yield();
        }
        App::uart().printfString("\n");
    }

    App::uart().printfString("\n");
}


//
// The init function
//
void eCxx_user_init()
{
    // Initialize application
    initBasicAVRApp();

    // Initialize UART
    App::uart().begin(115200);

    // Draw to the buffer
    const eCxx::WColor w(1);

    cfx.putHTextXY ( 0,  1, w, "(A'bc)", eCxx::DefaultASCIIFont5x7);

    cfx.putSpriteXY( 1, 14, ss2d, 0, 0);
    cfx.putSpriteXY( 8, 14, ss2d, 1, 0);
    cfx.putSpriteXY(15, 14, ss2d, 2, 0);
    cfx.putSpriteXY( 1, 21, ss2d, 0, 1);
    cfx.putSpriteXY( 8, 21, ss2d, 1, 1);
    cfx.putSpriteXY(15, 21, ss2d, 2, 1);

    cfx.fillEllipse(26, 21, 4, 7, w);

    // Dump to UART
    dumpBufferRaw1Dot(cfx);
}


//
// The task function
//
void eCxx_user_task()
{
    // Process event
    App::processEvent();
}
