/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include <animations.h>
#include <project.h>
#include <colortransform.h>
#include <stdlib.h>

uint8 i, j, ucRed, ucGreen, ucBlue;
uint16 us_AnimIncrementCounter; 
uint8 uc_KnightriderIncrementCounter = 0; 
uint8 uc_ColorKnightRiderHue = 0;

uint8 RGB_Buffer[255] = { 0 } ;
uint8 black[33] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };  
RgbColor RGBCol;
HsvColor HSVCol = {255, 255, 255 };


void ClearBuffer();
void LEDWriteData(uint8 *buffer, uint32 count);
void Knightriderfunc(int ledcount, int delay, uint8 intensity);
void Colorswirlfunc(int hue);
void test( void );
void newflash(int ledcount);
void Colorriderfunc();


void SetPixel(uint8 position, uint8 red, uint8 green, uint8 blue)
{
    RGB_Buffer[3*position]=red;
    RGB_Buffer[3*position+1]=green;
    RGB_Buffer[3*position+2]=blue;
}

void ClearBuffer()
{
    memset(RGB_Buffer,0,40);    
}

void LEDWriteData(uint8 *buffer, uint32 count)
{
    SPIMASTER_SpiUartPutArray(buffer, count);
}

void Display_new_Color(uint8 red, uint8 green, uint8 blue)
{
    for(i=0;i<11;i++)
    {
        SetPixel(i,red,green,blue);
    }
    LEDWriteData(RGB_Buffer,33);
}

void v_Animate_LEDs(e_Animation Select_Animation)
{
    switch(Select_Animation)
    {
        case Knightrider:
            // Nur jedes dritte Mal beim vorbeikommen ausführen
            if(us_AnimIncrementCounter%3 == 0)  Knightriderfunc(LEDS, WAITTIME, INTESITY);
            CyDelay(10+rand()%10);
            break;
       
        case Rainbowswirl:
            Colorswirlfunc(us_AnimIncrementCounter%255);
            CyDelay(10);
            break;
            
        case Rainbowswirlslow:
            Colorswirlfunc(us_AnimIncrementCounter%255);
            CyDelay(40);
            break;
        
        case ColorKnightRider:
            Colorriderfunc();
            CyDelay(40);
            break;   
        
        case ColorKnightRiderSlow:
            if(us_AnimIncrementCounter%3 == 0)  Colorriderfunc();
            CyDelay(30);
            break;      
            
        case ColorKnightRiderRealSlow:
            if(us_AnimIncrementCounter%10 == 0)  Colorriderfunc();
            CyDelay(30);
            break;
            
        default :
            
        break;
    }
    us_AnimIncrementCounter++;
    
}

/**************************************************


*/
void Knightriderfunc(int ledcount, int delay, uint8 intensity)
{
 	RGB_Buffer[3*uc_KnightriderIncrementCounter] = intensity;
    LEDWriteData(RGB_Buffer, 3*ledcount);
    memset(RGB_Buffer, 0, 3*ledcount);
    // CyDelay(30+rand()%50);
    uc_KnightriderIncrementCounter++;
    if(uc_KnightriderIncrementCounter>7) uc_KnightriderIncrementCounter = 0;
}


void Colorriderfunc()
{
    HSVCol.h=uc_ColorKnightRiderHue++;
    
    memset(RGB_Buffer, 0, 3*LEDS);
    RGBCol = HsvToRgb(HSVCol);
    
  	RGB_Buffer[3*uc_KnightriderIncrementCounter] = RGBCol.r;
    RGB_Buffer[3*uc_KnightriderIncrementCounter+1] = RGBCol.g;
    RGB_Buffer[3*uc_KnightriderIncrementCounter+2] = RGBCol.b;
    
    LEDWriteData(RGB_Buffer,3*LEDS);
    uc_KnightriderIncrementCounter++;
    if(uc_KnightriderIncrementCounter>7) uc_KnightriderIncrementCounter = 0;
}


void Colorswirlfunc(int hue)
{
    uint8 i;
    HSVCol.h=hue;
    
    RGBCol = HsvToRgb(HSVCol);
    
    for(i=0;i<LEDS;i++)
    {
        RGB_Buffer[3*i]=RGBCol.r;
        RGB_Buffer[1+3*i]=RGBCol.g;
        RGB_Buffer[2+3*i]=RGBCol.b;
    }
    LEDWriteData(RGB_Buffer,3*LEDS);
}

/* [] END OF FILE */
