// Philippa Mothersill
// blinking led pcb
// Based on hello.ftdi.44.echo.c by Neil Gershenfeld
//
//

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 8.5 // bit delay for 115200 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define char_delay() _delay_ms(10) // char delay
#define pwm_delay() _delay_ms(10) // pwm delay


#define led1_port PORTA
#define led1_direction DDRA
#define led1 (1<<PA7)

#define led2_port PORTB
#define led2_direction DDRB
#define led2 (1<<PB2)

#define button_port PORTA
#define button_direction DDRA
#define button (1<<PA3)
#define button_pins PINA


int main(void) {
    //
    // main
    //
    unsigned char pwm;
    unsigned int count=0, count2=0, i=0;
    //
    // set clock divider to /1
    //
    CLKPR = (1 << CLKPCE);
    CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
    //
    // initialize output pins
    //
    set(button_port, button);
    input(button_direction, button);
    
    set(led1_port, led1);
    output(led1_direction, led1);
    set(led2_port, led2);
    output(led2_direction, led2);
    
    clear(led2_port,led2);
    clear(led1_port,led1);
    
    
    
    //
    // main loop
    //
    while (1) {
        
        if (0 == pin_test(button_pins, button) && count == 0 && count!=1 && count!=2 && count!=3)
        {
            count = 1;
            pwm_delay();
        }
        
        if (0 == pin_test(button_pins, button) && count == 1 && count!=0 && count!=2 && count!=3)
        {
            count = 2;
            pwm_delay();
        }
        
        if (0 == pin_test(button_pins, button) && count == 2 && count!=0 && count!=1 && count!=3)
        {
            count = 3;
            pwm_delay();
        }
        
        if (0 == pin_test(button_pins, button) && count == 3 && count!=0 && count!=1 && count!=2)
        {
            count = 0;
            pwm_delay();
        }
        
        if (count == 0 && count!=1 && count!=2 && count!=3)
	    {clear(led2_port,led2);
        clear(led1_port,led1);
        }
        
        if (count == 1 && count!=0 && count!=2 && count!=3)
        {   clear(led2_port,led2);
            set(led1_port,led1);
        }
        
        if (count == 2 && count!=0 && count!=1 && count!=3)
        {clear(led1_port,led1);
            set(led2_port,led2);
        }
        
        if (count == 3 && count!=0 && count!=1 && count!=2)
        {set(led2_port,led2);
        set(led1_port,led1);
        }
        
        
    }
}
