piFace uses MCP23S17-chip, which is supported by scratchClient.
The chip is a 16bit port expander; in piFace one group is used as an input, the other group of 8 bits are used as outputs, connected to a open collector driver.
One of the interrupt pins of the chip ‘intB’ is connected to GPIO25. This is not used by scratchClient (but does not allow to use this pin for other purpose.
The hardware setup: two LED, a green one and red one, and a small motor powered by a battery and controlled with the relais.
This post demonstrates the use of scratchClient as a ‘hardware abstraction layer’, assigning meaningful names to scratch variables.
In this example, I connected a red and green LED to piFace.
With default configuration, the default names are ‘out_2’, ‘out_3’.
Here a sample of a blinking LED, using these definitions.
This works, but the variable names are not very conclusive. There is the need to keep a record on what this ‘out_2’ is meaning. But scratchClient allows to tweak the configuration files and provide more meaningful names for the variables controlling the hardware.
When editing scratchClient/config/config_mcp23s17.xml, the names of the variables in scratch can be changed. ‘green_led’ and ‘red_led’ are better describing what is connected to the hardware.
Restarting scratchClient is needed when configuration is changed.
Side topic: the ‘all’-variable, common to all outputs here, can be used to set all values immediately to either ‘0’ or ‘1’. Useful for a ’emergency shutdown’ or alike.
With the meaningful names, a scratch script looks like this:
The script gets more expressive. There is no need to map the abstract names to the thing connected.
Another example. Suppose a small motor is connected to relais_1.
Here, the variables ‘relais_1’, ‘out_1’, ‘all’ can be used to drive this output.
Setting this variable is straightforward, but ‘relais_1’ is not a descriptive name.
When editing the configuration, the scratch variable names can be changed to ‘motor’.
This changes the names in scratch to ‘motor’.
The following snippets show the changes: left side are the default definitions, right side the more meaningful names.