/*
 * portdefs.h
 *
 * Created: 29.11.2015 19:57:19
 *  Author: Peter Menges
 */ 


/* Definition of PORT and BITS for the 7-SEG-DISPLAY 2x 5 Segments */

#define sbi(port,bit)  	   (port) |= (1 << (bit))
#define cbi(port,bit)  	   (port) &= ~(1 << (bit))
#define NOP()			asm volatile ("nop"::)
#define DISP_DATA		PORTA
#define DISP_DATA_DDR	DDRA
#define DISP_DATA_DDR_MSK	0xff /* All bits as output */

#define DISP_CNTRL		PORTC
#define DISP_CNTRL_DDR	DDRC
#define DISP_CNTRL_MSK	0xc0	/* Output PC7 and PC6  changed to correct value 0xc0 instead of 0x0c at 18.12.2015*/

#define OE_UPPER		PC7							/* Bit for the OE-Input of first 374 */
#define OE_LOWER		PC6
#define DATA_CLOCK		PA7
#define OE_UPPER_EN		{cbi(DISP_CNTRL,OE_UPPER);}
#define OE_LOWER_EN		{cbi(DISP_CNTRL,OE_LOWER);}
#define OE_UPPER_DIS	{sbi(DISP_CNTRL,OE_UPPER);}
#define OE_LOWER_DIS	{sbi(DISP_CNTRL,OE_LOWER);}
						/* CLK_PULS: Rising Edge triggers the latch */
#define CLK_PULS		{sbi(DISP_DATA,DATA_CLOCK); NOP(); NOP(); cbi(DISP_DATA,DATA_CLOCK);}
#define SEG_0			PA0		/* selects one 7-segment unit */
#define SEG_1			PA1
#define SEG_2			PA2
#define SEG_3			PA3
#define SEG_4			PA4
#define LED_MINUS		PA5		/* Equal for upper and lower segment row */
#define LED_DOUBLE		PA6		/* Signals that the value displayed is double of the origin */
#define DISP_BLINK		0x80	/* Blink that row */

#define KEY_PIN			PINC
#define KEY_X_MASK		(1 << PC0)
#define KEY_Z_MASK		(1 << PC1)


#define DISP_BUF_MAX	10		/* Size of display buffer */
#define DISP_BUF_Z		disp_data[0]
#define DISP_BUF_X		disp_data[5]

/* SIGN definitions */
#define SIGN_POS_MINUS			(1 << LED_MINUS)	/* The sign of the value are stored in first byte*/
#define SIGN_POS_DOUBLE			( 1 << LED_DOUBLE)	/* Equal for the double sign */

/* Value positions (bit) from the calipers */
#define CAL_SIGN		20		// 1 == negative
#define CAL_UNIT		23		// Inches == 1, Millimeter == 0
#define CAL_INCHES		1		// only mm allowed!
#define CAL_LENGTH		24		// Length of the input string in bits

#define CAL_TIMER1_VAL	47		// 3 ms for Timer 1 im ctc modus

#define CAL_BIT16		15		// Only 16 bits are relevant in this application
#define CAL_XPIN		( 1 << PD5)
#define CAL_ZPIN		( 1 << PD4)
#define CAL_PORT		PIND
/* cal_sig bit positions */
#define CAL_NEW_X		0x01	// new value available
#define CAL_NEW_Z		0x02	// dto.
#define CAL_INVALID_Z	0x04	// data invalid or timed out
#define CAL_INVALID_X	0x08	// data invalid or timed out
#define CAL_DOUBLE_VAL_X	0x10	// display this value multiplied by 2 (z or x axis)
#define CAL_DOUBLE_VAL_Z	0x20	//
#define CAL_INCH_Z			0x40	// Signaling that the INCH mode is selected
#define CAL_INCH_X			0x80	// We do not want this, we are living in the metric universe!




