OnDelay

block OnDelay "Delay a rising edge of the input, but do not delay a falling edge."
    extends Modelica.Blocks.Interfaces.PartialBooleanSISO_small;

    parameter Modelica.SIunits.Time delayTime "Delay time";
protected
    Boolean delaySignal(start = false, fixed = true);
    discrete Modelica.SIunits.Time t_next;
initial equation
    pre(u) = false;
    pre(t_next) = time - 1;
algorithm
    when initial() then 
        delaySignal := u;
        t_next := time - 1;
    elsewhen u then 
        delaySignal := true;
        t_next := time + delayTime;
    elsewhen not u then 
        delaySignal := false;
        t_next := time - 1;
    end when;
equation
    if delaySignal then 
        y = t_next <= time;
    else 
        y = false;
    end if;

    annotation (
        Icon(graphics = {
            Text(
                extent = {
                    {-250, -120}, 
                    {250, -150}},
                textString = "%delayTime s"), 
            Line(points = {
                {-80, -66}, 
                {-60, -66}, 
                {-60, -22}, 
                {38, -22}, 
                {38, -66}, 
                {66, -66}}), 
            Line(
                points = {
                    {-80, 32}, 
                    {-4, 32}, 
                    {-4, 76}, 
                    {38, 76}, 
                    {38, 32}, 
                    {66, 32}},
                color = {255, 0, 255})}),
        Documentation(info = "<html>\n<p>\nA rising edge of the Boolean input u gives a delayed output.\nA falling edge of the input is immediately given to the output.\n</p>\n\n<p>\nSimulation results of a typical example with a delay time of 0.1 s\nis shown in the next figure.\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Blocks/MathBoolean/OnDelay1.png\"\n     alt=\"OnDelay1.png\">\n<br>\n<img src=\"modelica://Modelica/Resources/Images/Blocks/MathBoolean/OnDelay2.png\"\n     alt=\"OnDelay2.png\">\n</p>\n\n<p>\nThe usage is demonstrated, e.g., in example\n<a href=\"modelica://Modelica.Blocks.Examples.BooleanNetwork1\">Modelica.Blocks.Examples.BooleanNetwork1</a>.\n</p>\n\n</html>"));
end OnDelay;