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: Add TO_<BIT> conversion functions #1353

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
246 changes: 246 additions & 0 deletions libs/stdlib/iec61131-st/to_bit.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
FUNCTION TO_BOOL<T: ANY> : BOOL
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_BOOL__BYTE : BOOL
VAR_INPUT
in : BYTE;
END_VAR

TO_BOOL__BYTE := BYTE_TO_BOOL(in);
END_FUNCTION

FUNCTION TO_BOOL__WORD : BOOL
VAR_INPUT
in : WORD;
END_VAR

TO_BOOL__WORD := WORD_TO_BOOL(in);
END_FUNCTION

FUNCTION TO_BOOL__DWORD : BOOL
VAR_INPUT
in : DWORD;
END_VAR

TO_BOOL__DWORD := DWORD_TO_BOOL(in);
END_FUNCTION

FUNCTION TO_BOOL__LWORD : BOOL
VAR_INPUT
in : LWORD;
END_VAR

TO_BOOL__LWORD := LWORD_TO_BOOL(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_BYTE<T: ANY> : BYTE
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_BYTE__BOOL : BYTE
VAR_INPUT
in : BOOL;
END_VAR

TO_BYTE__BOOL := BOOL_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__CHAR : BYTE
VAR_INPUT
in : CHAR;
END_VAR

TO_BYTE__CHAR := CHAR_TO_BYTE(in);
END_FUNCTION
Comment on lines +55 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably also add TO_BYTE__WCHAR here and have it behave the same as TO_DWORD__LWORD. Might be nice if one is dealing with UART.


FUNCTION TO_BYTE__WORD : BYTE
VAR_INPUT
in : WORD;
END_VAR

TO_BYTE__WORD := WORD_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__DWORD : BYTE
VAR_INPUT
in : DWORD;
END_VAR

TO_BYTE__DWORD := DWORD_TO_BYTE(in);
END_FUNCTION

FUNCTION TO_BYTE__LWORD : BYTE
VAR_INPUT
in : LWORD;
END_VAR

TO_BYTE__LWORD := LWORD_TO_BYTE(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_WORD<T: ANY> : WORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_WORD__BOOL : WORD
VAR_INPUT
in : BOOL;
END_VAR

TO_WORD__BOOL := BOOL_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__CHAR : WORD
VAR_INPUT
in : CHAR;
END_VAR

TO_WORD__CHAR := CHAR_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__BYTE : WORD
VAR_INPUT
in : BYTE;
END_VAR

TO_WORD__BYTE := BYTE_TO_WORD(in);
END_FUNCTION
Comment on lines +103 to +117
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're missing TO_WORD__WCHAR here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what happens if I call TO_WORD(WORD#...) ? The signature seems to be missing also. I am guessing it is gonna default to BYTE and truncate.

I think the same-type functions are missing in general. I know it doesn't really make sense to call them, but we should cover it regardless I think - better to err on the safe side.


FUNCTION TO_WORD__DWORD : WORD
VAR_INPUT
in : DWORD;
END_VAR

TO_WORD__DWORD := DWORD_TO_WORD(in);
END_FUNCTION

FUNCTION TO_WORD__LWORD : WORD
VAR_INPUT
in : LWORD;
END_VAR

TO_WORD__LWORD := LWORD_TO_WORD(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_DWORD<T: ANY> : DWORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_DWORD__BOOL : DWORD
VAR_INPUT
in : BOOL;
END_VAR

TO_DWORD__BOOL := BOOL_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__CHAR : DWORD
VAR_INPUT
in : CHAR;
END_VAR

TO_DWORD__CHAR := CHAR_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__WCHAR : DWORD
VAR_INPUT
in : WCHAR;
END_VAR

TO_DWORD__WCHAR := WCHAR_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__BYTE : DWORD
VAR_INPUT
in : BYTE;
END_VAR

TO_DWORD__BYTE := BYTE_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__WORD : DWORD
VAR_INPUT
in : WORD;
END_VAR

TO_DWORD__WORD := WORD_TO_DWORD(in);
END_FUNCTION

FUNCTION TO_DWORD__LWORD : DWORD
VAR_INPUT
in : LWORD;
END_VAR

TO_DWORD__LWORD := LWORD_TO_DWORD(in);
END_FUNCTION

// ======================================================================================================== //

FUNCTION TO_LWORD<T: ANY> : LWORD
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

FUNCTION TO_LWORD__BOOL : LWORD
VAR_INPUT
in : BOOL;
END_VAR

TO_LWORD__BOOL := BOOL_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__CHAR : LWORD
VAR_INPUT
in : CHAR;
END_VAR

TO_LWORD__CHAR := CHAR_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__WCHAR : LWORD
VAR_INPUT
in : WCHAR;
END_VAR

TO_LWORD__WCHAR := WCHAR_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__BYTE : LWORD
VAR_INPUT
in : BYTE;
END_VAR

TO_LWORD__BYTE := BYTE_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__WORD : LWORD
VAR_INPUT
in : WORD;
END_VAR

TO_LWORD__WORD := WORD_TO_LWORD(in);
END_FUNCTION

FUNCTION TO_LWORD__DWORD : LWORD
VAR_INPUT
in : DWORD;
END_VAR

TO_LWORD__DWORD := DWORD_TO_LWORD(in);
END_FUNCTION

Loading
Loading