#include "mybool.h"
#include "packet.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "timers.h"

// vsetko pre pracu s displejom
#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/canvas.h"
#include "grlib/pushbutton.h"
#include "grlib/imgbutton.h"
#include "grlib/container.h"
#include "Drivers/kitronix320x240x16_ssd2119_idm_sbc.h"		// driver pre display

#include "MySound.h"

#include "MainTask.h"
#include "displayScreens/ButtonsImages.h"
#include "displayScreens/MenuScreen.h"
#include "displayScreens/MesageWindow.h"
#include "displayScreens/SMscreen.h"
#include "displayScreens/LogoScreen.h"

extern tContext sContext;

// ****************** menu SCREEN pre Spinaci modul ***************************************
extern	tCanvasWidget		g_sMenuSpinModul;
extern	tContainerWidget	g_sMenuSMSet;
extern	tContainerWidget	g_sMenuSMConfig;
extern tImageButtonWidget	g_sMenuSMBack;
extern tImageButtonWidget 	g_psMenuSpinModSetPushButt[];
extern tImageButtonWidget 	g_psMenuSpinModConfigPushButt[];

// ****************** SCREEN pre Spinaci modul ***************************************
unsigned char	SM_ScreanIndex;
unsigned char	SM_SettingsIndex;
unsigned char	SM_CheckboxStatus[4][3];		// 0 je manual 1 az 4 je eeprom
#define MAX_ScreanIndex	2

// Forward reference to various widget structures.
extern tCanvasWidget	g_sSpinModul;
extern tCanvasWidget	g_sSMCheckBoxPanel;
extern tCanvasWidget	g_sSMControlPanel;

extern tImageButtonWidget	g_psSpinModPushButt[];
extern tCanvasWidget		g_psSpinModIndicators[];

extern tImageButtonWidget	g_sSMPrevious;
extern tImageButtonWidget	g_sSMNext;
extern tImageButtonWidget	g_sSMSave;
extern tImageButtonWidget	g_sSMClose;


// ****************** menu SCREEN pre Spinaci modul ***************************************

Canvas(g_sMenuSpinModul, WIDGET_ROOT, 0, &g_sMenuSMSet, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 240, (/*CANVAS_STYLE_APP_DRAWN | */CANVAS_STYLE_FILL),
		ClrBlack, 0, 0, 0, 0, 0, 0);

Container(g_sMenuSMSet, &g_sMenuSpinModul, &g_sMenuSMConfig, &g_psMenuSpinModSetPushButt, &g_sKitronix320x240x16_SSD2119,
			0, 0, 159, 182, (CTR_STYLE_OUTLINE/* | CTR_STYLE_FILL*/ | CTR_STYLE_TEXT), ClrMidnightBlue,ClrLime /*ClrGray*/, ClrLime/*ClrSilver*/,
			&g_sFontCmss22, "Set");
Container(g_sMenuSMConfig, &g_sMenuSpinModul, &g_sMenuSMBack, &g_psMenuSpinModConfigPushButt, &g_sKitronix320x240x16_SSD2119,
			161, 0, 159, 182, (CTR_STYLE_OUTLINE/* | CTR_STYLE_FILL*/ | CTR_STYLE_TEXT), ClrMidnightBlue, ClrLime /*ClrGray*/, ClrLime/*ClrSilver*/,
			&g_sFontCmss22, "Config");

ImageButton(g_sMenuSMBack, &g_sMenuSpinModul, 0, 0,
			&g_sKitronix320x240x16_SSD2119, 180, 190, 120, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "Back", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
			0, 0, SM_OnBackButtonPress);
	
#define	YSMMENU_BUTT_SHIFT			10			
#define	XSMMENU_BUTT_SPACE			20
#define	XSMCONFIGMENU_BUTT_SPACE	180
#define	YSMMENU_BUTT_SPACE			15
#define	XSMMENU_BUTT_WIDTH			120
#define	YSMMENU_BUTT_HEIGHT			40

