Next: , Up: Defining and Managing Component States   [Contents]


2.3.1 Handling States Event-by-Event: “Event-Leading”

In this section we start tracking the LED component’s on/off state. This anticipates extending or refining state behaviour inside the component, while keeping the component’s externally visible behaviour exactly the same as its provided interface or interfaces.

Step 1. Enter or copy-paste the following new state-management code in the Dezyne editor. You can continue with the system design from the previous section, or restart with only the below code.

import ILED.dzn;

component LED {
  provides ILED iLed;

  behaviour {
    enum State { Off, On };
    State state = State.Off;
    on iLed.turnOn(): { state = State.On; }
    on iLed.turnOff(): { state = State.Off; }
  }
}

In the above component’s code, the enum line declares an enumeration type as in C/C++/Java. “Off” and “On” are unique enum values for this particular enumeration type, not strings. “State” is a case-sensitive user-defined name for the enumeration type.

The next line after the enum declaration defines a variable named “state” of type State and initializes the setting to “Off”. By convention in this tutorial we will always list the default state first when declaring an enum. Two important but non-obvious points:

Each handler line (on event) in the component’s behaviour block is followed either by one statement that ends in a semicolon, or by curly braces {} that contain zero (no-op) or one or more statements.

Step 2. View the component’s State Chart, shown below (you might have to refresh/update views first using the images/update_icon.75 icon in Dezyne-IDE, next to the verify images/verify_icon.75 icon)

images/state_2

Next: , Up: Defining and Managing Component States   [Contents]