#include "drawing.h"
#include "global.h"
#include "stm32f1xx_hal.h"
#include "stdlib.h"

void boom (void) {      
    uint16_t i, j;
    uint8_t index;
    int16_t radius = 0;
    instrNum++;
    for (uint16_t count = 0, radius = 0; count < 500; count++, radius++) 
    {
        while(!WS2812_TX);
        for(i = 0; i < NUMBER_OF_ROWS; i++) {
            for(j = 0; j < NUMBER_OF_COLUMNS; j++) { 
                index = rand() % 21; 
                WS2812_framedata_setPixel(i, j, brightness[index], brightness[index], brightness[index]);
            } 
        }
        WS2812_sendbuf(BUF_SIZE_IN_BYTES);           
        // wait some amount of time
        //HAL_Delay(1);
        if(lifeClock >= script[instrNum]) {
            break;
        }
    } 
    clearScreen();
}

//        while(!WS2812_TX);
//        if ((NUMBER_OF_ROWS/2 - radius) != 0 && (NUMBER_OF_ROWS/2 + radius) < NUMBER_OF_ROWS) {
//            for(i = (NUMBER_OF_ROWS/2 - radius); i <= (NUMBER_OF_ROWS/2 + radius); i++) {
//                for(j = (NUMBER_OF_COLUMNS/2 - radius); j <= (NUMBER_OF_COLUMNS/2 + radius); j++) { 
//                    index = rand() % 21; 
//                    WS2812_framedata_setPixel(i, j, brightness[index], brightness[index], brightness[index]);
//                } 
//            }
//            WS2812_sendbuf(BUF_SIZE_IN_BYTES);           
//            // wait some amount of time
//            //HAL_Delay(1);
//        }
        
