How to tell if simulating or running on hardware?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
chico
Posts: 34
Joined: Fri Dec 26, 2008 11:30 pm
Has thanked: 12 times
Been thanked: 10 times
Contact:

How to tell if simulating or running on hardware?

Post by chico »

Sometimes delay parameters need to be tweaked for operation on hardware vs. simulation. For example, I have a button that needs to detect a long press vs. a short press. It appears that simulation ignores debounce delays, thus affecting the time spent in loops checking switches.

Is there a technique to make such delays operate in simulation similar to how they operate on the target hardware without having to change a constant before running simulation or programming? Some way that the code can tell if it's running in simulation or on the target hardware, or conversely, some setting in Flowcode to set the simulation performance to match the hardware?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to tell if simulating or running on hardware?

Post by Benj »

Hello,

I've created a simple example for you using a property named Sim. The property is defaulted to False or No for download. When you simulate I have included event macros that will first set the property value to True or Yes, perform the simulation and then restore the property value ready for the next download.
SimDemo.fcfx
(9.97 KiB) Downloaded 257 times

chico
Posts: 34
Joined: Fri Dec 26, 2008 11:30 pm
Has thanked: 12 times
Been thanked: 10 times
Contact:

Re: How to tell if simulating or running on hardware?

Post by chico »

Thank, Benj, the test file works as expected. I tested both hardware and simulation and didn't have to change anything to get both to work correctly with your file.

But when I exported the macros and imported them into my own program, it didn't work. I see that your program has a decision that has a property, while the imported version loses that property.

In your program, decision box has a "sim" property:
Screen Shot 2017-10-30 at 10.08.05 AM.JPG
Screen Shot 2017-10-30 at 10.08.05 AM.JPG (109.98 KiB) Viewed 5048 times
In imported macro, decision has no property:
Screen Shot 2017-10-30 at 10.06.33 AM.JPG
Screen Shot 2017-10-30 at 10.06.33 AM.JPG (99.47 KiB) Viewed 5048 times
I've never played with the Sim macro stuff before...can you give me a very short explanation of how to make it work in this situation, then point me to the Flowcode documentation that would give me a more thorough overview?

Thanks as always...

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to tell if simulating or running on hardware?

Post by Benj »

Hello,

Here are some instructions to replicate what I did in the example.

On the properties window, ensure that Panel is selected on the drop down list at the top of the window, click the drop down next to properties and select Add new, Change cosmetic name to Sim, Property type to True or False, Property Variable to Sim. Click OK and ensure the property value is set to No. I also unticked Writable to stop the user from manually changing the value but this isn't required.
Sim1.jpg
Sim1.jpg (20.19 KiB) Viewed 5029 times
On the project explorer window click on the Events tab, scroll down to the Simulation -> Start, double click the Start item, double click <Add New> and click OK twice, repeat for the Simulation -> Stop item. This should create you two new macros named EV_Start and Ev_Stop which are automatically called when the sim starts and stops.
Sim2.jpg
Sim2.jpg (17.02 KiB) Viewed 5029 times
In the Ev_Start macro add a simulation macro icon. In the icon properties choose the Functions tab and then choose Component -> Property -> SetValue. Use the parameters this, "Sim", True. Do the same in the Ev_Stop macro, this time with the parameters this, "Sim", False. this refers to this component, "Sim" is the name of the property in quotes to avoid referring to the property value and the last parameter sets the state of the property.
Sim3.jpg
Sim3.jpg (60.8 KiB) Viewed 5029 times
You can now add the Sim decisions anywhere in your program where Sim and download need to do different things.

chico
Posts: 34
Joined: Fri Dec 26, 2008 11:30 pm
Has thanked: 12 times
Been thanked: 10 times
Contact:

Re: How to tell if simulating or running on hardware?

Post by chico »

Very nice explanation, thank you, Benj.

I can now see why simply exporting and importing the macros didn't work, there are a few more steps involved.

I hadn't looked into Flowcode events previously because I didn't really see any apparent benefits to real-world development aside from manipulating simulation graphics etc. But being able to use events to automatically change code, based on whether compiling or simulating, is a time-saving benefit.

I intend to take a closer look at the documentation for events, as well as component development, to see if I can discover additional beneficial usages for my projects. Can you point out any particular uses of events that might be considered beneficial to the development process, if you've already followed that path?

Thanks again....

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to tell if simulating or running on hardware?

Post by Benj »

Hello,

One of the best use of events so far for me is the ability to add dynamic code when compiling. For example you can have an array of pretty much unlimited size for the simulation and change the size of the array when compiling. You can also choose to add variables or not. Mouse events are good for mouse interaction in the simulation. Timer events are good for monitoring or animating, the formula allcode uses timer events to move around the panel. The Connection change events are good for re-working out connections etc.

This might be good to show you some tricks.
https://www.instructables.com/id/Two-Wh ... -Simulate/

Post Reply