tImageButtonWidget g_psMenuSpinModSetPushButt[] =
{	
	ImageButtonStruct(&g_sMenuSMSet, g_psMenuSpinModSetPushButt + 1, 0,
				&g_sKitronix320x240x16_SSD2119, XSMMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE, XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Settings 1", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnSetButtonPress),
	ImageButtonStruct(&g_sMenuSMSet, g_psMenuSpinModSetPushButt + 2, 0,
				&g_sKitronix320x240x16_SSD2119, XSMMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE + (YSMMENU_BUTT_SPACE + YSMMENU_BUTT_HEIGHT), XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Settings 2", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnSetButtonPress),
	ImageButtonStruct(&g_sMenuSMSet, g_psMenuSpinModSetPushButt + 3, 0,
				&g_sKitronix320x240x16_SSD2119, XSMMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE + 2*(YSMMENU_BUTT_SPACE + YSMMENU_BUTT_HEIGHT), XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Settings 3", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnSetButtonPress),
	ImageButtonStruct(&g_sMenuSMSet, 0, 0,
				&g_sKitronix320x240x16_SSD2119, XSMMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE + 3*(YSMMENU_BUTT_SPACE + YSMMENU_BUTT_HEIGHT), XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Manual", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnSetButtonPress),
};

tImageButtonWidget g_psMenuSpinModConfigPushButt[] =
{
	ImageButtonStruct(&g_sMenuSMConfig, g_psMenuSpinModConfigPushButt + 1, 0,
				&g_sKitronix320x240x16_SSD2119, XSMCONFIGMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE, XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Settings 1", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnConfigButtonPress),	
	ImageButtonStruct(&g_sMenuSMConfig, g_psMenuSpinModConfigPushButt + 2, 0,
				&g_sKitronix320x240x16_SSD2119, XSMCONFIGMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE + (YSMMENU_BUTT_SPACE + YSMMENU_BUTT_HEIGHT), XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Settings 2", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnConfigButtonPress),
	ImageButtonStruct(&g_sMenuSMConfig, 0, 0,
				&g_sKitronix320x240x16_SSD2119, XSMCONFIGMENU_BUTT_SPACE, YSMMENU_BUTT_SHIFT + YSMMENU_BUTT_SPACE + 2*(YSMMENU_BUTT_SPACE + YSMMENU_BUTT_HEIGHT), XSMMENU_BUTT_WIDTH, YSMMENU_BUTT_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				&g_sFontCmss20b, "Settings 3", g_pucButtonBlue120x40, g_pucButtonBlue120x40Press, 0, 0, 0,
				0, 0, SM_OnConfigButtonPress),
};


// ****************** SCREEN pre Spinaci modul ***************************************

Canvas(g_sSpinModul, WIDGET_ROOT, 0, &g_sSMCheckBoxPanel, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 240, (CANVAS_STYLE_APP_DRAWN | CANVAS_STYLE_FILL),
		ClrBlack, 0, 0, 0, 0, 0, OnSpinModulPaint);
		
Canvas(g_sSMCheckBoxPanel, &g_sSpinModul, &g_sSMControlPanel, g_psSpinModPushButt, &g_sKitronix320x240x16_SSD2119, 0, 0,
		320, 190, 0/*CANVAS_STYLE_OUTLINE*/,
		0, ClrWhite, 0, 0, 0, 0, 0);
		
Canvas(g_sSMControlPanel, &g_sSpinModul, 0, &g_sSMPrevious, &g_sKitronix320x240x16_SSD2119, 0, 188,
		320, 53, 0,
		ClrDimGray, ClrWhite, 0, 0, 0, 0, 0);

ImageButton(g_sSMPrevious, &g_sSMControlPanel, &g_sSMNext, 0,
			&g_sKitronix320x240x16_SSD2119, 5, 195, 40, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			0, 0, g_pucButtonBlueLeft40x40, g_pucButtonBlueLeft40x40Press, 0, 0, 0,
			0, 0, SM_OnPreviousPress);

ImageButton(g_sSMNext, &g_sSMControlPanel, &g_sSMSave, 0,
			&g_sKitronix320x240x16_SSD2119, 275, 195, 40, 40,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			0, 0, g_pucButtonBlueRight40x40, g_pucButtonBlueRight40x40Press, 0, 0, 0,
			0, 0, SM_OnNextPress);

ImageButton(g_sSMSave, &g_sSMControlPanel, &g_sSMClose, 0,
			&g_sKitronix320x240x16_SSD2119, 60, 197, 90, 35,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "Save", g_pucButtonBlue90x35, g_pucButtonBlue90x35Press, 0, 0, 0,
			0, 0, SM_OnSavePress);

ImageButton(g_sSMClose, &g_sSMControlPanel, 0, 0,
			&g_sKitronix320x240x16_SSD2119, 170, 197, 90, 35,
			(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
			&g_sFontCmss20b, "Close", g_pucButtonBlue90x35, g_pucButtonBlue90x35Press, 0, 0, 0,
			0, 0, SM_OnClosePress);


#define	SM_TLAC_1ODSKOK		0
#define	SM_TLAC_WIDTH		110
#define	SM_TLAC_HEIGHT		35
#define	SM_TLAC_SPACE_X	SM_INDICATORSX + 10 + 20

#define	SM_TLAC_SPACE_X_2S	160 + SM_INDICATORSX + 10 + 20
#define	SM_TLAC_SPACE_Y		10
#define	SM_INDICATORSX	10
#define	SM_INDICATORS_X_2S	170

char *g_pSMCheckBoxNames[] =
{
    "PC 1",		"PC 2",		"PC 3",		"PC 4",		"Monit. 1",	"Monit. 2",	"Monit. 3",	"Monit. 4",

    "DECK",		"OUT 2",	"OUT 3",	"OUT 4",	"OUT 5",	"OUT 6",	"OUT 7",	"OUT 8",

	"OUT 9",	"OUT 10",	"OUT 11",	"OUT 12",	"OUT 13",	"OUT 14",	"OUT 15",	"OUT 16"
};

tImageButtonWidget g_psSpinModPushButt[] =
{	
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 1, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y, SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),	
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 2, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT), SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 3, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+2*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT), SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 4, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+3*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT), SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),

	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 5, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X_2S, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y, SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 6, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X_2S, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT), SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModPushButt + 7, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X_2S, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+2*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT), SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),
	ImageButtonStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators, 0,
				&g_sKitronix320x240x16_SSD2119, SM_TLAC_SPACE_X_2S, SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+3*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT), SM_TLAC_WIDTH, SM_TLAC_HEIGHT,
				(IB_STYLE_KEYCAP_OFF | IB_STYLE_TEXT | IB_STYLE_RELEASE_NOTIFY), ClrWhite, 0, 0,
				 &g_sFontCmss20b, 0, g_pucButtonBlue110x35, g_pucButtonBlue110x35Press, 0, 0, 0,
				0, 0, SM_OnCheckBoxPress),
 };

