unit Defset;
{ -------------------------------------------------------
  WINWCP (c) J. Dempster, University of Strathclyde 1997
  Set default command voltage and digital output settings
  -------------------------------------------------------
  19/8/99 32 bit version for WinWCP V3.0
  20/12/01 ... D/A and digital default values now set using Main.WriteOutputPorts (V3.2.1)
  5/2/04 ..... Normal/Inverted trigger pulse setting added
  }

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, global, shared, ValEdit, ValidatedEdit ;

type
  TDefSetFrm = class(TForm)
    DACGroup: TGroupBox;
    DIGGroup: TGroupBox;
    GroupBox2: TGroupBox;
    rbOn1: TRadioButton;
    rbOff1: TRadioButton;
    GroupBox5: TGroupBox;
    rbOn3: TRadioButton;
    rboff3: TRadioButton;
    GroupBox6: TGroupBox;
    rbon4: TRadioButton;
    rboff4: TRadioButton;
    GroupBox7: TGroupBox;
    rbon5: TRadioButton;
    rboff5: TRadioButton;
    GroupBox8: TGroupBox;
    rbon6: TRadioButton;
    rboff6: TRadioButton;
    GroupBox9: TGroupBox;
    rbon7: TRadioButton;
    rboff7: TRadioButton;
    GroupBox1: TGroupBox;
    rbOn2: TRadioButton;
    rbOff2: TRadioButton;
    Dig0: TGroupBox;
    rbOn0: TRadioButton;
    rbOff0: TRadioButton;
    Label3: TLabel;
    Label4: TLabel;
    bOK: TButton;
    GroupBox3: TGroupBox;
    Label1: TLabel;
    edVDAC0: TValidatedEdit;
    Label2: TLabel;
    edVDivide0: TValidatedEdit;
    GroupBox4: TGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    edVDAC1: TValidatedEdit;
    edVDivide1: TValidatedEdit;
    procedure bOKClick(Sender: TObject);
    procedure bCancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DefSetFrm: TDefSetFrm;

implementation

{$R *.DFM}

uses mdiform  ;


procedure TDefSetFrm.FormShow(Sender: TObject);
// ------------------------------------------
// Initialise controls when form is displayed
// ------------------------------------------
var
   Bit : Word ;
begin

     { Stop laboratory interface activity }
     if Main.SESLabIO.ADCActive then Main.SESLabIO.ADCStop ;
     if Main.SESLabIO.DACActive then Main.SESLabIO.DACStop ;
     Main.WriteOutputPorts( Settings.DAC[0].HoldingVoltage*Settings.DAC[0].DivideFactor,
                            Settings.DAC[1].HoldingVoltage*Settings.DAC[1].DivideFactor,
                            Settings.DigitalPort.Value ) ;

     Main.mnDefaultSettings.enabled := false ;

     { Get default digital O/P settings }
     Bit := Settings.DigitalPort.Value and 1 ;
     if Bit <> 0 then rbOn0.checked := true
                 else rbOff0.checked := true ;
     Bit := Settings.DigitalPort.Value and 2 ;
     if Bit <> 0 then rbOn1.checked := true
                 else rbOff1.checked := true  ;
     Bit := Settings.DigitalPort.Value and 4 ;
     if Bit <> 0 then rbOn2.checked := true
                 else rbOff2.checked := true  ;
     Bit := Settings.DigitalPort.Value and 8 ;
     if Bit <> 0 then rbOn3.checked := true
                 else rbOff3.checked := true  ;
     Bit := Settings.DigitalPort.Value and 16 ;
     if Bit <> 0 then rbOn4.checked := true
                 else rbOff4.checked := true  ;
     Bit := Settings.DigitalPort.Value and 32 ;
     if Bit <> 0 then rbOn5.checked := true
                 else rbOff5.checked := true  ;
     Bit := Settings.DigitalPort.Value and 64 ;
     if Bit <> 0 then rbOn6.checked := true
                 else rbOff6.checked := true  ;
     Bit := Settings.DigitalPort.Value and 128 ;
     if Bit <> 0 then rbOn7.checked := true
                 else rbOff7.checked := true  ;

     { Get voltage settings }
     edVDAC0.Value := Settings.DAC[0].HoldingVoltage ;
     edVDivide0.Value := Settings.DAC[0].DivideFactor ;
     edVDAC1.Value := Settings.DAC[1].HoldingVoltage ;
     edVDivide1.Value := Settings.DAC[1].DivideFactor ;

     end;


procedure TDefSetFrm.bOKClick(Sender: TObject);
// -----------------------------------------
// Update settings when Apply button clicked
// -----------------------------------------
var
   Value : Word ;
   VDACS : Array[0..1] of Single ;
begin
     { Update default digital O/P settings }
     Value := 0 ;
     if rbOn0.checked then Value := Value or 1 ;
     if rbOn1.checked then Value := Value or 2 ;
     if rbOn2.checked then Value := Value or 4 ;
     if rbOn3.checked then Value := Value or 8 ;
     if rbOn4.checked then Value := Value or 16 ;
     if rbOn5.checked then Value := Value or 32 ;
     if rbOn6.checked then Value := Value or 64 ;
     if rbOn7.checked then Value := Value or 128 ;
     Settings.DigitalPort.Value := Value ;

     {Update voltage settings (DAC 0 & 1) }
     Settings.DAC[0].HoldingVoltage := edVDAC0.Value ;
     Settings.DAC[0].DivideFactor := edVDivide0.Value ;
     Settings.DAC[1].HoldingVoltage := edVDAC1.Value ;
     Settings.DAC[1].DivideFactor := edVDivide1.Value ;

     Main.WriteOutputPorts( Settings.DAC[0].HoldingVoltage*Settings.DAC[0].DivideFactor,
                            Settings.DAC[1].HoldingVoltage*Settings.DAC[1].DivideFactor,
                            Settings.DigitalPort.Value ) ;


     end;


procedure TDefSetFrm.bCancelClick(Sender: TObject);
begin
     close ;
     end;

procedure TDefSetFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
     Main.mnDefaultSettings.enabled := true ;
     Action := caFree ;
     end;



end.
