/*
 * P2 Pooling.c
 *
 * Created: 11.11.2023 21:06:54
 * Author : Safak
 */ 

#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <stdbool.h>

#define LED0 PINB0
#define LED9 PINB1
#define SW1 PIND2
#define SW2 PIND3

int activeLED = LED0;
bool blinking = true;

void setupPorts() {
	DDRB = 0xff;		// DDRB as output
	PORTB = 0x00;
	
	DDRD = 0x00;		// DDRD as input
	PORTD = 0xff;
}

void isButtonPressed() {
	if ( !(PIND & (1 << SW1)) ) {				// SW1 is pressed
		if (activeLED == LED0) {				// if D0 is on
			blinking = !blinking;				// toggle blinking
		} 
		else {			// switch LED
			PORTB = (1 << LED0);
		}
		activeLED = LED0;
	}
	else if ( !(PIND & (1 << SW2))) {			// SW2 is pressed
		if (activeLED == LED9) {				// if D9 is on
			blinking = !blinking;				// toggle blinking
		} 
		else {				// switch LED
			PORTB = (1 << LED9);
		}			
		activeLED = LED9;
	}
}
int main(void) {
	setupPorts();
	while (1) {
		isButtonPressed();
		if (blinking) {
			PORTB ^= (1 << activeLED);
		}
		else {
			PORTB = (1 << activeLED);
		}
		_delay_ms(250);
	}
}

