Previous | Next

Signals

Now you know how to write down commands, put them in sequence or parallel, create loops, and create you own macros. The next important topic is communication.

In ESPranto, commands can communicate through signals. This makes it possible to run commands in parallel, while letting them talk to each other: for instance, one macro could be activated as a reaction to another macro being activated.

To explain the use of signals, we are going to add a macro to our drawing application that flashes each border of the TagTiles board in red in a clockwise fashion each time the user lights a cell on the board: first the top row, the right column, the bottom row, the left column, then it repeats the top row again, and so on. We will create this application step-by-step. As a first step, lets first create an application that constantly flashes each border.

flashShape s = (
  drawShape s red;
  waitTime 200;
  drawShape s black
)

flashTopRow = (
  flashShape (Line (1,12) (12,12))
)

flashBorders = (
  loop (
    flashTopRow;
    flashRightCol;
    flashBottomRow;
    flashLeftCol
  )
)

main = (
  flashBorders
)

Exercise

  • The above code is not yet complete. Write the definitions of flashRightCol, flashBottomRow and flashLeftCol yourself. Compile and test the application.
  • The waitTime macro waits for a given amount of miliseconds. Change the code such that flashShape draws the shape in red for a full second. Test the application to see if it works correctly.
  • Integrate the application with drawingApp2.esp such that the borders are constantly flashing, and the user can create a drawing. Hints:
    1. Copy-paste the code into one file;
    2. Make sure you only define 1 main macro;
    3. Use parallelism in the main macro to glue everthing together.

Adding signals for communication

In its current state, borders are constantly flashing. However, we want to create an application in which the border only flashes when the user puts draws a cell. To achieve this, we need some communication between handleColor and flashBorders. We are going to use a signal for this communication. First, we will create the signal, by adding the line:
signal FlashPlease

to the application. This line indicates to the ESPranto compiler that we are going to use the signal FlashPlease. Basically, there are two things we can do with the signal:

  1. we can wait until a signal becomes present;
  2. we can emit the signal, making it present.
To wait for the signal to become present, you use await (present FlashPlease). To make the signal present, you use emit FlashPlease. What we have to do now, is make the flashBorders macro wait for the signal, and make the handleColor application emit the signal.

Exercise

  • Add the line await (present FlashPlease) to flashBorders in the appropriate places. Add the line emit FlashPlease to handleColor in the appropriate place.
  • Compile and test your application. Check if, indeed, one segment of the border flashes when the user draws a cell.
  • If you are having troubles, you can peek at our solution.


Previous | Next
Serious Toys logo -  tangible inspiration without limitation