//***********************************************************************************
//
// iomacros.h	(rev 1)
//
// Author:	Jeremy Greenwood
//
// Updated & extended by Ondřej Boštík
//
// Description:
//			Includes basic macro functions for input and output tasks. Intended for
//		use with AVR microcontrollers compiled with GCC.
//
//		To use this in a project:
//												
//		A)	1. Add this file to project	  or	B)	1.	Add this file to avr/include
//			2. #include "avriomacros.h"				2.	#include <avriomacros.h>
//
// Example usage to turn an LED on with a pushbutton:
/*

#include <avr/io.h>
#include "iomacros.h"

#define LED		3,B				// declare LED pin and port (always specify pin first)
#define BUTTON	2,B				// declare BUTTON pin and port

int main (void) {

	out(LED);					// set LED as an output
	in(BUTTON);					// set BUTTON as an input
	pullup(BUTTON);				// activate internal pullup resistor for BUTTON
	
	while(1) {					// forever loop

		if(get(BUTTON) == 0)	// if button is pressed...
			on(LED);			//     turn on LED
		else					// otherwise
			off(LED);			//     turn off LED
	}
}

*/
//***********************************************************************************

/* Macro function to declare an output pin */
#define out(x)			_out(x)
#define _out(bit,port)	DDR##port |= (1 << bit)

/* Macro function to declare an input pin */
#define in(x)			_in(x)
#define _in(bit,port)	DDR##port &= ~(1 << bit)

/* Macro function to set an output pin high */
#define on(x)			_on(x)
#define _on(bit,port)	PORT##port |= (1 << bit)

/* Macro function to set an output pin low */
#define off(x)			_off(x)
#define _off(bit,port)	PORT##port &= ~(1 << bit)

/* Macro function to toggle an output pin */
#define flip(x)			_flip(x)
#define _flip(bit,port)	PORT##port ^= (1 << bit)

/* Macro function to set internal pullup resistor of input pin (same as "on" macro)*/
#define pullup(x)		_on(x)

/* Macro function to get state of input pin */
#define get(x)			_get(x)
#define _get(bit,port)	(PIN##port & (1 << bit))>>bit



/*Added by O.B. */

/* Light up one LED from array */
#define toogleLED(x) (1 <<  (3-(x%4))  ) | ( (~(1<<  (6-(x/4))  )) & 0x70)
/* break down the formula:
	a = (3-(x%4));
	b = (6-(x/4));
	c = (1 <<  a  ) | ( (~(1<<  b  )) & 0x70)
*/

/* Timer0 macros, 256prescaler */
#define enableTimer0	TCCR0B |=  (1 << CS02)
#define disableTimer0	TCCR0B &= ~(1 << CS02)

/* EXT Interrupt 1 macros */
#define enableINT0		EIMSK |=  (1 << INT0)
#define disableINT0		EIMSK &= ~(1 << INT0)
#define enableINT1		EIMSK |=  (1 << INT1)
#define disableINT1		EIMSK &= ~(1 << INT1)


#define GPA(x)			(InputSTAT << REL_IN) | (BlockSTAT << REL_BLOCK) | (rel_uhf_2 << REL_UHF2) | (rel_uhf_1 << REL_UHF1) | x

#define USART_send(x)	USART_putstring((volatile char*) x)