#include "hmiworks.h"

int _CurrentFrame = 0;

char *g_FrameNames[] = 
{
  "Frame1",
  "Frame2",
  "Frame3",
  "Frame4",
};

const int g_FrameCount = (sizeof(g_FrameNames)/sizeof(char *));


HANDLE UART;
HANDLE I_7065_1;
HANDLE I_7058_5;
HANDLE I_7065_2;
HANDLE I_7065_3;

void CheckModbus()
{

}
int g_TimerTicksCount;
void hmi_TimerTrigger()
{
  g_TimerTicksCount++;
  if (table_init_flag && (g_TimerTicksCount%(100/10)) == 0) AddQueueMessage(0);
  
  if ((g_TimerTicksCount%50) == 0) CheckModbus();
  
switch(_CurrentFrame) {
 case 1: {
break;
 }
 case 2: {
break;
 }
 case 3: {
break;
 }
 case 4: {
break;
 }
}
}


void FreeCurrentFrame()
{
     switch (_CurrentFrame)
     {
	case 1: FreeWidget1(); break;
	case 2: FreeWidget2(); break;
	case 3: FreeWidget3(); break;
	case 4: FreeWidget4(); break;
     }
}


void hmi_GotoFrame(int nFrame)
{
    switch(nFrame)
    {
	case 1: _frame1(); break;
	case 2: _frame2(); break;
	case 3: _frame3(); break;
	case 4: _frame4(); break;
    }

}


int hmi_IndexOfFrame(const char *frame_name)
{
    int i;
    for (i=0; i<g_FrameCount; i++)
       if (strcmp(frame_name, g_FrameNames[i]) == 0) return i+1;
    return 0;
}


void hmi_GotoFrameByName(const char *frame_name)
{
    hmi_GotoFrame(hmi_IndexOfFrame(frame_name));
}

BYTE ladder_main(BYTE);
static BYTE r_out(BYTE);

long default_timer_proc(tWidget *pWidget, unsigned long ulMessage,
                     unsigned long ulParam1, unsigned long ulParam2)

{
  ladder_main(1);
  r_out(1);
  return 1;
}

