RealToLogic

block RealToLogic "Real to Logic converter"
    import L = Modelica.Electrical.Digital.Interfaces.Logic;

    Modelica.Blocks.Interfaces.RealInput x[n] annotation (Placement(transformation(extent = {
        {-60, -10}, 
        {-40, 10}})));
    Digital.Interfaces.DigitalOutput y[n] annotation (Placement(transformation(extent = {
        {40, -10}, 
        {60, 10}})));
    parameter Integer n(final min = 1, start = 1) "signal width";
    parameter Real upper_limit(start = 1) "upper limit";
    parameter Real lower_limit(start = 0) "lower limit";
    parameter L upper_value(start = L.'1') "output if input > upper_limit";
    parameter L lower_value(start = L.'0') "output if input < lower_limit";
    parameter L middle_value(start = L.'X') "output else";
equation
    for i in 1:n loop
        y[i] = if upper_limit < x[i] then upper_value else if x[i] < lower_limit then lower_value else middle_value;
    end for;

    annotation (
        Documentation(
            info = "<html>\n<p>\nConversion of a real input into a digital output without any delay according to:\n</p>\n<pre>\n                                 condition            output\n      first check:               input greater upp    lupp\n      second check:              input larger low     llow\n                                 else                 lmid\n</pre>\n<p>\nIf the signal width is greater than 1 this conversion is done for each signal.\n</p>\n</html>",
            revisions = "<html>\n<ul>\n<li><em>September 15, 2004</em>\n       by Christoph Clauss colors changed<br>\n       </li>\n<li><em>November 5, 2003</em>\n       by Christoph Clauss<br>\n       initially modelled.</li>\n</ul>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Polygon(
                    points = {
                        {-40, -40}, 
                        {40, 40}, 
                        {40, -40}, 
                        {-40, -40}},
                    lineColor = {127, 0, 127},
                    fillColor = {127, 0, 127},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {-40, 40}, 
                        {40, 40}, 
                        {-40, -40}, 
                        {-40, 40}},
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {148, -100}, 
                        {-152, -40}},
                    lineColor = {0, 0, 255},
                    textString = "%name"), 
                Rectangle(
                    extent = {
                        {-40, 40}, 
                        {40, -40}},
                    lineThickness = 0.5)}));
end RealToLogic;