6. NodeWire Sketch: Schematic Programming

NodeWire Script builds on the power of PlainTalk and NodeWire Sketch is a schematic programming environment that is based on NodeWire Script. In NodeWire script, Nodes are represented as blocks with input and output ports.

Input and output from this point of view is quite different from the Microprocessor’s point of view. An output is a port that provides a readable value for example a sensor. An input on the other hand is a port that can be written to. An example is a port that drives an appliance such as electric motor or heater. An input port can usually be read to get the current value. This means an input port can also be treated as an output port.

6.1. Elements of a sketch

There are five basic types of elements or building blocks that can be used to construct a sketch. 1. Node. Which we already described above.

2. Constant. Is an element that has an output port and is used to hold a value that can be written to the input of another element. When the port of a constant is wired to the input of a node, it is equivalent to an assignment, the input port takes the value of the constant. The value of a constant is usually a floating point number but can also be a string or even an object.

_images/constant_output.png

3. Variable. This is an element that has both input and output ports. It is used for storage. The name of a variable is merely a place-holder for the value it stores. The value of a variable is set by the value of ports that connects to it’s input. Variables can also be used to set the values of other ports by wiring the output of variables to the input of the port. A single port of a node can be mapped to a variable by naming it as node[port]. There is a special timer variable that is used to read the system time in seconds. This can be used to implement delays and scheduling.

_images/variable_in_out.png

The same element or port can appear multiple times in a sketch.

4. Operator. An operator is an element that is used to perform an operation on the values of two separate ports and then produce a new output. For example, the addition operator adds the values of two ports to produce the sum.

_images/operator.png

6.2. Initialization

All the examples we have seen above are initialization assignments. The rules engine performs the assignment once and that’s it.

_images/initialization_assignment.png

in the example above, the value of variable is initialized to the value of temp. If the value of temp should change later on, variable will not be updated with the latest value. If we want this behaviour, we will need to use a continuous assignment. And that brings us to the fifth element:

5. WhenDo (Rule). This is a special element that has a trigger input and surrounds one or more assignments. The enclosed assignments are executed each time the trigger input is true, i.e. non-zero.

6.3. Continuous Assignment

When we surround an assignment with a whendo and then connect an always true constant to the trigger input, we get a continuous assignment. In the sketch below, variable will be updated to the value of temp whenever the value of temp changes.

_images/continuous_assignment.png

6.4. Blinky

_images/blinky.png

The sketch above, switches on port 13 of node02 every 10th second of the minute and switches it off every 40th second.