{Copyright or © or Copr. :

CNRS - Laboratoire Mouvement & Perception
franck.buloup@univmed.fr

This software is a computer program whose purpose is to highly simplify  
the use of ADwinPro realtime systems when you develop experimental
protocols  on life's sciences area.

This software is governed by the CeCILL  license under French law and
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info". 

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited
liability. 

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.}
Unit StimuliClass;

interface
//{$DEFINE SIMUL}

{$IFDEF SIMUL}
uses Classes, SysUtils;
{$ELSE}
uses Contnrs, Series, Chart, Classes, SysUtils, Forms, Controls;
{$ENDIF}


type
{$IFDEF SIMUL}
  TStimulus = class
{$ELSE}
  TStimulus = class(TLineSeries)
{$ENDIF}
  private
  protected
  public
    Fe : double;
  {$IFDEF SIMUL}
    Name : string;
    Values : array of double;
    Index : integer;
    constructor CreateStimulus(pNom : string; pDuree,pFe : double);
    function GetValue : double;
  {$ELSE}
    Gain : double;
    Offset : Double;
    Modified : boolean;
    anORnumORcanint : string;
    constructor CreateStimulus(pNom : string; pDuree,pFe : double; pChart : TChart);
    procedure ImportFromTextFile(pFileName : string);
    procedure ImportFromBinFile(pFileName : string; readAllValues : boolean = false);
  {$ENDIF}
  end;

{$IFDEF SIMUL}
  TStimuli = class(TList)
{$ELSE}
  TStimuli = class(TObjectList)
  private
    Chart : TChart;
{$ENDIF}
  protected
  public
    Duree : double; //En ms
  {$IFDEF SIMUL}
    constructor Create(pFileName : string);
    function GetValue(pName : string):double;
    procedure FreeValues;
    function Has(pNom : string):TSTimulus;
  {$ELSE}
    Modified : boolean;
    constructor Create(pFileName : string; pChart : TChart);
    function DeleteStimulus(pStimulus : TStimulus): integer;
    procedure SaveToFile(pFileName : string);
    function Has(pNom : string):TSTimulus;
    procedure SetDuree(pDuree : double);
    function GetModified : boolean;
  {$ENDIF}
    function AddStimulus(pNom : string; pFe : double):TStimulus;
    procedure ReadFromFile(pFileName : string);
  end;

implementation

{$IFNDEF SIMUL}
uses UDocometre;
{$ENDIF}

{$IFDEF SIMUL}
constructor TStimuli.Create(pFileName : string);
begin
  inherited Create;
  Duree := 0.0;
  if pFileName <> '' then ReadFromFile(pFileName);
end;
{$ELSE}
constructor TStimuli.Create(pFileName : string; pChart : TChart);
var SaveCursor : TCursor;
begin
  inherited Create(true);
  Duree := 0.0;
  Chart :=  pChart;
  if pFileName <> '' then
  begin
    SaveCursor := Screen.Cursor;
    Screen.Cursor := crHourGlass;
    ReadFromFile(pFileName);
    Screen.Cursor := SaveCursor;
  end;
  Modified := false;
end;
{$ENDIF}

procedure TStimuli.ReadFromFile(pFileName : string);
var Feg,NbCan,i,RF,M: integer;
    FichierStim : TFileStream;
    StimName : string;
    CR : string[2];
    S : char;
{$IFNDEF SIMUL}
    j,N : integer;
    Values : array of double;
{$ENDIF}
    AStimulus : TStimulus;
