unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    Label1: TLabel;
    Button1: TButton;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Edit1: TEdit;
    Button2: TButton;
    Button3: TButton;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Edit1Exit(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    init : boolean;
    ad   : integer;
    dataread : integer;
    datawrite : integer;
    procedure initialize;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
 PCFad = 160;

function init_iic (comport : integer):integer; stdcall; external 'i2c_ser.dll';
procedure deinit_iic; stdcall; external 'i2c_ser.dll';
function iic_start :integer;stdcall; external 'i2c_ser.dll';
function iic_stop :integer;stdcall;external 'i2c_ser.dll';
function iic_read_byte(ack : integer): integer ;stdcall;external 'i2c_ser.dll';
function iic_send_byte (data : integer): integer ;stdcall;external 'i2c_ser.dll';
function set_port_delay (delay : integer):integer;stdcall; external 'i2c_ser.dll';




procedure TForm1.initialize;
var
  temp : integer;
begin
  set_port_delay(5);
  temp:=init_iic(0);
  if (temp=0) then
  begin
    StatusBar1.Panels[0].Text:='interface not found!';
    init :=false;
  end
  else
  begin
    StatusBar1.Panels[0].Text:='interface at adres '+IntToHex(temp,3);
    init:=true;
    Button1.Enabled:=true;
    Button2.Enabled:=true;
    Button3.Enabled:=true;

  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  initialize;
  if (not (init)) then
  begin
    StatusBar1.Panels[1].Text:='Stop!';

  end;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  deinit_iic;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  temp : integer;
begin
  iic_start;
  temp:=iic_send_byte (PCFad);
  if (temp=0) then
  begin
    MessageDlg('PCF8582 not found',mtWarning,[mbOk],0);
    StatusBar1.Panels[1].Text:='PCF8582 not found at 10100000 !';
  end
  else
  begin
    StatusBar1.Panels[1].Text:='PCF8582 Ok';
    Button1.Enabled:=true;
    Button2.Enabled:=true;
    Button3.Enabled:=true;
  end;
  iic_stop;
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin   {EConvertError}
  try
    TEdit(Sender).tag:=StrToInt(TEdit(Sender).Text);
  except on EConvertError do
    begin
      TEDit(Sender).Text:='0';
      TEdit(Sender).tag:=0;
    end;
  end;
  if TEdit(Sender).tag>255 then
  begin
    TEdit(Sender).Tag:=TEdit(Sender).Tag mod 256;
    TEdit(Sender).Text:=IntToStr(TEdit(Sender).Tag);
  end;
  ad:=Edit1.Tag;
  datawrite:=Edit2.Tag;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  temp:integer;
begin
  iic_start;
  iic_send_byte (PCFad);
  iic_send_byte (ad);
  iic_start;
  iic_send_byte (PCFad+1); {Read = ad +1 }
  temp:=iic_read_byte(0);
  iic_stop;
  Edit3.Text:=IntToStr(temp);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  iic_start;
  iic_send_byte (PCFad);
  iic_send_byte (ad);
  iic_send_byte (datawrite);
  iic_stop;
  {Edit3.Text:=IntToStr(temp);}

end;

end.