tCanvasWidget g_psSpinModIndicators[] =
{            
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 1, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORSX, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y,
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 2, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORSX, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT),
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 3, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORSX, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+2*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT),
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 4, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORSX, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+3*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT),
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
			
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 5, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORS_X_2S, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y,
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 6, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORS_X_2S, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT),
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
	CanvasStruct(&g_sSMCheckBoxPanel, g_psSpinModIndicators + 7, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORS_X_2S, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+2*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT),
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0),
	CanvasStruct(&g_sSMCheckBoxPanel, 0, 0, &g_sKitronix320x240x16_SSD2119, SM_INDICATORS_X_2S, 7 + SM_TLAC_1ODSKOK + SM_TLAC_SPACE_Y+3*(SM_TLAC_SPACE_Y+SM_TLAC_HEIGHT),
			20, 20,  CANVAS_STYLE_IMG, ClrBlue, ClrWhite, 0, 0, 0, g_pucLightOff, 0)
};
// ****************** Funkcie z menu SCREEN pre Spinaci modul ***************************************

void SetMenuSpinModulScrean(void)
{
		WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sMenuSpinModul);
}

void RemoveMenuSpinModulScrean(void)
{
	WidgetRemove((tWidget *)&g_sMenuSpinModul );
}

void	SetSMCheckBoxStatus( unsigned char *data )
{
	memcpy( SM_CheckboxStatus, data, 3*4 );	//ulozim nastavenia SM_CheckboxStatus
}

void	SM_OnBackButtonPress(tWidget *pWidget)
{
	RemoveMenuSpinModulScrean(); 
	SetMenuScreen();
	WidgetPaint(WIDGET_ROOT);
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}

