/
LM3.pl
executable file
·255 lines (237 loc) · 9.59 KB
/
LM3.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#! /home/pete/CitrusPerl/perl/bin/perl
# LM3.pl - wxPerl Process Control Example
# uses the LinearMeter3.pm modular meter
#
# Last modified by James M. Lynes, Jr - January 30,2013
#
# Draws and animates 8 Linear Meters, uses LinearMeter3.pm
# Meters change green/red to indicate limit violations
# Creates a 1 second timer to update the animation
# Left click selects/deselects the meter and sets the border to yellow/blue
# Right click on a selected meter pops a limit change dialog
# Right click on a deselected meter pops an error messsage box
# Multiple meters may be selected at the same time - may want to limit
# this in the future.
#
# The panel is created 40 units longer than the meter to allow space
# for drawing the meter label
#
package main;
use strict;
use warnings;
my $app = App->new();
$app->MainLoop;
package App;
use strict;
use warnings;
use base 'Wx::App';
sub OnInit {
my $frame = Frame->new();
$frame->Show(1);
}
package Frame;
use strict;
use warnings;
use Wx qw(:everything);
use base qw(Wx::Frame);
use LinearMeter3;
use Data::Dumper;
use Wx::Event qw(EVT_PAINT EVT_TIMER EVT_LEFT_DOWN EVT_RIGHT_DOWN);
sub new {
my ($self) = @_;
my($meters, $width, $height) = (8, 100, 550);
$self = $self->SUPER::new(undef, -1, "wxPerl Process Control Example", wxDefaultPosition,
[(($meters)+1)*10+20 + (($meters)*$width),($height+90)]);
my $font = Wx::Font->new(12, wxFONTFAMILY_SWISS, wxNORMAL, wxBOLD);
$self->SetFont($font);
Wx::StaticText->new($self, -1, "Boiler 1", [190, 15], wxDefaultSize, wxALIGN_LEFT);
Wx::StaticText->new($self, -1, "Boiler 2", [650, 15], wxDefaultSize, wxALIGN_LEFT);
# Create 8 panels to hold 8 meters - single row layout
$self->{MP1} = Wx::Panel->new($self, wxID_ANY, [10 ,40], [$width, $height+40]);
$self->{MP2} = Wx::Panel->new($self, wxID_ANY, [($width*1)+20 ,40], [$width, $height+40]);
$self->{MP3} = Wx::Panel->new($self, wxID_ANY, [($width*2)+30 ,40], [$width, $height+40]);
$self->{MP4} = Wx::Panel->new($self, wxID_ANY, [($width*3)+40 ,40], [$width, $height+40]);
$self->{MP5} = Wx::Panel->new($self, wxID_ANY, [($width*4)+70 ,40], [$width, $height+40]);
$self->{MP6} = Wx::Panel->new($self, wxID_ANY, [($width*5)+80 ,40], [$width, $height+40]);
$self->{MP7} = Wx::Panel->new($self, wxID_ANY, [($width*6)+90 ,40], [$width, $height+40]);
$self->{MP8} = Wx::Panel->new($self, wxID_ANY, [($width*7)+100 ,40], [$width, $height+40]);
# Create 8 meter objects - Override some default values
$self->{LM1} = LinearMeter->new();
$self->{LM1}->InitialValue(73);
$self->{LM1}->Limit(76);
$self->{LM1}->Label("Temp 1");
$self->{LM1}->MeterHeight($height);
$self->{LM1}->MeterWidth($width);
$self->{LM2} = LinearMeter->new();
$self->{LM2}->InitialValue(28);
$self->{LM2}->Limit(31);
$self->{LM2}->Label("Flow 1");
$self->{LM2}->MeterHeight($height);
$self->{LM2}->MeterWidth($width);
$self->{LM3} = LinearMeter->new();
$self->{LM3}->InitialValue(42);
$self->{LM3}->Limit(46);
$self->{LM3}->Label("Pressure 1");
$self->{LM3}->MeterHeight($height);
$self->{LM3}->MeterWidth($width);
$self->{LM4} = LinearMeter->new();
$self->{LM4}->InitialValue(62);
$self->{LM4}->Limit(66);
$self->{LM4}->Label("Level 1");
$self->{LM4}->MeterHeight($height);
$self->{LM4}->MeterWidth($width);
$self->{LM5} = LinearMeter->new();
$self->{LM5}->InitialValue(24);
$self->{LM5}->Limit(28);
$self->{LM5}->Label("Temp 2");
$self->{LM5}->MeterHeight($height);
$self->{LM5}->MeterWidth($width);
$self->{LM6} = LinearMeter->new();
$self->{LM6}->InitialValue(63);
$self->{LM6}->Limit(66);
$self->{LM6}->Label("Flow 2");
$self->{LM6}->MeterHeight($height);
$self->{LM6}->MeterWidth($width);
$self->{LM7} = LinearMeter->new();
$self->{LM7}->InitialValue(33);
$self->{LM7}->Limit(37);
$self->{LM7}->Label("Pressure 2");
$self->{LM7}->MeterHeight($height);
$self->{LM7}->MeterWidth($width);
$self->{LM8} = LinearMeter->new();
$self->{LM8}->InitialValue(81);
$self->{LM8}->Limit(85);
$self->{LM8}->Label("Level 2");
$self->{LM8}->MeterHeight($height);
$self->{LM8}->MeterWidth($width);
#
# Set up Event Handlers -------------------------------------------------------
#
# Timer
my $timer = Wx::Timer->new( $self );
$timer->Start( 1000 ); # 1 second period
EVT_TIMER($self, -1, \&onTimer);
# Paint
EVT_PAINT($self, \&onPaint);
# Mouse
EVT_LEFT_DOWN($self->{MP1}, sub{$self->_evt_left_down( $self->{LM1}, @_);});
EVT_LEFT_DOWN($self->{MP2}, sub{$self->_evt_left_down( $self->{LM2}, @_);});
EVT_LEFT_DOWN($self->{MP3}, sub{$self->_evt_left_down( $self->{LM3}, @_);});
EVT_LEFT_DOWN($self->{MP4}, sub{$self->_evt_left_down( $self->{LM4}, @_);});
EVT_LEFT_DOWN($self->{MP5}, sub{$self->_evt_left_down( $self->{LM5}, @_);});
EVT_LEFT_DOWN($self->{MP6}, sub{$self->_evt_left_down( $self->{LM6}, @_);});
EVT_LEFT_DOWN($self->{MP7}, sub{$self->_evt_left_down( $self->{LM7}, @_);});
EVT_LEFT_DOWN($self->{MP8}, sub{$self->_evt_left_down( $self->{LM8}, @_);});
EVT_RIGHT_DOWN($self->{MP1}, sub{$self->_evt_right_down( $self->{LM1}, @_);});
EVT_RIGHT_DOWN($self->{MP2}, sub{$self->_evt_right_down( $self->{LM2}, @_);});
EVT_RIGHT_DOWN($self->{MP3}, sub{$self->_evt_right_down( $self->{LM3}, @_);});
EVT_RIGHT_DOWN($self->{MP4}, sub{$self->_evt_right_down( $self->{LM4}, @_);});
EVT_RIGHT_DOWN($self->{MP5}, sub{$self->_evt_right_down( $self->{LM5}, @_);});
EVT_RIGHT_DOWN($self->{MP6}, sub{$self->_evt_right_down( $self->{LM6}, @_);});
EVT_RIGHT_DOWN($self->{MP7}, sub{$self->_evt_right_down( $self->{LM7}, @_);});
EVT_RIGHT_DOWN($self->{MP8}, sub{$self->_evt_right_down( $self->{LM8}, @_);});
return $self;
}
1;
#
# Right Mouse Pressed Event - Change the Selected Meter's Limit -----------------
#
sub _evt_right_down {
my($frame, $meter, $panel, $event) = @_;
if($meter->Selected()) {
my $label = $meter->Label();
my $dialog = Wx::TextEntryDialog->new( $frame,
"Select a New Limit", "Change the $label Alarm Limit",
$meter->Limit());
if($dialog->ShowModal == wxID_CANCEL) {
$meter->BorderColour(wxBLUE);
$meter->Selected(0);
return;
};
$meter->Limit($dialog->GetValue());
$meter->BorderColour(wxBLUE);
$meter->Selected(0);
}
else {
my $msg = Wx::MessageBox("No Meter Selected\nLeft Click a Meter to Select",
"Meter Limit Entry Error", wxICON_ERROR, $frame);
}
$event->Skip(1);
}
#
# Left Mouse Pressed Event - Selects a Meter - Selection will Toggle -----------
#
sub _evt_left_down {
my($frame, $meter, $panel, $event) = @_;
if($meter->Selected()) {
$meter->BorderColour(wxBLUE);
$meter->Selected(0);
}
else {
$meter->BorderColour(Wx::Colour->new("yellow"));
$meter->Selected(1);
}
$event->Skip(1);
}
# Simple version of Event Handler Closure
# More complex version fits better for this application
# EVT_LEFT_DOWN($self->{MP1}, sub{
# my($panel, $event) = @_;
# $self->{LM1}->BordorColour(Wx::Colour->new("yellow"));
# $event->Skip(1);
# });
#
# 1 second timer to simulate meter movement ---------------------------------
#
sub onTimer {
my($self, $event) = @_;
# Randomize for each meter
# for a more natural look
my $dir = (rand 10) < 5 ? -1 : 1;
my $inc = (rand 1) * $dir;
$self->{LM1}->InitialValue($self->{LM1}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP1}, $self->{LM1});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM2}->InitialValue($self->{LM2}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP2}, $self->{LM2});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM3}->InitialValue($self->{LM3}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP3}, $self->{LM3});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM4}->InitialValue($self->{LM4}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP4}, $self->{LM4});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM5}->InitialValue($self->{LM5}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP5}, $self->{LM5});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM6}->InitialValue($self->{LM6}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP6}, $self->{LM6});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM7}->InitialValue($self->{LM7}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP7}, $self->{LM7});
$dir = (rand 10) < 5 ? -1 : 1;
$inc = (rand 1) * $dir;
$self->{LM8}->InitialValue($self->{LM8}->InitialValue() + $inc);
LinearMeter->Draw($self->{MP8}, $self->{LM8});
}
#
# Paint the Meters ---------------------------------------------------------------------
#
sub onPaint {
my($self, $event) = @_;
# Draw the 8 meters
LinearMeter->Draw($self->{MP1}, $self->{LM1});
LinearMeter->Draw($self->{MP2}, $self->{LM2});
LinearMeter->Draw($self->{MP3}, $self->{LM3});
LinearMeter->Draw($self->{MP4}, $self->{LM4});
LinearMeter->Draw($self->{MP5}, $self->{LM5});
LinearMeter->Draw($self->{MP6}, $self->{LM6});
LinearMeter->Draw($self->{MP7}, $self->{LM7});
LinearMeter->Draw($self->{MP8}, $self->{LM8});
}