unit Zero;
{ -----------------------------------------------------------
  WinWCP (c) J.Dempster, University of Strathclyde 1996-97
  Channel zero level setup module
  -----------------------------------------------------------
  3/8/99 ... 32 bit version }

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Global, Mask, Shared, fileio, maths, ValEdit,
  ValidatedEdit;

type
  TZeroFrm = class(TForm)
    TypeGrp: TGroupBox;
    rbFromRecord: TRadioButton;
    Label1: TLabel;
    Label2: TLabel;
    rbFixed: TRadioButton;
    Label3: TLabel;
    edChannel: TEdit;
    bOK: TButton;
    bCancel: TButton;
    edNumAveraged: TValidatedEdit;
    edAtSample: TValidatedEdit;
    edFixedLevel: TValidatedEdit;
    procedure bOKClick(Sender: TObject);
    procedure rbFixedClick(Sender: TObject);
    procedure rbFromRecordClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ChSel : Integer ;
    NewZeroAt : Integer ;
  end;

var
  ZeroFrm: TZeroFrm;

implementation

{$R *.DFM}

procedure TZeroFrm.FormShow(Sender: TObject);
{ ----------------------------------
  Initialisation when form is opened
  ---------------------------------- }
begin

     edChannel.text := format('Ch.%d %s',[ChSel,Channel[ChSel].ADCName]) ;

     { Starting sample for zero level average (-1=fixed level) }
     edAtSample.LoLimit := 0 ;
     edAtSample.HiLimit := FH.NumSamples-1 ;
     edAtSample.Value := NewZeroAt ;
     { Number of samples averaged to compute zero level }
     edNumAveraged.LoLimit := 1 ;
     edNumAveraged.HiLimit := FH.NumSamples ;
     edNumAveraged.Value := FH.NumZeroAvg ;
     { Amplitude of zero level when in fixed zero mode }
     edFixedLevel.LoLimit := MinADCValue ;
     edFixedLevel.HiLimit := MaxADCValue ;
     edFixedLevel.Value := Channel[ChSel].ADCZero ;

     if Channel[ChSel].ADCZeroAt >= 0 then begin
        { Enable "From Record" zero level options }
        rbFromRecord.checked := True ;
        edAtSample.enabled := True ;
        edNumAveraged.enabled := True ;
        edFixedLevel.enabled := False ;
        end
     else begin
        { Enabled "Fixed" zero level options }
        rbFixed.checked := True ;
        edFixedLevel.enabled := True ;
        edAtSample.enabled := False ;
        edNumAveraged.enabled := False ;
        end ;
     end ;


procedure TZeroFrm.bOKClick(Sender: TObject);
{ -----------------
  Update zero level
  ----------------- }
begin
     if rbFromRecord.checked then begin
          Channel[ChSel].ADCZeroAt := Round(edAtSample.Value) ;
          fH.NumZeroAvg := Round(edNumAveraged.Value) ;
          end
     else begin
          Channel[ChSel].ADCZero := Round(edFixedLevel.Value) ;
          Channel[ChSel].ADCZeroAt := -1 ;
          end ;
     SaveHeader( fH ) ;
     end;


procedure TZeroFrm.rbFixedClick(Sender: TObject);
begin
        edFixedLevel.enabled := True ;
        edAtSample.enabled := False ;
        edNumAveraged.enabled := False ;
        end;

procedure TZeroFrm.rbFromRecordClick(Sender: TObject);
begin
        edAtSample.enabled := True ;
        edNumAveraged.enabled := True ;
        edFixedLevel.enabled := False ;
        end;


end.
