/************************************************************************/
/*                                                                      */
/*                      Motorpotiplatine Steuerung                      */
/*                                                                      */
/*              Author : Frank Link                                     */
/*                       franklink61@aol.com                            */
/*              Version: 1.1                                            */
/************************************************************************/

#include <avr/io.h>
#include <avr/eeprom.h>

#ifndef BUTTONSHEADER
#include "buttons.h"
#endif


/*
	Im folgenden werden alle Tasten als Arrays in der Form
	Devicecode, Keycode abgelegt.

	Der Zugriff erfolgt über rArrKey1[DEVICECODE] bzw. rArrKey1[KEYCODE]

	Die Arrays werden aus dem EEPROM in den Speicher kopiert.
*/
uint8_t eeArrKey1[2]    	EEMEM = { 0x11, 0x01 };		// Taste 1
uint8_t eeArrKey2[2]    	EEMEM = { 0x11, 0x02 };		// Taste 2
uint8_t eeArrKey3[2]    	EEMEM = { 0x11, 0x03 };		// Taste 3
uint8_t eeArrKey4[2]    	EEMEM = { 0x11, 0x04 };		// Taste 4
uint8_t eeArrKey5[2]    	EEMEM = { 0x11, 0x05 };		// Taste 5

uint8_t eeArrKeyChUp[2]  	EEMEM = { 0x11, 0x20 };		// Kanal +
uint8_t eeArrKeyChDown[2] 	EEMEM = { 0x11, 0x21 };		// Kanal +

uint8_t eeArrKeyVolUp[2] 	EEMEM = { 0x11, 0x10 };		// Volume +
uint8_t eeArrKeyVolDown[2] 	EEMEM = { 0x11, 0x11 };		// Volume -

uint8_t eeArrKeyOnOff[2] 	EEMEM = { 0x11, 0x0C };		// Ein-/Ausschalten

/*
	Grundlage der Tastentabelle ist die 8 in 1 Fernbedienung Total Control
	von Conrad Elektronik 
	Device = AUD
	Fernbedienungscode = 1112

	Programmieren der Fernbedienung:

	1. Setup-Taste betätigen, Setup blinkt erst zweimal und dann einmal
	2. Code 1112 über Nummertasten eingegben. Nach der 4. Taste blinkt 
	   die Setup-Taste zweimal.
*/

volatile uint8_t rArrKey1[2]    	= { 0x11, 0x01 };	// Taste 1
volatile uint8_t rArrKey2[2]    	= { 0x11, 0x02 };	// Taste 2
volatile uint8_t rArrKey3[2]    	= { 0x11, 0x03 };	// Taste 3
volatile uint8_t rArrKey4[2]    	= { 0x11, 0x04 };	// Taste 4
volatile uint8_t rArrKey5[2]    	= { 0x11, 0x05 };	// Taste 5

volatile uint8_t rArrKeyChUp[2]  	= { 0x11, 0x20 };	// Kanal +
volatile uint8_t rArrKeyChDown[2] 	= { 0x11, 0x21 };	// Kanal +

volatile uint8_t rArrKeyVolUp[2] 	= { 0x11, 0x10 };	// Volume< +
volatile uint8_t rArrKeyVolDown[2] 	= { 0x11, 0x11 };	// Volume -

volatile uint8_t rArrKeyOnOff[2] 	= { 0x11, 0x0C };	// Ein-/Ausschalten


/*
	Lesen aller Tastenbelegungen aus dem EEPROM
	Diese Funktion ist ist noch nicht fregegeben, da die Werte noch
	nicht korrekt gelesen werden.
*/
void initArrays( void )
{
	eeprom_read_block( (void*)&rArrKey1, (const void*)&eeArrKey1, 2 );
	eeprom_read_block( (void*)&rArrKey2, (const void*)&eeArrKey2, 2 );
	eeprom_read_block( (void*)&rArrKey3, (const void*)&eeArrKey3, 2 );
	eeprom_read_block( (void*)&rArrKey4, (const void*)&eeArrKey4, 2 );
	eeprom_read_block( (void*)&rArrKey5, (const void*)&eeArrKey5, 2 );

	eeprom_read_block( (void*)&rArrKeyChUp, (const void*)&eeArrKeyChUp, 2 );
	eeprom_read_block( (void*)&rArrKeyChDown, (const void*)&eeArrKeyChDown, 2 );

	eeprom_read_block( (void*)&rArrKeyVolUp, (const void*)&eeArrKeyVolUp, 2 );
	eeprom_read_block( (void*)&rArrKeyVolDown, (const void*)&eeArrKeyVolDown, 2 );

	eeprom_read_block( (void*)&rArrKeyOnOff, (const void*)&eeArrKeyOnOff, 2 );
}


uint8_t readKey1Eprom( void )
{
	return eeprom_read_byte( &eeArrKey1[0] );
}

