An interesting question from a customer came in that went something like this (I love that he gave lots of details/background so I easily get the big picture, and in this case, go in a different direction than his initial question).

At what point does a string table set as “persistent” lose its data? … I have a multi-language interface [using] two string tables stEnglish and stPORTUGESE [and] a numeric variable called nLANGUAGE… I use a button to toggle the bit in nLANGUAGE from 0 to 1.

I then have alternating visible labels which each look at the respecting table and become visible based on the bit. So initially, ENGLISH shows, then once the button is pushed the toggle sets the language to PORTUGESE. All of this works great. (if there’s a better way to toggle text from engine, PLEASE let me know)…

Should I initialize the table via hard code?

To answer his final question first, I’d say: “yes, hard-code those tables.” This is based on an assumption that he won’t need to be changing these table values very often or on-the fly. (Of course, with PAC Project you have many options for initializing and/or changing variables.)

Assuming he takes this suggestion, he won’t need to use persistent memory – which gets wiped out (as documented in form 1700, chapter 9) when:

A strategy with a different name is downloaded.

The RAM memory on the controller is cleared.

A new firmware kernel is downloaded to the controller.

The persistent object is changed (modified table length/width, etc.)

He also asked if there was a “better way to toggle text.” I’d suggest have PAC Display (or even groov) use just one string table, which gets swapped out if/when the language is changed.

Worried about speed? In the example above, I wasn’t sure how fast the “Swap languages” block would take to run. If it was 5 seconds that’d be too slow and I’d need to come up with another method.

Luckily, built right into PAC Control debugger is this indicator that shows how long it took a block to run. (Keep in mind that the debugger itself uses resources on the PAC so when this code is running WITHOUT the debugger looking at it, the code will run even faster.)

Here are a couple tricks I use to automate the process of writing repetitive code, and reduce the odds of human error. I like to use a free download called Notepad++. (Thanks, Ben, for telling me about that!) In particular, I love the built-in macro feature to record then play keystrokes.

Let’s suppose I want to initialize a numeric table. I might do it this, perhaps doing a massive copy/paste then changing the indices and values:

[INDENT]ftFakePIDdata[0] = 0.5;
ftFakePIDdata[1] = 1;
ftFakePIDdata[2] = 2;
ftFakePIDdata[3] = 3;
ftFakePIDdata[3] = 6;
ftFakePIDdata[4] = 12;
ftFakePIDdata[5] = 17;
[/INDENT]
But what if I have 500 elements? Or what if I’m human and make an error, like having two [3]s in there? (Did you notice that?) Could be hard to track/fix.

(Notice this is good for letting you insert an element somewhere other than the end w/out having to re-do the index values.) Take this a step further and easily automate even more by creating a macro to add the code around your individual data values, then the Notepad++ “play multiple” feature:

Note: check out the “-1” in there: a handy shortcut for this an other table commands where you can skip the “End Index” and just tell the command: “do the whole table.” Neat-o!

Not only is this code easier to step over than the original, but it’s also easier to maintain because it’ll work even when you change the size of your table. (Notice in the original code, if your table went from 100 elements to 200, you might have a hard-to-find bug.)

Since this thread came back to life, it seemed like a good place to toss in another idea for the original OP’s question. The approach Mary outlined has each Pac Display button getting its text from an element in a string table, which is itself defined by a loop that transfers in text from a string table filled with the chosen language. She showed that even if the table has 500 elements, it still populates very quickly.

However, a scalable approach that might be even faster would be to use pointers. Here is what the chart might look like, showing the variables used in it:

Somewhere in PAC Display, if a new language needs to be chosen, the nIndex_Language_New value gets set to the corresponding index. The conditional block looks for the change to occur.

Once the change in index is detected, the corresponding element of the pointer table gets transferred to the string table pointer that each button is looking to for its text.

The “Text in from Control Engine” dynamic attribute of those PAC Display buttons would look something like this, with each button looking at its assigned element in the string table pointed to by pstButton_Labels:

Ah-ha! Excellent point. You are approaching Black Belt OptoExpert status for your deft use of pointers and even pointer tables.

Now, you force me to point out a couple of limitations in our HMIs which I work around by, in this case, re-loading one string table (which is not-so-efficient, as you noticed).

I don’t actually recall if the HMI was specified as [I]groov[/I], or if I just picked that because it’s nifty and fun. Unfortunately, [I]groov[/I] doesn’t currently support pointers or timers. So, that forced my hand… lucky the copy was pretty fast.

Fortunately, the more mature product PAC Display is cool with pointers, at least, enough that your implementation would be lovely and faster as you mention!

I would love PAC Display (and the OptoScript compiler) even more they could do a double-derefence for us. In other words, if you could skip needed the pstButton_Labels pointer and just have PAC Display directly get the string from something like:

*(pstButton_Language[nIndex_Language_New])[2]

Even better if that 2 could also be a variable. Now were getting into some really crazy pointer stuff.

But I digress.

I should mention, though, the trade-offs we make in these kinds of choices. For example, here I avoided pointers because [I]groov [/I]doesn’t do pointers yet. But in another situation I’d make that same kind of trade-off on speed (if it’s not too much of a hit) vs. “can the next guy understand it.”

Let me explain that.

Obviously, you are perfectly content and happy to use pointers and pointer tables and that’s excellent. Not everyone is so comfortable (especially on a Friday afternoon) and might get a headache trying to understand all that. We have many non-programmers who love our stuff because it’s easy to program without any heavy-duty programming experience or special knowledge. They can do all their logic without ever using a pointer or a subroutine and they like it that way.

Let me give another example of a style/efficiency choice–a pet peeve of mine, actually. While in OptoScript, you could write code that looks like this, and it might even make sense to some people who think this way:

doOutsideLights = not (bDayTime);

I would much rather see this (or at least a comment explaining what that one line above is supposed to do):