Skip to content

Commit a6b4840

Browse files
authored
Merge pull request #24 from simraconsulting/add_has_changed
feat: Add HasChanged to all scalar input types
2 parents df9f640 + ffbf809 commit a6b4840

15 files changed

+220
-33
lines changed

apax-lock.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"name": "@simatic-ax/io",
88
"version": "0.0.0-placeholder",
99
"dependencies": {
10-
"@ax/system-timer": "^8.0.7"
10+
"@ax/system-timer": "^8.0.7",
11+
"@ax/system-math": "^8.0.7"
1112
},
1213
"devDependencies": {
1314
"@ax/sdk": "2411.0.0",
@@ -59,6 +60,13 @@
5960
"resolved": "https://registry.simatic-ax.siemens.io/@ax/system-timer/-/system-timer-8.0.7.tgz",
6061
"dependencies": {}
6162
},
63+
"@ax/system-math": {
64+
"name": "@ax/system-math",
65+
"version": "8.0.7",
66+
"integrity": "sha512-W+jM3gP4BguLMJJprVvEOXkqmsNsqR3BAWmwRM2lsQ6cgMSYheuzXhpBTR3MaIYX4UYM7RXEoQ6sDEszrQ2q9A==",
67+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/system-math/-/system-math-8.0.7.tgz",
68+
"dependencies": {}
69+
},
6270
"@ax/apax-build": {
6371
"name": "@ax/apax-build",
6472
"version": "2.0.20",
@@ -710,13 +718,6 @@
710718
"resolved": "https://registry.simatic-ax.siemens.io/@ax/st-docs/-/st-docs-8.0.17.tgz",
711719
"dependencies": {}
712720
},
713-
"@ax/system-math": {
714-
"name": "@ax/system-math",
715-
"version": "8.0.7",
716-
"integrity": "sha512-W+jM3gP4BguLMJJprVvEOXkqmsNsqR3BAWmwRM2lsQ6cgMSYheuzXhpBTR3MaIYX4UYM7RXEoQ6sDEszrQ2q9A==",
717-
"resolved": "https://registry.simatic-ax.siemens.io/@ax/system-math/-/system-math-8.0.7.tgz",
718-
"dependencies": {}
719-
},
720721
"@ax/system-datetime": {
721722
"name": "@ax/system-datetime",
722723
"version": "8.0.7",

apax.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ devDependencies:
3030
"@simatic-ax/snippetscollection": 1.0.0
3131
dependencies:
3232
"@ax/system-timer": ^8.0.7
33+
"@ax/system-math": ^8.0.7
3334
# Files, which will be shipped with the library
3435
files:
3536
- 'README.md'

src/Input/BinSignal.st

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ NAMESPACE Simatic.Ax.IO.Input
1212
_statusQRis : BOOL;
1313
_statusQFal : BOOL;
1414
END_VAR
15+
16+
/// Read the input value from Input area of type BOOL
17+
/// The BOOL input value
18+
/// The input value is valid (e.g. periphery is ok)
19+
/// Default value if invalid = FALSE
1520
METHOD PUBLIC ReadCyclic
1621
VAR_INPUT
1722
signal : BOOL;
@@ -28,6 +33,7 @@ NAMESPACE Simatic.Ax.IO.Input
2833
END_IF;
2934

3035
_statusQ := _signalInPrefiltered;
36+
_hasChanged := _signalInOld <> _signalInPrefiltered;
3137
_statusQRis := _statusQ AND NOT(_signalInOld);
3238
_statusQFal := NOT(_statusQ) AND _signalInOld;
3339
_signalInOld := _statusQ;

src/Input/DintInput.st

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
USING System.Math;
2+
13
NAMESPACE Simatic.Ax.IO.Input
24
/// Class to handle DINT input values
35
CLASS DintInput EXTENDS InputBase
@@ -8,7 +10,7 @@ NAMESPACE Simatic.Ax.IO.Input
810
_qBad : BOOL;
911
END_VAR
1012

11-
/// The max and min value of datataype Dint
13+
/// The max and min value of datataype DINT
1214
VAR CONSTANT PUBLIC
1315
MIN : DINT := DINT#-2147483648;
1416
MAX : DINT := DINT#2147483647;
@@ -17,15 +19,18 @@ NAMESPACE Simatic.Ax.IO.Input
1719
/// Read the input value from Input area of type Double Integer
1820
/// The double integer input value
1921
/// The input value is valid (e.g. periphery is ok)
20-
/// Default value if valid = FALSE
22+
/// Default value if invalid = 0
23+
/// Tolerance used with HasChanged, defaults to 0
2124
METHOD PUBLIC ReadCyclic
2225
VAR_INPUT
2326
value : DINT;
2427
valid : BOOL := TRUE;
2528
default : DINT := DINT#0;
29+
tolerance : DINT := DINT#0;
2630
END_VAR
2731

2832
THIS.QBad(value := NOT(valid));
33+
_hasChanged := ABS(_value - value) > tolerance;
2934
_value := value;
3035
_default := default;
3136
END_METHOD
@@ -39,5 +44,6 @@ NAMESPACE Simatic.Ax.IO.Input
3944
Q := _default;
4045
END_IF;
4146
END_METHOD
47+
4248
END_CLASS
4349
END_NAMESPACE

src/Input/InputBase.st

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ NAMESPACE Simatic.Ax.IO.Input
33
CLASS InputBase
44
VAR PROTECTED
55
_isQBad : BOOL; // TRUE : signal is invalid
6+
_hasChanged : BOOL; // TRUE : value changed between reads
67
END_VAR
78

89
/// Set the status to QBad
@@ -21,6 +22,11 @@ NAMESPACE Simatic.Ax.IO.Input
2122
;
2223
END_METHOD
2324

25+
/// Returns TRUE if the value has changed between this read and the one before
26+
METHOD PUBLIC HasChanged : BOOL;
27+
HasChanged := _hasChanged;
28+
END_METHOD
29+
2430
END_CLASS
2531
END_NAMESPACE
2632

src/Input/IntInput.st

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
USING System.Math;
2+
13
NAMESPACE Simatic.Ax.IO.Input
24
/// Class to handle INT input values
35
CLASS IntInput EXTENDS InputBase
@@ -6,29 +8,31 @@ NAMESPACE Simatic.Ax.IO.Input
68
_value : INT := 0;
79
_default : INT := 0;
810
END_VAR
9-
/// The max and min value of datataype int
11+
/// The max and min value of datataype INT
1012
VAR CONSTANT PUBLIC
1113
MAX : INT := 32767;
1214
MIN : INT := -32768;
1315
END_VAR
1416

15-
/// Read the input value from Input area of type Integer
16-
/// The integer input value
17+
/// Read the input value from Input area of type INT
18+
/// The INT input value
1719
/// The input value is valid (e.g. periphery is ok)
18-
/// Default value if valid = FALSE
20+
/// Default value if invalid = 0
1921
METHOD PUBLIC ReadCyclic
2022
VAR_INPUT
2123
value : INT;
2224
valid : BOOL := TRUE;
2325
default : INT := 0;
26+
tolerance : INT := 0;
2427
END_VAR
2528

2629
THIS.QBad(value := NOT(valid));
30+
_hasChanged := ABS(_value - value) > tolerance;
2731
_value := value;
2832
_default := default;
2933
END_METHOD
3034

31-
/// Returns the actual value of integer
35+
/// Returns the actual value of the INT
3236
/// Actual value of the INT
3337
METHOD PUBLIC Q : Int
3438
IF (NOT(THIS.QBad())) THEN
@@ -37,6 +41,6 @@ NAMESPACE Simatic.Ax.IO.Input
3741
Q := _default;
3842
END_IF;
3943
END_METHOD
40-
44+
4145
END_CLASS
4246
END_NAMESPACE

src/Input/LRealInput.st

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
USING System.Math;
2+
13
NAMESPACE Simatic.Ax.IO.Input
24
/// Class to handle LREAL input values
35
CLASS LRealInput EXTENDS InputBase
@@ -13,39 +15,45 @@ NAMESPACE Simatic.Ax.IO.Input
1315
MIN : LREAL := LREAL#-1.79769313486231e+308;
1416
END_VAR
1517

16-
/// Read the input value from Input area of type Real
17-
/// The integer input value
18+
/// Read the input value from Input area of type LWORD
19+
/// The LWORD input value
1820
/// The input value is valid (e.g. periphery is ok)
19-
/// Default value if valid = FALSE
21+
/// Default value if invalid = 0.0
22+
/// Tolerance used with HasChanged, defaults to 0.0
2023
METHOD PUBLIC ReadCyclic
2124
VAR_INPUT
2225
value : LWORD;
2326
valid : BOOL := TRUE;
2427
default : LREAL := LREAL#0.0;
28+
tolerance : LREAL := LREAL#0.0;
2529
END_VAR
2630

2731
THIS.QBad(value := NOT(valid));
32+
_hasChanged := ABS(_value - TO_LREAL(value)) > tolerance;
2833
_value := TO_LREAL(value);
2934
_default := default;
3035
END_METHOD
3136

32-
/// Read the input value from Input area of type Real
37+
/// Read the input value from Input area of type LREAL
3338
/// The LREAL input value
3439
/// The input value is valid (e.g. periphery is ok)
35-
/// Default value if valid = FALSE
40+
/// Default value if invalid = 0.0
41+
/// Tolerance used with HasChanged, defaults to 0.0
3642
METHOD PUBLIC ReadCyclic
3743
VAR_INPUT
3844
value : LREAL;
3945
valid : BOOL := TRUE;
4046
default : LREAL := LREAL#0.0;
47+
tolerance : LREAL := LREAL#0.0;
4148
END_VAR
4249

4350
THIS.QBad(value := NOT(valid));
51+
_hasChanged := ABS(_value - value) > tolerance;
4452
_value := value;
4553
_default := default;
4654
END_METHOD
4755

48-
/// Returns the actual value of integer
56+
/// Returns the actual value of the LREAL
4957
/// Actual value of the LREAL input
5058
METHOD PUBLIC Q : LREAL
5159
IF (NOT(THIS.QBad())) THEN
@@ -54,5 +62,6 @@ NAMESPACE Simatic.Ax.IO.Input
5462
Q := _default;
5563
END_IF;
5664
END_METHOD
65+
5766
END_CLASS
5867
END_NAMESPACE

src/Input/RealInput.st

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
USING System.Math;
2+
13
NAMESPACE Simatic.Ax.IO.Input
24
/// Class to handle REAL input values
35
CLASS RealInput EXTENDS InputBase
@@ -7,29 +9,51 @@ NAMESPACE Simatic.Ax.IO.Input
79
_default : REAL := REAL#0.0;
810
END_VAR
911

10-
/// The max and min value of datataype LReal
12+
/// The max and min value of datataype REAL
1113
VAR CONSTANT PUBLIC
1214
MAX : REAL := REAL#+3.402823e+38;
1315
MIN : REAL := REAL#-3.402823e+38;
1416
END_VAR
1517

16-
/// Read the input value from Input area of type Real
17-
/// The integer input value
18+
/// Read the input value from Input area of type REAL
19+
/// The REAL input value
1820
/// The input value is valid (e.g. periphery is ok)
19-
/// Default value if valid = FALSE
21+
/// Default value if invalid = 0.0
22+
/// Tolerance used with HasChanged, defaults to 0.0
2023
METHOD PUBLIC ReadCyclic
2124
VAR_INPUT
2225
value : DWORD;
2326
valid : BOOL := TRUE;
2427
default : REAL := REAL#0.0;
28+
tolerance : REAL := REAL#0.0;
2529
END_VAR
2630

2731
THIS.QBad(value := NOT(valid));
32+
_hasChanged := ABS(_value - TO_REAL(value)) > tolerance;
2833
_value := TO_REAL(value);
2934
_default := default;
3035
END_METHOD
3136

32-
/// Returns the actual value of integer
37+
/// Read the input value from Input area of type REAL
38+
/// The REAL input value
39+
/// The input value is valid (e.g. periphery is ok)
40+
/// Default value if invalid = 0.0
41+
/// Tolerance used with HasChanged, defaults to 0.0
42+
METHOD PUBLIC ReadCyclic
43+
VAR_INPUT
44+
value : REAL;
45+
valid : BOOL := TRUE;
46+
default : REAL := REAL#0.0;
47+
tolerance : REAL := REAL#0.0;
48+
END_VAR
49+
50+
THIS.QBad(value := NOT(valid));
51+
_hasChanged := ABS(_value - value) > tolerance;
52+
_value := value;
53+
_default := default;
54+
END_METHOD
55+
56+
/// Returns the actual value of the REAL
3357
/// Actual value of the REAL input
3458
METHOD PUBLIC Q : REAL
3559
IF (NOT(THIS.QBad())) THEN

src/Input/WordInput.st

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ NAMESPACE Simatic.Ax.IO.Input
99
END_VAR
1010

1111

12-
/// The max and min value of datataype word
12+
/// The max and min value of datataype WORD
1313
VAR CONSTANT PUBLIC
1414
MIN : WORD := WORD#16#0;
1515
MAX : WORD := WORD#16#FFFF;
1616
END_VAR
1717

1818

19-
/// Read the input value from Input area of type Integer
20-
/// The integer input value
19+
/// Read the input value from Input area of type WORD
20+
/// The WORD input value
2121
/// The input value is valid (e.g. periphery is ok)
2222
/// Default value if valid = FALSE
2323
METHOD PUBLIC ReadCyclic
@@ -28,12 +28,13 @@ NAMESPACE Simatic.Ax.IO.Input
2828
END_VAR
2929

3030
THIS.QBad(value := NOT(valid));
31+
_hasChanged := _value <> value;
3132
_value := value;
3233
_default := default;
3334
END_METHOD
3435

3536

36-
/// Returns the actual value of integer
37+
/// Returns the actual value of the WORD
3738
/// Actual value of the WORD input
3839
METHOD PUBLIC Q : WORD
3940
IF (NOT(THIS.QBad())) THEN
@@ -42,5 +43,6 @@ NAMESPACE Simatic.Ax.IO.Input
4243
Q := _default;
4344
END_IF;
4445
END_METHOD
46+
4547
END_CLASS
4648
END_NAMESPACE

test/Input/BinSignalTest.st

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,30 @@ NAMESPACE Simatic.Ax.IO.Input.Test
168168
AxUnit.Assert.Equal(expected := TRUE, actual := o.Q());
169169

170170
END_METHOD
171-
172-
171+
172+
//-----------------------------------------------------------
173+
// test HasChanged
174+
//-----------------------------------------------------------
175+
{Test}
176+
METHOD PUBLIC HasChanged
177+
178+
o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
179+
o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
180+
AxUnit.Assert.Equal(expected := FALSE, actual := o.HasChanged());
181+
182+
o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
183+
o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
184+
AxUnit.Assert.Equal(expected := FALSE, actual := o.HasChanged());
185+
186+
o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
187+
o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
188+
AxUnit.Assert.Equal(expected := TRUE, actual := o.HasChanged());
189+
190+
o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
191+
o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
192+
AxUnit.Assert.Equal(expected := TRUE, actual := o.HasChanged());
193+
194+
END_METHOD
173195

174196
END_CLASS
175197
END_NAMESPACE

0 commit comments

Comments
 (0)