#include "ordevice_template_device.h"
#include "ordevice_template_layout.h"
#define ORDEVICETEMPLATE__LAYOUT ORDevice_Template_Layout
ORDEVICETEMPLATE__CLASSSTR,
bool ORDevice_Template_Layout::FBCreate()
{
mDevice = ((ORDevice_Template *)(FBDevice *)Device);
UICreate ();
UIConfigure ();
UIReset ();
mDevice->OnStatusChange.Add (
this,(
FBCallback)&ORDevice_Template_Layout::EventDeviceStatusChange );
OnIdle.Add (
this,(
FBCallback)&ORDevice_Template_Layout::EventUIIdle );
return true;
}
void ORDevice_Template_Layout::FBDestroy()
{
OnIdle.Remove (
this,(
FBCallback)&ORDevice_Template_Layout::EventUIIdle );
mDevice->OnStatusChange.Remove (
this,(
FBCallback)&ORDevice_Template_Layout::EventDeviceStatusChange );
}
void ORDevice_Template_Layout::UICreate()
{
int lS, lH;
lS = 4;
lH = 25;
AddRegion (
"MainLayout",
"MainLayout", lS,
kFBAttachLeft,
"TabPanel", 1.00,
SetControl ( "TabPanel", mTabPanel );
SetControl ( "MainLayout", mLayoutGeneral );
UICreateLayout0();
UICreateLayout1();
}
void ORDevice_Template_Layout::UICreateLayout0()
{
int lS, lW, lH;
lS = 4;
lW = 150;
lH = 18;
mLayoutGeneral.AddRegion ( "LabelSamplingRate", "LabelSamplingRate",
mLayoutGeneral.AddRegion ( "EditNumberSamplingRate", "EditNumberSamplingRate",
mLayoutGeneral.AddRegion ( "LabelSamplingType", "LabelSamplingType",
mLayoutGeneral.AddRegion ( "ListSamplingType", "ListSamplingType",
mLayoutGeneral.AddRegion ( "ButtonSetCandidate", "ButtonSetCandidate",
mLayoutGeneral.AddRegion ( "ButtonTest", "ButtonTest",
mLayoutGeneral.SetControl("LabelSamplingRate", mLabelSamplingRate );
mLayoutGeneral.SetControl("EditNumberSamplingRate", mEditNumberSamplingRate );
mLayoutGeneral.SetControl("LabelSamplingType", mLabelSamplingType );
mLayoutGeneral.SetControl("ListSamplingType", mListSamplingType );
mLayoutGeneral.SetControl("ButtonSetCandidate", mButtonSetCandidate );
mLayoutGeneral.SetControl("ButtonTest", mButtonTest );
}
void ORDevice_Template_Layout::UICreateLayout1()
{
int lS = 4;
int lSx = 10;
int lSy = 15;
int lS_y = -10;
int lW = 90;
int lH = 18;
int lHlr = 55;
int lHlr2 = 80;
int lWlr = 200;
int lWrb = 55;
int lWrb2 = 70;
int lWrb3 = 75;
int lSlbx = 15;
int lSlby = 10;
int lWlb = 80;
mLayoutCommunication.AddRegion( "LayoutRegionSerial", "LayoutRegionSerial",
mLayoutCommunication.AddRegion( "ButtonSerialEnable", "ButtonSerialEnable",
mLayoutCommunication.AddRegion( "LabelSerialPort", "LabelSerialPort",
mLayoutCommunication.AddRegion( "ListSerialPort", "ListSerialPort",
mLayoutCommunication.AddRegion( "LabelSerialBaudRate", "LabelSerialBaudRate",
mLayoutCommunication.AddRegion( "ListSerialBaudRate", "ListSerialBaudRate",
mLayoutCommunication.SetControl( "ButtonSerialEnable", mButtonSerialEnable );
mLayoutCommunication.SetControl( "LabelSerialPort", mLabelSerialPort );
mLayoutCommunication.SetControl( "ListSerialPort", mListSerialPort );
mLayoutCommunication.SetControl( "LabelSerialBaudRate", mLabelSerialBaudRate );
mLayoutCommunication.SetControl( "ListSerialBaudRate", mListSerialBaudRate );
mLayoutCommunication.AddRegion( "LayoutRegionNetwork", "LayoutRegionNetwork",
mLayoutCommunication.AddRegion( "ButtonNetworkEnable", "ButtonNetworkEnable",
mLayoutCommunication.AddRegion( "LabelNetworkAddress", "LabelNetworkAddress",
mLayoutCommunication.AddRegion( "EditNetworkAddress", "EditNetworkAddress",
mLayoutCommunication.AddRegion( "LabelNetworkPort", "LabelNetworkPort",
mLayoutCommunication.AddRegion( "EditNetworkPort", "EditNetworkPort",
mLayoutCommunication.AddRegion( "LabelNetworkProtocol", "LabelNetworkProtocol",
mLayoutCommunication.AddRegion( "ListNetworkProtocol", "ListNetworkProtocol",
mLayoutCommunication.SetControl( "ButtonNetworkEnable", mButtonNetworkEnable );
mLayoutCommunication.SetControl( "LabelNetworkAddress", mLabelNetworkAddress );
mLayoutCommunication.SetControl( "EditNetworkAddress", mEditNetworkAddress );
mLayoutCommunication.SetControl( "LabelNetworkPort", mLabelNetworkPort );
mLayoutCommunication.SetControl( "EditNetworkPort", mEditNetworkPort );
mLayoutCommunication.SetControl( "LabelNetworkProtocol", mLabelNetworkProtocol );
mLayoutCommunication.SetControl( "ListNetworkProtocol", mListNetworkProtocol );
mLayoutCommunication.AddRegion( "LayoutRegionSimulator", "LayoutRegionSimulator",
mLayoutCommunication.AddRegion( "ButtonSimulatorEnable", "ButtonSimulatorEnable",
mLayoutCommunication.AddRegion( "LabelSimulatorFrequency", "LabelSimulatorFrequency",
mLayoutCommunication.AddRegion( "EditNumberSimulatorFrequency", "EditNumberSimulatorFrequency",
mLayoutCommunication.AddRegion( "LabelSimulatorAmplitude", "LabelSimulatorAmplitude",
mLayoutCommunication.AddRegion( "EditNumberSimulatorAmplitude", "EditNumberSimulatorAmplitude",
mLayoutCommunication.SetControl( "ButtonSimulatorEnable", mButtonSimulatorEnable );
mLayoutCommunication.SetControl( "LabelSimulatorFrequency", mLabelSimulatorFrequency );
mLayoutCommunication.SetControl( "EditNumberSimulatorFrequency",mEditNumberSimulatorFrequency );
mLayoutCommunication.SetControl( "LabelSimulatorAmplitude", mLabelSimulatorAmplitude );
mLayoutCommunication.SetControl( "EditNumberSimulatorAmplitude",mEditNumberSimulatorAmplitude );
}
void ORDevice_Template_Layout::UIConfigure()
{
mTabPanel.Items.SetString("General~Communication");
mTabPanel.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventTabPanelChange );
UIConfigureLayout0();
UIConfigureLayout1();
}
void ORDevice_Template_Layout::UIConfigureLayout0()
{
mLabelSamplingRate.Caption = "Sampling Rate :";
mLabelSamplingType.Caption = "Sampling Type :";
mEditNumberSamplingRate.LargeStep = 0.0;
mEditNumberSamplingRate.SmallStep = 0.0;
mEditNumberSamplingRate.OnChange.Add(
this, (
FBCallback)&ORDevice_Template_Layout::EventEditNumberSamplingRateChange );
mListSamplingType.OnChange.Add(
this, (
FBCallback)&ORDevice_Template_Layout::EventListSamplingTypeChange );
mButtonSetCandidate.OnClick.Add(
this, (
FBCallback)&ORDevice_Template_Layout::EventButtonSetCandidateClick );
mButtonSetCandidate.Caption = "Set Candidate";
mButtonTest.OnClick.Add(
this, (
FBCallback)&ORDevice_Template_Layout::EventButtonTestClick );
mButtonTest.Caption = "Test";
}
void ORDevice_Template_Layout::UIConfigureLayout1()
{
mLayoutCommunication.SetBorder(
"LayoutRegionSerial",
kFBEmbossBorder,
false,
true,2,1,90.0,0);
mLayoutCommunication.SetBorder(
"LayoutRegionNetwork",
kFBEmbossBorder,
false,
true,2,1,90.0,0);
mLayoutCommunication.SetBorder(
"LayoutRegionSimulator",
kFBEmbossBorder,
false,
true,2,1,90.0,0);
mButtonSerialEnable.Caption = "Serial";
mButtonSerialEnable.State = 1;
mButtonSerialEnable.OnClick.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventButtonSerialEnableClick );
mLabelSerialPort.Caption = "Port :";
int lPortNumber;
char lBuffer[40];
for( i=0; i<mSystem.CommPorts.GetCount(); i++ )
{
lPortNumber = mSystem.CommPorts[
i];
sprintf(lBuffer, "Comm %d", lPortNumber );
mListSerialPort.Items.Add( lBuffer, lPortNumber );
}
mListSerialPort.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventListSerialPortChange );
mLabelSerialBaudRate.Caption = "Speed :";
mListSerialBaudRate.Items.Add( "9600", 9600 );
mListSerialBaudRate.Items.Add( "19200", 19200 );
mListSerialBaudRate.Items.Add( "38400", 38400 );
mListSerialBaudRate.Items.Add( "57600", 57600 );
mListSerialBaudRate.Items.Add( "115200", 115200 );
mListSerialBaudRate.Items.Add( "230400", 230400 );
mListSerialBaudRate.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventListSerialBaudRateChange );
mButtonNetworkEnable.Caption = "Network";
mButtonNetworkEnable.State = 0;
mButtonNetworkEnable.OnClick.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventButtonNetworkEnableClick );
mLabelNetworkAddress.Caption = "Address :";
mEditNetworkAddress.Text = "127.0.0.1";
mEditNetworkAddress.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventEditNetworkAddressChange );
mLabelNetworkPort.Caption = "Port :";
mEditNetworkPort.Text = "3001";
mEditNetworkPort.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventEditNetworkPortChange );
mLabelNetworkProtocol.Caption = "Protocol :";
mListNetworkProtocol.Items.SetString("TCP~UDP");
mListNetworkProtocol.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventListNetworkProtocolChange );
mButtonSimulatorEnable.Caption = "Simulator";
mButtonSimulatorEnable.State = 0;
mButtonSimulatorEnable.OnClick.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventButtonSimulatorEnableClick );
mLabelSimulatorFrequency.Caption = "Frequency :";
mEditNumberSimulatorFrequency.Value = 1.0;
mEditNumberSimulatorFrequency.Precision = 3.3;
mEditNumberSimulatorFrequency.Min = 0.0;
mEditNumberSimulatorFrequency.Max = 10.0;
mEditNumberSimulatorFrequency.SmallStep = 0.001;
mEditNumberSimulatorFrequency.LargeStep = 0.010;
mEditNumberSimulatorFrequency.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventEditNumberSimulatorFrequencyChange );
mLabelSimulatorAmplitude.Caption = "Amplitude :";
mEditNumberSimulatorAmplitude.Value = 1.0;
mEditNumberSimulatorAmplitude.Precision = 3.3;
mEditNumberSimulatorAmplitude.Min = 0.0;
mEditNumberSimulatorAmplitude.Max = 10.0;
mEditNumberSimulatorAmplitude.SmallStep = 0.001;
mEditNumberSimulatorAmplitude.LargeStep = 0.010;
mEditNumberSimulatorAmplitude.OnChange.Add(
this, (
FBCallback) &ORDevice_Template_Layout::EventEditNumberSimulatorAmplitudeChange );
}
void ORDevice_Template_Layout::UIRefresh()
{
}
void ORDevice_Template_Layout::UIReset()
{
char lBuffer[40];
sprintf( lBuffer, "%d", mDevice->GetNetworkPort());
SelectCommunicationType( mDevice->GetCommunicationType() );
mListSerialPort.ItemIndex = mListSerialPort.Items.Find ( mDevice->GetSerialPort() );
mListSerialBaudRate.ItemIndex = mListSerialBaudRate.Items.Find( mDevice->GetSerialBaudRate() );
mEditNetworkAddress.Text = mDevice->GetNetworkAddress();
mEditNetworkPort.Text = lBuffer;
mEditNumberSimulatorFrequency.Value = mDevice->GetFrequency();
mEditNumberSimulatorAmplitude.Value = mDevice->GetAmplitude();
mEditNumberSamplingRate.Value = 1.0/((FBTime)mDevice->SamplingPeriod).GetSecondDouble();
mListSamplingType.ItemIndex = mListSamplingType.Items.Find( mDevice->SamplingMode );
mButtonSetCandidate.State = mDevice->GetSetCandidate();
}
void ORDevice_Template_Layout::EventTabPanelChange(
HISender pSender,
HKEvent pEvent )
{
switch( mTabPanel.ItemIndex )
{
case 0: SetControl("MainLayout", mLayoutGeneral ); break;
case 1: SetControl("MainLayout", mLayoutCommunication ); break;
}
}
void ORDevice_Template_Layout::EventListSerialPortChange(
HISender pSender,
HKEvent pEvent )
{
if( mDevice->Online )
{
mDevice->DeviceSendCommand( FBDevice::kOpStop );
}
mDevice->SetSerialPort(mListSerialPort.Items.GetReferenceAt(mListSerialPort.ItemIndex));
mListSerialPort.ItemIndex = mListSerialPort.Items.Find(mDevice->GetSerialPort());
}
void ORDevice_Template_Layout::EventListSerialBaudRateChange(
HISender pSender,
HKEvent pEvent )
{
if( mDevice->Online )
{
mDevice->DeviceSendCommand( FBDevice::kOpStop );
}
mDevice->SetSerialBaudRate( mListSerialBaudRate.Items.GetReferenceAt(mListSerialBaudRate.ItemIndex) );
mListSerialBaudRate.ItemIndex = mListSerialBaudRate.Items.Find( mDevice->GetSerialBaudRate() );
}
void ORDevice_Template_Layout::EventEditNetworkPortChange(
HISender pSender,
HKEvent pEvent )
{
int lPort;
char lBuffer[40];
sscanf(mEditNetworkPort.Text.AsString(), "%d", &lPort);
mDevice->SetNetworkPort(lPort);
lPort = mDevice->GetNetworkPort();
sprintf(lBuffer, "%d", lPort );
mEditNetworkPort.Text = lBuffer;
}
void ORDevice_Template_Layout::EventEditNetworkAddressChange(
HISender pSender,
HKEvent pEvent )
{
mDevice->SetNetworkAddress(mEditNetworkAddress.Text.AsString());
mEditNetworkAddress.Text = mDevice->GetNetworkAddress();
}
void ORDevice_Template_Layout::EventListNetworkProtocolChange(
HISender pSender,
HKEvent pEvent )
{
}
void ORDevice_Template_Layout::EventButtonTestClick(
HISender pSender,
HKEvent pEvent )
{
}
void ORDevice_Template_Layout::EventButtonSerialEnableClick(
HISender pSender,
HKEvent pEvent )
{
if( mDevice->Online )
{
mDevice->DeviceSendCommand( FBDevice::kOpStop );
}
SelectCommunicationType( mDevice->GetCommunicationType() );
}
void ORDevice_Template_Layout::EventButtonNetworkEnableClick(
HISender pSender,
HKEvent pEvent )
{
if( mDevice->Online )
{
mDevice->DeviceSendCommand( FBDevice::kOpStop );
}
mDevice->SetCommunicationType( (mListNetworkProtocol.ItemIndex==0) ?
kFBCommTypeNetworkTCP:kFBCommTypeNetworkUDP );
SelectCommunicationType( mDevice->GetCommunicationType() );
}
void ORDevice_Template_Layout::EventButtonSimulatorEnableClick(
HISender pSender,
HKEvent pEvent )
{
if( mDevice->Online )
{
mDevice->DeviceSendCommand( FBDevice::kOpStop );
}
SelectCommunicationType( mDevice->GetCommunicationType() );
}
void ORDevice_Template_Layout::EventEditNumberSimulatorFrequencyChange(
HISender pSender,
HKEvent pEvent )
{
mDevice->SetFrequency( mEditNumberSimulatorFrequency.Value );
mEditNumberSimulatorFrequency.Value = mDevice->GetFrequency();
}
void ORDevice_Template_Layout::EventEditNumberSimulatorAmplitudeChange(
HISender pSender,
HKEvent pEvent )
{
mDevice->SetAmplitude( mEditNumberSimulatorAmplitude.Value );
mEditNumberSimulatorAmplitude.Value = mDevice->GetAmplitude();
}
void ORDevice_Template_Layout::EventDeviceStatusChange(
HISender pSender,
HKEvent pEvent )
{
UIReset();
}
void ORDevice_Template_Layout::EventUIIdle(
HISender pSender,
HKEvent pEvent )
{
if( mDevice->Online && mDevice->Live )
{
if( mDevice->GetSetCandidate() )
{
mDevice->SetCandidates();
}
UIRefresh();
}
}
void ORDevice_Template_Layout::SelectCommunicationType( int pType )
{
bool lSerialTrue = false;
bool lNetworkTrue = false;
bool lSimulatorTrue = false;
switch( pType )
{
{
lSerialTrue = true;
}
break;
case kFBCommTypeNetworkUDP:
{
lNetworkTrue = true;
}
break;
{
lSimulatorTrue = true;
}
break;
}
mButtonSerialEnable.State = lSerialTrue ? 1 : 0;
mLabelSerialPort.Enabled = lSerialTrue;
mListSerialPort.Enabled = lSerialTrue;
mLabelSerialBaudRate.Enabled = lSerialTrue;
mListSerialBaudRate.Enabled = lSerialTrue;
mButtonNetworkEnable.State = lNetworkTrue ? 1 : 0;
mLabelNetworkAddress.Enabled = lNetworkTrue;
mEditNetworkAddress.Enabled = lNetworkTrue;
mLabelNetworkPort.Enabled = lNetworkTrue;
mEditNetworkPort.Enabled = lNetworkTrue;
mLabelNetworkProtocol.Enabled = false;
mListNetworkProtocol.Enabled = false;
mButtonSimulatorEnable.State = lSimulatorTrue ? 1 : 0;
mLabelSimulatorFrequency.Enabled = lSimulatorTrue;
mEditNumberSimulatorFrequency.Enabled = lSimulatorTrue;
mLabelSimulatorAmplitude.Enabled = lSimulatorTrue;
mEditNumberSimulatorAmplitude.Enabled = lSimulatorTrue;
}
void ORDevice_Template_Layout::EventEditNumberSamplingRateChange(
HISender pSender,
HKEvent pEvent )
{
bool lOnline = mDevice->Online;
double lVal = mEditNumberSamplingRate.Value;
if( lVal > 0.0 )
{
if( lOnline )
{
mDevice->DeviceSendCommand( FBDevice::kOpStop );
}
FBTime lTime;
lTime.SetSecondDouble( 1.0 / lVal );
mDevice->SamplingPeriod = lTime;
if( lOnline )
{
mDevice->DeviceSendCommand( FBDevice::kOpStart );
}
UIReset();
}
}
void ORDevice_Template_Layout::EventListSamplingTypeChange(
HISender pSender,
HKEvent pEvent )
{
mDevice->SamplingMode = (
FBDeviceSamplingMode)mListSamplingType.Items.GetReferenceAt( mListSamplingType.ItemIndex );
UIReset();
}
void ORDevice_Template_Layout::EventButtonSetCandidateClick(
HISender pSender,
HKEvent pEvent )
{
mDevice->SetSetCandidate( mButtonSetCandidate.State != 0 );
UIReset();
}