diff --git a/libs/stdlib/iec61131-st/to_bit.st b/libs/stdlib/iec61131-st/to_bit.st new file mode 100644 index 0000000000..96ce52a397 --- /dev/null +++ b/libs/stdlib/iec61131-st/to_bit.st @@ -0,0 +1,246 @@ +FUNCTION TO_BOOL : 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 : 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 + +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 : 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 + +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 : 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 : 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 + diff --git a/tests/lit/single/conversion/to_bit.st b/tests/lit/single/conversion/to_bit.st new file mode 100644 index 0000000000..dffb927365 --- /dev/null +++ b/tests/lit/single/conversion/to_bit.st @@ -0,0 +1,44 @@ +// Note: Tests are kept simple because there are already tests covering the underliny X_TO_Y functions, which +// the TO_Y(X) functions are based on. + +// RUN: (%COMPILE %s && %RUN) | %CHECK %s +FUNCTION main + printf('%d$N', TO_BOOL(BYTE#0)); // CHECK: 0 + printf('%d$N', TO_BOOL(BYTE#5)); // CHECK: 1 + printf('%d$N', TO_BOOL(WORD#0)); // CHECK: 0 + printf('%d$N', TO_BOOL(WORD#5)); // CHECK: 1 + printf('%d$N', TO_BOOL(DWORD#0)); // CHECK: 0 + printf('%d$N', TO_BOOL(DWORD#5)); // CHECK: 1 + printf('%d$N', TO_BOOL(LWORD#0)); // CHECK: 0 + printf('%d$N', TO_BOOL(LWORD#5)); // CHECK: 1 + + printf('%d$N', TO_BYTE(BOOL#FALSE)); // CHECK: 0 + printf('%d$N', TO_BYTE(BOOL#TRUE)); // CHECK: 1 + // printf('%d$N', TO_BYTE(CHAR#'a')); // CCHECK: 97 + printf('%d$N', TO_BYTE(WORD#5)); // CHECK: 5 + printf('%d$N', TO_BYTE(DWORD#5)); // CHECK: 5 + printf('%d$N', TO_BYTE(LWORD#5)); // CHECK: 5 + + printf('%d$N', TO_WORD(BOOL#FALSE)); // CHECK: 0 + printf('%d$N', TO_WORD(BOOL#TRUE)); // CHECK: 1 + // printf('%d$N', TO_WORD(CHAR#5)); // CCHECK: 5 + printf('%d$N', TO_WORD(BYTE#5)); // CHECK: 5 + printf('%d$N', TO_WORD(DWORD#5)); // CHECK: 5 + printf('%d$N', TO_WORD(LWORD#5)); // CHECK: 5 + + printf('%d$N', TO_DWORD(BOOL#FALSE)); // CHECK: 0 + printf('%d$N', TO_DWORD(BOOL#TRUE)); // CHECK: 1 + // printf('%d$N', TO_DWORD(CHAR#5)); // CCHECK: 5 + // printf('%d$N', TO_DWORD(WCHAR#5)); // CCHECK: 5 + printf('%d$N', TO_DWORD(BYTE#5)); // CHECK: 5 + printf('%d$N', TO_DWORD(WORD#5)); // CHECK: 5 + printf('%d$N', TO_DWORD(LWORD#5)); // CHECK: 5 + + printf('%d$N', TO_LWORD(BOOL#FALSE)); // CHECK: 0 + printf('%d$N', TO_LWORD(BOOL#TRUE)); // CHECK: 1 + // printf('%d$N', TO_LWORD(CHAR#5)); // CCHECK: 5 + // printf('%d$N', TO_LWORD(WCHAR#5)); // CCHECK: 5 + printf('%d$N', TO_LWORD(BYTE#5)); // CHECK: 5 + printf('%d$N', TO_LWORD(WORD#5)); // CHECK: 5 + printf('%d$N', TO_LWORD(DWORD#5)); // CHECK: 5 +END_FUNCTION \ No newline at end of file