//Distributed under the GPL v 2.0
//by fenn 10/03/06
//inspired by ladyada's investigation into the timing of apple's "breathing" sleep indicator led's*
//i decided to try and spruce up the simple "blink" test program.
//it looks like an upside-down parabola would fit the curve pretty well, and be easy to implement.
// * see http://www.ladyada.net/rant/?p=31

//#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>	// include interrupt support
const long PWM_PERIOD=16*16, SCALE=1;
//for a delay of 1 second, n = about 23000
void delay(long n) 
{
//	printf("%d", n);
	
	long a;
	for(a=0;a<n;a++)
	{
	//	asm volatile("nop");
	}
}

void led_on(void)
{
	//PORTA=0xFF;
	PORTA |= _BV(PA0);
//	printf("on: ");	
}

void led_off(void)
{
	//PORTA=0x00; 
	PORTA&= ~_BV(PA0);
//	printf("off: ");
}

void blink(long n)
{
	OCR1A=0;
	led_on();
	delay(SCALE*(n));
	led_off();
	//delay(SCALE*(PWM_PERIOD - n));
//	printf("\n");
}

void pwm_init() {
  // clear pwm levels
  OCR1A = 0; 
  // set up WGM, clock, and mode for timer 0
  TCCR1A = 0 ;//<< CTC1  | 
	//1 << PSR1  |
	//1 << CS13 | 
	//0 << CS12 |
	//1 << CS11 | 
	//0 << CS10  | 
	//0 << CS01  |
	//1 << CS00  ;
  
 }

int main(void)
{    
	int i;
	DDRB=0xFF; //set port B to output
//printf("PWM period = %d\n", PWM_PERIOD);
	while(1)
	{
		for(i=16; i>1; i--) blink((i*i));
		for(i=1; i<16; i++) blink(1); //pause for a bit between breaths
		for(i=1; i<16; i+=2) blink((i*i));//breathe in faster than we breathe out

		//for(i=PWM_PERIOD; i>0; i--) blink(PWM_PERIOD -(0x001<<i));
		//for(i=0; i<PWM_PERIOD; i++) blink(PWM_PERIOD -(0x001<<i));
		//delay(10000);

	}

}
