KHarms
Sep 23, 2009, 09:48 PM
We get a lot of questions about “Flags". In the early versions of HCA, the whole concept of persistent state consisted of variables that could only contain the values Yes and No. The only operations were to test for yes, test for no, set yes, set no, and not. “Flags” seemed like a good name.
In later versions of HCA we broadened this concept into variables that can contain any data type. This is similar to un-typed variables in VB declared with “Dim”. At that time we should have changed the name “Flags” to something else but we didn’t.
A Flag (read: Variable) can contain either a number, a string, a date/time, or a Boolean (Yes/ No) value. When HCA evaluates an expression it may change the type of terms in the expression to make things work. For example, suppose you have an expression like this:
Text = _ucase(myData)
This expression assigns to the flag “Text” a string all in uppercase of whatever myData contains. This works fine if the flag myData has a text value. If it has a number value HCA internally converts that value to a text string. Same for a date/time or a Boolean value.
To again illustrate how HCA converts types as needed, the X and Y expressions below both compute to the same value even those one is multiplying two numbers and the other multiplies a number and a string. HCA needs two numbers to multiply, so it just converts the string to a number.
A = 10;
B = “5”;
C = 5;
X = A * B;
Y = C * B;
As you can see flags contain data of many types. Now what can you do with them? To answer that we implemented two Visual Programmer elements: Compute and Compute Test.
In the Compute element you can place any number of assignment statements of the form “Flag name = expression”. More than one assignment statement can be used in the same Compute element. All you need do is to separate the expressions with a semicolon. When the Compute element executes the expressions evaluates and assignments to the flags made.
The ComputeTest element contains a single expression that when evaluated results in either a Yes or No. As described above, the expression is evaluated and whatever value results, it is changed into a yes/no value. Zero is No, any other number is Yes. An empty string is No. Any other string is Yes.
But the real interesting stuff happens when you use the functions built-in to HCA in expressions. These are documented in the user guide. Here is an example.
Suppose that you want to have a program triggered by some event change the level of a light and then after some time set the light level back to where it was before the program started. Here is how you could do that.
Start the program with a Compute element that contains this contents:
oldLevel=_DimPercent(“Home - Switch”)
Then the next elements in your program do whatever you need do with the light. Perhaps raise the level to 100%. Next, delay for some time using a Delay element. Then the last element is another Compute element that says:
NewLevel=_DimToPercent(“Home - Switch”, oldLevel)
That’s it. The program does what you need to do because of the two Compute elements. But there is something more.
The Compute element contains what we call the expression builder and here is how you can use that to make creating the expressions in Compute elements simpler.
After you add a Compute element, start the expression with “oldlevel = “.
70
But now we have to enter the function with the device name and get it all correct. Let’s use the expression builder to do the hard work. Press the Expression Builder button:
71
The first thing to notice about this dialog is the “Insert What” box. The expression builder can insert lots of things into a compute element – the name of an object in your design, a function or a constant. In this case we want to build the first expression in this example. So we choose Function. The “Functions” box says what kinds of function we want to insert. By narrowing our choices it makes it easier to find things. The functions that work with devices are “action” functions so we tick that. The dropdown then shows all the functions of the types we selected. In the dialog, below the selected function, is an short description of what the function does and any arguments to that function and the type of data they expect. Choose “_DimPercent”.
72
The _DimPercent function takes one argument: The name of the device as a string. We could just type in the name of the device we want, but there is an easier way. Each argument has a button to the right of it. Press it and this dialog appears:
73
Again you get to chose what sort of thing you want to insert. We want to insert a device name so we select that option and then choose the device we want. Close the dialog with OK takes us back to the expression builder:
74
Close the expression builder and what we built is inserted into the compute element.
75
All done except for checking the work with Validate.
In summary, the Compute element and the Compute Test element can really expand what the Visual Programmer can do. To go beyond that gets into text scripts and that’s for another time.
In later versions of HCA we broadened this concept into variables that can contain any data type. This is similar to un-typed variables in VB declared with “Dim”. At that time we should have changed the name “Flags” to something else but we didn’t.
A Flag (read: Variable) can contain either a number, a string, a date/time, or a Boolean (Yes/ No) value. When HCA evaluates an expression it may change the type of terms in the expression to make things work. For example, suppose you have an expression like this:
Text = _ucase(myData)
This expression assigns to the flag “Text” a string all in uppercase of whatever myData contains. This works fine if the flag myData has a text value. If it has a number value HCA internally converts that value to a text string. Same for a date/time or a Boolean value.
To again illustrate how HCA converts types as needed, the X and Y expressions below both compute to the same value even those one is multiplying two numbers and the other multiplies a number and a string. HCA needs two numbers to multiply, so it just converts the string to a number.
A = 10;
B = “5”;
C = 5;
X = A * B;
Y = C * B;
As you can see flags contain data of many types. Now what can you do with them? To answer that we implemented two Visual Programmer elements: Compute and Compute Test.
In the Compute element you can place any number of assignment statements of the form “Flag name = expression”. More than one assignment statement can be used in the same Compute element. All you need do is to separate the expressions with a semicolon. When the Compute element executes the expressions evaluates and assignments to the flags made.
The ComputeTest element contains a single expression that when evaluated results in either a Yes or No. As described above, the expression is evaluated and whatever value results, it is changed into a yes/no value. Zero is No, any other number is Yes. An empty string is No. Any other string is Yes.
But the real interesting stuff happens when you use the functions built-in to HCA in expressions. These are documented in the user guide. Here is an example.
Suppose that you want to have a program triggered by some event change the level of a light and then after some time set the light level back to where it was before the program started. Here is how you could do that.
Start the program with a Compute element that contains this contents:
oldLevel=_DimPercent(“Home - Switch”)
Then the next elements in your program do whatever you need do with the light. Perhaps raise the level to 100%. Next, delay for some time using a Delay element. Then the last element is another Compute element that says:
NewLevel=_DimToPercent(“Home - Switch”, oldLevel)
That’s it. The program does what you need to do because of the two Compute elements. But there is something more.
The Compute element contains what we call the expression builder and here is how you can use that to make creating the expressions in Compute elements simpler.
After you add a Compute element, start the expression with “oldlevel = “.
70
But now we have to enter the function with the device name and get it all correct. Let’s use the expression builder to do the hard work. Press the Expression Builder button:
71
The first thing to notice about this dialog is the “Insert What” box. The expression builder can insert lots of things into a compute element – the name of an object in your design, a function or a constant. In this case we want to build the first expression in this example. So we choose Function. The “Functions” box says what kinds of function we want to insert. By narrowing our choices it makes it easier to find things. The functions that work with devices are “action” functions so we tick that. The dropdown then shows all the functions of the types we selected. In the dialog, below the selected function, is an short description of what the function does and any arguments to that function and the type of data they expect. Choose “_DimPercent”.
72
The _DimPercent function takes one argument: The name of the device as a string. We could just type in the name of the device we want, but there is an easier way. Each argument has a button to the right of it. Press it and this dialog appears:
73
Again you get to chose what sort of thing you want to insert. We want to insert a device name so we select that option and then choose the device we want. Close the dialog with OK takes us back to the expression builder:
74
Close the expression builder and what we built is inserted into the compute element.
75
All done except for checking the work with Validate.
In summary, the Compute element and the Compute Test element can really expand what the Visual Programmer can do. To go beyond that gets into text scripts and that’s for another time.