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 4 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
65 changes: 65 additions & 0 deletions libs/stdlib/iec61131-st/to_num.py
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to keep these generative scripts around? I think they've served their purpose and there is no need to track them via git/keep them in our sources.

Copy link
Member Author

Choose a reason for hiding this comment

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

imo it doesn't hurt to track them in case anything changes in the future where we have to regenerate the ST files but I'm also fine with deleting them. Which one do you prefer?

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
f = open("to_x.txt", "w")

types = [
"SINT",
"USINT",
"INT",
"UINT",
"DINT",
"UDINT",
"LINT",
"ULINT",
"LREAL",
"REAL",
]
Copy link
Member

Choose a reason for hiding this comment

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

It seems like we are missing quite a few types here, i.e. TIME, LTIME, LWORD, (STRING?), etc. I've tried converting a TIME variable to USINT, which will just end up calling TO_USINT__USINT, silently truncating the input down to 8 bits, losing most of the information and returning wrong values.
Are these planned for a different commit?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll have to check what CodeSys supports here, but BOOL(?), BYTE, WORD, DWORD and LWORD are (probably) missing here. I forgot about them somehow


template = """(********************
*
* Converts any other numerical value to {0}
*
*********************)
FUNCTION TO_{0}<T: ANY_NUM> : {0}
VAR_INPUT
in : T;
END_VAR
END_FUNCTION

"""

src = """(********************
*
* Converts {0} to {1}
*
*********************)
FUNCTION TO_{1}__{0} : {1}
VAR_INPUT
in : {0};
END_VAR

TO_{1}__{0} := {0}_TO_{1}(in);
END_FUNCTION

"""

src_same = """(********************
*
* Converts {0} to {1}
*
*********************)
FUNCTION TO_{1}__{0} : {1}
VAR_INPUT
in : {0};
END_VAR

TO_{1}__{0} := in;
END_FUNCTION

"""

for type_a in types:
f.write(template.format(type_a))
for type_b in types:
if type_a == type_b:
f.write(src_same.format(type_b, type_a))
else:
f.write(src.format(type_b, type_a))
Loading
Loading