Next: , Previous: , Up: Dezyne Basics   [Contents]


2.5 Possible Events: Multiplexing Sensor Inputs

Language keyword: optional

Earlier we worked with a timer, which generates an event after a specified duration unless cancelled first. Another kind of time-related event is sensor activation. An enabled sensor might signal almost immediately, or it might never signal at all. Some control systems that use sensors have to account for events that might never occur; for example, a passcode-entry component should eventually time out and reset if a user starts keying in a passcode but never finishes and never cancels.

The alarm system we’ll build later uses multiple kinds of passive sensors, i.e., sensors that do not poll but instead only respond to stimuli. In this section we address optional events – ones that might never happen – and how to multiplex identical or similar components that provide the same defined interface.

Below is a sensor interface. When the sensor is on (enabled, sensing), at some point in the future it might trigger an event, or it might never trigger an event. The interface expects a caller to be aware of the sensor state and not call wrongly (illegal). The new keyword optional is highlighted. Note that keywords inevitable and optional occur only in interface behaviour, never in component behaviour, and that the illegal declarations are all necessary, this being an interface and not a component.

interface ISensor {
  in void turnOn();
  in void turnOff();
  out void triggered();

  behaviour {
    enum State { Off, Sensing, Triggered };
    State state = State.Off;
    [state.Off] {
      on turnOn: state = State.Sensing;
      on turnOff: illegal; // tests caller's logic
    }
    [state.Sensing] {
      on turnOn: illegal;
      on turnOff: state = State.Off;
      on optional: { // could trigger soon but might never triggered;
        state = State.Triggered;
      }
    }
    [state.Triggered] {
      on turnOn: illegal;
      on turnOff: state = State.Off;
    }
  }
}

These two components provide the same iSensor interface.

component HallEffectSensor {
  provides ISensor iSensor;
}

component VibrationSensor {
  provides ISensor iSensor;
}

Next: , Previous: , Up: Dezyne Basics   [Contents]