void drawInvertedQuestion (uint8_t red, uint8_t green, uint8_t blue) {
    
    while(!WS2812_TX);                  // wait until the last frame was transmitted
    
    for(uint16_t j = 0; j < NUMBER_OF_ROWS; j++) 
    {
        WS2812_framedata_setRow(j, NUMBER_OF_COLUMNS, red, green, blue);
    }
    
    WS2812_framedata_setPixel(5, 19, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(6, 19, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 19, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(8, 19, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(9, 19, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(4, 18, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(5, 18, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(6, 18, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 18, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(8, 18, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(9, 18, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(10, 18, 0x00, 0x00, 0x00);
    
    WS2812_framedata_setPixel(3, 17, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(4, 17, 0x00, 0x00, 0x00); 
    WS2812_framedata_setPixel(5, 17, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(9, 17, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(10, 17, 0x00, 0x00, 0x00); 
    WS2812_framedata_setPixel(11, 17, 0x00, 0x00, 0x00);
    
    WS2812_framedata_setPixel(2, 16, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(3, 16, 0x00, 0x00, 0x00); 
    WS2812_framedata_setPixel(4, 16, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(10, 16, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(11, 16, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(12, 16, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(2, 15, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(3, 15, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(11, 15, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(12, 15, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(11, 14, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(12, 14, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(11, 13, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(12, 13, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(10, 12, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(11, 12, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(12, 12, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(9, 11, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(10, 11, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(11, 11, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(7, 10, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(8, 10, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(9, 10, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(10, 10, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 9, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 9, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(8, 9, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(9, 9, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 8, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 8, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 7, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 7, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 6, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 6, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 5, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 5, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 3, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 3, 0x00, 0x00, 0x00);

    WS2812_framedata_setPixel(6, 2, 0x00, 0x00, 0x00);
    WS2812_framedata_setPixel(7, 2, 0x00, 0x00, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen
}
 

void drawNormalQuestion (void) {
    
    while(!WS2812_TX);                  // wait until the last frame was transmitted
    
    for(uint16_t j = 0; j < NUMBER_OF_ROWS; j++) 
    {
        WS2812_framedata_setRow(j, NUMBER_OF_COLUMNS, 0x00, 0x00, 0x00);
    }
    
    WS2812_framedata_setPixel(5, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(6, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 19, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(4, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(5, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(6, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 18, 0x00, 0x11, 0x00);
    
    WS2812_framedata_setPixel(3, 17, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(4, 17, 0x00, 0x11, 0x00); 
    WS2812_framedata_setPixel(5, 17, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 17, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 17, 0x00, 0x11, 0x00); 
    WS2812_framedata_setPixel(11, 17, 0x00, 0x11, 0x00);
    
    WS2812_framedata_setPixel(2, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(3, 16, 0x00, 0x11, 0x00); 
    WS2812_framedata_setPixel(4, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 16, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(2, 15, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(3, 15, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 15, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 15, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(11, 14, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 14, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(11, 13, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 13, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(10, 12, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 12, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 12, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(9, 11, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 11, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 11, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(7, 10, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 10, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 10, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 10, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 9, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 9, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 9, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 9, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 8, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 8, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 7, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 7, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 6, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 6, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 5, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 5, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 3, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 3, 0x00, 0x11, 0x00);

    WS2812_framedata_setPixel(6, 2, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 2, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen
}

    
void drawQuestionMark (void) 
{
    uint32_t delta;
    delta = (script[instrNum + 1] - script[instrNum]) / NUMBER_OF_COLUMNS;
    instrNum ++;
    
    while(lifeClock < script[instrNum]);
    drawVerLine (21, 16, 0);    // clear the line
    while(lifeClock < script[instrNum] + delta);
    drawVerLine (20, 16, 0);    // clear the line
    while(lifeClock < script[instrNum] + 2 * delta);
    drawVerLine (19, 16, 0);    // clear the line
    
    while(!WS2812_TX);                  // wait until the last frame was transmitted
    WS2812_framedata_setPixel(5, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(6, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 19, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 19, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen  
    while(lifeClock < script[instrNum] + 3 * delta);
    
    drawVerLine (18, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(4, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(5, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(6, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 18, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 18, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 4 * delta);
    
    drawVerLine (17, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(3, 17, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(4, 17, 0x00, 0x11, 0x00); 
    WS2812_framedata_setPixel(5, 17, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 17, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 17, 0x00, 0x11, 0x00); 
    WS2812_framedata_setPixel(11, 17, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 5 * delta);
    
    drawVerLine (16, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(2, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(3, 16, 0x00, 0x11, 0x00); 
    WS2812_framedata_setPixel(4, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 16, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 16, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 6 * delta);
    
    drawVerLine (15, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(2, 15, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(3, 15, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 15, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 15, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 7 * delta);
    
    drawVerLine (14, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(11, 14, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 14, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 8 * delta);
    
    drawVerLine (13, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(11, 13, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 13, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 9 * delta);
    
    drawVerLine (12, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(10, 12, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 12, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(12, 12, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 10 * delta);
    
    drawVerLine (11, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(9, 11, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 11, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(11, 11, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 11 * delta);
    
    drawVerLine (10, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(7, 10, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 10, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 10, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(10, 10, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 12 * delta);
    
    drawVerLine (9, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 9, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 9, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(8, 9, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(9, 9, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 13 * delta);
    
    drawVerLine (8, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 8, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 8, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 14 * delta);
    
    drawVerLine (7, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 7, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 7, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 15 * delta);
    
    drawVerLine (6, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 6, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 6, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 16 * delta);
    
    drawVerLine (5, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 5, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 5, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 17 * delta);
    
    drawVerLine (4, 16, 0);    // clear the line
    while(lifeClock < script[instrNum] + 18 * delta);
    
    drawVerLine (3, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 3, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 3, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen       
    while(lifeClock < script[instrNum] + 19 * delta);
    
    drawVerLine (2, 16, 0);    // clear the line
    while(!WS2812_TX);
    WS2812_framedata_setPixel(6, 2, 0x00, 0x11, 0x00);
    WS2812_framedata_setPixel(7, 2, 0x00, 0x11, 0x00);
    WS2812_sendbuf(BUF_SIZE_IN_BYTES);  // flush buffer to the screen   
    while(lifeClock < script[instrNum] + 20 * delta);   

    drawVerLine (1, 16, 0);    // clear the line
    while(lifeClock < script[instrNum] + 21 * delta);
    
    drawVerLine (0, 16, 0);    // clear the line
    while(lifeClock < script[instrNum] + 22 * delta);

}

