News:

  • November 23, 2026, 11:18:06 AM

Login with username, password and session length

Author Topic: How to Make Reusable Code Blocks  (Read 28333 times)

RBPLC

  • Hero Member
  • *****
  • Posts: 586
How to Make Reusable Code Blocks
« on: November 20, 2021, 10:26:09 PM »
What is the best way to make reusable code blocks within DmD? Is there any way to copy code from one Project and paste it into the current project and automatically re-assign memory elements from the pasted code in the event that the current project is already using those memory locations and they do not share the same Nicknames?

brucek

  • Sr. Member
  • ****
  • Posts: 62
Re: How to Make Reusable Code Blocks
« Reply #1 on: November 22, 2021, 07:39:13 AM »
I know you can copy from 1 project and paste to another but the documentation goes with it. When i do it I tend to put in a new project change the documentation then paste that into the project it belongs in. Just less confusing so i don't miss something.

MAEdwards

  • Sr. Member
  • ****
  • Posts: 59
Re: How to Make Reusable Code Blocks
« Reply #2 on: November 22, 2021, 08:09:08 AM »
Attached is a document that shows an example of how I have accomplished this in the past.  However, I have found caveats to this approach if your code block uses the DATAINFO instruction.  Look it over, build a test code, and try it out.  I hope this helps.

** This was originally a PowerPoint, but I had to paste the images into a Word doc in order to attach it to this thread.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to Make Reusable Code Blocks
« Reply #3 on: November 22, 2021, 01:26:52 PM »
These approaches allow you to import code blocks but they don't deal with the issue of not overwriting variables that are already in use. This is one major limitation that I frequently wish there was a good solution for.

MAEdwards

  • Sr. Member
  • ****
  • Posts: 59
Re: How to Make Reusable Code Blocks
« Reply #4 on: November 22, 2021, 01:49:59 PM »
These approaches allow you to import code blocks but they don't deal with the issue of not overwriting variables that are already in use. This is one major limitation that I frequently wish there was a good solution for.

True.  Without the support of local variables, one must be extremely mindful when creating modular code blocks and the variable names within these blocks.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: How to Make Reusable Code Blocks
« Reply #5 on: November 22, 2021, 02:20:08 PM »
...and a major justification for the instantiable program blocks we've talked about.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3628
  • Darth Ladder
Re: How to Make Reusable Code Blocks
« Reply #6 on: November 22, 2021, 10:52:57 PM »
Yes, locally scoped variables would be very cool.

There's also another thing that Siemens does that really works out well.  I'll post more about it tomorrow when I don't have to 'type' on my phone.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to Make Reusable Code Blocks
« Reply #7 on: November 23, 2021, 12:13:30 AM »
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match? 

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: How to Make Reusable Code Blocks
« Reply #8 on: November 23, 2021, 12:04:44 PM »
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?

It's doable, just a matter of available development resource. We're not Siemens or AB, so we have to choose our battles.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3858
    • Host Engineering
Re: How to Make Reusable Code Blocks
« Reply #9 on: November 23, 2021, 12:29:09 PM »
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?

If you make the "reassignable" memory locations as unassigned nicknames after you export them, the work flow to resolve them is already there.  You can even do it with your existing export file (assuming you assigned nicknames to everything that is reassignable).

Here's an example section of an export file:

Code: [Select]
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
"X1","SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
"WX1","RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
"C1","PauseSimRS","",""
#END

Say we want all the ID 1's to be "reassignable" (so X1, WX1, C1), change those lines to the following
Code: [Select]
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
UB,"SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
UN,"RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
UB,"PauseSimRS","",""
#END
UB - means Unassigned Bit
UN - means Unassigned Numeric
US - means Unassigned Struct

After you import the code using Tools->Insert Instructions from File, go to the Tools->Assign Nicknames utility and resolve them for THAT specific project, e.g. RawThemostat may be WX5 in one project, D42 in another.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3628
  • Darth Ladder
Re: How to Make Reusable Code Blocks
« Reply #10 on: November 23, 2021, 12:33:04 PM »
So the thing I was talking about with Siemens is this:

While FB's (function blocks) and FC's (functions -- single scan, non static) do have internal memory, and there are user defined types, there is also another thing that works out nicely for the programmer.   When you create a DB (data block), it's basically just a named heap.    It can contain pretty much anything.   INTs, DWORDs, Timers, UDT instances, counters, whatever, as well as multidimensional arrays of any of the above, in any combination.

So the effect comes out to a potentially very large one-off UDT instance, because both the DB and the members are all named, which makes the code very readable while still keeping associated variables together in one DB.

This is a thing that kind of peripherally relates to code-modularity that I'd very much like to get in DM at some point.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3858
    • Host Engineering
Re: How to Make Reusable Code Blocks
« Reply #11 on: November 23, 2021, 12:50:03 PM »
So the thing I was talking about with Siemens is this:

While FB's (function blocks) and FC's (functions -- single scan, non static) do have internal memory, and there are user defined types, there is also another thing that works out nicely for the programmer.   When you create a DB (data block), it's basically just a named heap.    It can contain pretty much anything.   INTs, DWORDs, Timers, UDT instances, counters, whatever, as well as multidimensional arrays of any of the above, in any combination.

So the effect comes out to a potentially very large one-off UDT instance, because both the DB and the members are all named, which makes the code very readable while still keeping associated variables together in one DB.

This is a thing that kind of peripherally relates to code-modularity that I'd very much like to get in DM at some point.

Absolutely.

In the mean time, you can get an 80% solution with existing features, with existing code blocks and existing memory model, as-is, as RBPLC was asking.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3628
  • Darth Ladder
Re: How to Make Reusable Code Blocks
« Reply #12 on: November 23, 2021, 02:44:08 PM »
Right, my post wasn't in response to his application, just putting up a wish list item on a related topic.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

MAEdwards

  • Sr. Member
  • ****
  • Posts: 59
Re: How to Make Reusable Code Blocks
« Reply #13 on: November 23, 2021, 03:23:34 PM »
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?

If you make the "reassignable" memory locations as unassigned nicknames after you export them, the work flow to resolve them is already there.  You can even do it with your existing export file (assuming you assigned nicknames to everything that is reassignable).

Here's an example section of an export file:

Code: [Select]
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
"X1","SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
"WX1","RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
"C1","PauseSimRS","",""
#END

Say we want all the ID 1's to be "reassignable" (so X1, WX1, C1), change those lines to the following
Code: [Select]
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
UB,"SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
UN,"RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
UB,"PauseSimRS","",""
#END
UB - means Unassigned Bit
UN - means Unassigned Numeric
US - means Unassigned Struct

After you import the code using Tools->Insert Instructions from File, go to the Tools->Assign Nicknames utility and resolve them for THAT specific project, e.g. RawThemostat may be WX5 in one project, D42 in another.

Nice!  Thank you for this information.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to Make Reusable Code Blocks
« Reply #14 on: November 23, 2021, 10:43:20 PM »
It doesn't appear as though you can program with unassigned structures. For example if I name a TMR tTestTimer and initially assign it as an unassigned structure, as I'm writing my generic code using unassigned variables, I'm not allowed to use tTestTimer.done elsewhere in my generic code.