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

#include "utils.h"
#include "bitmanip.h"
#include "timer.h"

#define PERIOD							1000
#define PRESCALE_VALUE					1024		// can be 1, 8, 64, 256, 1024
#define DUTY_CYCLE						10

#define MAX								100			// max duty cycle percentage
#define MIN								10			// min duty cycle percentage

// Table 16-4 : Waveform Generation Mode Bit Description
#define CTC_TOP_OCR1A_IN_TCCR1B			(0<<WGM13 | 1<<WGM12)
#define CTC_TOP_OCR1A_IN_TCCR1A									(0<<WGM11 | 0<<WGM10)
#define CTC_TOP_ICR1_IN_TCCR1B			(1<<WGM13 | 1<<WGM12)
#define CTC_TOP_ICR1_IN_TCCR1A									(0<<WGM11 | 0<<WGM10)
#define FAST_PWM_TOP_OCR1A_IN_TCCR1B	(1<<WGM13 | 1<<WGM12)
#define FAST_PWM_TOP_OCR1A_IN_TCCR1A							(1<<WGM11 | 1<<WGM10)
#define FAST_PWM_TOP_ICR1_IN_TCCR1B		(1<<WGM13 | 1<<WGM12)
#define FAST_PWM_TOP_ICR1_IN_TCCR1A								(1<<WGM11 | 0<<WGM10)

// END MACROS

volatile int duty_cycle = DUTY_CYCLE;

void increment_duty_cycle() {
	if(duty_cycle < MAX) {
		duty_cycle += 10;
	}
	OCR1A = TIME_MS(PERCENT(duty_cycle, PERIOD), PRESCALE_VALUE);
}

void decrement_duty_cycle() {
	if(duty_cycle > MIN) {
		duty_cycle -= 10;
	}
	OCR1A = TIME_MS(PERCENT(duty_cycle, PERIOD), PRESCALE_VALUE);
}

void on_press(int bit, void (*action)(void)) {
	if ((TEST(PIND, bit)) == 0) {
	    action();
	    _delay_ms(20);
	    while ((TEST(PIND, bit)) == 0) {
	        continue;
	    }
	    _delay_ms(20);
	}
}

// led turns on and off every PERIOD ms using Fast PWM timer, with duty cycle controlled by SW1 and SW2
int main() {
	MODE_OUTPUT(LED2);

    TCCR1A |= FAST_PWM_TOP_ICR1_IN_TCCR1A;							// Table 16-4 : set timer in Fast PWM (Pulse With Modulation) mode with TOP = ICR1 (Mode 14)
	TCCR1B |= FAST_PWM_TOP_ICR1_IN_TCCR1B;

	SET(TCCR1A, COM1A1);											// Table 16-2 : non-inverting mode, the LED will be ON for DUTY_CYCLE% of the time (CLEAR OC1A on compare match, SET OC1A at BOTTOM)

    ICR1 = TIME_MS(PERIOD, PRESCALE_VALUE);							// Table 16-4 : set the period (compare TOP value)
    
    OCR1A = TIME_MS(PERCENT(DUTY_CYCLE, PERIOD), PRESCALE_VALUE);	// 16.9.3 : set the duty cycle to DUTY_CYCLE% of the time in channel A -> OC1A (alternate function of PORTB1, aka LED2) is cleared when TCNT1 (the counter value) equals OCR1A

    TCCR1B |= (PRESCALE_SET(PRESCALE_VALUE));						// start the timer with the prescaler

	while(1) {
		on_press(SW1, increment_duty_cycle);
		on_press(SW2, decrement_duty_cycle);
	}
}