//Arduino Ottawa Contest One
//8MHz clock
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <PaperCar2014.h>

#define red  0x7c00
#define grn  0x3e0
#define blu  0x1f
#define wht  (red|grn|blu)
#define cyn  (grn|blu)
#define mag  (red|blu)
#define off  0

#define M1        (1<<2)		//PORTB
#define M2	  (1<<7)		//PORTA
#define StartM2() PORTA |= 1<<7;
#define StopM2()  PORTA &=~(1<<7);

PaperCar2014 LED(10); // 1 LED

// customizable parameters
int rows = 10;		        // row length
int row_timer = 3;		// time between row pixels
int column_timer = 15;		// time between columns
int columns = 30;	        // number of columns

   PROGMEM  prog_uint16_t image[] = {
//000,001,002,003,004,005,006,007,008,009,010,011,012,013,014,015,016,017,018,019,020,021,022,023,024,025,026,027,028,029
  red,red,red,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,red,red,red,    //9
  red,red,red,off,off,off,off,off,off,off,blu,blu,blu,off,off,off,off,off,blu,blu,blu,off,off,off,off,off,off,red,red,red,    //8
  red,red,red,off,off,off,off,off,off,off,blu,off,blu,off,off,off,off,off,blu,off,blu,off,off,off,off,off,off,red,red,red,    //7
  red,red,red,off,off,off,off,off,off,off,blu,blu,blu,off,off,off,off,off,blu,blu,blu,off,off,off,off,off,off,red,red,red,    //6
  red,red,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,off,red,red,    //5
  red,red,off,off,off,off,off,blu,off,off,off,off,off,off,off,off,off,off,off,off,off,off,blu,off,off,off,off,off,red,red,    //4
  red,off,off,off,off,off,off,blu,off,off,off,off,off,off,off,off,off,off,off,off,off,off,blu,off,off,off,off,off,off,red,    //3
  red,off,off,off,off,off,off,off,blu,off,off,off,off,off,off,off,off,off,off,off,off,blu,off,off,off,off,off,off,off,red,    //2
  red,off,off,off,off,off,off,off,off,blu,blu,off,off,off,off,off,off,off,off,blu,blu,off,off,off,off,off,off,off,off,red,    //1
  off,off,off,off,off,off,off,off,off,off,off,blu,off,blu,off,blu,off,blu,off,off,off,off,off,off,off,off,off,off,off,off,    //0
  }; 
  

void adc_setup(void)
{
  ADMUX |= (1 << 1);          // Set the ADC input to PA2/ADC2
  ADMUX |= (1 << ADLAR);     // Comment out for 10-bit resolution

  // Set the prescaler to clock/128 & enable ADC
  ADCSRA |= (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN);
  ADCSRA |= (1<<ADSC);
}

int adc_read(void)
{
  ADCSRA |= (1 << ADSC);        // Start the conversion
  while (ADCSRA & (1 << ADSC)); //Wait until the conversion is done
  return ADCH;
}

void setup() {
int i;
  CLKPR = 1<<CLKPCE;			//enable clock change
  CLKPR = 0x00;				//8Mhz internal clock
  for(i=0;i<10;i++){
    LED.setColorRGB(i, 0x00, 0x00, 0x00);      //all LEDs off 
  }
  pinMode(3,OUTPUT);       //IR LED
  digitalWrite(3,HIGH);    //turn on IR
  pinMode(2,INPUT);        //IR Rx
}

void loop() {
unsigned int i,IR_result,LED_colour;
unsigned char Red,Green,Blue;
int row,column;
long sync_pulse;

  DDRA |= 1<<3;//enable IR LED output
  PORTA |= 1<<3; //turn IR LED on
  
  StopM2();
  DDRA |= 1<<7;  
  StartM2();  
  
  row_timer = 2;
  for(i=0;i<10;i++){
    LED.setColorRGB(i, 0x80, 0x00, 0x80);      //ramp up motor
    LED.WS2812_render();
    delay(125);
  }   
  while(1){  
    for (column = 0; column < columns; column++){
      for (row = 0; row < rows; row++){
        LED_colour = pgm_read_word_near(image + row*columns + column);
        Red = ((LED_colour&red)>>7);
        Green = ((LED_colour&grn)>>4);
        Blue = ((LED_colour&blu)<<3);
        LED.setColorRGB(9-row, Red, Green, Blue);
      }
      LED.WS2812_render(); 
      delay(row_timer);
    }
    for (row = 0; row < rows; row++){      //blank remainder of display
      LED.setColorRGB(row, 0x00, 0x00, 0x00);
    }
    LED.WS2812_render();
    while(analogRead(2) <(9*1024/100)){};  //wait for sync pulse
  }
}