unit MainUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) StartButton: TButton; StopButton: TButton; LogList: TListBox; procedure StartButtonClick(Sender: TObject); procedure StopButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); protected procedure IdleFrame(Sender: TObject; var Done: Boolean); public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} uses Math, AIOUSB, DetectUnit; type TDACPoint = packed record X, Y, R, G, B: Word; end; const FullCircle = Pi * 2; //Pts = 7; //PtsPerDraw = Pts + 1; //TwoSeventhsOfFullCircle = FullCircle * 2 / 7; var DACData: array of TDACPoint; bGotDACData: Boolean; Rot: Double; DeviceIndex: LongWord; function ErrorText(ErrorCode: LongWord): String; var WindowsError: PChar; begin FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER + FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrorCode, 0, PChar(@WindowsError), 0, nil); Result := WindowsError; LocalFree(Cardinal(WindowsError)); end; procedure TForm1.FormCreate(Sender: TObject); var DetectForm: TDetectForm; begin DetectForm := TDetectForm.Create(Self); if DetectForm.ShowModal = mrOK then begin DeviceIndex := DetectForm.DeviceIndex; DetectForm.Free; end else begin DetectForm.Free; Application.Terminate; Exit; end; end; procedure TForm1.StartButtonClick(Sender: TObject); var ClockHz: Double; ErrorCode: LongWord; begin Rot := 0; ClockHz := 40000; LogList.Items.Add('Opening...'); ErrorCode := DACOutputOpen(DeviceIndex, ClockHz); if ErrorCode = ERROR_SUCCESS then begin LogList.Items.Add('Opened for output at ' + FloatToStr(ClockHz) + 'Hz.'); end else begin LogList.Items.Add('DACOutputOpen failed with error #' + IntToStr(ErrorCode) + ': ' + ErrorText(ErrorCode)); Exit; end; Application.OnIdle := IdleFrame; StopButton.Visible := True; StartButton.Visible := False; end; procedure TForm1.StopButtonClick(Sender: TObject); begin Application.OnIdle := nil; LogList.Items.Add('Closing...'); DACOutputClose(DeviceIndex, True); LogList.Items.Add('Closed.'); StartButton.Visible := True; StopButton.Visible := False; end; procedure TForm1.IdleFrame(Sender: TObject; var Done: Boolean); var ErrorCode: LongWord; I: Integer; EC, ES: Extended; begin repeat if not bGotDACData then begin //This frame produces a blue seven-pointed star, which rotates at 0.0004 radians per frame. Rot := Rot + 0.0004; if Rot > FullCircle then Rot := Rot - FullCircle; SetLength(DACData, 8); for I := 0 to 7 do with DACData[I] do begin SinCos(Rot + I * (2/7) * FullCircle, ES, EC); X := Round(2048 + ES * 2047); Y := Round(2048 + EC * 2047); R := 0; G := 0; B := $FFF; end; bGotDACData := True; end; ErrorCode := DACOutputFrame(DeviceIndex, Length(DACData), @DACData[0].X); if ErrorCode = ERROR_SUCCESS then begin bGotDACData := False; Caption := 'Sending'; end else if ErrorCode = ERROR_NOT_READY then begin // Caption := 'Waiting'; Sleep(1); Break; end else begin Caption := 'Error #' + IntToStr(ErrorCode) + ': ' + ErrorText(ErrorCode); Break; end; until False; Done := False; if Done then Application.OnIdle := nil; end; end.