void	SM_OnSetButtonPress(tWidget *pWidget)
{
	unsigned char index;
	for(index = 0; index < 4; index++)		// zistujem ktore tlacitko bolo stlacene
	{	
		if(pWidget == (tWidget *)(g_psMenuSpinModSetPushButt + index))
		{
			break;
		}   	
	}
	if( index == 4)return;
	
	if( index == 3 )		// manualne nastavenie vystupov
	{
		SM_SettingsIndex= 0;
		RemoveMenuSpinModulScrean(); 
		SetSMscrean();
		ImageButtonTextSet( (tPushButtonWidget *)&g_sSMSave, "Set" );
		WidgetPaint(WIDGET_ROOT);
	}
	else		// odoslem packet na nastavenie vysrupov z EEPROM
	{
		t_MainQueueStruct	Mesage;
		Mesage.Mesage_ID= C_QUEUE_SM_SET;	// uloz nastavenia alebo aplikuj aktualne
		Mesage.Data[0]= index + 1;
		sendToMainQueue( &Mesage, 0 );
		RemoveMenuSpinModulScrean();
		//ShowMesageWindow("Sending request", 1);
		SetMenuScreen();
		WidgetPaint(WIDGET_ROOT);
	}
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}

void	SM_OnConfigButtonPress(tWidget *pWidget)
{
	unsigned char index;
	for(index = 0; index < 3; index++)		// zistujem ktore tlacitko bolo stlacene
	{	
		if(pWidget == (tWidget *)(g_psMenuSpinModConfigPushButt + index))
		{
			break;
		}   	
	}
	if( index == 3)return;
	
	ImageButtonTextSet( (tPushButtonWidget *)&g_sSMSave, "Save" );

	// spustim nastavovaci screan SM s indexom o ktru bunku sa jedna 
	SM_SettingsIndex= index + 1;
	RemoveMenuSpinModulScrean(); 
	SetSMscrean();
	WidgetPaint(WIDGET_ROOT);
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}
// ****************** Funkcie z SCREEN pre Spinaci modul ***************************************
void SetSMscrean(void)
{
	SM_ScreanIndex= 0;
	WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sSpinModul);
	UpdateSM_Screan(false);
	UpdateSM_CheckBox(false);
	
	ImageButtonImageSet( &g_sSMPrevious, g_pucButtonBlack40x40 );
	ImageButtonImagePressedSet( &g_sSMPrevious, g_pucButtonBlack40x40 );

	
	ImageButtonImageSet( &g_sSMNext, g_pucButtonBlueRight40x40 );
	ImageButtonImagePressedSet( &g_sSMNext, g_pucButtonBlueRight40x40Press );
	
}

void RemoveSMscrean(void)
{
	WidgetRemove((tWidget *)&g_sSpinModul );
}

void UpdateSM_Screan( unsigned char redraw )
{
	unsigned char i;
	for(i= 0; i < 8; i++)
	{
		ImageButtonTextSet( (tPushButtonWidget *)(g_psSpinModPushButt + i), g_pSMCheckBoxNames[ ((8*SM_ScreanIndex) + i) ] );
		if( redraw > 0 ) WidgetPaint((tWidget *)(g_psSpinModPushButt + i)); 
	}	
}
void	UpdateSM_CheckBox( unsigned char redraw )
{
	unsigned char index;
	for(index = 0; index < 8; index++)		// zistujem ktore tlacitko bolo stlacene
	{
		CanvasImageSet( (tCanvasWidget *)(g_psSpinModIndicators + index), (SM_CheckboxStatus[SM_SettingsIndex][SM_ScreanIndex] & (1 << index)) ? g_pucLightOn : g_pucLightOff );               
		if( redraw > 0 ) WidgetPaint( (tWidget *)(g_psSpinModIndicators + index) );
	}
}
unsigned char	UpdateSM_Deck( void )
{
	SM_CheckboxStatus[0][1] ^= (1 << 0);
	
	t_MainQueueStruct	Mesage;		// odoslem nastaveie
	Mesage.Mesage_ID= C_QUEUE_SM_CONFIG;	// uloz nastavenia alebo aplikuj aktualne
	Mesage.Data[0]= SM_SettingsIndex;
	memcpy( (Mesage.Data + 1), SM_CheckboxStatus[SM_SettingsIndex], 3 );
	sendToMainQueue( &Mesage, 0 );
	return(SM_CheckboxStatus[0][1] & (1 << 0));
}
unsigned char	StateSM_Deck( void )
{
	return(SM_CheckboxStatus[0][1] & (1 << 0));	
}

