source: PinConnection/UCommConcentrator.pas

Last change on this file was 416, checked in by chronos, 12 years ago
  • Added: TDeviceConnector in PinConnection package for generalized routing serial data to various TCommNode objects.
File size: 3.2 KB
Line 
1// All pins received data is written to main pin
2// Data received on main pin is sent to all pins
3
4unit UCommConcentrator;
5
6{$mode Delphi}{$H+}
7
8interface
9
10uses
11 Classes, SysUtils, Contnrs, UCommPin, SpecializedList;
12
13type
14 TCommConcentrator = class;
15
16 { TPinList }
17
18 TPinList = class(TObjectList)
19 Concentrator: TCommConcentrator;
20 function Add(AObject: TObject): Integer;
21 function AddNew: TCommPin;
22 function Extract(Item: TObject): TObject;
23 procedure Insert(Index: Integer; AObject: TObject);
24 end;
25
26 { TCommConcentrator }
27
28 TCommConcentrator = class(TCommNode)
29 private
30 FActive: Boolean;
31 FPins: TPinList;
32 FMain: TCommPin;
33 procedure MainReceive(Sender: TCommPin; Stream: TListByte);
34 procedure MainSetStatus(Sender: TCommPin; Status: Integer);
35 procedure Receive(Sender: TCommPin; Stream: TListByte);
36 procedure SetStatus(Sender: TCommPin; Status: Integer);
37 public
38 constructor Create(AOwner: TComponent); override;
39 destructor Destroy; override;
40 property Pins: TPinList read FPins write FPins;
41 property Main: TCommPin read FMain write FMain;
42 end;
43
44implementation
45
46{ TPinList }
47
48function TPinList.Add(AObject: TObject): Integer;
49begin
50 Result := inherited Add(AObject);
51 TCommPin(AObject).OnReceive := Concentrator.Receive;
52 TCommPin(AObject).OnSetSatus := Concentrator.SetStatus;
53end;
54
55function TPinList.AddNew: TCommPin;
56begin
57 Result := TCommPin(Items[Add(TCommPin.Create)]);
58 Result.Node := Concentrator;
59end;
60
61function TPinList.Extract(Item: TObject): TObject;
62begin
63 TCommPin(Item).OnReceive := nil;
64 TCommPin(Item).OnSetSatus := nil;
65 Result := inherited Extract(Item);
66end;
67
68procedure TPinList.Insert(Index: Integer; AObject: TObject);
69begin
70 inherited Insert(Index, AObject);
71 TCommPin(AObject).OnReceive := Concentrator.Receive;
72 TCommPin(AObject).OnSetSatus := Concentrator.SetStatus;
73end;
74
75{ TCommConcentrator }
76
77procedure TCommConcentrator.MainReceive(Sender: TCommPin; Stream: TListByte);
78var
79 I: Integer;
80begin
81 if FActive then begin
82 // Broadcast received packet to all other pins
83 for I := 0 to FPins.Count - 1 do
84 if Sender <> FPins[I] then
85 TCommPin(FPins[I]).Send(Stream);
86 end;
87end;
88
89procedure TCommConcentrator.MainSetStatus(Sender: TCommPin; Status: Integer);
90var
91 I: Integer;
92begin
93 if FActive then begin
94 // Broadcast received packet to all other pins
95 for I := 0 to FPins.Count - 1 do
96 if Sender <> FPins[I] then
97 TCommPin(FPins[I]).Status := Status;
98 end;
99end;
100
101procedure TCommConcentrator.Receive(Sender: TCommPin; Stream: TListByte);
102begin
103 if FActive then FMain.Send(Stream);
104end;
105
106procedure TCommConcentrator.SetStatus(Sender: TCommPin; Status: Integer);
107begin
108 if FActive then FMain.Status := Status;
109end;
110
111constructor TCommConcentrator.Create(AOwner: TComponent);
112begin
113 inherited;
114 FPins := TPinList.Create;
115 FPins.Concentrator := Self;
116 FMain := TCommPin.Create;
117 FMain.Node := Self;
118 FMain.OnSetSatus := MainSetStatus;
119 FMain.OnReceive := MainReceive;
120end;
121
122destructor TCommConcentrator.Destroy;
123begin
124 Active := False;
125 FreeAndNil(FPins);
126 FreeAndNil(FMain);
127 inherited Destroy;
128end;
129
130end.
131
Note: See TracBrowser for help on using the repository browser.