Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TO_<NUM> library functions #1334

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
Loading