void OnSpinModulPaint(tWidget *pWidget, tContext *pContext)
{
	GrContextForegroundSet(&sContext, ClrDimGray);
	//GrLineDraw(&sContext,160, 0, 160, 240);
	GrLineDraw(&sContext,0, 187, 320, 187);
	GrLineDraw(&sContext,0, 188, 320, 188);
}

void SM_OnCheckBoxPress(tWidget *pWidget)
{
	unsigned char index;
	for(index = 0; index < 8; index++)		// zistujem ktore tlacitko bolo stlacene
	{	
		if(pWidget == (tWidget *)(g_psSpinModPushButt + index))
		{
			break;
		}   	
	}
	if( index == 8)return;
	
	SM_CheckboxStatus[SM_SettingsIndex][SM_ScreanIndex] ^= (1 << index);	// negujem stav

	CanvasImageSet( (tCanvasWidget *)(g_psSpinModIndicators + index), (SM_CheckboxStatus[SM_SettingsIndex][SM_ScreanIndex] & (1 << index)) ? g_pucLightOn : g_pucLightOff );
                   
	WidgetPaint( (tWidget *)(g_psSpinModIndicators + index) );
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}

void SM_OnNextPress(tWidget *pWidget)
{
	SM_ScreanIndex++;
	if( SM_ScreanIndex > MAX_ScreanIndex)
	{
		SM_ScreanIndex= MAX_ScreanIndex;
		return;
	}
	if( SM_ScreanIndex == 1)
	{
		ImageButtonImageSet( &g_sSMPrevious, g_pucButtonBlueLeft40x40 );
		ImageButtonImagePressedSet( &g_sSMPrevious, g_pucButtonBlueLeft40x40Press );
		WidgetPaint((tWidget *)&g_sSMPrevious); 
	}
	if( SM_ScreanIndex == MAX_ScreanIndex)
	{
		ImageButtonImageSet( &g_sSMNext, g_pucButtonBlack40x40 );
		ImageButtonImagePressedSet( &g_sSMNext, g_pucButtonBlack40x40 );
		WidgetPaint((tWidget *)&g_sSMNext); 
	}
	UpdateSM_Screan(true);
	UpdateSM_CheckBox(true);
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}

void SM_OnPreviousPress(tWidget *pWidget)
{
	SM_ScreanIndex--;
	if( SM_ScreanIndex > MAX_ScreanIndex)
	{
		SM_ScreanIndex= 0;
		return;
	}
	if( SM_ScreanIndex == 0)
	{
		ImageButtonImageSet( &g_sSMPrevious, g_pucButtonBlack40x40 );
		ImageButtonImagePressedSet( &g_sSMPrevious, g_pucButtonBlack40x40 );	
		WidgetPaint((tWidget *)&g_sSMPrevious); 
	}
	if( SM_ScreanIndex == (MAX_ScreanIndex - 1) )
	{
		ImageButtonImageSet( &g_sSMNext, g_pucButtonBlueRight40x40 );
		ImageButtonImagePressedSet( &g_sSMNext, g_pucButtonBlueRight40x40Press );
		WidgetPaint((tWidget *)&g_sSMNext); 
	}	

	UpdateSM_Screan(true);
	UpdateSM_CheckBox(true);
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}

void SM_OnSavePress(tWidget *pWidget)
{
	t_MainQueueStruct	Mesage;
	Mesage.Mesage_ID= C_QUEUE_SM_CONFIG;	// uloz nastavenia alebo aplikuj aktualne
	Mesage.Data[0]= SM_SettingsIndex;
	memcpy( (Mesage.Data + 1), SM_CheckboxStatus[SM_SettingsIndex], 3 );
	sendToMainQueue( &Mesage, 0 );
	RemoveSMscrean();
	//ShowMesageWindow("Sending request", 1);
	SetMenuScreen();
	WidgetPaint(WIDGET_ROOT);
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}

void SM_OnClosePress(tWidget *pWidget)
{
	RemoveSMscrean(); 
	SM_ScreanIndex= 0;
	SetMenuScreen();
	// zapnem next button ak by bolo vypnute
	WidgetPaint(WIDGET_ROOT);
	SoundPlayKeyClick();
	ResetLogoScreenTimer();
}
// *********************************************************************************

