unit uMain;

interface

uses
  Windows, ShellApi, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, StrUtils;

type
  TbKomStart = class(TForm)
    bOVlevo: TButton;
    bOVpravo: TButton;
    bVpred: TButton;
    bVzad: TButton;
    apStep: TButton;
    BitBtn1: TBitBtn;
    lLog: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Lkolo: TRadioGroup;
    Rkolo: TRadioGroup;
    Step: TRadioGroup;
    btnGO: TButton;
    Timer1: TTimer;

    procedure BitBtn1Click(Sender: TObject);
    procedure bVpredClick(Sender: TObject);
    procedure bOVlevoClick(Sender: TObject);
    procedure bOVpravoClick(Sender: TObject);
    procedure bVzadClick(Sender: TObject);
    procedure apStepclick(Sender: TObject);
    procedure GO(Sender: TObject);
    procedure btnGOClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  bKomStart: TbKomStart;

  // Handle okna, je treba pro komunikaci Delphi -> Blender
  hBlender : HWND;

implementation

{$R *.dfm}
// MicroMouse
function mmSetDelphiMsg( aText : string ): boolean; cdecl ; external 'microMouse_Virtual.dll';    // pridani 1 prikazu
function mmSetDelphiReady(): boolean;cdecl ; external 'microMouse_Virtual.dll';  //nastavi delphi na "pripraveno"
function mmSetDelphiBusy(): boolean;cdecl ; external 'microMouse_Virtual.dll';  //nastavi delphi na "pripraveno"
function mmOpenCommunication(): boolean;cdecl ; external 'microMouse_Virtual.dll';
function mmIsPythonReady(): boolean;cdecl ; external 'microMouse_Virtual.dll';  //kontroluje, jestli je python pripraven poslouchat
function mmGetPythonMsg(): PChar;cdecl ; external 'microMouse_Virtual.dll';  //co rika Python
function mmAddCommand( aText : string ): boolean; cdecl;cdecl ; external 'microMouse_Virtual.dll';    // pridani prikazu
function mmCloseCommunication(): boolean;cdecl ; external 'microMouse_Virtual.dll';      //pridani konecneho znaku a uzavre komunikaci
function mmSetMsgEnd(): boolean;cdecl ; external 'microMouse_Virtual.dll';          //poslani konecneho znaku (samotneho)
function mmSetSteps( aSteps : LongWord ): boolean;cdecl ; external 'microMouse_Virtual.dll';
function mmMoveForward( aCount : LongWord ): boolean;cdecl ; external 'microMouse_Virtual.dll';
function mmMoveBackward( aCount : LongWord ): boolean;cdecl ; external 'microMouse_Virtual.dll';
function mmTurnLeft( aCount : LongWord ): boolean;cdecl ; external 'microMouse_Virtual.dll';
function mmTurnRight( aCount : LongWord ): boolean;cdecl ; external 'microMouse_Virtual.dll';

var StaraZpravaID : LongInt;

procedure TbKomStart.BitBtn1Click(Sender: TObject);

begin
  if mmOpenCommunication() <> False then
  begin
    lLog.Items.Clear;
    lLog.Items.Add('Mys nalezena');
    bOVlevo.Enabled   := True;
    bOVpravo.Enabled  := True;
    bVpred.Enabled    := True;
    bVzad.Enabled     := True;
    StaraZpravaID     := 0;
  end
  else
  begin
    lLog.Items.Clear;
    lLog.Items.Add('Spustte aplikaci Mys!');

  end;
end;

procedure TbKomStart.apStepClick(Sender: TObject);
begin
  if Step.ItemIndex = 1 then
    mmSetSteps(200)
  else
    mmSetSteps(400);
end;

procedure TbKomStart.GO(Sender: TObject);
begin
 if (Lkolo.ItemIndex =0) and (Rkolo.ItemIndex = 0) then
    begin
      mmMoveForward(1);
    end;
end;

procedure TbKomStart.bVpredClick(Sender: TObject);
begin
  mmMoveForward(1);
end;

procedure TbKomStart.bOVlevoClick(Sender: TObject);
begin
  mmTurnLeft(1);
end;

procedure TbKomStart.bOVpravoClick(Sender: TObject);
begin
  mmTurnRight(1);
end;

procedure TbKomStart.bVzadClick(Sender: TObject);
begin
  mmMoveBackward(1);
end;



procedure TbKomStart.btnGOClick(Sender: TObject);
begin
  if (Lkolo.ItemIndex = 0) and (Rkolo.ItemIndex = 0) then
      mmMoveForward(1);
  if (Lkolo.ItemIndex = 1) and (Rkolo.ItemIndex = 0) then
      mmTurnLeft(1);
  if (Lkolo.ItemIndex = 0) and (Rkolo.ItemIndex = 1) then
      mmTurnRight(1);
  if (Lkolo.ItemIndex = 1) and (Rkolo.ItemIndex = 1) then
      mmMoveBackward(1);

end;

procedure TbKomStart.FormDestroy(Sender: TObject);
begin
  mmCloseCommunication();
end;

procedure TbKomStart.Timer1Timer(Sender: TObject);
  var temp, temp2 : string;
      CisloZpravy : LongInt;
begin
    temp := string(mmGetPythonMsg());

    if length(temp) > 0 then begin

     try
       temp2 := LeftStr(temp, Pos(';', temp)-1);
       CisloZpravy := StrToInt(temp2);
//       if CisloZpravy > StaraZpravaID then begin
         lLog.Items.Add(temp);
         StaraZpravaID := CisloZpravy;
  //     end;
     except
     end;
    end;

end;

end.
