diff --git a/libs/stdlib/iec61131-st/bit_conversion.st b/libs/stdlib/iec61131-st/bit_conversion.st index d2ced7aa90..315e6b2b33 100644 --- a/libs/stdlib/iec61131-st/bit_conversion.st +++ b/libs/stdlib/iec61131-st/bit_conversion.st @@ -286,6 +286,22 @@ END_VAR CHAR_TO_BYTE := ptr^; END_FUNCTION +(******************** +* +* Converts WCHAR to BYTE +* +*********************) +FUNCTION WCHAR_TO_BYTE : BYTE +VAR_INPUT + in : WCHAR; +END_VAR +VAR + ptr : REF_TO BYTE; +END_VAR + ptr := REF(in); + WCHAR_TO_BYTE := ptr^; +END_FUNCTION + (******************** * * Converts CHAR to WORD diff --git a/libs/stdlib/iec61131-st/to_bit.st b/libs/stdlib/iec61131-st/to_bit.st new file mode 100644 index 0000000000..a9f6c13101 --- /dev/null +++ b/libs/stdlib/iec61131-st/to_bit.st @@ -0,0 +1,302 @@ +FUNCTION TO_BOOL : BOOL + VAR_INPUT + in : T; + END_VAR +END_FUNCTION + +FUNCTION TO_BOOL__BOOL : BOOL + VAR_INPUT + in : BOOL; + END_VAR + + TO_BOOL__BOOL := in; +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__BYTE : BYTE + VAR_INPUT + in : BYTE; + END_VAR + + TO_BYTE__BYTE := in; +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__WCHAR : BYTE + VAR_INPUT + in : WCHAR; + END_VAR + + TO_BYTE__WCHAR := WCHAR_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__WORD : WORD + VAR_INPUT + in : WORD; + END_VAR + + TO_WORD__WORD := in; +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__WCHAR : WORD + VAR_INPUT + in : WCHAR; + END_VAR + + TO_WORD__WCHAR := WCHAR_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__DWORD : DWORD + VAR_INPUT + in : DWORD; + END_VAR + + TO_DWORD__DWORD := in; +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__LWORD : LWORD + VAR_INPUT + in : LWORD; + END_VAR + + TO_LWORD__LWORD := in; +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/libs/stdlib/tests/bit_conversion_tests.rs b/libs/stdlib/tests/bit_conversion_tests.rs index 7eb478fc77..1c973d3794 100644 --- a/libs/stdlib/tests/bit_conversion_tests.rs +++ b/libs/stdlib/tests/bit_conversion_tests.rs @@ -670,6 +670,35 @@ fn char_to_byte() { assert_eq!(maintype.c, 99u8); } +#[test] +fn wchar_to_byte() { + #[derive(Default)] + struct Main { + a: u8, + b: u8, + c: u8, + } + + let src = r#" + PROGRAM main + VAR + a : BYTE; + b : BYTE; + c : BYTE; + END_VAR + a := WCHAR_TO_BYTE(WCHAR#"a"); + b := WCHAR_TO_BYTE(WCHAR#"b"); + c := WCHAR_TO_BYTE(WCHAR#"c"); + END_PROGRAM + "#; + let sources = add_std!(src, "bit_conversion.st"); + let mut maintype = Main::default(); + let _res: u8 = compile_and_run(sources, &mut maintype); + assert_eq!(maintype.a, 97u8); + assert_eq!(maintype.b, 98u8); + assert_eq!(maintype.c, 99u8); +} + #[test] fn char_to_word() { #[derive(Default)] diff --git a/tests/lit/single/conversion/to_bit.st b/tests/lit/single/conversion/to_bit.st new file mode 100644 index 0000000000..2c38fef776 --- /dev/null +++ b/tests/lit/single/conversion/to_bit.st @@ -0,0 +1,52 @@ +// 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(BOOL#FALSE)); // CHECK: 0 + printf('%d$N', TO_BOOL(BOOL#TRUE)); // CHECK: 1 + 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(BYTE#5)); // CHECK: 5 + 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')); // CHECK: 97 + printf('%d$N', TO_BYTE(WCHAR#"a")); // CHECK: 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(WORD#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#'a')); // CHECK: 97 + printf('%d$N', TO_WORD(WCHAR#"a")); // CHECK: 97 + 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(DWORD#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#'a')); // CHECK: 97 + printf('%d$N', TO_DWORD(WCHAR#'a')); // CHECK: 97 + 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(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#'a')); // CHECK: 97 + printf('%d$N', TO_LWORD(WCHAR#'a')); // CHECK: 97 + 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