begin
  CR := #13#10;
  FichierStim := TFileStream.Create(pFileName,fmOpenRead);

  FichierStim.Read(Feg,SizeOf(Feg));
  FichierStim.Read(Duree,SizeOf(Duree));
  FichierStim.Read(NbCan,SizeOf(NbCan));

  for i := 0 to NbCan - 1 do
  begin
    StimName := '';
    FichierStim.Read(S,1);
    repeat
      StimName := StimName + S;
      FichierStim.Read(S,1);
    until ((S=CR[2]) and (StimName[Length(StimName)]=CR[1]));
    system.delete(StimName,Length(StimName),1);
    FichierStim.Read(RF,SizeOf(RF));
    Self.AddStimulus(StimName,1.0*Feg/RF);
  end;

  for i := 0 to NbCan - 1 do
  begin
  {$IFDEF SIMUL}
    AStimulus := Self.Items[i];
  {$ELSE}
    AStimulus := Self.GetItem(i) as TStimulus;
  {$ENDIF}
    M := trunc(AStimulus.Fe*Duree/1000) + 1;
  {$IFDEF SIMUL}
    Setlength(AStimulus.Values,M);
    FichierStim.Read(AStimulus.Values[0],M*8);
  {$ELSE}
    AStimulus.clear;
    Setlength(Values,M);
    FichierStim.Read(Values[0],M*8);
    N := high(Values);
    for j :=0 to N do AStimulus.AddXY(1000.0*j/AStimulus.Fe,Values[j]);
    AStimulus.Active := false;
  {$ENDIF}
  end;
{$IFNDEF SIMUL}
  Values := nil;
{$ENDIF}
  FichierStim.Free;
end;

{$IFNDEF SIMUL}
procedure TStimuli.SaveToFile(pFileName : string);
var Feg,NbCan,i,j,RF,M : integer;
    FichierStim : TFileStream;
    CR : string;
    AStimulus : TStimulus;
    Values : array of double;
begin
  CR := #13#10;
  Feg := DocDocometre.EXPERIENCE.FEG;
  NbCan := Self.Count;
  FichierStim := TFileStream.Create(pFileName,fmCreate);

  FichierStim.Write(Feg,SizeOf(Feg));
  FichierStim.Write(Duree,SizeOf(Duree));
  FichierStim.Write(NbCan,SizeOf(NbCan));
                                     
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.GetItem(i) as TStimulus;
    RF := integer(trunc(Feg/AStimulus.Fe));
    FichierStim.Write(PChar(AStimulus.Title)^,length(AStimulus.Title));
    FichierStim.Write(PChar(CR)^,length(CR));
    FichierStim.Write(RF,SizeOf(RF));
  end;

  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.GetItem(i) as TStimulus;
    SetLength(Values,AStimulus.Count);
    for j := 0 to AStimulus.Count - 1
    do Values[j] := AStimulus.YValues.Value[j];
    M := trunc(AStimulus.Fe*Duree/1000) + 1;
    FichierStim.Write(Values[0],8*M);
  end;

  FichierStim.Free;

end;

procedure TStimuli.SetDuree(pDuree : double);
var i,j,M1,M2,N : integer;
    AStimulus : TStimulus;
begin
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.GetItem(i) as TStimulus;
    if pDuree > Duree then
    begin
      M1 := trunc(Duree*AStimulus.Fe/1000) + 1;
      M2 := trunc(pDuree*AStimulus.Fe/1000) + 1;
      for j := M1 to M2 - 1
      do AStimulus.AddXY(1000.0*j/AStimulus.Fe,0);
    end;
    if pDuree < Duree then
    begin
      M2 := trunc(Duree*AStimulus.Fe/1000) + 1;
      M1 := trunc(pDuree*AStimulus.Fe/1000) + 1;
      repeat
        N := AStimulus.Count;
        AStimulus.Delete(N-1);
        dec(M2);
      until M2=M1;
    end;
  end;
  Duree := pDuree;
  Modified := true;
end;

function TStimuli.DeleteStimulus(pStimulus : TStimulus):integer;
begin
  result := Self.Remove(pStimulus);
  Modified := true;
end;

function TStimuli.Has(pNom : string):TSTimulus;
var i : integer;
    AStimulus : TStimulus;
begin
  result := nil;
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.GetItem(i) as TStimulus;
    if AStimulus.Title = pNom
    then result := AStimulus;
  end;
end;

function TStimuli.GetModified : boolean;
var i : integer;
    AStimulus : TStimulus;
begin
  Result := false;
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.GetItem(i) as TStimulus;
    Result := Result or AStimulus.Modified;
  end;
  Result := Result or Modified;
end;

{$ENDIF}

{$IFDEF SIMUL}
function TStimuli.Has(pNom : string):TSTimulus;
var i : integer;
    AStimulus : TStimulus;
begin
  result := nil;
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.Get(i);
    if AStimulus.Name = pNom
    then result := AStimulus;
  end;
end;

