//
// Button escape hello.servo.44.2.c
//
// two-channel software PWM servo motor hello-world
//
// set lfuse to 0x5E for 20 MHz xtal
//
// Neil Gershenfeld
// 4/8/12
//
// (c) Massachusetts Institute of Technology 2012
// This work may be reproduced, modified, distributed,
// performed, and displayed for any purpose. Copyright is
// retained and must be preserved. The work is provided
// as is; no warranty is provided, and users accept all 
// liability.
//

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

#define output(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 position_delay() _delay_ms(1000)

#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 serial_port PORTA
#define serial_direction DDRA
#define serial_pins PINA
#define serial_pin_in (1 << PA0)
#define serial_pin_out (1 << PA1)

#define max_buffer 25

#define PWM_port PORTA
#define PWM_direction DDRA
#define PWM_pin (1 << PA7)

#define loop_count 30


/*void move_to(int position, int hold_length) {
  // position can be any number between 0 and 1000
  int i;
  for (i = 0; i < hold_length; ++i) {
    set(PWM_port,PWM_pin);
    _delay_us(position);
    clear(PWM_port,PWM_pin);
    _delay_us(20000-position);
  }
}
*/

int main(void) {
   //
   // main
   //
   uint8_t i;
   //
   // set clock divider to /1
   //
   CLKPR = (1 << CLKPCE);
   CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
   //
   // set PWM pins to output
   //
   //clear(PWM_port, PWM_pin_0);
   //output(PWM_direction, PWM_pin_0);
   clear(PWM_port, PWM_pin);
   output(PWM_direction, PWM_pin);

   // set the internal pull up for the button 
   //
   // main loop
   //
   // something with port register;
   while (1) {
     
     //if(pin_test(PINA, (1 << PA2))){
       //  set(PORTB, (1 << PB2));
       //}
       //else{
        //clear(PORTB, (1 << PB2));
       //}
     // check that button is pressed :D
     
      //
      // 1 ms on time, both
      //
   //pulse width modulation (PWM)
   // relative magnitudes
     set(PORTA, (1 << PA2));
     
     if(!pin_test(PINA, (1<<PA2))){
       

       // turn on the light
       set(PORTA, (1 << PA3));

       // move_to(0, 30);
       for (i = 0; i < loop_count; ++i) {
         set(PWM_port,PWM_pin);
         _delay_us(1000);
         clear(PWM_port,PWM_pin);
         _delay_us(19000);
      }
      //
      // 1.5 ms on time
      //

       // move_to(1000, 30);
       
       for (i = 0; i < loop_count; ++i) {
         set(PWM_port,PWM_pin);
         _delay_us(2000); // was 1500
         clear(PWM_port,PWM_pin);
         _delay_us(18000); // was 18500
      }

      
      clear(PORTA, (1 << PA3));

      //
      // 2 ms on time
      //
      /*for (i = 0; i < loop_count; ++i) {
         set(PWM_port,PWM_pin);
         _delay_us(2000);
         clear(PWM_port,PWM_pin);
         _delay_us(18000);
         }*/
      //}
/*else{
       clear(PORTA, (1<<PA2));
       }*/
     }
   }
   }
