source: PinConnection/CommConcentrator.pas

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