function TStimuli.GetValue(pName : string):double;
var i : integer;
    AStimulus : TStimulus;
begin
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.Get(i);
    if AStimulus.Name = pName then
    begin
      result := AStimulus.GetValue;
      break;
    end;
  end;
end;

procedure TStimuli.FreeValues;
var i : integer;
    AStimulus : TStimulus;
begin
  for i := 0 to Self.Count - 1 do
  begin
    AStimulus := Self.Get(i);
    AStimulus.Values := nil;
  end;
end;
{$ENDIF}

function TStimuli.AddStimulus(pNom : string; pFe : double):TStimulus;
var NewStimulus : TStimulus;
begin
{$IFDEF SIMUL}
  NewStimulus := TStimulus.CreateStimulus(pNom,Duree,pFe);
  Self.Add(NewStimulus);
{$ELSE}
  NewStimulus := TStimulus.CreateStimulus(pNom,Duree,pFe,Chart);
  Self.Add(NewStimulus);
  Modified := true;
{$ENDIF}
  result := NewStimulus;
end;

{$IFDEF SIMUL}
constructor TStimulus.CreateStimulus(pNom : string; pDuree,pFe : double);
var M : integer;
{$ELSE}
constructor TStimulus.CreateStimulus(pNom : string; pDuree,pFe : double;
                                     pChart : TChart);
var i,M : integer;
{$ENDIF}
begin
{$IFDEF SIMUL}
  inherited Create;
  Self.Name := pNom;
  Self.Fe := pFe;
  M := trunc(Fe*pDuree/1000) + 1;
  SetLength(Self.Values,M);
  Index := 0;
{$ELSE}
  inherited Create(pChart);
  Self.Title := pNom;
  Self.Stairs := true;
  Self.Fe := pFe;
  M := trunc(Fe*pDuree/1000) + 1;
  for i := 0 to M - 1
  do Self.AddXY(1000.0*i/Fe,0);
  Self.ParentChart := pChart;
  Gain := 1.0;
  Offset := 0.0;
{$ENDIF}
end;

{$IFDEF SIMUL}
function TStimulus.GetValue : double;
begin
  if index <= high(Values) then
  begin
    result := Values[index];
    inc(index);
  end else result := 0.0;
end;
{$ENDIF}

{$IFNDEF SIMUL}
procedure TStimulus.ImportFromTextFile(pFileName : string);
var TextFile : TStringList;
    i : integer;
begin
  TextFile := TStringList.Create;
  TextFile.Sorted := false;
  try
    TextFile.LoadFromFile(pFileName);
    for i := 0 to Count - 1 do
    begin
      if i <  TextFile.Count
      then YValues[i] := strtofloat(TextFile[i])
      else YValues[i] := 0.0;
    end;
    if Self.anORnumORcanint = 'num' then
    begin
      for i := 0 to Self.Count - 1
      do if YValues[i] >= 1 then YValues[i] := 1 else YValues[i] := 0;
    end;
  finally
    TextFile.Free;
    Repaint;
  end;

end;

procedure TStimulus.ImportFromBinFile(pFileName : string; readAllValues : boolean = false);
var BinFile : TFileStream;
    Values : array of single;
    t0 : single;
    M,i : integer;
begin
  BinFile := TFileStream.Create(pFileName,fmOpenRead);
  try
    M := BinFile.Size;
    Setlength(Values,M div 4);//Les fichiers sau sont en single (32 bits - 4 octets)
    BinFile.Read(Values[0],M);

    if readAllValues then
    begin
      if (M div 4) > Count then
      begin
        t0 := Count/Fe;
        i := 0;
        repeat
          Self.AddXY(t0 + i/Fe,0.0);
          inc(i);
        until Count = (M div 4);
      end;
    end;

    for i := 0 to Count - 1 do
    begin
      if i <= high(Values)
      then YValues[i] := Gain*Values[i] + Offset
      else YValues[i] := 0.0;
    end;
    if Self.anORnumORcanint = 'num' then
    begin
      for i := 0 to Self.Count - 1
      do if YValues[i] >= 1 then YValues[i] := 1 else YValues[i] := 0;
    end;
  finally
    Values := nil;
    BinFile.Free;
    Repaint;
  end;
end;

{$ENDIF}

end.
