#include "button.h"
#include "main.h"
#include "led.h"

#include <avr/interrupt.h>

void button_init() 
{
	led_init();

	// enable pullup on button pin
	BUTTON_DDR &= ~(1<<BUTTON_PIN);
	BUTTON_PORT |= (1<<BUTTON_PIN);
	
	// activate pin change interrupt
	// PCINT9
	PCMSK1 |= (1<<PCINT9);
	GIMSK  |= (1<<PCIE1); // enable PCINT1
}

button_state_t button_is_pressed() 
{
	return (BUTTON_REG&(1<<BUTTON_PIN)) > 0;
}

ISR(PCINT1_vect) {
	if(!(BUTTON_REG&(1<<BUTTON_PIN))) {
		app.state.state.button = !app.state.state.button;
		led_toggle();
	}
}