uint8_t readKey1Ram( void )
{
	return rArrKey1[0];
}

char sendCode( char test )
{
	if ( test == 0x01 )
	{
		DDRD  = CHANNEL1;
		PORTD = CHANNEL1;  
		return 0x01;  	
	}
	if ( test == 0x02 )
	{
		DDRD  = CHANNEL2;
		PORTD = CHANNEL2;    	
		return 0x02;  	
	}
	if ( test == 0x03 )
	{
		DDRD  = CHANNEL3;
		PORTD = CHANNEL3;    	
		return 0x03;  	
	}
	if( test == 0x04 )
	{
		DDRD  = CHANNEL4;
		PORTD = CHANNEL4;    	
		return 0x04;  	
	}
	if( test == 0x05 )
	{
		DDRD  = CHANNEL5;
		PORTD = CHANNEL5;    	
		return 0x05;
	}
	return 0;
};

char isKey_on_off( char device, char key )
{
#ifdef TYPE2
	if ( ( key == rArrKeyOnOff[KEYCODE] ) && ( device == rArrKeyOnOff[DEVICECODE] ) )
	{
		return 1;
	}
#endif
	return -1;
}

char isKey_1( char device, char key )
{
	if ( ( key == rArrKey1[KEYCODE] ) && ( device == rArrKey1[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_2( char device, char key )
{
	if ( ( key == rArrKey2[KEYCODE] ) && ( device == rArrKey2[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_3( char device, char key )
{
	if ( ( key == rArrKey3[KEYCODE] ) && ( device == rArrKey3[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_4( char device, char key )
{
	if ( ( key == rArrKey4[KEYCODE] ) && ( device == rArrKey4[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_5( char device, char key )
{
#ifdef TYPE1
	if ( ( key == rArrKey5[KEYCODE] ) && ( device == rArrKey5[DEVICECODE] ) )
	{
		return 1;
	}
#endif
	return -1;
}

char isKey_Channel_Up( char device, char key )
{
	if ( ( key == rArrKeyChUp[KEYCODE] ) && ( device == rArrKeyChUp[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_Channel_Down( char device, char key )
{
	if ( ( key == rArrKeyChDown[KEYCODE] ) && ( device == rArrKeyChDown[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_Motor_Left( char device, char key )
{
	if ( ( key == rArrKeyVolUp[KEYCODE] ) && ( device == rArrKeyVolUp[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char isKey_Motor_Right( char device, char key )
{
	if ( ( key == rArrKeyVolDown[KEYCODE] ) && ( device == rArrKeyVolDown[DEVICECODE] ) )
	{
		return 1;
	}
	return -1;
}

char getKeyCode( char Key )
{
	if ( Key == KEYCODE_1 )
	{
		return rArrKey1[KEYCODE];
	}
	if ( Key == KEYCODE_2 )
	{
		return rArrKey2[KEYCODE];
	}
	if ( Key == KEYCODE_3 )
	{
		return rArrKey3[KEYCODE];
	}
	if ( Key == KEYCODE_4 )
	{
		return rArrKey4[KEYCODE];
	}
	if ( Key == KEYCODE_5 )
	{
		return rArrKey5[KEYCODE];
	}
	if ( Key == KEYCODE_VOL_UP )
	{
		return rArrKeyVolUp[KEYCODE];
	}
	if ( Key == KEYCODE_VOL_DOWN )
	{
		return rArrKeyVolDown[KEYCODE];
	}
	if ( Key == KEYCODE_CHA_UP )
	{
		return rArrKeyChUp[KEYCODE];
	}
	if ( Key == KEYCODE_CHA_DOWN )
	{
		return rArrKeyChDown[KEYCODE];
	}
	return -1;
}

char getDeviceCode( char Device )
{
	if ( Device == DEVICECODE_1 )
	{
		return rArrKey1[DEVICECODE];
	}
	if ( Device == DEVICECODE_2 )
	{
		return rArrKey2[DEVICECODE];
	}
	if ( Device == DEVICECODE_3 )
	{
		return rArrKey3[DEVICECODE];
	}
	if ( Device == DEVICECODE_4 )
	{
		return rArrKey4[DEVICECODE];
	}
	if ( Device == DEVICECODE_5 )
	{
		return rArrKey5[DEVICECODE];
	}
	if ( Device == DEVICECODE_VOL_UP )
	{
		return rArrKeyVolUp[DEVICECODE];
	}
	if ( Device == DEVICECODE_VOL_DOWN )
	{
		return rArrKeyVolDown[DEVICECODE];
	}
	if ( Device == DEVICECODE_CHA_UP )
	{
		return rArrKeyChUp[DEVICECODE];
	}
	if ( Device == DEVICECODE_CHA_DOWN )
	{
		return rArrKeyChDown[DEVICECODE];
	}
	return -1;
}