static BYTE r_out(BYTE en)
{
  default_output(1);



static int polling_module;
switch(polling_module++%4) {
case 0:
BEGIN_FUNCTION_BLOCK();

DWORD v_do = 0;
DWORD v_di = 0;

if (VAR_VALUE(I_7065_1_W_ACTION)) 
{
	if(VAR_VALUE(I_7065_1_DO0)) v_do |= (1<<0);
	if(VAR_VALUE(I_7065_1_DO1)) v_do |= (1<<1);
	if(VAR_VALUE(I_7065_1_DO2)) v_do |= (1<<2);
	if(VAR_VALUE(I_7065_1_DO3)) v_do |= (1<<3);
	if(VAR_VALUE(I_7065_1_DO4)) v_do |= (1<<4);
	dcon_WriteDO(I_7065_1, 1, 5, v_do & 0xFF);
}

if (VAR_VALUE(I_7065_1_R_ACTION)) 
if (dcon_ReadDIO(I_7065_1, 1, 4, 5, &v_di, &v_do))
{
	VAR_SET(I_7065_1_DI0, v_di & (1<<0));
	VAR_SET(I_7065_1_DI1, v_di & (1<<1));
	VAR_SET(I_7065_1_DI2, v_di & (1<<2));
	VAR_SET(I_7065_1_DI3, v_di & (1<<3));

	VAR_SET(I_7065_1_DO0, v_do & (1<<0));
	VAR_SET(I_7065_1_DO1, v_do & (1<<1));
	VAR_SET(I_7065_1_DO2, v_do & (1<<2));
	VAR_SET(I_7065_1_DO3, v_do & (1<<3));
	VAR_SET(I_7065_1_DO4, v_do & (1<<4));
}


END_FUNCTION_BLOCK();

break;

case 1:
BEGIN_FUNCTION_BLOCK();

DWORD v_di = 0;

if (VAR_VALUE(I_7058_5_R_ACTION)) 
if (dcon_ReadDI(I_7058_5, 5, 8, &v_di))
{
	VAR_SET(I_7058_5_DI0, v_di & (1<<0));
	VAR_SET(I_7058_5_DI1, v_di & (1<<1));
	VAR_SET(I_7058_5_DI2, v_di & (1<<2));
	VAR_SET(I_7058_5_DI3, v_di & (1<<3));
	VAR_SET(I_7058_5_DI4, v_di & (1<<4));
	VAR_SET(I_7058_5_DI5, v_di & (1<<5));
	VAR_SET(I_7058_5_DI6, v_di & (1<<6));
	VAR_SET(I_7058_5_DI7, v_di & (1<<7));
}

END_FUNCTION_BLOCK();

break;

case 2:
BEGIN_FUNCTION_BLOCK();

DWORD v_do = 0;
DWORD v_di = 0;

if (VAR_VALUE(I_7065_2_W_ACTION)) 
{
	if(VAR_VALUE(I_7065_2_DO0)) v_do |= (1<<0);
	if(VAR_VALUE(I_7065_2_DO1)) v_do |= (1<<1);
	if(VAR_VALUE(I_7065_2_DO2)) v_do |= (1<<2);
	if(VAR_VALUE(I_7065_2_DO3)) v_do |= (1<<3);
	if(VAR_VALUE(I_7065_2_DO4)) v_do |= (1<<4);
	dcon_WriteDO(I_7065_2, 2, 5, v_do & 0xFF);
}

if (VAR_VALUE(I_7065_2_R_ACTION)) 
if (dcon_ReadDIO(I_7065_2, 2, 4, 5, &v_di, &v_do))
{
	VAR_SET(I_7065_2_DI0, v_di & (1<<0));
	VAR_SET(I_7065_2_DI1, v_di & (1<<1));
	VAR_SET(I_7065_2_DI2, v_di & (1<<2));
	VAR_SET(I_7065_2_DI3, v_di & (1<<3));

	VAR_SET(I_7065_2_DO0, v_do & (1<<0));
	VAR_SET(I_7065_2_DO1, v_do & (1<<1));
	VAR_SET(I_7065_2_DO2, v_do & (1<<2));
	VAR_SET(I_7065_2_DO3, v_do & (1<<3));
	VAR_SET(I_7065_2_DO4, v_do & (1<<4));
}


END_FUNCTION_BLOCK();

break;

case 3:
BEGIN_FUNCTION_BLOCK();

DWORD v_do = 0;
DWORD v_di = 0;

if (VAR_VALUE(I_7065_3_W_ACTION)) 
{
	if(VAR_VALUE(I_7065_3_DO0)) v_do |= (1<<0);
	if(VAR_VALUE(I_7065_3_DO1)) v_do |= (1<<1);
	if(VAR_VALUE(I_7065_3_DO2)) v_do |= (1<<2);
	if(VAR_VALUE(I_7065_3_DO3)) v_do |= (1<<3);
	if(VAR_VALUE(I_7065_3_DO4)) v_do |= (1<<4);
	dcon_WriteDO(I_7065_3, 3, 5, v_do & 0xFF);
}

if (VAR_VALUE(I_7065_3_R_ACTION)) 
if (dcon_ReadDIO(I_7065_3, 3, 4, 5, &v_di, &v_do))
{
	VAR_SET(I_7065_3_DI0, v_di & (1<<0));
	VAR_SET(I_7065_3_DI1, v_di & (1<<1));
	VAR_SET(I_7065_3_DI2, v_di & (1<<2));
	VAR_SET(I_7065_3_DI3, v_di & (1<<3));

	VAR_SET(I_7065_3_DO0, v_do & (1<<0));
	VAR_SET(I_7065_3_DO1, v_do & (1<<1));
	VAR_SET(I_7065_3_DO2, v_do & (1<<2));
	VAR_SET(I_7065_3_DO3, v_do & (1<<3));
	VAR_SET(I_7065_3_DO4, v_do & (1<<4));
}


END_FUNCTION_BLOCK();

break;

} //end switch

  default_input(1);

return 0;
}

void _frame0()
{
  tContext sContext;
  
  hmi_Beep();
  UART = uart_Open("COM0,115200,N,8,1");
  I_7065_1 = UART;
  I_7058_5 = UART;
  I_7065_2 = UART;
  I_7065_3 = UART;
  hmi_Beep();
  
  
  RegisterNullWidgetMsgProc(default_timer_proc);
  
  GrContextInit(&sContext, HMI_DISPLAY_DRIVER_PTR);
  hmi_GotoFrame(1);
  GrFlush(&sContext);
}
