unit SteckdosenMain;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, ExtCtrls, IPConnection, BrickletIO16;

type

  { TFormSteckdosenMain }

  TFormSteckdosenMain = class(TForm)
    ButtonAOn: TButton;
    ButtonAOff: TButton;
    ButtonBOn: TButton;
    ButtonBOff: TButton;
    ButtonCOn: TButton;
    ButtonCOff: TButton;
    ButtonDOn: TButton;
    ButtonDOff: TButton;
    procedure ButtonAOnClick(Sender: TObject);
    procedure ButtonAOffClick(Sender: TObject);
    procedure ButtonBOnClick(Sender: TObject);
    procedure ButtonBOffClick(Sender: TObject);
    procedure ButtonCOnClick(Sender: TObject);
    procedure ButtonCOffClick(Sender: TObject);
    procedure ButtonDOnClick(Sender: TObject);
    procedure ButtonDOffClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    ipcon: TIPConnection;
    io: TBrickletIO16;
    procedure Send(device: char; state: integer);
  public
    { public declarations }
  end;


const
  HOST = 'localhost';
  PORT = 4223;
  UID = 'b6E';

var
  FormSteckdosenMain: TFormSteckdosenMain;

implementation

{$R *.lfm}

{ TFormSteckdosenMain }

procedure TFormSteckdosenMain.FormCreate(Sender: TObject);
begin
  ipcon:= TIPConnection.Create;
  io:=TBrickletIO16.Create(UID, ipcon);

  ipcon.Connect(HOST, PORT);

  // lines a,b,c,d (mapped to port a 0,1,2,3)
  io.SetPortConfiguration('a', 1 shl 0, 'o', false);
  io.SetPortConfiguration('a', 1 shl 1, 'o', false);
  io.SetPortConfiguration('a', 1 shl 2, 'o', false);
  io.SetPortConfiguration('a', 1 shl 3, 'o', false);
  // on/off lines (port b, 0 off, 1 on)
  io.SetPortConfiguration('b', 1 shl 0, 'o', false);
  io.SetPortConfiguration('b', 1 shl 1, 'o', false);

end;


procedure TFormSteckdosenMain.Send(device: char; state: integer);
var numdevice: integer;
begin
  case device of
    'a': numdevice:= 0;
    'b': numdevice:= 1;
    'c': numdevice:= 2;
    'd': numdevice:= 3;
    //else RAISE ERROR
  end;

  //set line 'device' and line 'state' true (simulates button press of button 'device on/off')
  io.SetPortConfiguration('a', 1 shl numdevice, 'o', true);
  io.SetPortConfiguration('b', 1 shl state, 'o', true);
  //turn them back off after 300ms
  SysUtils.Sleep(300);
  io.SetPortConfiguration('a', 1 shl numdevice, 'o', false);
  io.SetPortConfiguration('b', 1 shl state, 'o', false);
end;


procedure TFormSteckdosenMain.ButtonAOnClick(Sender: TObject);
begin
  Send('a',1);
end;

procedure TFormSteckdosenMain.ButtonAOffClick(Sender: TObject);
begin
  Send('a',0);
end;

procedure TFormSteckdosenMain.ButtonBOnClick(Sender: TObject);
begin
  Send('b',1);
end;

procedure TFormSteckdosenMain.ButtonBOffClick(Sender: TObject);
begin
  Send('b',0);
end;

procedure TFormSteckdosenMain.ButtonCOnClick(Sender: TObject);
begin
  Send('c',1);
end;

procedure TFormSteckdosenMain.ButtonCOffClick(Sender: TObject);
begin
  Send('c',0);
end;

procedure TFormSteckdosenMain.ButtonDOnClick(Sender: TObject);
begin
  Send('d',1);
end;

procedure TFormSteckdosenMain.ButtonDOffClick(Sender: TObject);
begin
  Send('d',0);
end;

end.

