Learn how to display coloured pressure maps on the Lightpad Block.
Launch the BLOCKS CODE application and open the script called ColourPressureMap.littlefoot
. You can find the script in the littlefoot/scripts/Example Scripts
folder. If you don't have the BLOCKS CODE IDE installed on your system, please refer to the section Getting started with BLOCKS CODE for help.
Let's start by drawing white pressure map points onto the screen whenever pressure is applied onto the touch surface of a Lightpad.
We start by clearing the display in the repaint() function using the clearDisplay() function to reset the state of our LEDs. We than call two handy functions that are defined in the littlefoot language that allow us to draw and fade pressure points automatically. These are respectively the drawPressureMap() and fadePressureMap() functions.
However, if we run the script at the moment no pressure points are shown on the screen because we need to tell the program where to draw them by calling the addPressurePoint() function with the colour and coordinates of the point.
The littlefoot language has several callback functions that are called when a special event happens. In our case we are interested in the start and movement of a touch gesture and therefore we can implement these as follows:
When the callback functions are called, we add a pressure point to the pressure map by specifying a colour in hexadecimal (in this case white is 0xffffff) and the coordinates of the touch event.
Notice here that we multiply the depth z by a scaler in order to make the surface of the pressure point bigger and easier to see.
Now let's try to add different colours to the pressure points depending on the number of fingers on the screen. To make these variables accessible in realtime to the Parameters tab of the IDE, we create some variables in the metadata section of the code.
We also implement a helper function called getPressureColour() that takes an index of the finger from the touch event and returns the desired colour as shown below:
Finally we need to modify the callbacks to incorporate this helper function instead of hardcoding the colour white.
Notice we also allow the scaling of the pressure point to be controlled by a slider in the Parameters tab in order to change its size in realtime.
In this example, we learnt how to display pressure maps onto the Lightpad and add colours depending on the number of fingers pressed onto the touch surface.