SimulatorFans/AssettoCorsa/source/MainFrm.pas

209 lines
4.6 KiB
ObjectPascal

unit MainFrm;
interface
uses
System.Classes,
System.SysUtils,
Vcl.Controls,
Vcl.ExtCtrls,
Vcl.Forms,
Vcl.StdCtrls,
CPort;
type
TMainForm = class(TForm)
PortComboBox: TComboBox;
RefreshPortsButton: TButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Edit1: TEdit;
Button7: TButton;
Edit2: TEdit;
procedure FormCreate(Sender: TObject);
procedure RefreshPortsButtonClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
FComPort: TComPort;
FReceived: string;
FOnResponse: TProc<string>;
procedure OnReceiveChar(Sender: TObject; Count: Integer);
procedure RefreshPorts;
procedure SendCommand(const ACommand: string; AOnResponse: TProc<string>);
property ComPort: TComPort read FComPort;
end;
implementation
uses
System.RegularExpressions, Vcl.Dialogs;
{$R *.dfm}
procedure TMainForm.Button1Click(Sender: TObject);
begin
SendCommand('>Info',
procedure(Response: string)
var
match: TMatch;
begin
match := TRegEx.Match(Response, '<Info:Fans=(\d+)', [roSingleLine]);
if match.Success then
ShowMessage('Connected, got ' + match.Groups[1].Value + ' fan(s)')
else
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
Edit1.Text := '255';
SendCommand('>SetFans:255,0',
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.Button3Click(Sender: TObject);
begin
Edit2.Text := '255';
SendCommand('>SetFans:0,255',
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.Button4Click(Sender: TObject);
begin
Edit1.Text := '0';
Edit2.Text := '0';
SendCommand('>SetFans:0,0',
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.Button5Click(Sender: TObject);
begin
Edit1.Text := '128';
SendCommand('>SetFans:128,0',
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.Button6Click(Sender: TObject);
begin
Edit2.Text := '128';
SendCommand('>SetFans:0,128',
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.Button7Click(Sender: TObject);
begin
SendCommand('>SetFans:' + Edit1.Text + ',' + Edit2.Text,
procedure(Response: string)
begin
if Response <> '<SetFans' then
ShowMessage('Invalid response: ' + Response);
end);
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
RefreshPorts;
PortComboBox.ItemIndex := Pred(PortComboBox.Items.Count);
end;
procedure TMainForm.RefreshPortsButtonClick(Sender: TObject);
begin
RefreshPorts;
end;
procedure TMainForm.RefreshPorts;
var
currentPort: string;
begin
currentPort := '';
if PortComboBox.ItemIndex > -1 then
currentPort := PortComboBox.Items[PortComboBox.ItemIndex];
EnumComPorts(PortComboBox.Items);
if Length(currentPort) > 0 then
PortComboBox.ItemIndex := PortComboBox.Items.IndexOf(currentPort);
end;
procedure TMainForm.OnReceiveChar(Sender: TObject; Count: Integer);
var
data: string;
terminatorPos: Integer;
begin
(Sender as TComPort).ReadStr(data, Count);
FReceived := FReceived + data;
terminatorPos := AnsiPos(#10, FReceived);
if terminatorPos > 0 then
begin
// Since the protocol is quite synchronous, this is good enough for now
SetLength(FReceived, terminatorPos - 1);
if Assigned(FOnResponse) then
FOnResponse(FReceived);
FReceived := '';
FOnResponse := nil;
end;
end;
procedure TMainForm.SendCommand(const ACommand: string; AOnResponse: TProc<string>);
begin
if not Assigned(FComPort) then
begin
FComPort := TComPort.Create(Self);
FComPort.Port := PortComboBox.Items[PortComboBox.ItemIndex];
FComPort.BaudRate := br19200;
FComPort.OnRxChar := OnReceiveChar;
FComPort.Open;
end;
FOnResponse := AOnResponse;
ComPort.WriteStr(ACommand + #10);
end;
end.