#include <avr/io.h>



//forward declarations
//=========================
void ioinit(void);
void delay_ms(uint16_t x);
void led_delay();
void clear_rows();
void clear_cols();
void display_row(uint8_t row);
void display_green_cols();
void display_red_cols();
void green_col_one_by_one(uint8_t cols);
void test_rows();
void test_individual_leds();
void turn_on_all_rows();
void display_one_col_all_rows(uint8_t col);
void test_cols();
//=========================

int main(void)
{
    
   ioinit(); //Initializes the IO pins
   
   while(1)
   {

	  //test_individual_leds();
	  //test_rows();
	  test_cols();

   }

	return (0);

}

void ioinit(void)
{

	//set pins to output 1 = output 0 = input
	//I only set the pins that I need to set.

	DDRD = 0b11111110;
	DDRB = 0b01001111;
	DDRA = 0b00101111;
	DDRE = 0b00000110;
	DDRC = 0b11111000;

}

void delay_ms(uint16_t x)
{
  uint8_t y, z;
  for ( ; x > 0 ; x--)
  {
    for ( y = 0 ; y < 90 ; y++)
	{
      for ( z = 0 ; z < 6 ; z++)
	  {
        asm volatile ("nop");
      }
    }
  }
}

void led_delay() {
	uint16_t count1 = 10000;
	while(count1--);
}


/*This function turns off the ports for the rows*/
void clear_rows() // |= set bit high
{
 PORTD |=(1<<7); 
 PORTD |=(1<<4);
 PORTD |=(1<<1);
 PORTB |=(1<<2);
 PORTC |=(1<<6);
 PORTC |=(1<<3);
 PORTE |=(1<<1);
 PORTA |=(1<<2);
}

/*This function turns off the ports for the columns*/
void clear_cols()  // &=~ set bit low
{
// green columns
 PORTD &=~(1<<6);
 PORTD &=~(1<<3);
 PORTB &=~(1<<6);
 PORTB &=~(1<<1);
 PORTC &=~(1<<4); 
 PORTC &=~(1<<7);
 PORTA &=~(1<<5);
 PORTA &=~(1<<1);
// red columns
 PORTD &=~(1<<5);
 PORTD &=~(1<<2);
 PORTB &=~(1<<3);
 PORTB &=~(1<<0); 
 PORTC &=~(1<<5);
 PORTE &=~(1<<2);
 PORTA &=~(1<<3);
 PORTA &=~(1<<0);
}


/*This function sets the row passed in to on allowing the row to be displayed*/
void display_row(uint8_t row)
{
   if(row == 1)
      PORTD &=~(1<<7);
   else 
      if(row == 2)
         PORTD &=~(1<<4);
   else 
      if(row == 3)
         PORTD &=~ (1<<1);
   else 
      if(row == 4)
         PORTB &=~(1<<2);
   else 
      if(row == 5)
         PORTC &=~(1<<3);
   else 
      if(row == 6)
         PORTC &=~(1<<6);
   else 
      if(row == 7)
         PORTE &=~(1<<1);
   else
      if(row == 8)
      PORTA &=~(1<<2);
}


/*This turns all the green columns on */
void display_green_cols()
{
   PORTD |= (1<<6);
   PORTD |= (1<<3);
   PORTB |= (1<<6);
   PORTB |= (1<<1);
   PORTC |= (1<<4); 
   PORTC |= (1<<7);
   PORTA |= (1<<5);
   PORTA |= (1<<1);
}
/*This function turns all the red columns on*/
void display_red_cols()
{
   PORTD |=(1<<5);
   PORTD |=(1<<2);
   PORTB |=(1<<3);
   PORTB |=(1<<0); 
   PORTC |=(1<<5);
   PORTE |=(1<<2);
   PORTA |=(1<<3);
   PORTA |=(1<<0);
}

/*This turns on the green column that is passed into the function*/
void green_col_one_by_one(uint8_t cols)
{
   if(cols == 1)
      PORTD |= (1<<6);
   else 
      if(cols == 2)
         PORTD |= (1<<3);
   else 
      if(cols == 3)
         PORTB |= (1<<6);
   else 
      if(cols == 4)
         PORTB |= (1<<1);
   else 
      if(cols == 5)
         PORTC |= (1<<4);
   else 
      if(cols == 6)
         PORTC |= (1<<7);
   else 
      if(cols == 7)
         PORTA |= (1<<5);
   else
      if(cols == 8)
      PORTA |= (1<<1);
}

/*This turns on the red column that is passed into the function*/
void red_col_one_by_one(uint8_t cols)
{
   if(cols == 1)
      PORTD |= (1<<5);
   else 
      if(cols == 2)
         PORTD |= (1<<2);
   else 
      if(cols == 3)
         PORTB |= (1<<3);
   else 
      if(cols == 4)
         PORTB |= (1<<0);
   else 
      if(cols == 5)
         PORTC |= (1<<5);
   else 
      if(cols == 6)
         PORTE |= (1<<2);
   else 
      if(cols == 7)
         PORTA |= (1<<3);
   else
      if(cols == 8)
      PORTA |= (1<<0);
}

/*This function displays first a row of green lights then red lights.
  It iterates through all 8 rows.*/
void test_rows()
{
        uint8_t row;
	    clear_rows();
		clear_cols();
		for(row = 1; row <9; row++)
		{
		   display_row(row);
		   display_green_cols();
		   delay_ms(1000);
		   clear_cols();
		   display_red_cols();
		   delay_ms(1000);
		   clear_cols();
		   clear_rows();
        }
}

/*This function iterates through the rows displaying individual lights. 
  First a green light is displayed then a red.  All 64 lights (both colors)
  are displayed one at a time.*/
void test_individual_leds()
{

   uint8_t row;
   uint8_t cols;
   clear_rows();
   clear_cols();
   for(row = 1; row < 9; row++)
   {
      clear_rows();
	  display_row(row);
      for(cols = 1; cols < 9; cols++)
	  {
		 green_col_one_by_one(cols);
		 delay_ms(500);
		 clear_cols();
		 red_col_one_by_one(cols);
		 delay_ms(500);
		 clear_cols();
      }
   }
}

/*This function turns on all rows.*/
void turn_on_all_rows()
{
      PORTD &=~(1<<7);
      PORTD &=~(1<<4);
      PORTD &=~(1<<1);
      PORTB &=~(1<<2);
      PORTC &=~(1<<3);
      PORTC &=~(1<<6);
      PORTE &=~(1<<1);
      PORTA &=~(1<<2);	
}

/*This function first turns on all rows then turns on the column that 
  is passed into the function.  This displays a whole column at a time.*/
void display_one_col_all_rows(uint8_t col)
{
   turn_on_all_rows();
   red_col_one_by_one(col);    	
}

/*This function iterates through all 8 columns turning them on one at a time.
  Giving the effect that the column is scrolling across the screen.  */
void test_cols()
{
        uint8_t col;
	  
		for(col = 1; col <9; col++)
		{
		   display_one_col_all_rows(col);
		   delay_ms(150);
		   clear_cols();
        }
}