#include "MPOD.h"
#include <utility.h>
#include "HVtest_ui.h"
#define HVSLOT 3
#define NTICSMAX 240
static int p1h, pID, rID; //, tfID;
int CVICALLBACK refreshTimer (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2) {
QueueUserEvent (9000, p1h, P1_REFRESHTIMER);
return (0);
}
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow) {
const int mo=HVSLOT;
const int ch0=100*mo;
int mainonoff,HVonoffall;
int HVonoff[4]={0,0,0,0};
int HVonoff_rID[4]={P1_HVonoff_1,P1_HVonoff_2,P1_HVonoff_3,P1_HVonoff_4};
int HVvdis_rID[4]={P1_HVvdis_1,P1_HVvdis_2,P1_HVvdis_3,P1_HVvdis_4};
int HVcdis_rID[4]={P1_HVcdis_1,P1_HVcdis_2,P1_HVcdis_3,P1_HVcdis_4};
int iret;
int i;
int TESTon=0,ntics=0;
double HVvset[4],HVcset[4],HVvrise; //,HVvfall;
double HVvdis[4],HVcdis[4];
// char cret[2000];
char cMainSwitch[2][30]= {"Turn system OFF?","Turn system ON?"};
char HVBid[4][4];
int HVBid_rID[4]={P1_HVBid_1,P1_HVBid_2,P1_HVBid_3,P1_HVBid_4};
char HVBfname[4][200];
FILE *HVBfp[4]={NULL,NULL,NULL,NULL};
FILE *LIDfp=NULL;
HSNMP crate1;
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
MPOD_Start();
// MPOD_Open(0,"f9mpod.ijs.si");
MPOD_Open(0,"arich-mpod1.kek.jp");
crate1 = crateHsnmp[0];
// iret=MPOD_GetInt(0,"moduleNumber.0");
// printf("Module Number = %i\n", iret);
// strcpy(cret, MPOD_GetString(0,"moduleDescription.ma2"));
// printf("Module type: %s\n",cret);
// iret=MPOD_GetIntCh(0,"fanNominalSpeed",0);
// printf("Fan nominal speed = %i\n", iret);
if ((p1h = LoadPanel (0, "HVtest_ui.uir", P1)) < 0) return -1;
DisplayPanel (p1h);
SetCtrlVal (p1h, P1_TESTon, 0);
LIDfp
=fopen("lastids.txt","r");
for (i=0;i<4;i++) {
SetCtrlVal(p1h,HVBid_rID[i],HVBid[i]);
}
LIDfp=NULL;
//Initial state
mainonoff=getMainSwitch(crate1);
SetCtrlVal (p1h, P1_MSW, mainonoff);
SetCtrlVal (p1h, P1_MSWLED, mainonoff);
iret=getMainStatus(crate1);
SetCtrlVal (p1h, P1_MST, iret);
iret=getFanNominalSpeed(crate1);
SetCtrlVal (p1h, P1_MFS, iret);
if (mainonoff) {
HVonoffall=0;
for (i=0;i<4;i++) {
HVonoff[i]=getChannelSwitch(crate1, ch0+i);
if (HVonoff[i]) HVonoffall=1;
}
for (i=0;i<4;i++) {
if (!HVonoffall) HVonoff[i]=1;
SetCtrlVal (p1h, HVonoff_rID[i], HVonoff[i]);
}
HVvset[0]=getOutputVoltage(crate1,ch0);
SetCtrlVal (p1h, P1_HVvset, HVvset[0]);
HVcset[0]=1e+6*getOutputCurrent(crate1,ch0);
SetCtrlVal (p1h, P1_HVcset, HVcset[0]);
SetAxisScalingMode (p1h, P1_G1, VAL_LEFT_YAXIS, VAL_MANUAL, -(HVcset[0]+1), 1.);
HVvrise=getModuleRampSpeedVoltage(crate1,mo);
SetCtrlVal (p1h, P1_HVvrise, HVvrise);
// vfall[0]=abs(getOutputFallRate(crate1,ch));
// SetCtrlVal (p1h, P1_VFA0, vfall[0]);
}
// QueueUserEvent (1000, p1h, P1_RESET);
QueueUserEvent (9000, p1h, P1_REFRESHTIMER);
do {
GetUserEvent (1, &pID, &rID);
switch (rID) {
case P1_REFRESHTIMER:
mainonoff=getMainSwitch(crate1);
SetCtrlVal (p1h, P1_MSW, mainonoff);
SetCtrlVal (p1h, P1_MSWLED, mainonoff);
if (mainonoff) {
iret=getMainStatus(crate1);
SetCtrlVal (p1h, P1_MST, iret);
// ret=getOutputSenseMeasurement(crate1, ch);
for (i=0; i<4; i++) {
HVvdis[i]=getOutputTerminalMeasurement(crate1, ch0+i);
SetCtrlVal (p1h, HVvdis_rID[i], HVvdis[i]);
HVcdis[i]=1e+6*getCurrentMeasurement(crate1, ch0+i);
SetCtrlVal (p1h, HVcdis_rID[i], HVcdis[i]);
}
PlotStripChart (p1h, P1_G1, HVcdis, 4, 0, 0, VAL_DOUBLE);
if (TESTon) {
ntics++;
for (i=0;i<4;i++)
fprintf(HVBfp
[i
],"%5d %10.1f %10.4f\n",ntics
,HVvdis
[i
],HVcdis
[i
]);
switch (ntics) {
case 5:
SetCtrlVal(p1h,P1_HVonoffall, 1);
QueueUserEvent (1001, p1h, P1_HVonoffall);
break;
case NTICSMAX-25:
SetCtrlVal(p1h,P1_HVonoffall, 0);
QueueUserEvent (1001, p1h, P1_HVonoffall);
break;
case NTICSMAX:
SetCtrlVal (p1h, P1_TESTon, 0);
QueueUserEvent (1001, p1h, P1_TESTon);
break;
default:
break;
}
}
}
break;
case P1_MSW:
GetCtrlVal (p1h, P1_MSW, &mainonoff);
if (ConfirmPopup("Main switch!!!", cMainSwitch[mainonoff]))
setMainSwitch(crate1, mainonoff);
if (mainonoff) {
Delay(5);
HVonoffall=0;
for (i=0; i<4; i++) {
HVonoff[i]=getChannelSwitch(crate1, ch0+i);
if (HVonoff[i]) HVonoffall=1;
}
for (i=0; i<4; i++) {
if (!HVonoffall) HVonoff[i]=1;
SetCtrlVal (p1h, HVonoff_rID[i], HVonoff[i]);
}
HVvset[0]=getOutputVoltage(crate1,ch0);
SetCtrlVal (p1h, P1_HVvset, HVvset[0]);
HVcset[0]=1e+6*getOutputCurrent(crate1,ch0);
SetCtrlVal (p1h, P1_HVcset, HVcset[0]);
SetAxisScalingMode (p1h, P1_G1, VAL_LEFT_YAXIS, VAL_MANUAL, -(HVcset[0]+1), 1.);
HVvrise=getModuleRampSpeedVoltage(crate1,mo);
SetCtrlVal (p1h, P1_HVvrise, HVvrise);
}
break;
case P1_HVBidinc:
if (!TESTon) {
for (i=0;i<4;i++) {
GetCtrlVal(p1h, HVBid_rID[i], HVBid[i]);
iret+=4;
SetCtrlVal(p1h, HVBid_rID[i], HVBid[i]);
}
}
break;
}
if (!mainonoff) continue;
switch (rID) {
case P1_HVonoffall:
GetCtrlVal (p1h, P1_HVonoffall, &HVonoffall);
for (i=0;i<4;i++) {
GetCtrlVal (p1h, HVonoff_rID[i], &HVonoff[i]);
if (HVonoff[i])
setChannelSwitch(crate1, ch0+i, HVonoffall);
}
break;
case P1_HVvset:
GetCtrlVal (p1h, P1_HVvset, &HVvset[0]);
for (i=0;i<4;i++)
setOutputVoltage(crate1, ch0+i, HVvset[0]);
break;
case P1_HVcset:
GetCtrlVal (p1h, P1_HVcset, &HVcset[0]);
SetAxisScalingMode (p1h, P1_G1, VAL_LEFT_YAXIS, VAL_MANUAL, -(HVcset[0]+1), 1.);
HVcset[0]/=1e+6; //[uA]
for (i=0;i<4;i++)
setOutputCurrent(crate1, ch0+i, HVcset[0]);
break;
case P1_HVvrise:
GetCtrlVal (p1h, P1_HVvrise, &HVvrise);
setModuleRampSpeedVoltage(crate1, mo, HVvrise);
HVvrise=getModuleRampSpeedVoltage(crate1,mo);
SetCtrlVal (p1h, P1_HVvrise, HVvrise);
break;
case P1_TESTon:
GetCtrlVal (p1h, P1_TESTon, &TESTon);
if (TESTon) {
ntics=0;
ClearStripChart (p1h, P1_G1);
LIDfp
=fopen("lastids.txt","w");
for (i=0;i<4;i++) {
GetCtrlVal(p1h, HVBid_rID[i], HVBid[i]);
sprintf(HVBfname
[i
],"data/HVB_%s.dat",HVBid
[i
]);
HVBfp
[i
]=fopen(HVBfname
[i
],"w");
}
LIDfp=NULL;
} else {
SetCtrlVal(p1h,P1_HVonoffall, 0);
QueueUserEvent (1001, p1h, P1_HVonoffall);
for (i=0;i<4;i++) {
HVBfp[i]=NULL;
}
QueueUserEvent(1002,p1h,P1_HVBidinc);
}
break;
// case P1_VFA0:
// GetCtrlVal (p1h, P1_VFA0, &ret);
// setOutputFallRate(crate1, ch, ret);
// vfall[0]=abs(getOutputFallRate(crate1,ch));
// SetCtrlVal (p1h, P1_VFA0, vfall[0]);
// break;
}
} while ((rID != P1_EXIT));
DiscardPanel (p1h);
MPOD_Close(0);
MPOD_End();
return 0;
}