//#DOC Main, program entry and main loop
#include "main.h"
#include <otSerial.h>
#include <otTimer.h>
#include <otPrintfLite.h>
#include <otILI9341.h>

void CLineDemo();
void CRectangleFilledDemo();
void CRectangleDemo();
void CCircleDemo();

otILI9341 tft;

void setup()
{
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    setStdout(UART0); // printf output is now directed on UART0
    TimerSetAsCounter(TIMER0);
    TimerEnable(TIMER0, true);
    tft.init();
    tft.setRotation(otILI9341::IR_270);
    tft.clear();
    tft.setTextColor(RGB565_YELLOW, RGB565_BLACK);
    tft.setTextSize(3);
}

void loop()
{
    char    buf[16];
    CLineDemo();
    CRectangleFilledDemo();
    CRectangleDemo();
    CCircleDemo();
    for(int i = 0; i < 5000; i++)
    {
        sprintf(buf, "%04d", i);
        tft.setCursor(0, 120);
        tft.writeString(buf);
    }
}

unsigned randx(unsigned val)
{
    return TimerGetCounts(TIMER0) * 0x1961957 + val;
}

U16 const colorIndex[16] ={ RGB565_BLACK,
                            RGB565_NAVY,
                            RGB565_DARKGREEN,
                            RGB565_DARKCYAN,
                            RGB565_MAROON,
                            RGB565_PURPLE,
                            RGB565_OLIVE,
                            RGB565_LIGHTGREY,
                            RGB565_DARKGRAY,
                            RGB565_BLUE,
                            RGB565_GREEN,
                            RGB565_CYAN,
                            RGB565_RED,
                            RGB565_MAGENTA,
                            RGB565_YELLOW,
                            RGB565_WHITE
};

void CLineDemo()
{
    U16     x0, y0;
    U16     x1, y1;
    U16     color;   
    U16     no;
    const   U16 CENTER_X    = 160;
    const   U16 CENTER_Y    = 120;
    tft.clear();
    for(no = 0; no < 5000; no++)
    {
        x0 = randx(x0) % 320;
        y0 = randx(y0) % 160;
        color = no % 16;
        x1 = 2 * CENTER_X - x0;
        y1 = 2 * CENTER_Y - y0;
        tft.drawLine(x0, y0, x1, y1, colorIndex[color]);
    }
    tft.clear();
}

void CRectangleFilledDemo()
{
    U16     x0, y0;
    U16     x1, y1;
    U16     color;
    U16     no;
      
    for(no = 0; no < 500; no++)
    {
        x0 = randx(x0) % 160;
        y0 = randx(y0) % 120;
        color = no % 16;
        x1 = x0 + randx(x1) % 160;
        y1 = y0 + randx(y1) % 120;
        tft.fillRect(x0, y0, x1, y1, colorIndex[color]);
    }
    tft.clear();
}

void CRectangleDemo()
{
    U16     x0, y0;
    U16     x1, y1;
    U16     color;
    U16     no;
      
    for(no = 0; no < 5000; no++)
    {
        x0 = randx(x0) % 160;
        y0 = randx(y0) % 120;
        color = no % 16;
        x1 = x0 + randx(x1) % 160;
        y1 = y0 + randx(y1) % 120;
        tft.drawRect(x0, y0, x1, y1, colorIndex[color]);
    }
    tft.clear();
}

void CCircleDemo()
{
    U16     x0, y0;
    U16     radius;
    U16     color;
    U16     no;
      
    for(no = 0; no < 5000; no++)
    {
        x0 = randx(x0) % 240;
        y0 = randx(y0) % 320;
        color = no % 16;
        radius = randx(radius) % 100;
        tft.drawCircle(x0, y0, radius, colorIndex[color]);
    }
    tft.clear();
}