Skip to content

Commit

Permalink
Merge pull request #24 from simraconsulting/add_has_changed
Browse files Browse the repository at this point in the history
feat: Add HasChanged to all scalar input types
  • Loading branch information
sjuergen authored Dec 18, 2024
2 parents df9f640 + ffbf809 commit a6b4840
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 33 deletions.
17 changes: 9 additions & 8 deletions apax-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"name": "@simatic-ax/io",
"version": "0.0.0-placeholder",
"dependencies": {
"@ax/system-timer": "^8.0.7"
"@ax/system-timer": "^8.0.7",
"@ax/system-math": "^8.0.7"
},
"devDependencies": {
"@ax/sdk": "2411.0.0",
Expand Down Expand Up @@ -59,6 +60,13 @@
"resolved": "https://registry.simatic-ax.siemens.io/@ax/system-timer/-/system-timer-8.0.7.tgz",
"dependencies": {}
},
"@ax/system-math": {
"name": "@ax/system-math",
"version": "8.0.7",
"integrity": "sha512-W+jM3gP4BguLMJJprVvEOXkqmsNsqR3BAWmwRM2lsQ6cgMSYheuzXhpBTR3MaIYX4UYM7RXEoQ6sDEszrQ2q9A==",
"resolved": "https://registry.simatic-ax.siemens.io/@ax/system-math/-/system-math-8.0.7.tgz",
"dependencies": {}
},
"@ax/apax-build": {
"name": "@ax/apax-build",
"version": "2.0.20",
Expand Down Expand Up @@ -710,13 +718,6 @@
"resolved": "https://registry.simatic-ax.siemens.io/@ax/st-docs/-/st-docs-8.0.17.tgz",
"dependencies": {}
},
"@ax/system-math": {
"name": "@ax/system-math",
"version": "8.0.7",
"integrity": "sha512-W+jM3gP4BguLMJJprVvEOXkqmsNsqR3BAWmwRM2lsQ6cgMSYheuzXhpBTR3MaIYX4UYM7RXEoQ6sDEszrQ2q9A==",
"resolved": "https://registry.simatic-ax.siemens.io/@ax/system-math/-/system-math-8.0.7.tgz",
"dependencies": {}
},
"@ax/system-datetime": {
"name": "@ax/system-datetime",
"version": "8.0.7",
Expand Down
1 change: 1 addition & 0 deletions apax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ devDependencies:
"@simatic-ax/snippetscollection": 1.0.0
dependencies:
"@ax/system-timer": ^8.0.7
"@ax/system-math": ^8.0.7
# Files, which will be shipped with the library
files:
- 'README.md'
Expand Down
6 changes: 6 additions & 0 deletions src/Input/BinSignal.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ NAMESPACE Simatic.Ax.IO.Input
_statusQRis : BOOL;
_statusQFal : BOOL;
END_VAR

/// Read the input value from Input area of type BOOL
/// The BOOL input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if invalid = FALSE
METHOD PUBLIC ReadCyclic
VAR_INPUT
signal : BOOL;
Expand All @@ -28,6 +33,7 @@ NAMESPACE Simatic.Ax.IO.Input
END_IF;

_statusQ := _signalInPrefiltered;
_hasChanged := _signalInOld <> _signalInPrefiltered;
_statusQRis := _statusQ AND NOT(_signalInOld);
_statusQFal := NOT(_statusQ) AND _signalInOld;
_signalInOld := _statusQ;
Expand Down
10 changes: 8 additions & 2 deletions src/Input/DintInput.st
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
USING System.Math;

NAMESPACE Simatic.Ax.IO.Input
/// Class to handle DINT input values
CLASS DintInput EXTENDS InputBase
Expand All @@ -8,7 +10,7 @@ NAMESPACE Simatic.Ax.IO.Input
_qBad : BOOL;
END_VAR

/// The max and min value of datataype Dint
/// The max and min value of datataype DINT
VAR CONSTANT PUBLIC
MIN : DINT := DINT#-2147483648;
MAX : DINT := DINT#2147483647;
Expand All @@ -17,15 +19,18 @@ NAMESPACE Simatic.Ax.IO.Input
/// Read the input value from Input area of type Double Integer
/// The double integer input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if valid = FALSE
/// Default value if invalid = 0
/// Tolerance used with HasChanged, defaults to 0
METHOD PUBLIC ReadCyclic
VAR_INPUT
value : DINT;
valid : BOOL := TRUE;
default : DINT := DINT#0;
tolerance : DINT := DINT#0;
END_VAR

THIS.QBad(value := NOT(valid));
_hasChanged := ABS(_value - value) > tolerance;
_value := value;
_default := default;
END_METHOD
Expand All @@ -39,5 +44,6 @@ NAMESPACE Simatic.Ax.IO.Input
Q := _default;
END_IF;
END_METHOD

END_CLASS
END_NAMESPACE
6 changes: 6 additions & 0 deletions src/Input/InputBase.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ NAMESPACE Simatic.Ax.IO.Input
CLASS InputBase
VAR PROTECTED
_isQBad : BOOL; // TRUE : signal is invalid
_hasChanged : BOOL; // TRUE : value changed between reads
END_VAR

/// Set the status to QBad
Expand All @@ -21,6 +22,11 @@ NAMESPACE Simatic.Ax.IO.Input
;
END_METHOD

/// Returns TRUE if the value has changed between this read and the one before
METHOD PUBLIC HasChanged : BOOL;
HasChanged := _hasChanged;
END_METHOD

END_CLASS
END_NAMESPACE

16 changes: 10 additions & 6 deletions src/Input/IntInput.st
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
USING System.Math;

NAMESPACE Simatic.Ax.IO.Input
/// Class to handle INT input values
CLASS IntInput EXTENDS InputBase
Expand All @@ -6,29 +8,31 @@ NAMESPACE Simatic.Ax.IO.Input
_value : INT := 0;
_default : INT := 0;
END_VAR
/// The max and min value of datataype int
/// The max and min value of datataype INT
VAR CONSTANT PUBLIC
MAX : INT := 32767;
MIN : INT := -32768;
END_VAR

/// Read the input value from Input area of type Integer
/// The integer input value
/// Read the input value from Input area of type INT
/// The INT input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if valid = FALSE
/// Default value if invalid = 0
METHOD PUBLIC ReadCyclic
VAR_INPUT
value : INT;
valid : BOOL := TRUE;
default : INT := 0;
tolerance : INT := 0;
END_VAR

THIS.QBad(value := NOT(valid));
_hasChanged := ABS(_value - value) > tolerance;
_value := value;
_default := default;
END_METHOD

/// Returns the actual value of integer
/// Returns the actual value of the INT
/// Actual value of the INT
METHOD PUBLIC Q : Int
IF (NOT(THIS.QBad())) THEN
Expand All @@ -37,6 +41,6 @@ NAMESPACE Simatic.Ax.IO.Input
Q := _default;
END_IF;
END_METHOD

END_CLASS
END_NAMESPACE
21 changes: 15 additions & 6 deletions src/Input/LRealInput.st
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
USING System.Math;

NAMESPACE Simatic.Ax.IO.Input
/// Class to handle LREAL input values
CLASS LRealInput EXTENDS InputBase
Expand All @@ -13,39 +15,45 @@ NAMESPACE Simatic.Ax.IO.Input
MIN : LREAL := LREAL#-1.79769313486231e+308;
END_VAR

/// Read the input value from Input area of type Real
/// The integer input value
/// Read the input value from Input area of type LWORD
/// The LWORD input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if valid = FALSE
/// Default value if invalid = 0.0
/// Tolerance used with HasChanged, defaults to 0.0
METHOD PUBLIC ReadCyclic
VAR_INPUT
value : LWORD;
valid : BOOL := TRUE;
default : LREAL := LREAL#0.0;
tolerance : LREAL := LREAL#0.0;
END_VAR

THIS.QBad(value := NOT(valid));
_hasChanged := ABS(_value - TO_LREAL(value)) > tolerance;
_value := TO_LREAL(value);
_default := default;
END_METHOD

/// Read the input value from Input area of type Real
/// Read the input value from Input area of type LREAL
/// The LREAL input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if valid = FALSE
/// Default value if invalid = 0.0
/// Tolerance used with HasChanged, defaults to 0.0
METHOD PUBLIC ReadCyclic
VAR_INPUT
value : LREAL;
valid : BOOL := TRUE;
default : LREAL := LREAL#0.0;
tolerance : LREAL := LREAL#0.0;
END_VAR

THIS.QBad(value := NOT(valid));
_hasChanged := ABS(_value - value) > tolerance;
_value := value;
_default := default;
END_METHOD

/// Returns the actual value of integer
/// Returns the actual value of the LREAL
/// Actual value of the LREAL input
METHOD PUBLIC Q : LREAL
IF (NOT(THIS.QBad())) THEN
Expand All @@ -54,5 +62,6 @@ NAMESPACE Simatic.Ax.IO.Input
Q := _default;
END_IF;
END_METHOD

END_CLASS
END_NAMESPACE
34 changes: 29 additions & 5 deletions src/Input/RealInput.st
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
USING System.Math;

NAMESPACE Simatic.Ax.IO.Input
/// Class to handle REAL input values
CLASS RealInput EXTENDS InputBase
Expand All @@ -7,29 +9,51 @@ NAMESPACE Simatic.Ax.IO.Input
_default : REAL := REAL#0.0;
END_VAR

/// The max and min value of datataype LReal
/// The max and min value of datataype REAL
VAR CONSTANT PUBLIC
MAX : REAL := REAL#+3.402823e+38;
MIN : REAL := REAL#-3.402823e+38;
END_VAR

/// Read the input value from Input area of type Real
/// The integer input value
/// Read the input value from Input area of type REAL
/// The REAL input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if valid = FALSE
/// Default value if invalid = 0.0
/// Tolerance used with HasChanged, defaults to 0.0
METHOD PUBLIC ReadCyclic
VAR_INPUT
value : DWORD;
valid : BOOL := TRUE;
default : REAL := REAL#0.0;
tolerance : REAL := REAL#0.0;
END_VAR

THIS.QBad(value := NOT(valid));
_hasChanged := ABS(_value - TO_REAL(value)) > tolerance;
_value := TO_REAL(value);
_default := default;
END_METHOD

/// Returns the actual value of integer
/// Read the input value from Input area of type REAL
/// The REAL input value
/// The input value is valid (e.g. periphery is ok)
/// Default value if invalid = 0.0
/// Tolerance used with HasChanged, defaults to 0.0
METHOD PUBLIC ReadCyclic
VAR_INPUT
value : REAL;
valid : BOOL := TRUE;
default : REAL := REAL#0.0;
tolerance : REAL := REAL#0.0;
END_VAR

THIS.QBad(value := NOT(valid));
_hasChanged := ABS(_value - value) > tolerance;
_value := value;
_default := default;
END_METHOD

/// Returns the actual value of the REAL
/// Actual value of the REAL input
METHOD PUBLIC Q : REAL
IF (NOT(THIS.QBad())) THEN
Expand Down
10 changes: 6 additions & 4 deletions src/Input/WordInput.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ NAMESPACE Simatic.Ax.IO.Input
END_VAR


/// The max and min value of datataype word
/// The max and min value of datataype WORD
VAR CONSTANT PUBLIC
MIN : WORD := WORD#16#0;
MAX : WORD := WORD#16#FFFF;
END_VAR


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

THIS.QBad(value := NOT(valid));
_hasChanged := _value <> value;
_value := value;
_default := default;
END_METHOD


/// Returns the actual value of integer
/// Returns the actual value of the WORD
/// Actual value of the WORD input
METHOD PUBLIC Q : WORD
IF (NOT(THIS.QBad())) THEN
Expand All @@ -42,5 +43,6 @@ NAMESPACE Simatic.Ax.IO.Input
Q := _default;
END_IF;
END_METHOD

END_CLASS
END_NAMESPACE
26 changes: 24 additions & 2 deletions test/Input/BinSignalTest.st
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,30 @@ NAMESPACE Simatic.Ax.IO.Input.Test
AxUnit.Assert.Equal(expected := TRUE, actual := o.Q());

END_METHOD



//-----------------------------------------------------------
// test HasChanged
//-----------------------------------------------------------
{Test}
METHOD PUBLIC HasChanged

o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
AxUnit.Assert.Equal(expected := FALSE, actual := o.HasChanged());

o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
AxUnit.Assert.Equal(expected := FALSE, actual := o.HasChanged());

o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
AxUnit.Assert.Equal(expected := TRUE, actual := o.HasChanged());

o.ReadCyclic(signal := TRUE, valid := TRUE, default := FALSE);
o.ReadCyclic(signal := FALSE, valid := TRUE, default := FALSE);
AxUnit.Assert.Equal(expected := TRUE, actual := o.HasChanged());

END_METHOD

END_CLASS
END_NAMESPACE
Loading

0 comments on commit a6b4840

Please sign in to comment.