unit IRQThreadUnit;

interface

uses
  Classes, Windows, MainUnit;

type
  TIRQThread = class(TThread)
  public
    COSPPIs: LongWord;
    CardNum: Integer;
  private
    COSData: TCOSData;
    LastError: LongWord;

    procedure ReportIRQ;
    function WaitForIRQType: LongWord;
    procedure ReportError;
  protected
    procedure Execute; override;
  end;

implementation

uses
  SysUtils, AIOWDM;

procedure TIRQThread.Execute;
begin
  SetLength(COSData, COSPPIs);
  while WaitForIRQType <> 0 do
    Synchronize(ReportIRQ)
  ;
  LastError := GetLastError;
  Synchronize(ReportError);
end;

procedure TIRQThread.ReportError;
begin
  if LastError = ERROR_OPERATION_ABORTED then
    MainForm.AddLine(CardNum, 'IRQ request aborted.')
  else if LastError = ERROR_INVALID_FUNCTION then
    MainForm.AddLine(CardNum, 'Error: this card already has a pending IRQ request. IRQ request aborted.')
  else
    MainForm.AddLine(CardNum, 'Some error occurred in the driver. IRQ request aborted.')
  ;
end;

procedure TIRQThread.ReportIRQ;
begin
  if COSPPIs = 0 then
    MainForm.ReceiveGenIRQ(CardNum)
  else
    MainForm.ReceiveCOSData(CardNum, COSData)
  ;
end;

function TIRQThread.WaitForIRQType: LongWord;
begin
  if COSPPIs <> 0 then
    Result := COSWaitForIRQ(CardNum, COSPPIs, @COSData[0])
  else
    Result := WaitForIRQ(CardNum)
  ;
end;

end.
