#include <rs232.h>
#include <utility.h>
#include "toolbar.h"
#include <formatio.h>
#include <ansi_c.h>
#include "asynctmr.h"
#include <cvirte.h>
#include <userint.h>
#include "vezerlo.h"
#include "IIC.h"
//Prototypes
int SyncronizeLedsStates (int, int, int,void *, int, int);
void UpdateLineState(int,int, unsigned char,unsigned char,unsigned char, unsigned char);
void ReadCurrLine (int *, unsigned char *, unsigned char *, unsigned char *, unsigned char *);
void UdateAllScreen (int);
void UpdateLeds (void);
void CalcTime (void);
int WriteEEPORMpage (int, int, int, int);
static int panelHandle,panel2Handle;
static unsigned char databuffer[131144];
static int playline = 1;
static int defaultdwelltime = 0;
static int portopened = 0;
static int showpanel = 0;
static int senddata = 0;
static int destaddr = 0,sourceaddr = 0,errors = 0;
static int memorysize = 0;
static int fillff = 0;
//main function
int main (int argc, char *argv[])
{
int temp,timerID,filehandle;
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "vezerlo.uir", PANEL)) < 0)
return -1;
//Toolbar_Display (Menu);
timerID = NewAsyncTimer(0.15,-1,1,SyncronizeLedsStates,&temp);
DisplayPanel (panelHandle);
RunUserInterface ();
if(timerID >= 0 ) DiscardAsyncTimer (timerID);
DiscardPanel (panelHandle);
if (showpanel )DiscardPanel (panel2Handle);
return 0;
}
//close panel function
int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
switch (event)
{
case EVENT_CLOSE:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK OpenFiler (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char fileName[MAX_PATHNAME_LEN];
int retVal=0;
switch (event) {
case EVENT_COMMIT:
if (1)
{
GetProjectDir (fileName);
if (FileSelectPopup (fileName, "*.vez", "*.vez",
"Open file...", VAL_LOAD_BUTTON,
0, 1, 1, 0, fileName)>0)
{
SetWaitCursor (1);
retVal = OpenFile(fileName,VAL_READ_ONLY,VAL_TRUNCATE,VAL_BINARY);
}
if (retVal < 0) MessagePopup("Warning","Can't open file!");
if (retVal > 0)
{
ReadFile(retVal,databuffer,32768);
CloseFile(retVal);
UdateAllScreen(1);
}
SetWaitCursor (0);
}
break;
}
return 0;
}
//----------------------------------------------------------------------------
// SaveFile
//----------------------------------------------------------------------------
int CVICALLBACK SaveFile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
HRESULT error = 0;
int retVal = 0;
int i;
char fileName[MAX_PATHNAME_LEN];
switch (event) {
case EVENT_COMMIT:
if (1)
{
GetProjectDir (fileName);
if (FileSelectPopup (fileName, "*.vez", "*.vez",
"Save file as...", VAL_SAVE_BUTTON,
0, 1, 1, 1, fileName)>0)
{
SetWaitCursor (1);
retVal = OpenFile(fileName,VAL_WRITE_ONLY,VAL_TRUNCATE,VAL_BINARY);
}
if (retVal < 0) MessagePopup("Warning","Can't save file!");
if (retVal > 0)
{
WriteFile(retVal,databuffer,32768);
CloseFile(retVal);
}
SetWaitCursor (0);
}
break;
}
return 0;
}
//save line function
int CVICALLBACK SaveLine (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
unsigned char data1, data2, data3, data4;
int line;
switch (event)
{
case EVENT_COMMIT:
ReadCurrLine(&line, &data1, &data2, &data3, &data4);
line = (line - 1)*4;
databuffer [line] = data1;
databuffer [line + 1] = data2;
databuffer [line + 2] = data3;
databuffer [line + 3] = data4;
break;
}
return 0;
}
//***************************************************************
//Syncronize LED timer
int CVICALLBACK SyncronizeLedsStates (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int error,buffersize;
switch (event)
{
case EVENT_TIMER_TICK: UpdateLeds();
if (portopened ==0) senddata = 0;
if ((senddata) & (portopened > 0))
{
if (memorysize < 128) buffersize = 32;
else buffersize = 64;
// buffersize = 16; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111
// memorysize = 16;
error = WriteEEPORMpage(buffersize,destaddr,sourceaddr,portopened);
if (error) errors ++;
else
{
destaddr = destaddr +buffersize;
sourceaddr = sourceaddr +buffersize;
}
if (errors > 5)
{
senddata = 0;
errors = 0;
MessagePopup("Warning","Transfer failed !");
}
if (destaddr >= (memorysize*128)) senddata = 0;
SetCtrlAttribute(panel2Handle,Downloader_Downloaded,ATTR_CTRL_VAL,(100*destaddr/(memorysize*128)));
}
if (senddata == 0) fillff = 0;
break;
}
return 0;
}
//Syncronize LEDs
void UpdateLeds (void)
{
int temp,i,j,address[3], destaddress[3];
address[0] = 169, destaddress[0] = 17;
address[1] = 113, destaddress[1] = 9;
address[2] = 57, destaddress[2] = 1;
for(j=0;j<3;j++)
{
for(i=0;i<8;i++)
{
address[j] ++;
destaddress[j] ++;
GetCtrlAttribute(panelHandle,address[j],ATTR_CTRL_VAL,&temp);
SetCtrlAttribute(panelHandle,destaddress[j],ATTR_CTRL_VAL,temp);
}
}
}
//Update line state function
void UpdateLineState(int line,int linedata, unsigned char data1,unsigned char data2,
unsigned char data3, unsigned char data4)
{
int temp,i,j,address[3],datas[3];
datas[0] = data1;
datas[1] = data2;
datas[2] = data3;
switch(line)
{
case 1: address[0] = 193; address[1] = 137; address[2] = 81; break;
case 2: address[0] = 185; address[1] = 129; address[2] = 73; break;
case 3: address[0] = 177; address[1] = 121; address[2] = 65; break;
case 4: address[0] = 169; address[1] = 113; address[2] = 57; break;
case 5: address[0] = 161; address[1] = 105; address[2] = 49; break;
case 6: address[0] = 153; address[1] = 97; address[2] = 41; break;
case 7: address[0] = 145; address[1] = 89; address[2] = 33; break;
default: break;
}
for (j=0;j<3;j++)
{
for(i=0;i<8;i++)
{
temp = datas[j] & 0x01;
datas[j] = datas[j] >> 1;
address[j] ++;
SetCtrlAttribute(panelHandle,address[j],ATTR_CTRL_VAL,temp);
}
}
switch (line)
{
case 1 : SetCtrlAttribute(panelHandle,PANEL_DwellTime_3, ATTR_CTRL_VAL,data4);
SetCtrlAttribute(panelHandle,PANEL_Currline_3, ATTR_CTRL_VAL,linedata); break;
case 2 : SetCtrlAttribute(panelHandle,PANEL_DwellTime_2, ATTR_CTRL_VAL,data4);
SetCtrlAttribute(panelHandle,PANEL_Currline_2, ATTR_CTRL_VAL,linedata); break;
case 3 : SetCtrlAttribute(panelHandle,PANEL_DwellTime_1, ATTR_CTRL_VAL,data4);
SetCtrlAttribute(panelHandle,PANEL_Currline_1, ATTR_CTRL_VAL,linedata); break;
case 4 : SetCtrlAttribute(panelHandle,PANEL_DwellTime, A