Next: , Previous: , Up: The Alarm System   [Contents]


2.6.3 Adding the LED

Next we add simple control for an LED.

interface ILED {
  // assumes green automatically on power-up
  in void setGreen();
  in void setYellow();
  in void setRed();
  behaviour { on setGreen, setYellow, setRed: {} }
}

component LED { provides ILED iLed; }

Integrating the LED would make a good exercise.

Note that the below integrated code imports the LED interface as opposed to declaring it.

import LED.dzn;

interface IController {
  in void validPincode();
  in void sensorTriggered();

  behaviour {
    on validPincode: {}
    on sensorTriggered: {}
  }
}

component Controller {
  provides IController iController;
  requires ILED iLed;

  behaviour {
    enum State { Unarmed, Armed, Alarming };
    State state = State.Unarmed;
    [state.Unarmed] {
      on iController.validPincode(): {
        state = State.Armed;
        iLed.setYellow();
      }
      on iController.sensorTriggered(): {}
    }
    [state.Armed] {
      on iController.validPincode(): {
        state = State.Unarmed;
        iLed.setGreen();
      }
      on iController.sensorTriggered(): {
        state = State.Alarming;
        iLed.setRed();
      }
    }
    [state.Alarming] {
      on iController.validPincode(): {
        state = State.Armed;
        iLed.setYellow();
      }
      on iController.sensorTriggered(): {}
    }
  }
}