LogicToReal

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

    Modelica.Electrical.Digital.Interfaces.DigitalInput x[n] annotation (Placement(transformation(extent = {
        {-60, -10}, 
        {-40, 10}})));
    Modelica.Blocks.Interfaces.RealOutput y[n] annotation (Placement(transformation(extent = {
        {40, -10}, 
        {60, 10}})));
    parameter Integer n(final min = 1, start = 1) "signal width";
    parameter Real value_U(start = 0.5) "value for digital U (uninitialized)";
    parameter Real value_X(start = 0.5) "value for digital X (Forcing Unknown)";
    parameter Real value_0(start = 0) "value for digital 0 (Forcing 0)";
    parameter Real value_1(start = 1) "value for digital 1 (Forcing 1)";
    parameter Real value_Z(start = 0.5) "value for digital Z (High Impedance)";
    parameter Real value_W(start = 0.5) "value for digital W (Weak    Unknown)";
    parameter Real value_L(start = 0) "value for digital L (Weak 0)";
    parameter Real value_H(start = 1) "value for digital H (Weak 1)";
    parameter Real value_m(start = 0.5) "value for digital m (Do not care)";
equation
    for i in 1:n loop
        y[i] = if x[i] == L.'U' then value_U else if x[i] == L.'X' then value_X else if x[i] == L.'0' then value_0 else if x[i] == L.'1' then value_1 else if x[i] == L.'Z' then value_Z else if x[i] == L.'W' then value_W else if x[i] == L.'L' then value_L else if x[i] == L.'H' then value_H else value_m;
    end for;

    annotation (
        Documentation(
            info = "<html>\n<p>\nConversion of a digital input into a Real output without any delay according to:\n</p>\n<pre>\n  input                 output\n  'U'  (coded by 1)     val_U\n  'X'  (coded by 2)     val_X\n  '0'  (coded by 3)     val_0\n  '1'  (coded by 4)     val_1\n  'Z'  (coded by 5)     val_Z\n  'W'  (coded by 6)     val_W\n  'L'  (coded by 7)     val_L\n  'H'  (coded by 8)     val_H\n  '-'  (coded by 9)     val_m\n</pre>\n<p>\nThe values val... are given by parameters.</p>\n<p>If 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 = {
                        {152, -100}, 
                        {-148, -40}},
                    lineColor = {0, 0, 255},
                    textString = "%name"), 
                Rectangle(
                    extent = {
                        {-40, 40}, 
                        {40, -40}},
                    lineThickness = 0.5)}));
end LogicToReal;