Skip to content

Commit

Permalink
test: Fix and change test cases
Browse files Browse the repository at this point in the history
REAL and LREAL for bit / byte types seems to be wrong as of now.
Maybe we need an external keyword here?
  • Loading branch information
volsa committed Oct 16, 2024
1 parent 51da74a commit 2988383
Show file tree
Hide file tree
Showing 5 changed files with 2,723 additions and 147 deletions.
305 changes: 305 additions & 0 deletions libs/stdlib/iec61131-st/bit_conversion.st
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ END_VAR
LWORD_TO_BOOL := in.0;
END_FUNCTION

(********************
*
* Converts LWORD to REAL
*
*********************)
FUNCTION LWORD_TO_REAL : REAL
VAR_INPUT
in : LWORD;
END_VAR
LWORD_TO_REAL := in;
END_FUNCTION

(********************
*
* Converts DWORD to LWORD
Expand Down Expand Up @@ -94,6 +106,18 @@ END_VAR
DWORD_TO_BOOL := in.0;
END_FUNCTION

(********************
*
* Converts DWORD to LREAL
*
*********************)
FUNCTION DWORD_TO_LREAL : LREAL
VAR_INPUT
in : DWORD;
END_VAR
DWORD_TO_LREAL := in;
END_FUNCTION

(********************
*
* Converts WORD to LWORD
Expand Down Expand Up @@ -142,6 +166,30 @@ END_VAR
WORD_TO_BOOL := in.0;
END_FUNCTION

(********************
*
* Converts WORD to REAL
*
*********************)
FUNCTION WORD_TO_REAL : REAL
VAR_INPUT
in : WORD;
END_VAR
WORD_TO_REAL := in;
END_FUNCTION

(********************
*
* Converts WORD to LREAL
*
*********************)
FUNCTION WORD_TO_LREAL : LREAL
VAR_INPUT
in : WORD;
END_VAR
WORD_TO_LREAL := in;
END_FUNCTION

(********************
*
* Converts BYTE to LWORD
Expand Down Expand Up @@ -190,6 +238,30 @@ END_VAR
BYTE_TO_BOOL := in.0;
END_FUNCTION

(********************
*
* Converts BYTE to REAL
*
*********************)
FUNCTION BYTE_TO_REAL : REAL
VAR_INPUT
in : BYTE;
END_VAR
BYTE_TO_REAL := in;
END_FUNCTION

(********************
*
* Converts BYTE to LREAL
*
*********************)
FUNCTION BYTE_TO_LREAL : LREAL
VAR_INPUT
in : BYTE;
END_VAR
BYTE_TO_LREAL := in;
END_FUNCTION

(********************
*
* Converts BYTE to CHAR
Expand Down Expand Up @@ -270,6 +342,38 @@ END_VAR
END_IF;
END_FUNCTION

(********************
*
* Converts BOOL to REAL
*
*********************)
FUNCTION BOOL_TO_REAL : REAL
VAR_INPUT
in : BOOL;
END_VAR
IF in <> 0 THEN
BOOL_TO_REAL := 1.0;
ELSE
BOOL_TO_REAL := 0.0;
END_IF;
END_FUNCTION

(********************
*
* Converts BOOL to LREAL
*
*********************)
FUNCTION BOOL_TO_LREAL : LREAL
VAR_INPUT
in : BOOL;
END_VAR
IF in <> 0 THEN
BOOL_TO_LREAL := 1.0;
ELSE
BOOL_TO_LREAL := 0.0;
END_IF;
END_FUNCTION

(********************
*
* Converts CHAR to BYTE
Expand Down Expand Up @@ -361,3 +465,204 @@ VAR_INPUT
END_VAR
WCHAR_TO_LWORD := WCHAR_TO_WORD(in);
END_FUNCTION


(********************
*
* Converts SINT to BOOL
*
*********************)
FUNCTION SINT_TO_BOOL : BOOL
VAR_INPUT
in : SINT;
END_VAR
IF in = 0 THEN
SINT_TO_BOOL := FALSE;
ELSE
SINT_TO_BOOL := TRUE;
END_IF;
END_FUNCTION

(********************
*
* Converts USINT to BOOL
*
*********************)
FUNCTION USINT_TO_BOOL : BOOL
VAR_INPUT
in : USINT;
END_VAR
IF in = 0 THEN
USINT_TO_BOOL := 0;
ELSE
USINT_TO_BOOL := 1;
END_IF;
END_FUNCTION

(********************
*
* Converts INT to BOOL
*
*********************)
FUNCTION INT_TO_BOOL : BOOL
VAR_INPUT
in : INT;
END_VAR
IF in = 0 THEN
INT_TO_BOOL := 0;
ELSE
INT_TO_BOOL := 1;
END_IF;
END_FUNCTION

(********************
*
* Converts UINT to BOOL
*
*********************)
FUNCTION UINT_TO_BOOL : BOOL
VAR_INPUT
in : UINT;
END_VAR
IF in = 0 THEN
UINT_TO_BOOL := 0;
ELSE
UINT_TO_BOOL := 1;
END_IF;
END_FUNCTION

(********************
*
* Converts UDINT to BOOL
*
*********************)
FUNCTION UDINT_TO_BOOL : BOOL
VAR_INPUT
in : UDINT;
END_VAR
IF in = 0 THEN
UDINT_TO_BOOL := 0;
ELSE
UDINT_TO_BOOL := 1;
END_IF;
END_FUNCTION

(********************
*
* Converts ULINT to BOOL
*
*********************)
FUNCTION ULINT_TO_BOOL : BOOL
VAR_INPUT
in : ULINT;
END_VAR
IF in = 0 THEN
ULINT_TO_BOOL := 0;
ELSE
ULINT_TO_BOOL := 1;
END_IF;
END_FUNCTION

(********************
*
* Converts REAL to BOOL
*
*********************)
FUNCTION REAL_TO_BOOL : BOOL
VAR_INPUT
in : REAL;
END_VAR
IF in > 0 THEN
REAL_TO_BOOL := 1;
ELSE
REAL_TO_BOOL := 0;
END_IF;
END_FUNCTION

(********************
*
* Converts LREAL to BOOL
*
*********************)
FUNCTION LREAL_TO_BOOL : BOOL
VAR_INPUT
in : LREAL;
END_VAR
IF in > 0 THEN
LREAL_TO_BOOL := 1;
ELSE
LREAL_TO_BOOL := 0;
END_IF;
END_FUNCTION

(********************
*
* Converts REAL to BYTE
*
*********************)
FUNCTION REAL_TO_BYTE : BYTE
VAR_INPUT
in : LREAL;
END_VAR
REAL_TO_BYTE := in;
END_FUNCTION

(********************
*
* Converts LREAL to BYTE
*
*********************)
FUNCTION LREAL_TO_BYTE : BYTE
VAR_INPUT
in : LREAL;
END_VAR
LREAL_TO_BYTE := in;
END_FUNCTION

(********************
*
* Converts REAL to WORD
*
*********************)
FUNCTION REAL_TO_WORD : WORD
VAR_INPUT
in : REAL;
END_VAR
REAL_TO_WORD := in;
END_FUNCTION

(********************
*
* Converts REAL to LWORD
*
*********************)
FUNCTION REAL_TO_LWORD : LWORD
VAR_INPUT
in : REAL;
END_VAR
REAL_TO_LWORD := in;
END_FUNCTION

(********************
*
* Converts LREAL to WORD
*
*********************)
FUNCTION LREAL_TO_WORD : WORD
VAR_INPUT
in : LREAL;
END_VAR
LREAL_TO_WORD := in;
END_FUNCTION

(********************
*
* Converts LREAL to DWORD
*
*********************)
FUNCTION LREAL_TO_DWORD : DWORD
VAR_INPUT
in : LREAL;
END_VAR
LREAL_TO_DWORD := in;
END_FUNCTION
18 changes: 17 additions & 1 deletion libs/stdlib/iec61131-st/to_num.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
f = open("to_x.txt", "w")
f = open("to_num.st", "w")

types = [
"SINT",
Expand All @@ -11,8 +11,20 @@
"ULINT",
"LREAL",
"REAL",

# "BIT",
"BOOL",
"BYTE",
"WORD",
"DWORD",
"LWORD",

"STRING",
"WSTRING",
]

# time, ltime, date, ldate, dt, ldt, tod, ltod

template = """(********************
*
* Converts any other numerical value to {0}
Expand Down Expand Up @@ -59,6 +71,10 @@
for type_a in types:
f.write(template.format(type_a))
for type_b in types:
# Skip something like "TO_INT__STRING"
if type_b == "STRING" or type_b == "WSTRING":
continue

if type_a == type_b:
f.write(src_same.format(type_b, type_a))
else:
Expand Down
Loading

0 comments on commit 2988383

Please sign in to comment.